Assigning a single GPU on a multi-GPU system

If you have more than one GPU in your system, the GPU with the lowest ID will be selected by default. If you would like to run on a different GPU, you will need to specify the preference explicitly.

For example, we can try to change the GPU assignation in the previous code:

with tf.device('/gpu:1'): 
a = tf.placeholder(tf.float32, [10000, 10000])
b = tf.placeholder(tf.float32, [10000, 10000])
c1.append(matpow(a, n))
c1.append(matpow(b, n))

In this way, we are telling GPU to execute the kernel function.

If the device we have specified does not exist (as in my case), you will get the following error message on the console (or terminal):

InvalidArgumentError : 
InvalidArgumentError (see above for traceback): Cannot assign a device to node 'Placeholder_1': Could not satisfy explicit device specification '/device:GPU:1' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[100,100], _device="/device:GPU:1"]()]]

If you would like TensorFlow to automatically choose an existing and supported device to run the operations in case the specified one doesn't exist, you can set allow_soft_placement to True in the configuration option when creating the session.

Again, we fix /gpu:1 for the following node:

with tf.device('/gpu:1'): 
a = tf.placeholder(tf.float32, [10000, 10000])
b = tf.placeholder(tf.float32, [10000, 10000])
c1.append(matpow(a, n))
c1.append(matpow(b, n))

Then we build a session with the following allow_soft_placement parameter set to True:

with tf.Session(config=tf.ConfigProto 
(allow_soft_placement=True,
log_device_placement=log_device_placement))
as sess:

In this way, when running the session InvalidArgumentError will not be displayed, but instead a correct result will be displayed, in this case, with a slight delay:

GPU computation time: 0:00:15.006644
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset