如何使用pyopencl将参数传递给ocl内核?
如何传递一些参数,这些参数将在.cl文件中作为使用pyopencl定义的预处理器处理 意思是: foo.cl如何使用pyopencl将参数传递给ocl内核?,opencl,pyopencl,Opencl,Pyopencl,如何传递一些参数,这些参数将在.cl文件中作为使用pyopencl定义的预处理器处理 意思是: foo.cl # define LIMIT 12 typedef struct { uint i[LIMIT]; } foomatic; typedef struct { uint i[LIMIT]; // python script passing LIMIT to set it } foomatic; 转向 foo_nodefs.cl # define LIMIT 12 typ
# define LIMIT 12
typedef struct {
uint i[LIMIT];
} foomatic;
typedef struct {
uint i[LIMIT]; // python script passing LIMIT to set it
} foomatic;
转向
foo_nodefs.cl
# define LIMIT 12
typedef struct {
uint i[LIMIT];
} foomatic;
typedef struct {
uint i[LIMIT]; // python script passing LIMIT to set it
} foomatic;
谢谢
John编辑:扩展答案,使其尽可能详细 有两种方法可以做到这一点:
import pyopencl as cl
import numpy
import numpy.linalg as la
a = numpy.random.rand(50000).astype(numpy.float32)
b = numpy.random.rand(50000).astype(numpy.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
defines = """
#define AXIS 0
#define COEFF 1
"""
prg = cl.Program(ctx,
defines +
"""
__kernel void sum(__global const float *a,
__global const float *b, __global float *c)
{
int gid = get_global_id(AXIS);
c[gid] = a[gid] + b[gid] + COEFF;
}
""").build()
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_copy(queue, a_plus_b, dest_buf)
print(la.norm(a_plus_b - (a+b+1)), la.norm(a_plus_b))
Program.build的options
关键字将构建选项直接传递给:
(我使用了PyOpenCL文档主页上修改过的源代码。在PyOpenCL 2013.1上进行了测试)。编辑:扩展答案,使其尽可能详细 有两种方法可以做到这一点:
import pyopencl as cl
import numpy
import numpy.linalg as la
a = numpy.random.rand(50000).astype(numpy.float32)
b = numpy.random.rand(50000).astype(numpy.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
defines = """
#define AXIS 0
#define COEFF 1
"""
prg = cl.Program(ctx,
defines +
"""
__kernel void sum(__global const float *a,
__global const float *b, __global float *c)
{
int gid = get_global_id(AXIS);
c[gid] = a[gid] + b[gid] + COEFF;
}
""").build()
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_copy(queue, a_plus_b, dest_buf)
print(la.norm(a_plus_b - (a+b+1)), la.norm(a_plus_b))
Program.build的options
关键字将构建选项直接传递给:
(我使用了PyOpenCL文档主页上修改过的源代码。在PyOpenCL 2013.1上进行了测试)。你是在问如何在Python中连接两个字符串吗?请为我提供一些简单的例子,因为我不确定我是否理解这个问题。不。我想知道,您希望我将参数作为字符串放置在哪里?您是指您读入的字符串,用于加载cl源以构建它吗?意思是cl.Program(ctx,).build()是的,没错。只要修改你传递的字符串。好的,但是没有其他常规方法吗?我在某个地方读到,你可以通过一些论证来做到这一点。那怎么办?是的,有办法。将它添加到答案中(尽管我个人认为没有任何理由选择它而不是字符串操作)。您是否在问如何在Python中连接两个字符串?请为我提供一些简单的例子,因为我不确定我是否理解这个问题。不。我想知道,您希望我将参数作为字符串放置在哪里?您是指您读入的字符串,用于加载cl源以构建它吗?意思是cl.Program(ctx,).build()是的,没错。只要修改你传递的字符串。好的,但是没有其他常规方法吗?我在某个地方读到,你可以通过一些论证来做到这一点。那怎么办?是的,有办法。将它添加到答案中(尽管我个人认为没有任何理由选择它而不是字符串操作)。