【发布时间】:2014-06-29 22:08:45
【问题描述】:
我尝试在控制台环境和 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的traceback信息如下:
0 0x00007ffff782d267 in kill () from /lib/x86_64-linux-gnu/libc.so.6
1 0x000000000041f44e in ?? ()
2 0x000000000052c6d5 in PyEval_EvalFrameEx ()
3 0x000000000052cf32 in PyEval_EvalFrameEx ()
4 0x000000000055c594 in PyEval_EvalCodeEx ()
5 0x000000000052ca8d in PyEval_EvalFrameEx ()
6 0x000000000056d0aa in ?? ()
7 0x000000000052e1e6 in PyEval_EvalFrameEx ()
8 0x000000000056d0aa in ?? ()
9 0x000000000052e1e6 in PyEval_EvalFrameEx ()
10 0x000000000056d0aa in ?? ()
11 0x000000000052e1e6 in PyEval_EvalFrameEx ()
12 0x000000000055c594 in PyEval_EvalCodeEx ()
13 0x000000000052ca8d in PyEval_EvalFrameEx ()
14 0x000000000052cf32 in PyEval_EvalFrameEx ()
15 0x000000000055c594 in PyEval_EvalCodeEx ()
16 0x000000000052ca8d in PyEval_EvalFrameEx ()
17 0x000000000055c594 in PyEval_EvalCodeEx ()
18 0x00000000005b7392 in PyEval_EvalCode ()
19 0x0000000000469663 in ?? ()
20 0x00000000004699e3 in PyRun_FileExFlags ()
21 0x0000000000469f1c in PyRun_SimpleFileExFlags ()
22 0x000000000046ab81 in Py_Main ()
谢谢!
【问题讨论】:
-
pycuda 线程安全吗?
-
@Thomas 我猜不是。这可能是原因。我试过龙卷风,它奏效了。 tornado 是一个轻量级的 python 网络框架,它并不总是为 get 方法创建线程。
-
在 GPU 上运行的代码永远不会导致主机 CPU 出现分段错误。回溯在 Python 解释器 API 调用中非常清楚。我将从纯 numpy 代码开始,并检查在没有 PyCUDA 或 CUDA scikit 的情况下是否可以正常工作,然后逐步添加功能,直到找到破坏它的地方。我已经从这个问题中删除了 CUDA 标签,它本身与 CUDA 编程没有任何关系。
-
@talonmies 感谢您的回答。我会试试看。
标签: python django segmentation-fault scipy pycuda