import tensorflow as tf

if tf.test.is_built_with_cuda(): pass

# 7. 我们希望分配指定操作给GPU。下面是一个示例代码，做了一些简单的计算，并将它们分配给主CPU和两个副GPU
with tf.device('/cpu:0'):
    a = tf.constant([1.0, 3.0, 5.0], shape=[1,3])
    b = tf.constant([2.0, 4.0, 6.0], shape=[3, 1])

    with tf.device('/gpu:0'):
        c = tf.matmul(a,b)
        c = tf.reshape(c, [-1])

    with tf.device('/gpu:1'):
        d = tf.matmul(b, a)
        flat_d = tf.reshape(d, [-1])

    combined = tf.multiply(c, flat_d)
print(sess.run(combined))

