Skip to content

Adapt the memory backend of a FixedSizeArray for adaptors that are not array types - #235

Open
vchuravy wants to merge 1 commit into
mainfrom
vc/adapt-fixedsizearray-structure
Open

vchuravy wants to merge 1 commit into
mainfrom
vc/adapt-fixedsizearray-structure

Conversation

@vchuravy

Copy link
Copy Markdown
Contributor

Problem

With Adapt.jl loaded, adapt(to, a) applies only the storage rule of to to a FixedSizeArray a as a whole:

Adapt.adapt_structure(to, a::FixedSizeArray) = Adapt.adapt_storage(to, a)

That is what we want for array types (adapt(CuArray, a) is a CuArray), but adaptors which are not array types usually have storage rules only for the arrays they know. In particular, the adaptors that convert the arguments of a GPU kernel turn a Vector or CuVector into an isbits device array and fall back to the identity for everything else. A FixedSizeArray therefore reaches the kernel unchanged and compilation fails. This shows up in trixi-framework/Trixi.jl#3357 with the POCL backend of KernelAbstractions.jl:

KernelError: passing non-bitstype argument
Argument 8 to your kernel function is of type FixedSizeArrays.FixedSizeArrayDims{Float64, 4, NTuple{4, Int64}, Vector{Float64}}, which is not a bitstype:
  .mem is of type Vector{Float64} which is not isbits.

Arrays with static dimensions are not affected, since their rule already adapts the memory backend and wraps it again. In the same kernel call the solution array (static dimensions) is converted correctly.

Change

The conversion as a whole is restricted to adaptors which are array types (to::Type{<:AbstractArray}). All other adaptors now use the rule of FixedSizeArrayDims: adapt the memory backend and wrap it again, with and without static dimensions.

Unchanged: adapt(Array, a), adapt(CuArray, a), etc. still convert a FixedSizeArray as a whole, and adapt(FixedSizeArray, x) behaves as before.

Changed: an adaptor that is not an array type but has a storage rule matching a FixedSizeArray as a whole (e.g. one for AbstractArray) is now given the memory backend instead, so the result is a FixedSizeArray over the adapted vector rather than whatever that rule returned for the whole array.

Testing

  • New tests with a small kernel-like adaptor (storage rule only for Vector, returning an isbits device vector), for arrays with and without static dimensions and inside a Transpose.
  • The package tests pass locally on Julia 1.14.0-beta1.
  • With this branch, the KernelAbstractions.jl test items of Trixi.jl that fail in Replace unsafe memory management in Trixi with FixedSizeArray trixi-framework/Trixi.jl#3357 compile and run their kernels again (checked for two of them, with the POCL backend).

The CHANGELOG entry is under a new "Unreleased" heading; I did not bump the version.

This PR was prepared with the assistance of Claude Code.

🤖 Generated with Claude Code

…not array types

`adapt(to, a)` applied only the storage rule of `to` to a `FixedSizeArray` as a
whole. Adaptors which are not array types (e.g. the ones converting the
arguments of a GPU kernel) have storage rules only for the arrays they know, so
they returned the `FixedSizeArray` unchanged, and a `FixedSizeArray` backed by
a `Vector` or a GPU vector could not be passed to a kernel ("passing
non-bitstype argument"). Arrays with static dimensions already adapted the
memory backend and were wrapped again.

Restrict the conversion as a whole to adaptors which are array types, e.g.
`adapt(CuArray, a)` is still a `CuArray`, and adapt the memory backend for all
other adaptors, with and without static dimensions.

Assisted-by: Claude Code (Fable 5.2)
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.48%. Comparing base (ab67d3f) to head (8aebb9f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #235      +/-   ##
==========================================
+ Coverage   94.73%   95.48%   +0.74%     
==========================================
  Files           8        8              
  Lines         323      332       +9     
==========================================
+ Hits          306      317      +11     
+ Misses         17       15       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vchuravy
vchuravy requested a review from giordano November 26, 2026 09:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants