当有两个 gpu 时,如何设置 Torch 只使用一个 gpu?

How to set Torch to use only one gpu when there are two gpu?

我的电脑有两个 GPU。这是我第一次使用两个 GPU。当我有一个 GPU 时,我只是 运行 Cuda 程序,它 运行 在唯一的一个 GPU 上运行。但是,我不知道如何控制程序使用哪个GPU以及如何运行在唯一的一个GPU上编程。我搜索了 Internet,post

export CUDA_VISIBLE_DEVICES=0

这必须在 运行 程序之前使用。我有两个程序要运行。一个是torch脚本,一个是cuda脚本。我打开了两个终端,在第一个终端中,我使用了上面的命令和 运行 火炬程序。之后,在第二个终端中,我也使用了上面的命令,只是将数字从 0 更改为 1 和 运行 cuda 程序。

但是看到nvidia-smi的图片,显示这两个程序分配给了第0个GPU。我想将 torch 程序(PID 19520)分配给第 0 个 GPU,将 cuda 程序(PID 20351)分配给第一个 GPU。

如何将两个程序分配给不同的GPU设备?

以下是火炬脚本的设置。 (Ubuntu 14.04, nvidia titan gtx x, cuda-7.5)

--[[command line arguments]]--
cmd = torch.CmdLine()
cmd:text()
cmd:text('Train a Recurrent Model for Visual Attention')
cmd:text('Example:')
cmd:text('$> th rnn-visual-attention.lua > results.txt')
cmd:text('Options:')
cmd:option('--learningRate', 0.01, 'learning rate at t=0')
cmd:option('--minLR', 0.00001, 'minimum learning rate')
cmd:option('--saturateEpoch', 800, 'epoch at which linear decayed LR will reach minLR')
cmd:option('--momentum', 0.9, 'momentum')
cmd:option('--maxOutNorm', -1, 'max norm each layers output neuron weights')
cmd:option('--cutoffNorm', -1, 'max l2-norm of contatenation of all gradParam tensors')
cmd:option('--batchSize', 20, 'number of examples per batch')
cmd:option('--cuda', true, 'use CUDA')
cmd:option('--useDevice', 1, 'sets the device (GPU) to use')
cmd:option('--maxEpoch', 2000, 'maximum number of epochs to run')
cmd:option('--maxTries', 100, 'maximum number of epochs to try to find a better local minima for early-stopping')
cmd:option('--transfer', 'ReLU', 'activation function')
cmd:option('--uniform', 0.1, 'initialize parameters using uniform distribution between -uniform and uniform. -1 means default initialization')
cmd:option('--xpPath', '', 'path to a previously saved model')
cmd:option('--progress', false, 'print progress bar')
cmd:option('--silent', false, 'dont print anything to stdout')
CUDA_VISIBLE_DEVICES=0 th [torch script]
CUDA_VISIBLE_DEVICES=1 [CUDA script]