Supposedly I have either an int[]
array or Vector256<int>
s. How can I copy the values from one to another using an array index?
At the moment I have to iterate over the array indices and copy the values one by one:
int[] input = ...; // length divisible by Vector256<int>.Count
int[] output = new int[intput.Length];
for (int i = 0; i < input.Length; i += Vector256<int>.Count)
{
Vector256<int> v = Vector256.Create(
array[i], array[i + 1], array[i + 2], array[i + 3],
array[i + 4], array[i + 5], array[i + 6], array[i + 7]);
Vector256<int> v2 = DoSomeWork(v);
for (int j = 0; j < Vector256<int>.Count; ++j)
{
output[i + j] = v2.GetElement(i + j);
}
}
In Java SDK 16, there are functions that can do exactly what I need. Is there any similar function in C#?
int[] input = ...;
int[] output = new int[values.length];
for (int i = 0; i < input.length; i += IntVector.SPECIES_256.length()) {
IntVector v = IntVector.fromArray(IntVector.SPECIES_256, input, i);
IntVector v2 = DoSomeWork(v);
v2.intoArray(output, i);
}
You can use Vector
from System.Numerics
. Something like this:
var vector = new Vector<int>(new Span<int>(ints, i, 8));
Vector256<int> v = vector.AsVector256();
......
v2.AsVector().CopyTo(output, i);
Also you can try straightly use System.Numerics.Vector
for your computations.
Also can use unsafe
with Avx.LoadVector256
and Avx.Store
from System.Runtime.Intrinsics.X86
. Something like this:
fixed (int* ptr = input)
fixed (int* ptrRes = output)
{
var vectorCount = Vector256<int>.Count;
for (int i = 0; i <= input.Length - vectorCount; i += vectorCount)
{
var v = Avx.LoadVector256(ptr + i);
....
Avx.Store(ptrRes + i, v2);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With