[(ReadOnly)Span2D] Expose `Stride`
Overview
Span2D<T> (and its read-only counterpart) seems perfect for numerical problems. When wanting to use numerical routines from HPC libraries the types do not expose the stride though. The stride is really helpful when calling a routine that accepts a pointer, number of rows, number of columns and a stride. Otherwise one would have to copy to contiguous memory first.
Some examples of routines that accept strides:
- in BLAS, e.g. here is dgemm in Intel MKL and Reference LAPACK Arguments
ldxrefer to the leading dimension ofxwhich is the stride (but here in column-major) - here is another routine, this time row-major, from the NAG Library,
tdxis the stride argument
API breakdown
https://github.com/CommunityToolkit/dotnet/blob/e6257d8c65126f2f977f2dcbce3fe6045086f270/CommunityToolkit.HighPerformance/Memory/Span2D%7BT%7D.cs#L89-L96
makes this part public, i.e.
public readonly int Stride;
Same for ReadOnlySpan<T>
Usage example
private static unsafe extern dgemv(char trans, int m, int n, double alpha, double* a,
int ldA, double* x, double incX, double beta, double* y, int incY);
public static unsafe dgemv(double alpha, ReadOnlySpan2D<double> a, ReadOnlySpan<double> x,
double beta, Span<double> y)
{
// Dimension check skipped
fixed (double* aPtr = a, xPtr = x, yPtr = y)
{
dgemv('T', a.Height, a.Width, alpha, aPtr, a.Stride, xPtr, 1, beta, yPtr, 1);
}
}
Breaking change?
No
Alternatives
Use TryGetSpan and if it doesn’t work copy discontiguous 2D spans to contiguous 1D spans first.
Additional context
This seems like a small PR I could create if the change in visibility doesn’t have any unintended consequences (I don’t see any). But it’d be good if someone could assess that first.
Help us help you
Yes, I'd like to be assigned to work on this item