【发布时间】:2018-06-07 09:11:54
【问题描述】:
我在 python3 中有一些这样的代码:
import numpy as np
import pycuda.driver as cuda
from pycuda.compiler import SourceModule, compile
import tensorflow as tf
# create device and context
cudadevice=cuda.Device(gpuid1)
cudacontext=cudadevice.make_context()
config = tf.ConfigProto()
config.gpu_options.visible_device_list={}.format(gpuid2)
sess = tf.Session(config=config)
# compile from a .cu file
cuda_mod = SourceModule(cudaCode, include_dirs = [dir_path], no_extern_c = True, options = ['-O0'])
# in the .cu code a texture named "map" is defined as:
# texture<float4, cudaTextureType2D, cudaReadModeElementType> map;
texRef = cuda_mod.get_texref('map')
# tex is a np.ndarray with shape 256*256*4, and it is the output of a tensorflow's graph by calling sess.run()
tex = np.ascontiguousarray(tex).astype(np.float32)
tex_gpu = cuda.make_multichannel_2d_array(tex, 'C')
# error here!!!!!
texRef.set_array(tex_gpu)
以及错误信息:pycuda._driver.LogicError: cuTexRefSetArray failed: peer access has not been enabled
同时使用tensorflow 时出现对等访问错误(即使gpuid1 和gpuid2 相同),但没有tensorflow 一切正常。
我发现“对等访问”与 GPU(设备)之间的通信有关。但是我在这里所做的只是将numpy 数组设置为GPU 内存作为纹理,所以我认为它与在不同GPU 之间传输数据无关。那么它有什么问题呢?谢谢!
【问题讨论】:
-
我猜你是在隐式地尝试在不同进程持有的不同 CUDA 上下文之间共享资源。这可以工作,但只能通过 CUDA API 的 IPC 工具。除非您能够对 PyCUDA 和 Tensorflow 的内部进行重大修改,否则我非常怀疑这是否会像您尝试的那样工作
-
感谢 talonmies。看来我找到了解决方案。当在 cudadevice.cudacontext.push() 和 cudadevice.cudacontext.pop() 之间插入 texRef.set_array(tex_gpu) 以显式切换 cuda 上下文时,一切正常。
-
如果这是您的解决方案,请写下描述它的答案,以便其他人可以从您的经验中受益
标签: python tensorflow cuda gpu pycuda