How to change the default device of GPU? device_ids[0]

Great answer !!! It helps me a lot.

I tried

CUDA_VISIBLE_DEVICES=3 python test.py

and it doesn’t work for me.

But

export CUDA_VISIBLE_DEVICES=3
python test.py

does work for me.

2 Likes

@lynnea718
The following works properly.

import os

os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"   
os.environ["CUDA_VISIBLE_DEVICES"]=<YOUR_GPU_NUMBER_HERE>

Usually GPU numbers start from 0. Since you have issue with the default device, try anything other that 0.

6 Likes

Thanks a lot for this solution!

Thank you, life saver

Just want to add to this answer, this environment variable should be set at the top of the program ideally. Changing the CUDA_VISIBLE_DEVICES var will not work if it is called after setting torch.backends.cudnn.benchmark.
This might also be true for other torch/cuda related calls as well so it’s better to set the environment vars at the program start or use export CUDA_VISIBLE_DEVICES="NUM" before starting the program.

1 Like