torch.cuda keeps track of currently selected GPU, and all CUDA tensors you allocate will be created on it. The selected device can be changed with a torch.cuda.device context manager.
ex:
with torch.cuda.device(1):
w = torch.FloatTensor(2,3).cuda()
# w was placed in device_1 by default.
Or you can specify gpu.id via .cuda() directly.
w = torch.FloatTensor(2,3).cuda(2)
# w was placed in device_2
Thanks for your reply, I will do some experiments to verify these functions. Besides, I found out some other useful functions at How to specify GPU usage?.
I have tried to set CUDA_VISIBLE_DEVICES in shell, then I run a simple script to test if the setting has taken effect, unfortunately, it seems does not work.
CUDA_VISIBLE_DEVICES=3; python test.py
the script test.py is
import torch
print(torch.cuda.current_device())
the above script still shows that current device is 0.
I find that torch.cuda.set_device(device_num) works fine in setting the desired GPU to use.
there shouldn’t be semicolon. with the semicolon, they are on two different lines, and python won’t see it.
even with the correct command CUDA_VISIBLE_DEVICES=3 python test.py, you won’t see torch.cuda.current_device() = 3, because it completely changes what devices pytorch can see. So in pytorch land device#0 is actually your device#3 of the system. You can verify that from nvidia-smi.
According to the tutorial, it’s better to use environment variable
torch.cuda.set_device(device)
Sets the current device.
Usage of this function is discouraged in favor of device. In most cases it’s better to use CUDA_VISIBLE_DEVICES environmental variable.
Parameters: device (torch.device or int) – selected device. This function is a no-op if this argument is negative.