# CUDA example in Julia doesn't use the GPU

**URL:** <https://discourse.julialang.org/t/cuda-example-in-julia-doesnt-use-the-gpu/75838>\
**Category:** General Usage\
**Tags:** cuda\
**Created:** [February 5, 2022, 1:37am UTC](https://discourse.julialang.org/t/cuda-example-in-julia-doesnt-use-the-gpu/75838 "2022-02-05T01:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [February 5, 2022, 1:37am UTC](https://discourse.julialang.org/t/cuda-example-in-julia-doesnt-use-the-gpu/75838/1 "2022-02-05T01:37:19Z")

</div>

I’m doing my first steps on running Julia 1.6.5 code on GPU. For some reason, it seems the GPU is not being used at all. These are the steps:

First of all, my GPU passed on the test recommended at [https://cuda.juliagpu.org/stable/](https://cuda.juliagpu.org/stable/):

```julia
# install the package
using Pkg
Pkg.add("CUDA")
  
# smoke test (this will download the CUDA toolkit)
using CUDA
CUDA.versioninfo()

using Pkg
Pkg.test("CUDA") # takes ~40 minutes if using 1 thread

```

Secondly, the below code took around 8 minutes (real time) for supposedly running on my GPU. It loads and multiplies, for 10 times, two matrices 10000 x 10000:

```julia
using CUDA
using Random
N = 10000

a_d = CuArray{Float32}(undef, (N, N))
b_d = CuArray{Float32}(undef, (N, N))
c_d = CuArray{Float32}(undef, (N, N))

for i in 1:10
    global a_d = randn(N, N)
    global b_d = randn(N, N)

    global c_d = a_d * b_d
end

global a_d = nothing
global b_d = nothing
global c_d = nothing
GC.gc()

```

Outcome on terminal as follows:

```julia
(base) ciro@ciro-G3-3500:~/projects/julia/cuda$ time julia cuda-gpu.jl

real 8m13,016s
user 50m39,146s
sys 13m16,766s

```

Then, an equivalent code for the CPU is run. Execution time is equivalent:

```julia
using Random
N = 10000

for i in 1:10
    a = randn(N, N)
    b = randn(N, N)

    c = a * b
end

```

Execution:

```julia
(base) ciro@ciro-G3-3500:~/projects/julia/cuda$ time julia cuda-cpu.jl

real 8m2,689s 
user 50m9,567s 
sys 13m3,738s

```

Moreover, by following the info on NVTOP screen command, it is weird to see the GPU memory and cores being loaded/unloaded accordingly, besides still using the same 800% CPUs (or eight cores) of my regular CPU, which is the same usage the CPU-version has.

Any hint is greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 5, 2022, 1:53am UTC](https://discourse.julialang.org/t/cuda-example-in-julia-doesnt-use-the-gpu/75838/2 "2022-02-05T01:53:24Z")

</div>

You’re just assigning a new CPU array to the same variable names you assigned the GPU arrays to. Your second `a_d` variable is unrelated to your first `a_d` variable.

To run `rand` on the GPU I think you can use `rand!(a_d)`. But the general idea is you have to assign the array and then operate on it. You can use a broadcast `a .= x` or a function like `rand!` that has a CUDA.jl version that will dispatch on your gpu array.

---

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [February 5, 2022, 3:17am UTC](https://discourse.julialang.org/t/cuda-example-in-julia-doesnt-use-the-gpu/75838/3 "2022-02-05T03:17:49Z")

</div>

It just works. Thanks.
