HPMC User Guide v 1.00
© 2026 Bassem W. Jamaleddine


4-7

   GPU Programming with Theano


HPMC calculators have Theano already installed on them. You can program in python and run theano on your HPMC.

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. Theano combines the convenience of NumPy's syntax.

Make sure the following stanza is in your ~/.theanorc as this will define the option gpu-architecture as sm_52 for the nvcc program.

#  cat ~/.theanorc

[nvcc]
fastmath = True
flags = -arch=sm_30


To have theano started with (base) prompt, you can add the following script to /tools.

 -LIS- Listing. 4.7.1   [Script to source theano at the command prompt]
(raw text)
1.     export THEANO_FLAGS='device=cuda,floatX=float32'
2.     eval "$(/cuda9/miniconda3/bin/conda shell.bash hook)"

HPMC 2026


Get the prompt by simpy sourcing the script at the command prompt.

20:18 root@HPMC: ~ #  source /tools/theano

20:18 root@HPMC: ~ #  source /tools/theano
(base) 20:18 root@HPMC: ~ #  python exthea3.py


■ Theano Example


The following show an example of executing a theano script. Consider the following first example of a theano script.

 -LIS- Listing. 4.7.2   [Example of a Theano Script]
(raw text)
1.     from theano import function, config, shared, sandbox
2.     import theano.tensor as T
3.     import numpy
4.     import time
5.     
6.     vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
7.     iters = 1000
8.     
9.     rng = numpy.random.RandomState(22)
10.    x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
11.    f = function([], sandbox.cuda.basic_ops.gpu_from_host(T.exp(x)))
12.    print f.maker.fgraph.toposort()
13.    t0 = time.time()
14.    for i in xrange(iters):
15.        r = f()
16.    t1 = time.time()
17.    print 'Looping %d times took' % iters, t1 - t0, 'seconds'
18.    print 'Result is', r
19.    print 'Numpy result is', numpy.asarray(r)
20.    if numpy.any([isinstance(x.op, T.Elemwise) for x in 
      f.maker.fgraph.toposort()]): 
21.        print 'Used the cpu'
22.    else:
23.        print 'Used the gpu'
24.    

HPMC 2026




#  python exthea2.py

07:11 qn001: /appz/theano # python exthea2.py
Using gpu device 0: Graphics Device
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>)]
Looping 1000 times took 0.204324007034 seconds
Result is <CudaNdarray object at 0x6f05130>
Numpy result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu


Consider the following second example of a theano script.

 -LIS- Listing. 4.7.3   [Example of a Theano Script]
(raw text)
1.     import numpy
2.     import theano
3.     import theano.tensor as T
4.     rng = numpy.random
5.     
6.     N = 400
7.     feats = 784
8.     D = (rng.randn(N, feats).astype(theano.config.floatX),
9.     rng.randint(size=N,low=0, high=2).astype(theano.config.floatX))
10.    training_steps = 10000
11.    
12.    # Declare Theano symbolic variables
13.    x = T.matrix("x")
14.    y = T.vector("y")
15.    w = theano.shared(rng.randn(feats).astype(theano.config.floatX), 
      name="w") 
16.    b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), 
      name="b") 
17.    x.tag.test_value = D[0]
18.    y.tag.test_value = D[1]
19.    #print "Initial model:"
20.    #print w.get_value(), b.get_value()
21.    
22.    # Construct Theano expression graph
23.    p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one
24.    prediction = p_1 > 0.5 # The prediction that is done: 0 or 1
25.    xent = -y*T.log(p_1) - (1-y)*T.log(1-p_1) # Cross-entropy
26.    cost = xent.mean() + 0.01*(w**2).sum() # The cost to optimize
27.    gw,gb = T.grad(cost, [w,b])
28.    
29.    # Compile expressions to functions
30.    train = theano.function(
31.                inputs=[x,y],
32.                outputs=[prediction, xent],
33.                updates={w:w-0.01*gw, b:b-0.01*gb},
34.                name = "train")
35.    predict = theano.function(inputs=[x], outputs=prediction,
36.                name = "predict")
37.    
38.    if any([x.op.__class__.__name__ in ['Gemv', 'CGemv', 'Gemm', 'CGemm'] 
      for x in 
39.            train.maker.fgraph.toposort()]):
40.        print 'Used the cpu'
41.    elif any([x.op.__class__.__name__ in ['GpuGemm', 'GpuGemv'] for x in
42.              train.maker.fgraph.toposort()]):
43.        print 'Used the gpu'
44.    else:
45.        print 'ERROR, not able to tell if theano used the cpu or the gpu'
46.        print train.maker.fgraph.toposort()
47.    
48.    for i in range(training_steps):
49.        pred, err = train(D[0], D[1])
50.    #print "Final model:"
51.    #print w.get_value(), b.get_value()
52.    
53.    print "target values for D"
54.    print D[1]
55.    
56.    print "prediction on D"
57.    print predict(D[0])

HPMC 2026


#  python exthea3.py

07:11 qn001: /appz/theano # python exthea3.py
Using gpu device 0: Graphics Device
Used the gpu
target values for D
[ 1.  0.  1.  1.  0.  1.  1.  0.  0.  0.  0.  1.  0.  0.  1.  0.  0.  1.
  0.  0.  0.  1.  0.  0.  1.  0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  0.
  0.  0.  0.  1.  1.  0.  1.  0.  0.  0.  1.  0.  0.  1.  1.  0.  0.  0.
  1.  0.  1.  1.  0.  1.  1.  1.  1.  0.  0.  1.  1.  1.  0.  1.  1.  0.
  1.  1.  1.  1.  0.  1.  0.  1.  1.  1.  1.  0.  0.  0.  1.  1.  1.  0.
  1.  0.  0.  0.  0.  1.  0.  0.  1.  1.  1.  1.  0.  1.  1.  1.  0.  0.
  0.  0.  1.  0.  0.  0.  1.  0.  0.  1.  1.  1.  1.  0.  0.  0.  1.  1.
  1.  0.  1.  0.  0.  0.  0.  0.  1.  0.  1.  0.  1.  1.  1.  0.  0.  0.
  1.  1.  0.  0.  0.  1.  1.  0.  0.  1.  1.  0.  1.  1.  0.  0.  0.  1.
  0.  1.  0.  1.  0.  0.  1.  0.  1.  0.  0.  1.  1.  1.  0.  1.  0.  0.
  1.  1.  0.  1.  0.  0.  0.  0.  1.  1.  1.  1.  0.  1.  1.  1.  0.  0.
  0.  1.  1.  1.  0.  0.  1.  1.  0.  1.  1.  0.  1.  1.  1.  1.  1.  0.
  0.  0.  0.  0.  0.  1.  0.  0.  1.  0.  0.  0.  0.  1.  0.  0.  0.  1.
  0.  1.  1.  0.  1.  1.  1.  1.  1.  1.  1.  1.  0.  1.  0.  0.  1.  1.
  1.  0.  1.  1.  0.  1.  0.  0.  0.  1.  0.  0.  1.  1.  0.  1.  0.  0.
  0.  1.  1.  1.  0.  1.  0.  0.  0.  1.  1.  1.  0.  0.  0.  1.  1.  1.
  0.  1.  0.  0.  0.  1.  1.  0.  0.  1.  1.  1.  0.  0.  1.  1.  1.  0.
  1.  0.  1.  1.  1.  1.  1.  0.  1.  0.  0.  1.  0.  0.  1.  1.  0.  1.
  1.  1.  1.  1.  1.  1.  0.  0.  0.  0.  0.  1.  1.  0.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  0.  1.  1.  0.  1.  1.  1.  1.  0.  0.  0.  0.
  0.  0.  0.  1.  0.  1.  1.  1.  0.  1.  1.  0.  1.  0.  1.  1.  1.  1.
  1.  0.  0.  1.  1.  1.  0.  0.  0.  1.  0.  1.  0.  1.  0.  1.  1.  1.
  1.  1.  0.  0.]
prediction on D
[1 0 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 1 1 1 1 0 0
 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 1 1 1 0 1 1 0 1 1
 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 1
 0 0 0 1 0 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1 0 0
 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0
 0 0 0 1 1 1 1 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 0 0 0 0 0 1
 0 0 1 0 0 0 0 1 0 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0
 0 0 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 0 1 0 0 0 1 1 0
 0 1 1 1 0 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 0 1 0 0 1 1 0 1 1 1 1 1 1 1 0 0 0
 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1
 1 0 1 0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 0 1 0 1 1 1 1 1 0 0]