首页 > 解决方案 > 使用 numba 原子操作函数时遇到问题 (cuda.atomic.compare_and_swap)

问题描述

我正在尝试使用 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 中的其他原子操作配合得很好。这是唯一对我不起作用的。有人可以帮我找出问题所在,还是有其他替代方法可以做到这一点?谢谢!

标签: pythoncudaatomicnumbacompare-and-swap

解决方案


错误消息中的关键是:

array(float64, 1d, A), int64, Literal[int](0))

CUDA atomicCAS 仅支持整数类型。您不能传递浮点类型。


推荐阅读