How Can I use my GPU on Ipython Notebook?

user3704652 picture user3704652 · Dec 17, 2015 · Viewed 11.1k times · Source

OS : Ubuntu 14.04LTS
Language : Python Anaconda 2.7 (keras, theano)
GPU : GTX980Ti CUDA : CUDA 7.5

I wanna run keras python code on IPython Notebook by using my GPU(GTX980Ti)
But I can't find it.

I want to test below code. When I run it on to Ubuntu terminal, I command as below (It uses GPU well. It doesn't have any problem)

First I set the path like below

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH 

Second I run the code as below

THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True'  python myscript.py


And it runs well.

But when i run the code on pycharm(python IDE) or When I run it on Ipython Notebook, It doesn't use gpu. It only uses CPU

myscript.py code is as below.

from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time
vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

To solve it, I force the code use gpu as below (Insert two lines more on myscript.py)

import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")

Then It generate the error like below

ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.

how to do it??? I spent two days..
And I surely did the way of using '.theanorc' file at home directory.

Answer

Sentient07 picture Sentient07 · Feb 10, 2016

I'm using theano on an ipython notebook making use of my system's GPU. This configuration seems to work fine on my system.(Macbook Pro with GTX 750M)

My ~/.theanorc file :

[global]
cnmem = True
floatX = float32
device = gpu0

Various environment variables (I use a virtual environment(macvnev):

echo $LD_LIBRARY_PATH
/opt/local/lib:

echo $PATH
/Developer/NVIDIA/CUDA-7.5/bin:/opt/local/bin:/opt/local/sbin:/Developer/NVIDIA/CUDA-7.0/bin:/Users/Ramana/projects/macvnev/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

echo $DYLD_LIBRARY_PATH
/Developer/NVIDIA/CUDA-7.5/lib:/Developer/NVIDIA/CUDA-7.0/lib:

How I run ipython notebook (For me, the device is gpu0) :

$THEANO_FLAGS=mode=FAST_RUN,device=gpu0,floatX=float32 ipython notebook

Output of $nvcc -V :

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Thu_Sep_24_00:26:39_CDT_2015
Cuda compilation tools, release 7.5, V7.5.19

From your post, probably you've set the $PATH variable wrong.