Open songjhaha opened 2 years ago
If it's true that numpy could set contiguous flag automatically, the next questions is could we just wrap every StridedArray
without copy?
normalized_x = if x isa StridedArray
x
else
collect(x)
end
It seems numpy could work correctly if the data pointer, strides, shape and ndims are correct, so it should support all StridedArray
. But let me check the mechanism behind numpy first.
In numpy C-API document:
The NPY_ARRAY_ALIGNED, NPY_ARRAY_C_CONTIGUOUS, and NPY_ARRAY_F_CONTIGUOUS flags can actually be determined from the other parameters
https://numpy.org/doc/stable/reference/c-api/types-and-structures.html#c.PyArrayInterface.flags
And numpy could update flag with: https://github.com/numpy/numpy/blob/4a9b7145eb1a0731821eb8912c2474352cadc682/numpy/core/src/multiarray/flagsobject.c#L62
And a simple rule to check contiguous is given by: https://github.com/numpy/numpy/blob/4a9b7145eb1a0731821eb8912c2474352cadc682/numpy/core/src/multiarray/flagsobject.c#L91-L115
Actually, we don't always set the right contiguous flag when constructing
PyArrayInterface
. For example,Vector
should be both C and F contiguous when converted to ndarray, but we set the flagis_c_style
tofalse
when constructing aDynamicArray
, but after casting it to ndarray, numpy seems to reset this flag to true, which is correct. Not sure if numpy could always do this check in all situation, need to check carefully.