Skip to content

Strided arrays interface unclear #59435

@nhz2

Description

@nhz2

In JuliaIO/HDF5.jl#1207 I am trying to create a custom resizable strided array to get the HDF5 C library, HDF5.jl, and ChunkCodecs.jl to all work together.

Looking at the docs:

## [Strided Arrays](@id man-interface-strided-arrays)

Methods to implement Brief description
strides(A) Return the distance in memory (in number of elements) between adjacent elements in each dimension as a tuple. If A is an AbstractArray{T,0}, this should return an empty tuple.
Base.unsafe_convert(::Type{Ptr{T}}, A) Return the native address of an array.
Base.elsize(::Type{<:A}) Return the stride between consecutive elements in the array.
Optional methods Default definition Brief description
stride(A, i::Int) strides(A)[i] Return the distance in memory (in number of elements) between adjacent elements in dimension k.

I find the description of unsafe_convert and elsize confusing.

unsafe_convert says it should "Return the native address of an array", but this seems to be missing an important point that unsafe_convert needs to be paired with a cconvert.

Should all strided arrays also implement cconvert, or is there a default definition that can be documented here?

elsize says it should "Return the stride between consecutive elements in the array."
How is this different from stride(A, 1)?

I think the answer is in #38128 (comment) that elsize is "the multiplication factor for converting strides from elements to bytes". I think this should be the brief description.

Also, there seems to be a missing isstrided(A)::Bool function to check if an array supports the strided array interface.

Related issues: #54715 #36553 #51962

Also, here are some packages that implement the interface, but that don't seem quite right to me.

In:
https://github.com/JuliaArrays/FixedSizeArrays.jl/blob/0587d03a31844ac284d15a8142f8f9236b5b9a3d/src/FixedSizeArray.jl#L387-L391

Doesn't parent(A) need to be replaced with Base.cconvert(Ptr{T}, parent(A)) and why is Base.unsafe_convert needed at all?

In:
https://github.com/JuliaPy/PythonCall.jl/blob/0b32b08cbe0f68e5eae9ab862b8767a9d41c9e67/src/Wrap/PyArray.jl#L600

Base.elsize(::Type{PyArray{T,N,M,L,T}}) where {T,N,M,L} = sizeof(T)

Is there a case where this doesn't work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions