我正在尝试使用Numba为我的代码编写cuda内核。我想以某种方式在部分代码中使用原子操作,并编写了一个测试内核,以查看cuda.atomic.compare_and_swap的工作方式。在文档上说:
from numba import cuda
import numpy as np
@cuda.jit
def atomicCAS(N,out1):
idx = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x
if idx >= N:
return
A = out1[idx:]
cuda.atomic.compare_and_swap(A,idx,0)
N = 1024
out1 = np.arange(N)
out1 = np.zeros(N)
dout1 = cuda.to_device(out1)
tpb = 32
bpg = int(np.ceil(N/tpb))
atomicCAS[bpg,tpb](N,dout1)
hout1 = dout1.copy_to_host()
然后我收到此错误:
TypingError: Invalid use of Function(<class 'numba.cuda.stubs.atomic.compare_and_swap'>) with argument(s) of type(s): (array(float64, 1d, A), int64, Literal[int](0))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<class 'numba.cuda.stubs.atomic.compare_and_swap'>)
[2] During: typing of call at /home/qinyu/test.py (20)
这是一个非常幼稚的代码,我认为我输入了变量的写入类型,但是却遇到了这种打字错误。它与Numba中的其他原子操作效果很好。这是唯一对我不起作用的。有人可以帮我解决问题,还是有其他替代方法可以解决此问题?谢谢!
错误消息中的键是这样:
array(float64, 1d, A), int64, Literal[int](0))
仅CUDA atomicCAS supports integer types。您不能传递浮点类型。