1

谁能向我解释为什么每次我运行这段代码时,我的电脑都会死机?

from numbapro import cuda
import numpy as np
from timeit import default_timer as time

n = 100
dtype = np.float32

@cuda.jit('void(float32[:,:], float32[:], float32[:])')
def cu_matrix_vector(A, b, c):
    y, x = cuda.grid(2)

    if x < n and y < n:
        c[y] = 0.0
        for i in range(n):
            c[y] += A[y, i] * b[i]


A = np.array(np.random.random((n, n)), dtype=dtype)
B = np.array(np.random.random((n, 1)), dtype=dtype)
C = np.empty_like(B)

blockDim = 32, 8
gridDim = (n + blockDim[0] - 1)/blockDim[0], (n + blockDim[1] - 1)/blockDim[1]

print 'blockDim = (%d,%d)' %blockDim

s = time()
stream = cuda.stream()
with stream.auto_synchronize():
    dA = cuda.to_device(A,stream)
    dB = cuda.to_device(B,stream)
    dC = cuda.to_device(C,stream)
    cu_matrix_vector[(bpg, bpg), (tpb, tpb),stream](dA, dB, dC)
    dC.to_host(stream)

e = time()
tcuda = e - s

print tcuda

点击代码后,我的电脑死机了。我不确定为什么。我提前感谢所有帮助。

4

1 回答 1

2

数组 B 不应是二维数组:

B = np.array(np.random.random((n, 1)), dtype=dtype)

它应该是一维的:

B = np.array(np.random.random(n), dtype=dtype)

关于冻结,我假设您使用的是 OSX。CUDA 驱动程序应在内核启动错误时返回错误代码,但在 OSX 上,显示管理器似乎将冻结。

于 2014-02-05T16:20:52.140 回答