我尝试在控制台环境和Django框架中运行以下简单的cublas示例。在
"""
Demonstrates multiplication of two matrices on the GPU.
"""
import pycuda
import pycuda.gpuarray as gpuarray
import pycuda.driver as drv
import numpy as np
drv.init() #init pycuda driver
current_dev = drv.Device(0) #device we are working on
ctx = current_dev.make_context() #make a working context
ctx.push() #let context make the lead
import scikits.cuda.linalg as culinalg
import scikits.cuda.misc as cumisc
culinalg.init()
# Double precision is only supported by devices with compute
# capability >= 1.3:
import string
demo_types = [np.float32]
for t in demo_types:
print 'Testing matrix multiplication for type ' + str(np.dtype(t))
if np.iscomplexobj(t()):
a = np.asarray(np.random.rand(10, 5)+1j*np.random.rand(10, 5), t)
b = np.asarray(np.random.rand(5, 5)+1j*np.random.rand(5, 5), t)
c = np.asarray(np.random.rand(5, 5)+1j*np.random.rand(5, 5), t)
else:
a = np.asarray(np.random.rand(10, 5), t)
b = np.asarray(np.random.rand(5, 5), t)
c = np.asarray(np.random.rand(5, 5), t)
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)
c_gpu = gpuarray.to_gpu(c)
temp_gpu = culinalg.dot(a_gpu, b_gpu)
d_gpu = culinalg.dot(temp_gpu, c_gpu)
temp_gpu.gpudata.free()
del(temp_gpu)
print 'Success status: ', np.allclose(np.dot(np.dot(a, b), c) , d_gpu.get())
print 'Testing vector multiplication for type ' + str(np.dtype(t))
if np.iscomplexobj(t()):
d = np.asarray(np.random.rand(5)+1j*np.random.rand(5), t)
e = np.asarray(np.random.rand(5)+1j*np.random.rand(5), t)
else:
d = np.asarray(np.random.rand(5), t)
e = np.asarray(np.random.rand(5), t)
d_gpu = gpuarray.to_gpu(d)
e_gpu = gpuarray.to_gpu(e)
temp = culinalg.dot(d_gpu, e_gpu)
print 'Success status: ', np.allclose(np.dot(d, e), temp)
ctx.pop() #deactivate again
ctx.detach() #delete it
在控制台环境下,我成功了。但当我想在django中运行时(我将示例作为一个函数插入URL的get方法中),它给了我一个分段错误(核心转储)。在
有人知道这个问题的原因吗?cuda gdb的回溯信息如下:
^{pr2}$谢谢!在
我创建了一个新流程,使用子流程处理CUDA计算,解决了这个问题。原因可能是pycuda不是线程安全的。在
相关问题 更多 >
编程相关推荐