我正在使用 PyOpenCL 编写 OpenCL 代码。我的内核程序有一个作为 float2 的输入。
__kernel void Pack_Cmplx( __global float2* Data_In, __global float2* Data_Out, int N)
我需要在 python 中声明一个缓冲区来存储输出并为内核传递输入。
python中与float2等效的数据类型是什么?我在numpy上尝试了dtype但没有成功:(
这是在程序中使用 float2 的 MWE pyOpenCL
:
import numpy as np
###################################################
# openCL libraries
###################################################
import pyopencl as cl
import pyopencl.array as cl_array
deviceID = 0
platformID = 0
workGroup=(1,1)
N = 10
testData = np.zeros(N, dtype=cl_array.vec.float2)
dev = cl.get_platforms()[platformID].get_devices()[deviceID]
ctx = cl.Context([dev])
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
Data_In = cl.Buffer(ctx, mf.READ_WRITE, testData.nbytes)
prg = cl.Program(ctx, """
__kernel void Pack_Cmplx( __global float2* Data_In, int N)
{
int gid = get_global_id(0);
Data_In[gid] = 1;
}
""").build()
prg.Pack_Cmplx(queue, (N,1), workGroup, Data_In, np.int32(N))
cl.enqueue_copy(queue, testData, Data_In)
print testData
我希望这会有所帮助。
使用的替代方法cl_array.vec.float2
是仅使用np.float32
类型并使您的 numpy(和 opencl)缓冲区大两倍。
testData = np.zeros(N*2, dtype=np.float32)
Data_In = cl.Buffer(ctx, mf.READ_WRITE, testData.nbytes)
prg = cl.Program(ctx, """
__kernel void Pack_Cmplx( __global float2* Data_In, int N)
{
int gid = get_global_id(0);
Data_In[gid] = 1; // not sure about this tbh. do we set both values to 1 here ?
}
""").build()
prg.Pack_Cmplx(queue, (N,1), workGroup, Data_In, np.int32(N))