digitalmars.D.learn - How to use Vector Extensions in an opBinary
- HuskyNator (34/34) Apr 17 2022 I recently found out there is [support for vector
- user1234 (6/41) Apr 17 2022 I'd experiment with CompilerExplorer. For example
- Bastiaan Veelo (4/9) Apr 17 2022 You might want to have a look at
- HuskyNator (3/12) Apr 21 2022 This does not discuss core.simd or __vector type, or did I
- Bastiaan Veelo (7/14) Apr 21 2022 It wraps `core.simd` with an eye on portability. I haven’t used
- Guillaume Piolat (3/6) Apr 21 2022 For your particular case, it is very likely LDC and GDC will be
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
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, HNI'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
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
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:This does not discuss core.simd or __vector type, or did I miss/mininterpret something?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 21 2022
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: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.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
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