How to declare a function inside a kernel function in OpenCL?

I want to define a function inside a kernel function to make the indexing code more understandable:

kernel void do_something (const int some_offset, const int other_offset, global float* buffer) { int index(int x, int y) { return some_offset+other_offset*x+y; } float value = buffer[index(1,2)]; ... } 

Otherwise, I will have to declare an index function outside my kernel and an index like

 float value = buffer[index(1,2,some_offset,other_offset)]; 

which would make it more ugly, etc. Is there any way to do this? The compiler gives me an error saying:

 OpenCL Compile Error: clBuildProgram failed (CL_BUILD_PROGRAM_FAILURE). Line 5: error: expected a ";" 

Is it possible to do what I want, or is there another way to achieve the same? Thanks!

+4
source share
1 answer

C does not support nested functions. However, your case is simple enough to be implemented with a macro:

 #define index(x,y) some_offset+other_offset*(x)+(y) 

The brackets around x and y are critical to making the macro the way you want it if you pass in more complex expressions like index(a+b,c) .

+5
source

All Articles