# How to convert a cup array to a gpu array

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983>\
**Category:** General Usage\
**Created:** [October 30, 2024, 8:04am UTC](https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983 "2024-10-30T08:04:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yaozhong\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/a587f6/32.png) [@Yaozhong\_Liu](https://discourse.julialang.org/u/Yaozhong_Liu)\
**Post date:** [October 30, 2024, 8:04am UTC](https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983/1 "2024-10-30T08:04:08Z")

</div>

Hi.  
I know how to create a gpu array, but has no clue how to convert an initialized array to a gpu array in a julia file rather than the julia prompt.  
I tried code like this:

```julia
# In a julia file
A = [1.0, 2.0, 3.0] |> gpu
C = [1.0, 2.0, 3.0] 
println(typeof(C)) #Vector{Float64}
C |> gpu
println(typeof(C)) #Vector{Float64}
println(typeof(A)) #CuArray{Float32, 1, CUDA.DeviceMemory}

```

So in a julia file, I cannot convert using “|\> gpu”

However, it works in julia prompt.

```julia
julia> C = [1.0,2.0,3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> C |> gpu
3-element CuArray{Float32, 1, CUDA.DeviceMemory}:
 1.0
 2.0
 3.0

```

I wonder how to convert in a julia file

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 30, 2024, 8:08am UTC](https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983/3 "2024-10-30T08:08:19Z")

</div>

> [@Yaozhong\_Liu](#):
>
> `C |> gpu`

you’re not assigning the result to anything, you need

```julia
C1 = gpu(C) #or C |> gpu
@show typeof(C1)

```

* * *

> [@Yaozhong\_Liu](#):
>
> ```julia
> julia> C |> gpu
> 3-element CuArray{Float32, 1, CUDA.DeviceMemory}:
> 1.0
> 2.0
> 3.0
> 
> ```

this is just displaying the result of `gpu(C)`, it’s not changing the binding to the variable name `C`, if you `C |> typeof |> println` in the next prompt you will see what I mean

---

<div class="post-metadata">

**Author:** ![Yaozhong\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/a587f6/32.png) [@Yaozhong\_Liu](https://discourse.julialang.org/u/Yaozhong_Liu)\
**Post date:** [October 30, 2024, 8:08am UTC](https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983/4 "2024-10-30T08:08:40Z")

</div>

I both used the Flux and CUDA package

---

<div class="post-metadata">

**Author:** ![Yaozhong\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/a587f6/32.png) [@Yaozhong\_Liu](https://discourse.julialang.org/u/Yaozhong_Liu)\
**Post date:** [October 30, 2024, 8:10am UTC](https://discourse.julialang.org/t/how-to-convert-a-cup-array-to-a-gpu-array/121983/5 "2024-10-30T08:10:20Z")

</div>

Oh yes. I made a stupid mistake.  
Thanks for helping!
