I've never needed to use pointers in C# before, however, the library I'm using requires that method parameters are passed as pointers. The library allows for the usage of SIMD instruction sets.
To test how to use the library, I've attempted to write a method which uses SIMD to calculate the cosine value of all the elements of an array in one go.
This is what I've got:
double[] ValuesToCalculate = new double[MAX_SIZE];
double[] CalculatedCosines = new double[MAX_SIZE];
long Result;
Result = CalculateCosineArray(ValuesToCalculate, CalculatedCosines);
public static long CalculateCosineArraySIMD(double[] array, double[] result)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < array.Length; i++)
{
Yeppp.Math.Cos_V64f_V64f(*array, result, MAX_SIZE);
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
However, I get these errors:
The best overloaded method match for 'Yeppp.Math.Cos_V64f_V64f(double*, double*, int)' has some invalid arguments
Argument 1: cannot convert from 'double[]' to 'double*'
The * or -> operator must be applied to a pointer
Argument 2: cannot convert from 'double[]' to 'double*'
How do I get the pointers to work in this code? Again, this is the first time pointers has come up while I've been using C#.
The methods in Yeppp! library have two overloads: one that takes array + offset and another that takes pointers (e.g. if you want to call them with stackalloc'ed memory). E.g. for cosine computation Yeppp! offers two overloads:
Yeppp.Math.Cos_V64f_V64f(double[] xArray, int xOffset, double[] yArray, int yOffset, int length)
which operates on array argumentsYeppp.Math.Cos_V64f_V64f(double* x, double* y, int length)
which operates on pointer argumentsThus, your example should be rewritten as:
Note that only one call per array is required (even if you used pointers).