The strided array interface is documented in Interfaces · The Julia Language
New AbstractArray subtypes can implement this interface to enable fast BLAS, LAPACK, IO, and Python interop methods. In addition, we would like some wrappers and views of strided arrays to be strided arrays as well.
However, due to bugs and unclear documentation, this composability often fails in practice. For example,
https://github.com/JuliaPy/PythonCall.jl/issues/579 and https://github.com/JuliaLang/julia/pull/60533
I propose adding a function to Test, an exported syntax sugar function to Base, and two optional functions to the strided array interface, in order to help test if an array has a valid implementation of the strided array interface. I’m looking for feedback on these new functions.
Test.test_strided_interface(a::AbstractArray)
If isbitstype(eltype(a)) and has_strided_get(a), iterate through the array checking that elements obtained through the strided interface match elements obtained using getindex.
If isbitstype(eltype(a)) and has_strided_set(a), iterate through the array setting elements with the strided interface, and checking that elements match elements obtained using getindex.
Base.has_strided_get(a::AbstractArray)::Bool = false and Base.has_strided_set(a::AbstractArray)::Bool = false
These two functions have fallback definitions that return false and would be specialized to return true for arrays that implement the strided array interface, for example:
has_strided_get(a::Array) = true and has_strided_set(a::Array) = true
The fallback definition ensures backwards compatibility with existing array types.
strided_ptr(f, a::AbstractArray)
This is a simple syntax sugar for the cconvert, unsafe_convert, and GC.@preserve block.
The goal is to have a nice-to-use alternative to the easy-to-misuse pointer function.
function strided_ptr(f, a::AbstractArray{T})
a_cconv = Base.cconvert(Ptr{T}, a)
GC.@preserve a_cconv begin
f(Base.unsafe_convert(Ptr{T}, a_cconv))
end
end