1
+ from __future__ import print_function
1
2
'''
2
3
Basic Multi GPU computation example using TensorFlow library.
3
4
12
13
"/gpu:1": The second GPU of your machine
13
14
'''
14
15
15
- from __future__ import print_function
16
+
16
17
17
18
import numpy as np
18
19
import tensorflow as tf
31
32
* Multi GPU computation time: 0:00:07.131701
32
33
'''
33
34
# 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' )
36
37
37
38
# Create a graph to store results
38
39
c1 = []
@@ -48,8 +49,8 @@ def matpow(M, n):
48
49
Single GPU computing
49
50
'''
50
51
with 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 ] )
53
54
# Compute A^n and B^n and store results in c1
54
55
c1 .append (matpow (a , n ))
55
56
c1 .append (matpow (b , n ))
@@ -60,7 +61,7 @@ def matpow(M, n):
60
61
t1_1 = datetime .datetime .now ()
61
62
with tf .Session (config = tf .ConfigProto (log_device_placement = log_device_placement )) as sess :
62
63
# Run the op.
63
- sess .run (sum )
64
+ sess .run (sum , { a : A , b : B } )
64
65
t2_1 = datetime .datetime .now ()
65
66
66
67
@@ -70,13 +71,13 @@ def matpow(M, n):
70
71
# GPU:0 computes A^n
71
72
with tf .device ('/gpu:0' ):
72
73
# Compute A^n and store result in c2
73
- a = tf .constant ( A )
74
+ a = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
74
75
c2 .append (matpow (a , n ))
75
76
76
77
# GPU:1 computes B^n
77
78
with tf .device ('/gpu:1' ):
78
79
# Compute B^n and store result in c2
79
- b = tf .constant ( B )
80
+ b = tf .placeholder ( tf . float32 , [ 10000 , 10000 ] )
80
81
c2 .append (matpow (b , n ))
81
82
82
83
with tf .device ('/cpu:0' ):
@@ -85,7 +86,7 @@ def matpow(M, n):
85
86
t1_2 = datetime .datetime .now ()
86
87
with tf .Session (config = tf .ConfigProto (log_device_placement = log_device_placement )) as sess :
87
88
# Run the op.
88
- sess .run (sum )
89
+ sess .run (sum , { a : A , b : B } )
89
90
t2_2 = datetime .datetime .now ()
90
91
91
92
0 commit comments