Python OpenCL主机程序以cl程序参数传递

时间:2012-10-19 17:00:04

标签: python opencl

您好我正在尝试使用python进行OpenCL。我试图将一个数组和一个const变量传递给cl程序,并简单地将const变量复制到cl设备上的数组。这应该很简单,但我收到以下错误:

Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
test()
File "D:/Programming/Programs_OpenCL_Python/Host_CL_Parameter_Passing.py", line 141,    in test
event = prg.test( queue, (10,1), None, a_dev, b)
File "C:\Python27\lib\site-packages\pyopencl-2012.1-py2.7-win32.egg\pyopencl\__init__.py", line 457, in kernel_call
self.set_args(*args)
File "C:\Python27\lib\site-packages\pyopencl-2012.1-py2.7-win32.egg\pyopencl\__init__.py", line 509, in kernel_set_args
% (i+1, str(e), advice))
LogicError: when processing argument #2 (1-based): Kernel.set_arg failed: invalid     value - invalid kernel argument

这是代码:

def test():
    ctx = cl.create_some_context()
    queue = cl.CommandQueue(ctx)
    a = np.empty(10, dtype = int)
    b = int(1)
    a_dev = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, a.nbytes)
    prg = cl.Program( ctx, """__kernel void test(__global int *a, const int b){
        int i = get_global_id(0);
        a[i] = b;
    }""").build()
    event = prg.test( queue, (10,1), None, a_dev, b)
    event.wait()
    cl.enqueue_copy( queue, a, a_dev)
    print a

有人可以告诉我这个问题并给我一个解决方案吗?这真让我抓狂。 三江源

1 个答案:

答案 0 :(得分:2)

您需要将整数参数转换为numpy int32类型:

event = prg.test( queue, (10,1), None, a_dev, np.int32(b))

BTW我能够通过查看Mandelbrot Example

来解决这个问题