Thread overview
How to use Vector Extensions in an opBinary
Apr 17, 2022
HuskyNator
Apr 17, 2022
user1234
Apr 17, 2022
Bastiaan Veelo
Apr 21, 2022
HuskyNator
Apr 21, 2022
Bastiaan Veelo
Apr 21, 2022
Guillaume Piolat
April 17, 2022

I recently found out there is support for vector extensions
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.

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:

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

April 17, 2022

On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:

>

I recently found out there is support for vector extensions
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.

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:

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 shows that the codegen is identical for both opBinary version for "+".

About time spent to compile, static foreach is known not to scale well.

April 17, 2022

On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:

>

I recently found out there is support for vector extensions
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.

April 21, 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:

>

I recently found out there is support for vector extensions
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?

April 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:

>

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.

April 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.