Egor Khromov
Egor Khromov

Reputation: 33

How to assign pointer to element of array of some struct in this array(OpenCL)?

May be you can advise me how to create a hierarchical structure in OpenCL. It is easy if you have "new" or "malloc", but I don't know how do it in GPGPU. So I created 3 kernels:

  1. To send sizeof struct to host.
  2. To initialize data.
  3. And kernel which uses data initialized in second kernel.

I have this struct in OpenCL:

    typedef struct some some;
struct some{
    char data[4];
    some* children[8];
};

First kernel says that size of this structure is 36 bytes(4 for data and 32 for pointers).

Next I allocate memory on GPU based on previous information and call second kernel:

kernel void import(global some *buffer){
for(int i=0;i<4;i++){
buffer[0].data[i]=255; //For example, doesn't matter
}
//Now I need to assign pointer to next element of array(buffer) to first element
buffer[0].children[0]=&buffer[1];
}

But kernel not compiles. Also I tried:

*buffer[0].children[0]=buffer[0];

It compiles, but crashes of course. It is logically wrong) Without assignments of pointers everything works fine. Very cool program for 1 element)

Upvotes: 0

Views: 345

Answers (2)

doqtor
doqtor

Reputation: 8494

If your device supports OpenCL 2.0 then you can use Shared Virtual Memory. The pointers created on the host will be valid on the device too.

The description of Shared Virtual Memory concept and the examples you can find here and here.

Upvotes: 0

Tim Child
Tim Child

Reputation: 3012

Try using offsets or array indices instead of pointers.

typedef struct some some; struct some{ char data[4]; size_t children[8]; // an array of subscripts };

...

// buffer[0].children[0]=&buffer[1]; becomes buffer[0].children[0] = 1;

So now you can reference a child via its subscript

buffer[ buffer[0].children[0] ].char[0]

Upvotes: 2

Related Questions