Search code examples
pythonopenclpyopencl

How to use float2 in pyopencl?


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 :(


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.