gpt4 book ai didi

python - PyOpenCL,无法设置参数。无效值

转载 作者:行者123 更新时间:2023-11-30 23:30:25 27 4
gpt4 key购买 nike

我已经获得了提供的 OpenCL 内核以在 C 环境中执行,但是当我尝试使用 PyOpenCL 和提供的代码运行它时,出现以下错误:

> 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

所以看来我向内核传递了一个无效的参数,但我不知道为什么它会提示这一点。有什么想法吗?

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()
__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;
}

最佳答案

您的内核调用存在两个错误。与回溯相关的错误是 self.a 是一个 Python int 对象,而内核需要一个 OpenCL unsigned int,即 32 位。您需要使用(例如)numpy.int32(self.a) 显式传入 32 位整数。第二个错误是全局工作大小参数需要是一个元组。

因此,内核调用的正确代码应该是:

self.program.integrate_f(self.queue, (self.a,), None, numpy.int32(self.a), self.dest_buf)

关于python - PyOpenCL,无法设置参数。无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599276/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com