# Select a device for the entire program

**URL:** <https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603>\
**Category:** GPU\
**Created:** [June 24, 2019, 1:52pm UTC](https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603 "2019-06-24T13:52:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 24, 2019, 1:52pm UTC](https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603/1 "2019-06-24T13:52:53Z")

</div>

**Question:**  
To my understanding, CuArrays/CUDAnative/CUDAdrv selects the device 0 by default. How can I select a different device at the beginning of my program, that will be used in the rest of the program for any GPU operations (from any of CuArrays/CUDAnative/CUDAdrv).

**Motivation:**  
At the beginning of an MPI program, I would like to select a GPU in function of the MPI rank…

Thanks!

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [June 24, 2019, 2:07pm UTC](https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603/2 "2019-06-24T14:07:56Z")

</div>

You can use `CUDAnative.device!`. That should work, but I can imagine that not every part of the Julia/GPU ecosystem works perfectly together with that. In that case you can always use the `CUDA_VISIBLE_DEVICES` environment variable to have CUDA select a device.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [June 24, 2019, 2:09pm UTC](https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603/3 "2019-06-24T14:09:17Z")

</div>

Hello @samo With CUDA the usual method is to set the environment variable CUDA\_VISIBLE\_DEVICES  
For example in te bash shell to run on GPU number 1  
export CUDA\_VISIBLE\_DEVICES=1

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 24, 2019, 4:22pm UTC](https://discourse.julialang.org/t/select-a-device-for-the-entire-program/25603/4 "2019-06-24T16:22:20Z")

</div>

Thanks a lot! As `CUDAnative.device!` is not totally sure to work, we will probably use `CUDA_VISIBLE_DEVICES`…
