PyOpenCL, failed to set arguments. Invalid values
我已经获得了提供的 OpenCL 内核以在 C 环境中执行,但是当我尝试使用 PyOpenCL 和提供的代码运行它时,我收到以下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 | > Traceback (most recent call last): > File"integral.py", line 38, in <module> > example.execute() > File"integral.py", line 26, in execute > self.program.integrate_f(self.queue, self.a, None, self.a, self.dest_buf) > File"/Library/Python/2.7/site-packages/pyopencl-2013.3-py2.7-macosx-10.9- > x86_64.egg/pyopencl/__init__.py", line 506, in kernel_call > self.set_args(*args) > File"/Library/Python/2.7/site-packages/pyopencl-2013.3-py2.7-macosx-10.9- > x86_64.egg/pyopencl/__init__.py", line 559, in kernel_set_args > % (i+1, str(e), advice)) > pyopencl.LogicError: when processing argument #1 (1-based): Kernel.set_arg failed: invalid value - > invalid kernel argument |
看来我向内核传递了一个无效参数,但我不知道它为什么抱怨这个。有什么想法吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import pyopencl as cl import numpy class CL: def __init__(self): self.ctx = cl.create_some_context() self.queue = cl.CommandQueue(self.ctx) def loadProgram(self, filename): #read in the OpenCL source file as a string f = open(filename, 'r') fstr ="".join(f.readlines()) print fstr #create the program self.program = cl.Program(self.ctx, fstr).build() def popCorn(self, n): mf = cl.mem_flags self.a = int(n) #create OpenCL buffers self.dest_buf = cl.Buffer(self.ctx, mf.WRITE_ONLY, bumpy.empty(self.a). nbytes) def execute(self): self.program.integrate_f(self.queue, self.a, None, self.a, self.dest_buf) c = numpy.empty_like(self.dest_buf) cl.enqueue_read_buffer(self.queue, self.dest_buf, c).wait() print"a", self.a print"c", c if __name__ =="__main__": example = CL() example.loadProgram("integrate_f.cl") example.popCorn(1024) example.execute() |
1 2 3 4 5 6 7 8 9 10 | __kernel void integrate_f(const unsigned int n, __global float* c) { unsigned int i = get_global_id(0); float x_i = 0 + i*((2*M_PI_F)/(float)n); if (x_i != 0 || x_i != 2*M_PI_F) { c[i] = exp(((-1)*(x_i*x_i))/(4*(M_PI_F*M_PI_F))); } else c[i] = 0; } |
您的内核调用有两个错误。与您的回溯相关的错误是
因此,内核调用的正确代码应该是:
1 | self.program.integrate_f(self.queue, (self.a,), None, numpy.int32(self.a), self.dest_buf) |