# Adapt BroadcastStyle for CUDA

**URL:** <https://discourse.julialang.org/t/adapt-broadcaststyle-for-cuda/126861>\
**Category:** GPU\
**Tags:** cuda, broadcasting, adapt\
**Created:** [March 12, 2025, 10:59am UTC](https://discourse.julialang.org/t/adapt-broadcaststyle-for-cuda/126861 "2025-03-12T10:59:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RainerHeintzmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rainerheintzmann/32/19726_2.png) [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Post date:** [March 12, 2025, 10:59am UTC](https://discourse.julialang.org/t/adapt-broadcaststyle-for-cuda/126861/1 "2025-03-12T10:59:32Z")

</div>

I am trying to get CUDA.jl to work seamlessly with a [`MutableShiftedArray`](https://github.com/RainerHeintzmann/MutableShiftedArrays.jl) .  
By writing a CUDASupportExt.jl class:

```julia
module CUDASupportExt
using CUDA 
using Adapt
using MutableShiftedArrays
indexing CUDA error

# lets do this for the MutableShiftedArray type
Adapt.adapt_structure(to, x::MutableShiftedArray) = MutableShiftedArray(adapt(to, parent(x)), shifts(x), size(x); default=MutableShiftedArrays.default(x));

function Base.Broadcast.BroadcastStyle(::Type{T}) where {CT, N, CD, T<:MutableShiftedArray{<:Any,<:Any,<:Any,<:CuArray{CT,N,CD}}}
    CUDA.CuArrayStyle{N,CD}()
end

# Define the BroadcastStyle for SubArray of MutableShiftedArray with CuArray
function Base.Broadcast.BroadcastStyle(::Type{T}) where {CT, N, CD, T<:SubArray{<:Any, <:Any, <:MutableShiftedArray{<:Any,<:Any,<:Any,<:CuArray{CT,N,CD}}}}
    CUDA.CuArrayStyle{N,CD}()
end

end

```

this works in general fine and for a `MutableShiftedArray` `ma` wrapping a `CuArray` you can broadcast: `q = ma .+ 1` or even use views: `(@view ma[1:4]) .+1`, since the broadcasting mechanism somehow compiles the single element access version of `getindex` correctly.  
However, this does not seem to be the case for `copy(ma)` or subindexing like `ma[1:4]` without the `view`. One could start specifying `copy` and the `getindex()` function for `Union{Int, AbstractRange}` to handle this, but this sound wrong.  
Is there no other way, such that this is not necessary and the broadcasting system can take care of the individual element access as it does in the other broadcast cases?  
In the end, single-element accesses are anyway generated, but one does get the warning that this is not handled by CUDA and therefore slow.

---

<div class="post-metadata">

**Author:** ![RainerHeintzmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rainerheintzmann/32/19726_2.png) [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Post date:** [March 18, 2025, 5:50pm UTC](https://discourse.julialang.org/t/adapt-broadcaststyle-for-cuda/126861/2 "2025-03-18T17:50:40Z")

</div>

I ended up needing to implement a number of such function such as:  
`copy`  
`collect`  
`Array`  
`==`  
Each of them performs the wanted operations in a broadcasting way instead. I assume that more such functions are missing.
