In [2]:
import numpy as np
import tensorflow as tf
import datetime

In [13]:
A = np.random.rand(1, 100).astype('int32')
B = np.random.rand(1, 100).astype('int32')

n = 10

In [11]:
c1 = []
c2 = []

In [17]:
def matpow(M, n):
    if n < 1:
        return M
    else:
        return tf.malmul(M, matpow(M, n-1))

In [21]:
with tf.device('/gpu:0'):
    a = tf.constant(A)
    b = tf.constant(B)
    c1.append(matpow(a, n))
    c1.append(matpow(b, n))
    
with tf.device('/cpu:0'):
    sum = tf.add_n(c1)
    t1_1 = datetime.datetime.now()

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    sess.run(sum)
t2_1 = datetime.datetime.now()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-24ab413c3e25> in <module>()
      2     a = tf.constant(A)
      3     b = tf.constant(B)
----> 4     c1.append(matpow(a, n))
      5     c1.append(matpow(b, n))
      6 

<ipython-input-17-6435cbcc6a5a> in matpow(M, n)
      3         return M
      4     else:
----> 5         return tf.malmul(M, matpow(M, n-1))

AttributeError: 'module' object has no attribute 'malmul'

In [19]:
with tf.device('/gpu:0'):
    # compute A^n and store result in c2
    a = tf.constant(A)
    c2.append(matpow(a, n))
    
with tf.device('/gpu:1'):
    # compute B^n and store result in c2
    b = tf.constant(B)
    c2.append(matpow(b, n))
    
with tf.device('/cpu:0'):
    # Addition of all elements in c2, i.e. A^n + B^n
    sum = tf.add_n(c2)
    t1_2 = datetime.datetime.now()

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    # runs the op.
    sess.run(sum)
    t2_2 = datatime.datetime.now()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-19-9c19078dbfe3> in <module>()
      2     # compute A^n and store result in c2
      3     a = tf.constant(A)
----> 4     c2.append(matpow(a, n))
      5 
      6 with tf.device('/gpu:1'):

<ipython-input-17-6435cbcc6a5a> in matpow(M, n)
      3         return M
      4     else:
----> 5         return tf.malmul(M, matpow(M, n-1))

AttributeError: 'module' object has no attribute 'malmul'

In [22]:
print "Single GPU computation time: " + str(t2_1 - t1_1)
print "Multi GPU computation time: " + str(t2_2 - t1_2)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-b013bb58f5c6> in <module>()
----> 1 print "Single GPU computation time: " + str(t2_1 - t1_1)
      2 print "Multi GPU computation time: " + str(t2_2 - t1_2)

NameError: name 't2_1' is not defined

In [ ]: