簡體   English   中英

TensorFlow獨立使用CPU和GPU

[英]TensorFlow use CPU and GPU independently

我有一個簡單的問題:在Windows 10和CUDA 8上,我在Tensorflow 1.3.0(GPU)中有兩個帶有python 3的小進程,我想在GPU上運行一個,而在CPU上另一個則相互隔離(它們應該不合作!)。 這是一個小的廢話示例代碼:

import tensorflow as tf
import time
import os
import sys

dim = 6096
try:
    device = sys.argv[1]
except:
    device = "gpu"

print("Running for "+device)
cur_graph = tf.Graph()

with cur_graph.as_default():
    with tf.device("/"+device+":0"):
        x = tf.Variable(tf.random_normal([dim, dim]), dtype=tf.float32)
        y = tf.Variable(tf.random_normal([dim, dim]), dtype=tf.float32)
        z = tf.Variable(tf.random_normal([dim, dim]), dtype=tf.float32)

        a = tf.matmul(x, y)
        b = tf.matmul(a, z)

        training_start = time.time()
        with tf.Session() as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)
            sess.run(a)
        training_time = (time.time() - training_start)
        print("Time: %5.3f" % training_time)

如果我使用以下命令在CLI上啟動它

my_prog.py cpu

GPU空閑時需要約10秒才能完成並利用CPU

如果我使用以下命令在CLI上啟動它

my_prog.py gpu

在GPU空閑時需要約1秒鍾才能完成並使用GPU

到現在為止還挺好。 現在,我想在不同的CMD中並行啟動它,並且我希望兩個進程都可以獨立工作並利用CPU和GPU。 但是我總是從我的GPU進程中得到一個異常:

G:\\ Workspace \\ Python> device_test.py gpu

Running for gpu
2017-11-05 17:27:52.940625: W C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library
wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-11-05 17:27:52.940879: W C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library
wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-11-05 17:27:53.373137: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:955] Found device 0 with
 properties:
name: GeForce GTX 780
major: 3 minor: 5 memoryClockRate (GHz) 0.941
pciBusID 0000:01:00.0
Total memory: 3.00GiB
Free memory: 2.45GiB
2017-11-05 17:27:53.377292: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:976] DMA: 0
2017-11-05 17:27:53.393137: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:986] 0:   Y
2017-11-05 17:27:53.394838: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1045] Creating TensorFlo
w device (/gpu:0) -> (device: 0, name: GeForce GTX 780, pci bus id: 0000:01:00.0)
2017-11-05 17:27:54.406490: E C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_blas.cc:366] failed to create cublas
 handle: CUBLAS_STATUS_ALLOC_FAILED
2017-11-05 17:27:54.431348: W C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\stream.cc:1756] attempting to perform BLAS ope
ration using StreamExecutor without BLAS support

...

> InternalError (see above for traceback): Blas GEMM launch failed :
> a.shape=(6096, 6096), b.shape=(6096, 6096), m=6096, n=6096, k=6096
>          [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false,
> _device="/job:localhost/replica:0/task:0/gpu:0"](Variable/read, Variable_1/re ad)]]
>          [[Node: MatMul/_1 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0",
> send_device="/job:localhost/replica:0/task:0/gp u:0",
> send_device_incarnation=1, tensor_name="edge_9_MatMul",
> tensor_type=DT_FLOAT,
> _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

是否可以在CPU和GPU上獨立運行TensorFlow內容?

尤里卡。 我已經解決了這個問題,但是不知道為什么...我在代碼中添加了以下幾行:

[...]

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess

[...]

現在它可以按預期工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM