bubblebath
bubblebath

Reputation: 999

initialise an OpenCL object

Hi I was wondering if I can create an openCL object like this struct

struct Product
{

    int n_id;
    char n_name;

    Item(id, name){n_id = id; n_name = name;}
} item;

I was wanting to pass in this information to a kernel to make the struct re-usable from another struct.

I want to add items via a kernel

kernel addItem(int name, char name)
{
    Item it(id,name);
    Items.add(it);
}

So my question is how do I pass this information over and how to I make the objects that I create re-usable from another kernel?

Cheers

Upvotes: 0

Views: 1221

Answers (1)

user2088790
user2088790

Reputation:

OpenCL kernels are written in a restricted (e.g. no pointers in structs) and extended (e.g. float4 data type) C99 like language. They are not written in C++.

You initialize C like structures on the host and then copy them over. C like structures don't have methods. Using the C++ OpenCL bindings (from cl.hpp) on the host with Visual Studio I do something like this:

#pragma pack(16)
struct Light {
    cl_float4 pos;
    cl_float4 dir;
    cl_float4 intensity;
    cl_int type; 
    cl_int pad[3];
};
#pragma pack()

const int nlight = 10
Light lights[nlight];
//...code to initialize array of structs

cl::Buffer lights_mem = cl::Buffer(context, CL_MEM_COPY_HOST_PTR,  sizeof(Light)*nlight, lights);

kernel1.setArg(0, lights_mem);

That copies the lights to the OpenCL device. In the kernel you can access the struct of lights like this:

__kernel void trace(__global Light* lights, ...) {
    float4 pos = lights[0].pos
    //find a new position (pos_new)
    lights[0].pos = pos_new;

When the kernel finishes you can pass cl::Buffer lights_mem to the next kernel.

kernel2.setArg(0, lights_mem); 

However, you can get better speeds by using read only or write only buffers so it may help to separate your kernels into those that read only and those that write only.

I don't know if the pack() pragma and padding are still necessary but I keep using them because everytime someone says they are not necessary anymore I get a problem which goes away when I put them back.

Upvotes: 2

Related Questions