Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy from an array to a Vector256 and vice versa based on the array index?

Tags:

c#

.net

simd

avx2

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);
}
like image 227
b1105029 Avatar asked Sep 03 '25 05:09

b1105029


1 Answers

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);
    }
}
like image 198
Guru Stron Avatar answered Sep 04 '25 22:09

Guru Stron