# Problems copying data on the GPU

**URL:** <https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518>\
**Category:** GPU\
**Created:** [September 30, 2020, 2:51am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518 "2020-09-30T02:51:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 30, 2020, 2:51am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/1 "2020-09-30T02:51:04Z")

</div>

I’m having trouble with a basic copy operation on the GPU. My goal is to copy a chosen subset of data from an N-D array into a vector. My following two attempts have failed.

```julia
using CUDA
CUDA.allowscalar(false)
a=CUDA.rand(2,4)
c=cu([CartesianIndex(1,2), CartesianIndex(2,3)])
b = a[c]

```

Fails due to scalar operation

```julia
b=CUDA.zeros(2)
copyto!(b, CartesianIndices(b), a, c)

```

Produces a `MethodError`

I must be missing something very simple. Any advice? I know I can write a custom kernel, but this seems like a fairly generic copy operation.

---

<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:** [September 30, 2020, 6:48am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/2 "2020-09-30T06:48:30Z")

</div>

Use a `view`:

```julia
julia> c = view(a, [CartesianIndex(1,2), CartesianIndex(2,3)])
2-element view(::CuArray{Float32,2}, CartesianIndex{2}[CartesianIndex(1, 2), CartesianIndex(2, 3)]) with eltype Float32:
 0.81763107
 0.610714

julia> copyto!(b, c)
2-element CuArray{Float32,1}:
 0.81763107
 0.610714

```

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 30, 2020, 11:03am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/3 "2020-09-30T11:03:45Z")

</div>

> [@maleadt](#):
>
> `c = view(a, [CartesianIndex(1,2), CartesianIndex(2,3)])`

I should have mentioned that I started with a `view` and found it is treated as a scalar operation.

```julia
using CUDA
CUDA.allowscalar(false)
a=CUDA.rand(2,4)
c=cu([CartesianIndex(1,2), CartesianIndex(2,3)])
c = view(a, c)

```

If the CartesianIndices are on the CPU, then view works. In my case they begin on the GPU (returned from `findall()`) and benchmarking shows I should avoid copying them to the 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:** [September 30, 2020, 11:35am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/4 "2020-09-30T11:35:51Z")

</div>

Note that I’m using the CUDA.jl master branch, where’s there has been a bunch of SubArray changes and fixes.

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 30, 2020, 2:16pm UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/5 "2020-09-30T14:16:29Z")

</div>

The master branch works. Thanks!

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 30, 2020, 4:58pm UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/6 "2020-09-30T16:58:08Z")

</div>

Do you know when you plan to release v1.3.4?

---

<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:** [September 30, 2020, 6:46pm UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/7 "2020-09-30T18:46:28Z")

</div>

This week 🙂 Tentative release notes up here: [https://juliagpu.org/2020-09-28-cuda\_2.0/](https://juliagpu.org/2020-09-28-cuda_2.0/)

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 30, 2020, 9:54pm UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/8 "2020-09-30T21:54:08Z")

</div>

I’m finding 2.0 is breaking a lot of our codebase.

Objects created from view(reinterpret(x)) and view(reshape(x)) are now producing errors when I broadcast into them. Should I start opening bug reports?

---

<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:** [October 1, 2020, 6:22am UTC](https://discourse.julialang.org/t/problems-copying-data-on-the-gpu/47518/9 "2020-10-01T06:22:24Z")

</div>

Please do. Note that the new behavior is much more in line with Base, so if you’re getting errors its likely that corresponding Array operations would have been slow too (i.e., they wouldn’t have dispatched to BLAS).
