How to convert a cup array to a gpu array

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:

# 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> 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

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

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

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

2 Likes

I both used the Flux and CUDA package

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

1 Like