cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

david_aiken
Journeyman III

structure navigation

hi all..

I'm passing in an array of structures, with the size of each structure to a kernel. The first member of the structure is a uint and i would like to be able to change only this member within the kernel. I've tried this:

typedef struct

{

unsigned int dataOfInterestToKernel;

char s[64] notOfInterestToKernel;

}AStruct;

AStruct data[40];

call clEnqueueNDRangeKernel with pointer to data[], sizeof(AStruct) using 40 threads.

 

__kernel void test(__global char *structData, uint structSz)

{

    uint ndx = get_local_id(0);

    uint* s = (uint*)(structData + ndx *structSz);

    *s = ndx;

}



Unfortunately the kernel doesn't build:

error: invalid type conversion

      uint* s = (uint*)(structData + ndx * structSz);



Is there a good way to do this? It is possible to do it in CUDA.

0 Likes
3 Replies
RyFo18
Journeyman III

I've done this before.  I'm not sure how you have your OpenCL code laid out.  I typically have all of mine in a separte file (i.e. hello.cl).  If I pass a structure to a kernel such as the one you suggested, I will also declare this same structure within hello.cl.  Then you can change the value simply by:

 

structData[ndx]->dataOfInterestToKernel = ndx;

0 Likes

david_aiken,
The problem is you are converting between address spaces, which is illegal in OpenCl. The correct way to do this is as follows:
__kernel void test(__global char *structData, uint structSz)
{
uint ndx = get_local_id(0);
global uint* s = (global uint*)(structData + ndx *structSz);
*s = ndx;
}

0 Likes

Thanks, that fixes it. Interestingly, the NVidia compiler is not insistent on the memory specifier, but fails later. 

0 Likes