Cuda combination algorithm error : CUDA_ERROR_LAUNCH_FAILED
Cuda combination algorithm error : CUDA_ERROR_LAUNCH_FAILED
我正在尝试在 CUDA 上实现本文 https://www.developertyrone.com/blog/generating-the-mth-lexicographical-element-of-a-mathematical-combination/ 中描述的组合算法。
它适用于 C(100,4)
n=100
k=4
但是当我尝试 C(200,4) 时,我收到错误消息 CUDA_ERROR_LAUNCH_FAILED.
n=200
k=4
我几乎可以肯定计算n=200的组合没有溢出,但我想不出原因。
我调试了代码,没有溢出。
一个有趣的地方是,C(150,4) 的代码有时 运行 有时会失败。
我强制所有整数为 int64。
我试图逐行注释以找到导致错误的确切位置。
当问题似乎出在函数 largestV
中的这两行时
while choose(v, b) > x:
v -= 1
当我注释这两行时,它没有崩溃。
完整代码如下:
import numba
from numba import cuda
import math
import numpy as np
from pdb import set_trace
@cuda.jit(device=True)
def choose(n, k):
if n < k:
return 0
if n == k:
return 1
delta = imax = 0
if k < n-k:
delta = n-k
imax = k
else:
delta = k
imax = n-k
ans = numba.int64(delta + 1)
for i in range(2, imax+1):
ans = numba.int64((ans * (delta + i)) / i)
return ans
@cuda.jit(device=True)
def largestV(a, b, x):
v = numba.int64(a-1)
while choose(v, b) > x:
v -= 1
return v
@cuda.jit
def cuda_calculateMth(n, k, d_result):
pos = cuda.grid(1) # pylint: disable=not-callable
if pos >= len(d_result):
return
m = numba.int64(pos)
a = numba.int64(n)
b = numba.int64(k)
x = numba.int64((choose(a, b) - 1) - m)
for i in range(k):
d_result[pos][i] = largestV(a, b, x)
x = x - choose(d_result[pos][i], b)
a = d_result[pos][i]
b -= 1
for i in range(k):
d_result[m][i] = (n-1) - d_result[m][i]
if __name__ == "__main__":
n = 200
k = 4
totalcount = int((n*(n-1)*(n-2)*(n-3)) / (4 * 3 * 2))
result = np.zeros((totalcount, 4), dtype="uint")
temp = np.zeros(10, dtype="uint")
d_result = cuda.to_device(result)
d_temp = cuda.to_device(temp)
threadsperblock = 128
blockspergrid = (totalcount +
(threadsperblock - 1)) // threadsperblock
cuda_calculateMth[blockspergrid, threadsperblock](
n, k, d_result)
result = d_result.copy_to_host()
print(result[-30:])
感谢talonmies,这是超时问题。
我将 TDR 超时设置为 30,现在可以使用了。
我正在尝试在 CUDA 上实现本文 https://www.developertyrone.com/blog/generating-the-mth-lexicographical-element-of-a-mathematical-combination/ 中描述的组合算法。
它适用于 C(100,4)
n=100
k=4
但是当我尝试 C(200,4) 时,我收到错误消息 CUDA_ERROR_LAUNCH_FAILED.
n=200
k=4
我几乎可以肯定计算n=200的组合没有溢出,但我想不出原因。
我调试了代码,没有溢出。 一个有趣的地方是,C(150,4) 的代码有时 运行 有时会失败。
我强制所有整数为 int64。 我试图逐行注释以找到导致错误的确切位置。 当问题似乎出在函数 largestV
中的这两行时 while choose(v, b) > x:
v -= 1
当我注释这两行时,它没有崩溃。
完整代码如下:
import numba
from numba import cuda
import math
import numpy as np
from pdb import set_trace
@cuda.jit(device=True)
def choose(n, k):
if n < k:
return 0
if n == k:
return 1
delta = imax = 0
if k < n-k:
delta = n-k
imax = k
else:
delta = k
imax = n-k
ans = numba.int64(delta + 1)
for i in range(2, imax+1):
ans = numba.int64((ans * (delta + i)) / i)
return ans
@cuda.jit(device=True)
def largestV(a, b, x):
v = numba.int64(a-1)
while choose(v, b) > x:
v -= 1
return v
@cuda.jit
def cuda_calculateMth(n, k, d_result):
pos = cuda.grid(1) # pylint: disable=not-callable
if pos >= len(d_result):
return
m = numba.int64(pos)
a = numba.int64(n)
b = numba.int64(k)
x = numba.int64((choose(a, b) - 1) - m)
for i in range(k):
d_result[pos][i] = largestV(a, b, x)
x = x - choose(d_result[pos][i], b)
a = d_result[pos][i]
b -= 1
for i in range(k):
d_result[m][i] = (n-1) - d_result[m][i]
if __name__ == "__main__":
n = 200
k = 4
totalcount = int((n*(n-1)*(n-2)*(n-3)) / (4 * 3 * 2))
result = np.zeros((totalcount, 4), dtype="uint")
temp = np.zeros(10, dtype="uint")
d_result = cuda.to_device(result)
d_temp = cuda.to_device(temp)
threadsperblock = 128
blockspergrid = (totalcount +
(threadsperblock - 1)) // threadsperblock
cuda_calculateMth[blockspergrid, threadsperblock](
n, k, d_result)
result = d_result.copy_to_host()
print(result[-30:])
感谢talonmies,这是超时问题。 我将 TDR 超时设置为 30,现在可以使用了。