Select GPU during execution in Theano
Asked Answered
R

2

10

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?

Redundancy answered 13/8, 2015 at 9:0 Comment(0)
B
11

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.

Brauer answered 13/8, 2015 at 9:7 Comment(0)
H
5

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.

  1. Make sure you're not choosing a device in your .theanorc file. So nothing like:

    device=gpu

  2. 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
    
Hakeem answered 24/4, 2016 at 21:27 Comment(2)
I'm getting this error: RuntimeError: ('initCnmem: cnmemInit call failed! Reason=CNMEM_STATUS_OUT_OF_MEMORY. numdev=1\n', 'You asked to force this device and it failed. No fallback to the cpu or other gpu device.'). This was after the first import statement, couldn't even get to setting the gpu.Artillery
@Sachin_ruk that's a different issue. You might want to ask a new question.Hakeem

© 2022 - 2024 — McMap. All rights reserved.