www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use Vector Extensions in an opBinary

reply HuskyNator <HuskyNator protonmail.ch> writes:
I recently found out there is [support for vector 
extensions](https://dlang.org/spec/simd.html)
But I have found I don't really understand how to use it, not 
even mentioning the more complex stuff. I couldn't find any good 
examples either.

I'm trying to figure out how to implement the following opBinary 
using vector extensions.
```dlang
alias MatType = typeof(this);

union{
   T[rows*columns] vec;
   T[rows][columns] mat;
}

MatType opBinary(string op, T)(const T right) const {
   MatType result = this;
   mixin("result.vec[] = this.vec[] " ~ op ~ " right;");
   return result;
}
```
Or alternatively, as I'm not sure which is more efficient/faster:
```dlang
MatType opBinary(string op, T)(const T right) const {
   MatType result;
   static foreach(i; 0..rows*columns)
     mixin("result.vec[i] = this.vec[i] " ~ op ~ " right;");
   return result;
}
```

But going off the documentation, I have no idea how to use vector 
extensions to achieve something like this.

As a small disclaimer; I don't know to what extent the compiler 
already automates these kind of operations, and mostly want to 
use this as a learning experience.

Kind regards, HN
Apr 17 2022
next sibling parent user1234 <user1234 12.de> writes:
On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:
 I recently found out there is [support for vector 
 extensions](https://dlang.org/spec/simd.html)
 But I have found I don't really understand how to use it, not 
 even mentioning the more complex stuff. I couldn't find any 
 good examples either.

 I'm trying to figure out how to implement the following 
 opBinary using vector extensions.
 ```dlang
 alias MatType = typeof(this);

 union{
   T[rows*columns] vec;
   T[rows][columns] mat;
 }

 MatType opBinary(string op, T)(const T right) const {
   MatType result = this;
   mixin("result.vec[] = this.vec[] " ~ op ~ " right;");
   return result;
 }
 ```
 Or alternatively, as I'm not sure which is more 
 efficient/faster:
 ```dlang
 MatType opBinary(string op, T)(const T right) const {
   MatType result;
   static foreach(i; 0..rows*columns)
     mixin("result.vec[i] = this.vec[i] " ~ op ~ " right;");
   return result;
 }
 ```

 But going off the documentation, I have no idea how to use 
 vector extensions to achieve something like this.

 As a small disclaimer; I don't know to what extent the compiler 
 already automates these kind of operations, and mostly want to 
 use this as a learning experience.

 Kind regards, HN
I'd experiment with CompilerExplorer. For example [this](https://godbolt.org/z/a51r8GEv4) shows that the codegen is identical for both opBinary version for "+". About time spent to compile, `static foreach` is known not to scale well.
Apr 17 2022
prev sibling next sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:
 I recently found out there is [support for vector 
 extensions](https://dlang.org/spec/simd.html)
 But I have found I don't really understand how to use it, not 
 even mentioning the more complex stuff. I couldn't find any 
 good examples either.
You might want to have a look at https://code.dlang.org/packages/intel-intrinsics — Bastiaan.
Apr 17 2022
parent reply HuskyNator <HuskyNator protonmail.ch> writes:
On Sunday, 17 April 2022 at 17:04:57 UTC, Bastiaan Veelo wrote:
 On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:
 I recently found out there is [support for vector 
 extensions](https://dlang.org/spec/simd.html)
 But I have found I don't really understand how to use it, not 
 even mentioning the more complex stuff. I couldn't find any 
 good examples either.
You might want to have a look at https://code.dlang.org/packages/intel-intrinsics — Bastiaan.
This does not discuss core.simd or __vector type, or did I miss/mininterpret something?
Apr 21 2022
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Thursday, 21 April 2022 at 15:31:04 UTC, HuskyNator wrote:
 On Sunday, 17 April 2022 at 17:04:57 UTC, Bastiaan Veelo wrote:
 You might want to have a look at 
 https://code.dlang.org/packages/intel-intrinsics

 — Bastiaan.
This does not discuss core.simd or __vector type, or did I miss/mininterpret something?
It wraps `core.simd` with an eye on portability. I haven’t used it myself, but my impression is that if you’re interested in `core.simd`, intel-intrinsics may be a better option. I think there is also better documentation. There is a video of a DConf presentation that you may want to watch. — Bastiaan.
Apr 21 2022
prev sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:
 As a small disclaimer; I don't know to what extent the compiler 
 already automates these kind of operations, and mostly want to 
 use this as a learning experience.
For your particular case, it is very likely LDC and GDC will be able to optimize your loops using SIMD.
Apr 21 2022