# Is there a way to use @allowscalar in a heterogeneous manner using KernelAbstractions?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828>\
**Category:** GPU\
**Tags:** question, gpuarrays, kernelabstractions\
**Created:** [April 8, 2025, 3:24am UTC](https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828 "2025-04-08T03:24:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![0samuraiE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0samuraie/32/209825_2.png) [@0samuraiE](https://discourse.julialang.org/u/0samuraiE)\
**Post date:** [April 8, 2025, 3:24am UTC](https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828/1 "2025-04-08T03:24:09Z")

</div>

Hi all,

Is it correct that scalar indexing like `Out[1]` is not allowed with `KernelAbstractions.jl` alone, and that we need `GPUArrays.@allowscalar` for this? Should we always use `GPUArrays` together with `KernelAbstractions` for such cases?

```julia
using KernelAbstractions
using CUDA
using GPUArrays

backend = CUDABackend()
Out = KernelAbstractions.zeros(backend, Float64, 1)
GPUArrays.@allowscalar Out[1]

```

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [April 9, 2025, 8:24am UTC](https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828/2 "2025-04-09T08:24:34Z")

</div>

> [@0samuraiE](#):
>
> we need `GPUArrays.@allowscalar` for this

You can simply copy the array back to the CPU first, which is what `Out[1]` does behind the scenes anyway.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [April 9, 2025, 12:06pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828/3 "2025-04-09T12:06:05Z")

</div>

Is CUDA.jl copy the whole array back to CPU?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [April 10, 2025, 6:33am UTC](https://discourse.julialang.org/t/is-there-a-way-to-use-allowscalar-in-a-heterogeneous-manner-using-kernelabstractions/127828/4 "2025-04-10T06:33:57Z")

</div>

It does not: [GPUArrays.jl/src/host/indexing.jl at e8e9b031613f31818e75a6c7f8745788fb80b71f · JuliaGPU/GPUArrays.jl · GitHub](https://github.com/JuliaGPU/GPUArrays.jl/blob/e8e9b031613f31818e75a6c7f8745788fb80b71f/src/host/indexing.jl#L48-L54)  
So yes, in the case your data is very large it’s better to index a single item or perform a fine-grained `copy` yourself.
