tensorflow多GPU同时运行?

时间:2018-04-21 14:33:20

标签: tensorflow deep-learning distributed-computing multi-gpu

为了了解分布式张量流的机制,我用multi-gpus写了一个简单的张量流测试代码

def cv_data(SEED):
    np.random.seed(SEED)
    return np.random.rand(5,2,2)


def test(data):
    for i in range(5):
        with tf.device('/gpu:%d' %i ):
            with tf.name_scope('cv%d' %i):
                x = tf.placeholder(tf.float32,[2,2],name='x')
                y = tf.matmul(x,x)
    init = tf.initialize_all_variables()
    sess = tf.Session()
    with sess as sess:
        writer=tf.summary.FileWriter("test_graph",sess.graph)
        sess.run(init)
        print("y is ")
        print(sess.run(y,feed_dict={'cv0/x:0':np.ones((2,2)),'cv1/x:0':2*np.ones((2,2)),'cv2/x:0':3*np.ones((2,2)),'cv3/x:0':4*np.ones((2,2)),'cv4/x:0':5*np.ones((2,2))))
        #tf.train.Saver.save(sess,"./model")
        writer.close()

但是sess.run()只执行/ gpu:4的图表,我怎样才能让所有的gpus同时运行?

1 个答案:

答案 0 :(得分:1)

您可以构建一个Python操作列表,并将所有操作传递给sess.run。或者您可以汇总结果(例如tf.add_n)并运行该操作。

无论哪种方式,您可能都希望在循环外部使用单个占位符,这意味着您将提供该值并将其复制到所有设备。

相关问题