How to change the default device of GPU? device_ids[0]
<SCODE>if output_device is None:
output_device =device_ids[0]
<ECODE> to <SCODE>if output_device is None:
output_device =device_ids[1]
<ECODE>
lynnea718
<SCODE>CUDA_VISIBLE_DEVICES=1,2 python myscript.py
<ECODE> so your script will only see GPUs number 1 and 2, and won’t touch the other GPUs
fmassa
We have 8 gpu. I used the devices[1,2] ,the rest is used by others
lynnea718
colesbury
So in the Python script, How to choose the GPU devices. Especially, I want to change GPU devices in the training processing
cold_wind
I also want to know how to choose the GPU device in the python script.
uzeful
ex: <SCODE>with torch.cuda.device(1):
    w = torch.FloatTensor(2,3).cuda()
    # w was placed in  device_1 by default.
<ECODE> <SCODE>w = torch.FloatTensor(2,3).cuda(2)
# w was placed in device_2

<ECODE>
Jing
uzeful
<SCODE>CUDA_VISIBLE_DEVICES=3; python test.py
<ECODE> <SCODE>import torch
print(torch.cuda.current_device())
<ECODE> the above script still shows that current device is 0.
jdhao
two things you did wrong:
SimonW
jdhao
That controls what devices CUDA exposes and PyTorch can’t do nothing in this regards.
SimonW
Hi, you can specify used gpu in python script as following:
hszhao
it cannot work for me, it always use the first(ie, 0) gpu
jianzhong
Thanks, this is the easiest way to solve this problem.
kaiyuyue
It shouldn’t happen. That is a CUDA flag. Once set, PyTorch will never have access to the excluded device(s).
SimonW
moonlightlane
This is a very useful solution, especially you are going to run with someone else’s code without specifying the cuda id.
maplewizard
<SCODE>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.
<ECODE>
Sun_ShiJie