# multiple-GPUs per process

**URL:** https://discourse.julialang.org/t/multiple-gpus-per-process/98008
**Category:** GPU
**Created:** [April 27, 2023, 7:09pm UTC](https://discourse.julialang.org/t/multiple-gpus-per-process/98008 "2023-04-27T19:09:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [April 27, 2023, 7:09pm UTC](https://discourse.julialang.org/t/multiple-gpus-per-process/98008/1 "2023-04-27T19:09:57Z")

</div>

i’m not sure whether i should file an issue on github because this is a bug, or post a plea for help here because i’m doing something wrong, but riffing on the [multiple GPUs per process](https://cuda.juliagpu.org/stable/usage/multigpu/#Scenario-2:-Multiple-GPUs-per-process) example, and adding to it an Array of CuArrays, and trying to be careful about [garbage collection](https://discourse.julialang.org/t/arrays-of-arrays-and-arrays-of-structures-in-cuda-kernels-cause-random-errors/69739), i’m getting “ **ERROR:** CUDA error: an illegal memory access was encountered”:

```julia
julia> using CUDA

julia> function alloc(x)
           sum(x) # commenting out this line results in no errors
           CUDA.zeros(3)
       end
alloc (generic function with 1 method)

julia> results = Vector{Any}(undef, ndevices())
2-element Vector{Any}:
 #undef
 #undef

julia> not_used = [(device!(idevice-1); CuArray([1,2,3])) for idevice=1:ndevices()];

julia> synchronize()

julia> GC.@preserve not_used begin
           @sync for idevice = 1:ndevices()
               @async begin
                   device!(idevice-1)
                   results[idevice] = Array(alloc(not_used))
               end
           end
       end
ERROR: CUDA error: an illegal memory access was encountered (code 700, ERROR_ILLEGAL_ADDRESS)
Stacktrace:
 [1] throw_api_error(res::CUDA.cudaError_enum)
   @ CUDA /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/lib/cudadrv/libcuda.jl:27
 [2] isdone
   @ /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/lib/cudadrv/stream.jl:109 [inlined]
 [3] nonblocking_synchronize
   @ /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/lib/cudadrv/stream.jl:139 [inlined]
 [4] nonblocking_synchronize
   @ /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/lib/cudadrv/context.jl:325 [inlined]
 [5] device_synchronize()
   @ CUDA /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/lib/cudadrv/context.jl:319
 [6] top-level scope
   @ /groups/scicompsoft/home/arthurb/.julia/packages/CUDA/s0e3j/src/initialization.jl:164

caused by: WARNING: Error while freeing DeviceBuffer(12 bytes at 0x0000000402000400):
CUDA.CuError(code=CUDA.cudaError_enum(0x000002bc), meta=nothing)

```

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [April 27, 2023, 7:18pm UTC](https://discourse.julialang.org/t/multiple-gpus-per-process/98008/2 "2023-04-27T19:18:51Z")

</div>

note that this error only occurs on a machine with multiple GPUs. the error can be fixed not only by commenting out `sum(x)`, but also by starting julia with `CUDA_VISIBLE_DEVICES=0`

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 27, 2023, 7:36pm UTC](https://discourse.julialang.org/t/multiple-gpus-per-process/98008/3 "2023-04-27T19:36:22Z")

</div>

It could also be a CUDA.jl discussion:

> **[JuliaGPU/CUDA.jl · Discussions](https://github.com/JuliaGPU/CUDA.jl/discussions)**
>
> Explore the GitHub Discussions forum for JuliaGPU CUDA.jl. Discuss code, ask questions & collaborate with the developer community.

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [April 27, 2023, 8:18pm UTC](https://discourse.julialang.org/t/multiple-gpus-per-process/98008/4 "2023-04-27T20:18:26Z")

</div>

nevermind. found the bug. works fine if i pass `not_used[idevice]` to alloc().
