Reputation: 13218
I am training neural nets with theano and lasagne on a 4 GPU machine. My .theanorc
contains the following lines:
[global]
device = gpu0
So when in python I execute import theano
, I get Using gpu device 0: GRID K520
What if, after importing theano, I chose to use say gpu1? I'd like to do this dynamically, that is, without editing .theanorc
is it possible? Or even to choose it at runtime?
Upvotes: 10
Views: 5482
Reputation: 2323
EDIT: Theano is now based on the GPU array backend and the following API is no longer available.
As @EelkeSpaak mentioned, you can't change the GPU device after theano was imported. But if you want to choose it programmatically before that's possible without changing environment variables.
Make sure you're not choosing a device in your .theanorc file. So nothing like:
device=gpu
before calling import theano
choose the GPU device as follows:
import theano.sandbox.cuda
theano.sandbox.cuda.use('gpu1')
#Results in Using gpu device 1: Tesla K80
Upvotes: 5
Reputation: 2825
I'm afraid it's not possible to change the execution device after Theano has been imported. From the documentation:
config.device
String value: either 'cpu', 'gpu', 'gpu0', 'gpu1', 'gpu2', or 'gpu3'
[...]
This flag’s value cannot be modified during the program execution.
Bonus: however, let's say you wanted to have two Python processes each running on a separate GPU (is that what you want?), then you could do something like:
import os
os.system("THEANO_FLAGS='device=gpu0' python myscript.py")
os.system("THEANO_FLAGS='device=gpu1' python myscript.py")
or hack into/extend Python's multiprocessing module (which works by spawning subprocesses) to ensure the flag is set before a child process is spawned.
Upvotes: 11