Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does opencl support Function Pointers?

Tags:

opencl

Is there a way to use C style function pointers in OpenCL?

In other words, I'd like to fill out a OpenCL struct with several values, as well as pointers to a OpenCL function. I'm not talking about going from a CPU function to a GPU function, I'm talking about going from a GPU function to a GPU function.

Is this possible?

--- EDIT ---

If not, it there a way around this? In CUDA we have object inheritance, and in 4.0 we even have virtual functions. About the only way I can find to implement a runtime dispatch like this is to resort to if statements, and that will get ugly really fast.

like image 635
Timothy Baldridge Avatar asked Sep 12 '11 16:09

Timothy Baldridge


2 Answers

From the OpenCL 1.1 specification:

Section 6.8 (Restrictions) (a):

The use of pointers is somewhat restricted. The following rules apply:

  • Arguments to kernel functions declared in a program that are pointers must be declared with the __global, __constant or __local qualifier.
  • A pointer declared with the __constant, __local or __global qualifier can only be assigned to a pointer declared with the __constant, __local or __global qualifier respectively.
  • Pointers to functions are not allowed.

The usual work around I use for this is with macros. Evil, but currently inescapable. So I typically end up with something like:

#define FEATURENAME_START impl1_start
#define FEATURENAME_END   impl1_end

I then either inject this into the kernel at compilation time or pass it as an argument to the OpenCL compiler. It's not quite runtime in the usual sense, but it can be runtime from the Host's perspective still, even if not the device.

like image 111
Flexo Avatar answered Nov 08 '22 00:11

Flexo


AMD has future plans for hardware support for this, so there may be future extensions for them.

like image 36
arsenm Avatar answered Nov 08 '22 00:11

arsenm