Преглед изворни кода

Merge pull request #71 from Helw150/master

Fixes for Multi-Gpu_basics No Longer hangs on CPU
Aymeric Damien пре 8 година
родитељ
комит
bf80d81e7d
2 измењених фајлова са 10 додато и 94 уклоњено
  1. 10 9
      examples/5_MultiGPU/multigpu_basics.py
  2. 0 85
      multigpu_basics.py

+ 10 - 9
examples/5_MultiGPU/multigpu_basics.py

@@ -1,3 +1,4 @@
+from __future__ import print_function
 '''
 Basic Multi GPU computation example using TensorFlow library.
 
@@ -12,7 +13,7 @@ This tutorial requires your machine to have 2 GPUs
 "/gpu:1": The second GPU of your machine
 '''
 
-from __future__ import print_function
+
 
 import numpy as np
 import tensorflow as tf
@@ -31,8 +32,8 @@ Results on 8 cores with 2 GTX-980:
  * Multi GPU computation time: 0:00:07.131701
 '''
 # Create random large matrix
-A = np.random.rand(1e4, 1e4).astype('float32')
-B = np.random.rand(1e4, 1e4).astype('float32')
+A = np.random.rand(10000, 10000).astype('float32')
+B = np.random.rand(10000, 10000).astype('float32')
 
 # Create a graph to store results
 c1 = []
@@ -48,8 +49,8 @@ def matpow(M, n):
 Single GPU computing
 '''
 with tf.device('/gpu:0'):
-    a = tf.constant(A)
-    b = tf.constant(B)
+    a = tf.placeholder(tf.float32, [10000, 10000])
+    b = tf.placeholder(tf.float32, [10000, 10000])
     # Compute A^n and B^n and store results in c1
     c1.append(matpow(a, n))
     c1.append(matpow(b, n))
@@ -60,7 +61,7 @@ with tf.device('/cpu:0'):
 t1_1 = datetime.datetime.now()
 with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
     # Run the op.
-    sess.run(sum)
+    sess.run(sum, {a:A, b:B})
 t2_1 = datetime.datetime.now()
 
 
@@ -70,13 +71,13 @@ Multi GPU computing
 # GPU:0 computes A^n
 with tf.device('/gpu:0'):
     # Compute A^n and store result in c2
-    a = tf.constant(A)
+    a = tf.placeholder(tf.float32, [10000, 10000])
     c2.append(matpow(a, n))
 
 # GPU:1 computes B^n
 with tf.device('/gpu:1'):
     # Compute B^n and store result in c2
-    b = tf.constant(B)
+    b = tf.placeholder(tf.float32, [10000, 10000])
     c2.append(matpow(b, n))
 
 with tf.device('/cpu:0'):
@@ -85,7 +86,7 @@ with tf.device('/cpu:0'):
 t1_2 = datetime.datetime.now()
 with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
     # Run the op.
-    sess.run(sum)
+    sess.run(sum, {a:A, b:B})
 t2_2 = datetime.datetime.now()
 
 

+ 0 - 85
multigpu_basics.py

@@ -1,85 +0,0 @@
-#Multi GPU Basic example
-'''
-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
-'''
-
-import numpy as np
-import tensorflow as tf
-import datetime
-
-#Processing Units logs
-log_device_placement = True
-
-#num of multiplications to perform
-n = 10
-
-'''
-Example: compute A^n + B^n on 2 GPUs
-Results on 8 cores with 2 GTX-980:
- * Single GPU computation time: 0:00:11.277449
- * Multi GPU computation time: 0:00:07.131701
-'''
-#Create random large matrix
-A = np.random.rand(1e4, 1e4).astype('float32')
-B = np.random.rand(1e4, 1e4).astype('float32')
-
-# Creates a graph to store results
-c1 = []
-c2 = []
-
-def matpow(M, n):
-    if n < 1: #Abstract cases where n < 1
-        return M
-    else:
-        return tf.matmul(M, matpow(M, n-1))
-
-'''
-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()
-
-
-'''
-Multi GPU computing
-'''
-#GPU:0 computes A^n
-with tf.device('/gpu:0'):
-    #compute A^n and store result in c2
-    a = tf.constant(A)
-    c2.append(matpow(a, n))
-
-#GPU:1 computes B^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'):
-  sum = tf.add_n(c2) #Addition of all elements in c2, i.e. A^n + B^n
-
-t1_2 = datetime.datetime.now()
-with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
-    # Runs the op.
-    sess.run(sum)
-t2_2 = datetime.datetime.now()
-
-
-print("Single GPU computation time: " + str(t2_1-t1_1))
-print("Multi GPU computation time: " + str(t2_2-t1_2))