Select a device for the entire program

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!

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.

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

1 Like

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