在Django中执行scikits.cuda示例时核心转储段错误

0 投票
1 回答
578 浏览
提问于 2025-04-18 11:31

我尝试在控制台环境和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方法里),它给我报了一个段错误(core dump)。

有没有人知道这可能是什么原因呢?cuda-gdb的错误追踪信息如下:

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 ()

谢谢!

1 个回答

1

我用subprocess创建了一个新进程来处理CUDA计算,这样就解决了问题。可能的原因是pycuda不支持多线程安全。

撰写回答