【问题标题】:How to use float2 in pyopencl?如何在 pyopencl 中使用 float2?
【发布时间】:2017-04-15 12:46:48
【问题描述】:

我正在使用 PyOpenCL 编写 OpenCL 代码。我的内核程序有一个输入作为 float2。

__kernel void   Pack_Cmplx( __global float2* Data_In, __global float2* Data_Out, int  N)

我需要在 python 中声明一个缓冲区来存储输出并为内核传递输入。

python中与float2等价的数据类型是什么?我在 numpy 上尝试了 dtype 没有成功:(

【问题讨论】:

  • 这实际上适用于 float2。让我知道是否有更好的方法! 'dt=np.dtype([("num1",np.float16),("num2",np.float16)]) Data_Out = np.empty(100, dtype=dt)'.
  • 相当肯定它应该是两个 np.float32 值的向量,而不是 float16。

标签: python opencl pyopencl


【解决方案1】:

这是一个在 pyOpenCL 程序中使用 float2 的 MWE:

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

希望对你有帮助。

【讨论】:

  • 哦,我不知道 pyopencl 有一个用于 float2 的内置数据类型。这有帮助。
【解决方案2】:

使用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))

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 2014-11-25
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多