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

Viewed 346

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);
}
2 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);
    }
}

Another solution.

int[] input = ...;
int[] output = new int[input.Length];
Span<Vector256<int>> inputVectors = MemoryMarshal.Cast<int, Vector256<int>>(input);
Span<Vector256<int>> outputVectors = MemoryMarshal.Cast<int, Vector256<int>>(output);
for (int i = 0; i < inputVectors.Length; i++)
    outputVectors[i] = DoSomeWork(inputVectors[i]); 

The result will be automagically in the output array.

Unsafe version

int[] input = ...;
int[] output = new int[input.Length];
fixed (int* inPtr = input, outPtr = output)
{
    Vector256<int>* src = (Vector256<int>*)inPtr;
    Vector256<int>* dst = (Vector256<int>*)outPtr;
    Vector256<int>* srcEnd = src + (input.Length >> 3);
    while (src < srcEnd)
    {
        *dst = DoSomeWork(*src);
        src++;
        dst++;
    }
}

All 3 solutions: unsafe from the accepted answer, and both above ones have nearly the same performance.

Related