Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

* With Adapt.jl, an adaptor which is not an array type now adapts the memory backend of a `FixedSizeArray` and wraps it again, as it already did for arrays with static dimensions. Previously only the storage rule of the adaptor was applied to the `FixedSizeArray` as a whole, so that adaptors with rules only for the arrays they know left it unchanged, e.g. a `FixedSizeArray` passed to a GPU kernel was not converted to an `isbits` array. Adapting to an array type is unchanged, e.g. `adapt(CuArray, a)` is still a `CuArray`.

## Version [v1.5.2](https://github.com/JuliaArrays/FixedSizeArrays.jl/releases/tag/v1.5.2) - 2026-11-08

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion docs/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ The static dimensions are also available to other packages through the package e
The package has [extensions](https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions)) which are loaded automatically together with the following packages:

* [Adapt.jl](https://github.com/JuliaGPU/Adapt.jl): `adapt(FixedSizeArray, x)` converts the arrays in `x` to `FixedSizeArray`s, also inside wrappers such as `Adjoint`, see the [constructors](@ref "Constructors") above.
Conversely, `adapt(to, a)` converts a `FixedSizeArray` `a` as a whole, e.g. `adapt(CuArray, a)` is a `CuArray`, while for an array with static dimensions it adapts the memory backend and wraps it again, so that `adapt(CuArray, a)` is a `FixedSizeArrayDims` backed by a `CuVector`, with the same static dimensions.
Conversely, adapting to an array type converts a `FixedSizeArray` `a` as a whole, e.g. `adapt(CuArray, a)` is a `CuArray`, while for an array with static dimensions it adapts the memory backend and wraps it again, so that `adapt(CuArray, a)` is a `FixedSizeArrayDims` backed by a `CuVector`, with the same static dimensions.
All other adaptors (e.g. the ones converting the arguments of a GPU kernel) adapt the memory backend and wrap it again, with and without static dimensions.
`FixedSizeArrayDims` itself is not an `adapt` target: `adapt(FixedSizeArrayDims, x)` returns `x` unchanged, since the static dimensions can not be derived from `x`, use the [constructors](@ref "Constructing arrays with static dimensions") instead;
* [MPI.jl](https://github.com/JuliaParallel/MPI.jl) (v0.20.28 or later): the arrays of this package, and `SubArray`s of them, can be used directly as communication buffers in MPI operations, e.g. `MPI.Send`, `MPI.Recv!`, `MPI.Bcast!`, `MPI.Allreduce!` and `MPI.Gatherv!`;
* [Random.jl](https://docs.julialang.org/en/v1/stdlib/Random/): `rand!(a)` fills the array in place;
Expand Down
17 changes: 10 additions & 7 deletions ext/FixedSizeArraysAdaptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ using FixedSizeArrays: FixedSizeArray, FixedSizeArrayDims, new_fixed_size_array,

Adapt.adapt_storage(fixed::Type{<:FixedSizeArray}, A) = fixed(A)

# A `FixedSizeArray` (all dimensions dynamic) is converted as a whole, as other arrays, e.g.
# `adapt(CuArray, a)` is a `CuArray`.
Adapt.adapt_structure(to, a::FixedSizeArray) = Adapt.adapt_storage(to, a)
# A `FixedSizeArray` (all dimensions dynamic) is converted as a whole to another array type,
# as other arrays, e.g. `adapt(CuArray, a)` is a `CuArray`.
Adapt.adapt_structure(to::Type{<:AbstractArray}, a::FixedSizeArray) = Adapt.adapt_storage(to, a)
Adapt.adapt_structure(fixed::Type{<:FixedSizeArray}, a::FixedSizeArray) = Adapt.adapt_storage(fixed, a)

# With static dimensions, adapt the memory backend and wrap it again, e.g. for converting an
# array backed by a GPU vector for use in a kernel, or for moving it to the GPU with
# `adapt(CuArray, a)`. This is independent of the memory backend, since the static
# dimensions cannot be represented by other array types.
# For all other adaptors, and for arrays with static dimensions, adapt the memory backend and
# wrap it again, as for other array wrappers. Adaptors which are not array types usually have
# storage rules only for the arrays they know (e.g. the adaptors converting the arguments of
# a GPU kernel, which turn a `Vector` or a `CuVector` into an `isbits` device array), so that
# they would leave a `FixedSizeArray` unchanged as a whole. With static dimensions this also
# applies to array types, e.g. `adapt(CuArray, a)` moves the memory backend to the GPU, since
# the static dimensions cannot be represented by other array types.
function Adapt.adapt_structure(to, a::FixedSizeArrayDims)
new_fixed_size_array(adapt(to, parent(a)), static_dims(a))
end
Expand Down
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ using StaticArrayInterface: StaticArrayInterface
using Static: static, StaticInt
import Aqua

# An adaptor with a storage rule only for `Array`, as the adaptors of the GPU packages
# converting the arguments of a kernel to `isbits` device arrays (e.g. `CuDeviceArray`).
struct TestDeviceVector{T} <: DenseVector{T}
ptr::Ptr{T}
len::Int
end
Base.size(v::TestDeviceVector) = (v.len,)
Base.getindex(v::TestDeviceVector, i::Int) = (@boundscheck checkbounds(v, i); unsafe_load(v.ptr, i))
struct TestKernelAdaptor end
Adapt.adapt_storage(::TestKernelAdaptor, v::Vector) = TestDeviceVector(pointer(v), length(v))

# The memory backends to test with.
const storage_types = (FixedSizeArrays.optional_memory..., Vector)

Expand Down Expand Up @@ -952,6 +963,25 @@ Base.cconvert(::Type{MPI.MPIPtr}, x::MPITestVector) = Base.cconvert(MPI.MPIPtr,
@test f == s
@test parent(f) !== parent(s)
@test adapt(FixedSizeArray, transpose(s)) isa Transpose{Float64, <:FixedSizeArray}
# An adaptor which is not an array type (e.g. the one converting the arguments of
# a GPU kernel) has storage rules only for the arrays it knows, so the memory
# backend is adapted and the array rewrapped, with and without static dimensions
v = wrap(FixedSizeArray, [1.0, 3.0, 2.0, 4.0], (2, 2))
GC.@preserve v s begin
d = adapt(TestKernelAdaptor(), v)
@test d isa FixedSizeArray{Float64, 2, TestDeviceVector{Float64}}
@test isbits(d)
@test size(d) == size(v)
@test d == v
@test adapt(TestKernelAdaptor(), d) === d
ds = adapt(TestKernelAdaptor(), s)
@test ds isa FixedSizeArrayDims{Float64, 2, Tuple{StaticInt{2}, Int}, TestDeviceVector{Float64}}
@test isbits(ds)
@test ds == s
@test adapt(TestKernelAdaptor(), transpose(v)) isa Transpose{Float64, <:FixedSizeArray{Float64, 2, TestDeviceVector{Float64}}}
end
# Adapting to an array type still converts as a whole
@test adapt(Array, v) isa Matrix{Float64}
end


Expand Down
Loading