Question

I am using PyOpenCL to write OpenCL codes.My kernel program has an input as float2.

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

I need to declare a buffer in python to store output and to pass input for the Kernel.

What is the equivalent data type in python to float2? I tried dtype on numpy with no success :(

Was it helpful?

Solution

Here is a MWE to use float2 in a pyOpenCL program:

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

I hope that helps.

OTHER TIPS

An alternative to using cl_array.vec.float2 is to just use the np.float32 type and make your numpy (and opencl) buffer twice as large.

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top