# Assigning to an array that is on the GPU

**URL:** https://discourse.julialang.org/t/assigning-to-an-array-that-is-on-the-gpu/38498
**Category:** Machine Learning
**Created:** [April 30, 2020, 5:49pm UTC](https://discourse.julialang.org/t/assigning-to-an-array-that-is-on-the-gpu/38498 "2020-04-30T17:49:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)
#### Post date: [April 30, 2020, 5:49pm UTC](https://discourse.julialang.org/t/assigning-to-an-array-that-is-on-the-gpu/38498/1 "2020-04-30T17:49:41Z")

</div>

Suppose an array is on the gpu. In assigning new values to it, should we put the right hand side of the assignment on the gpu first before doing the assignment. I.e.,

```julia
  A = cu(rand(5,1))
  B = zeros(5,1)

```

is this ok

```julia
   A .= B

```

or this is needed

```julia
  A .= cu(B)

```

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [April 30, 2020, 7:47pm UTC](https://discourse.julialang.org/t/assigning-to-an-array-that-is-on-the-gpu/38498/2 "2020-04-30T19:47:39Z")

</div>

Is the goal to concatenate A and B or to actually assign it a new value? You are broadcasting the assignment operator which I’m not familiar with.

Have a look at these examples and maybe it will answer your question:

```julia
using CuArrays

A = cu(rand(5,1))
B = zeros(5,1)

julia> C = vcat(A,B)
10×1 CuArray{Float64,2,Nothing}:
 0.8463374376296997
 0.604820966720581
 0.7880327105522156
 0.4833880066871643
 0.39194774627685547
 0.0
 0.0
 0.0
 0.0
 0.0

```

It looks like concatenating the two returns a CuArray. This can be further verified like so:

```julia
julia> typeof(C)
CuArray{Float64,2,Nothing}

```

If you wanted to push each item in B to A you could do this (but note the Warning message it produces):

```julia
A = cu(vec(rand(5,1)))
B = zeros(5,1)

julia> push!(A, B...)
┌ Warning: Performing scalar operations on GPU arrays: This is very slow, consider disallowing these operations with `allowscalar(false)`
└ @ GPUArrays C:\Users\mthel\.julia\packages\GPUArrays\WZupy\src\host\indexing.jl:43
10-element CuArray{Float32,1,Nothing}:
 0.21138956
 0.0049796794
 0.7363381
 0.58986074
 0.8373987
 0.0
 0.0
 0.0
 0.0
 0.0

```

For an actual re-assignment:

```julia
A = cu(rand(5,1))
B = zeros(5,1)

julia> A = cu(B)
5×1 CuArray{Float32,2,Nothing}:
 0.0
 0.0
 0.0
 0.0
 0.0

```

---

<div class="post-metadata">

### Author: ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)
#### Post date: [May 2, 2020, 10:30pm UTC](https://discourse.julialang.org/t/assigning-to-an-array-that-is-on-the-gpu/38498/3 "2020-05-02T22:30:42Z")

</div>

The goal is to update the array with new data (that is available on the cpu).

The .= syntax is important.

I believe that `A .= B`` causes the contents of A to be updated with those of B but the memory location of A does not change, whereas `A = cu(B)``` causes A to point to cu(B), so is pointing to a different area of memory.

Of course I should just try it also, however sometimes in asking your expert answer reveals the more general principle
