1+ from __future__ import print_function
12'''
23Basic Multi GPU computation example using TensorFlow library.
34
1213"/gpu:1": The second GPU of your machine
1314'''
1415
15- from __future__ import print_function
16+
1617
1718import numpy as np
1819import tensorflow as tf
3132 * Multi GPU computation time: 0:00:07.131701
3233'''
3334# Create random large matrix
34- A = np .random .rand (1e4 , 1e4 ).astype ('float32' )
35- B = np .random .rand (1e4 , 1e4 ).astype ('float32' )
35+ A = np .random .rand (10000 , 10000 ).astype ('float32' )
36+ B = np .random .rand (10000 , 10000 ).astype ('float32' )
3637
3738# Create a graph to store results
3839c1 = []
@@ -48,8 +49,8 @@ def matpow(M, n):
4849Single GPU computing
4950'''
5051with tf .device ('/gpu:0' ):
51- a = tf .constant ( A )
52- b = tf .constant ( B )
52+ a = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
53+ b = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
5354 # Compute A^n and B^n and store results in c1
5455 c1 .append (matpow (a , n ))
5556 c1 .append (matpow (b , n ))
@@ -60,7 +61,7 @@ def matpow(M, n):
6061t1_1 = datetime .datetime .now ()
6162with tf .Session (config = tf .ConfigProto (log_device_placement = log_device_placement )) as sess :
6263 # Run the op.
63- sess .run (sum )
64+ sess .run (sum , { a : A , b : B } )
6465t2_1 = datetime .datetime .now ()
6566
6667
@@ -70,13 +71,13 @@ def matpow(M, n):
7071# GPU:0 computes A^n
7172with tf .device ('/gpu:0' ):
7273 # Compute A^n and store result in c2
73- a = tf .constant ( A )
74+ a = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
7475 c2 .append (matpow (a , n ))
7576
7677# GPU:1 computes B^n
7778with tf .device ('/gpu:1' ):
7879 # Compute B^n and store result in c2
79- b = tf .constant ( B )
80+ b = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
8081 c2 .append (matpow (b , n ))
8182
8283with tf .device ('/cpu:0' ):
@@ -85,7 +86,7 @@ def matpow(M, n):
8586t1_2 = datetime .datetime .now ()
8687with tf .Session (config = tf .ConfigProto (log_device_placement = log_device_placement )) as sess :
8788 # Run the op.
88- sess .run (sum )
89+ sess .run (sum , { a : A , b : B } )
8990t2_2 = datetime .datetime .now ()
9091
9192
0 commit comments