``````

In :

# Basic Multi GPU computation example using TensorFlow library.

# Author: Aymeric Damien
# Project: https://github.com/aymericdamien/TensorFlow-Examples/

# This tutorial requires your machine to have 2 GPUs
# "/cpu:0": The CPU of your machine.
# "/gpu:0": The first GPU of your machine
# "/gpu:1": The second GPU of your machine
# For this example, we are using 2 GTX-980

``````
``````

In :

import numpy as np
import tensorflow as tf
import datetime

``````
``````

In :

#Processing Units logs
log_device_placement = True

#num of multiplications to perform
n = 10

``````
``````

In [ ]:

# Example: compute A^n + B^n on 2 GPUs

# Create random large matrix
A = np.random.rand(10000, 10000).astype('float32')
B = np.random.rand(10000, 10000).astype('float32')

# Creates a graph to store results
c1 = []
c2 = []

# Define matrix power
def matpow(M, n):
if n < 1: #Abstract cases where n < 1
return M
else:
return tf.matmul(M, matpow(M, n-1))

``````
``````

In :

# Single GPU computing

with tf.device('/gpu:0'):
a = tf.constant(A)
b = tf.constant(B)
#compute A^n and B^n and store results in c1
c1.append(matpow(a, n))
c1.append(matpow(b, n))

with tf.device('/cpu:0'):
sum = tf.add_n(c1) #Addition of all elements in c1, i.e. A^n + B^n

t1_1 = datetime.datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
# Runs the op.
sess.run(sum)
t2_1 = datetime.datetime.now()

``````
``````

In :

print "Single GPU computation time: " + str(t2_1-t1_1)

``````
``````

Single GPU computation time: 0:00:11.833497
Multi GPU computation time: 0:00:07.085913

``````