使用pycuda和tensorflow时,“对等访问”失败

时间:2017-12-26 14:40:17

标签: python tensorflow cuda gpu pycuda

我在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之间传输数据无关。那有什么不对呢?谢谢!

1 个答案:

答案 0 :(得分:1)

似乎我找到了解决方案。当在cudadevice.cudacontext.push()和cudadevice.cudacontext.pop()之间插入texRef.set_array(tex_gpu)来显式切换cuda上下文时,一切顺利。