Open dmalkr opened 7 months ago
That's quite neat trick. It allows to make array of fields of structs out of array of structs. Consider for example: struct complex double foo; double bar;}
. So you can turn of struct to array referencing it's field foo. And that won't be possible with Vec
.
It won't work for us however. BLAS API expects stride with stride as multiple of element size. And we build around BLAS and don't have a lot C structs to work with
@Shimuuar Some Julia's nuts&bolts extracted :)
@Shimuuar Some C++ stuff :)
Numpy
Numpy uses
strides
property for each np-array object. Strides are represented by tuples: each tuple element for each dimension. Stride is the byte offset to next item (item size described bydtype
). For example, we can easily construct a 5-byte stride for a 4-byte int32 array (and yes, array elements can be un-aligned in Numpy).References:
BTW, Numpy uses copy-to-temp-array technique to copy (
np.copyto()
) overlapped arrays. And it doesn't try to do those tricky transpositions like in #5. I suspect this is because the stride may not be a multiple of the element size. Implementationcopyto
usesarray_copyto
, which usesPyArray_AssignArray
: https://github.com/numpy/numpy/blob/main/numpy/_core/src/multiarray/array_assign_array.c#L305Julia
Strides in Julia implemented BLAS/LAPACK compatible:
https://docs.julialang.org/en/v1/manual/arrays/#Implementation
So nothing new here.
And as for #5, Julia simply uses
memmove
for copying contiguous arrays (https://github.com/JuliaLang/julia/blob/master/base/array.jl#L273), and element-wise copying in the case of strided arrays, views, or other non-contiguous arrays (https://github.com/JuliaLang/julia/blob/master/base/multidimensional.jl#L1154).So we can show that overwriting elements as described here https://github.com/Shimuuar/vecvec/blob/71219aacf6aed2a844e388cb5e7dad5e46633bd5/vecvec-lapack/Vecvec/LAPACK/Internal/Vector/Mutable.hs#L222-L239 can occurs:
C++, GCC implementation
So I tried copying with
slice_array
fromstd
. It has the same problem as Julia above, elements copied from left to right, at least in GCCstd
library (but it seems it may be different implementation in otherstd
libraries). Theoperator=()
fromslice_array
simply uses__valarray_copy()
, which copies elements from left to right: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/valarray_array.h#L218-L223 . Testing:output: