OpenCL "virtual" functions


Does OpenCL support some kind of C++ virtual functions or C function pointers as kernel arguments?

The idea is to call functions compiled from other .cl file.

For example, I have a file called

__device int MyCustomAdd ( const int a, const int b)


   return a+b;


and I have this file

__device int (*deviceFunctionPointer) ( const int a, const int b );

__kernel __device void MyOpenCLKernel ( deviceFunctionPointer *p )


   //Do some things...

//Now call the user-defined function pointer

   const int result = deviceFunctionPointer(1,2);


in my CPU code I could do this:

void main ()


   //init OpenCL, etc...

//load the user-defined shader function/interface

   clModule *mod = clCompileModule("");

   void *MyCustomAddPtr = clGetDeviceFunction(mod,"MyCustomAdd");

//load my kernel

   clModule *krnMod = clCompileModule("");

//fire my kernel

   array<void*> krnParams;


clExecuteKernel ( krnMod, "MyOpenCLKernel", 256_threads_per_block, krnParams );



No, OpenCL doesn’t support function pointers in device code (most current hardware doesn’t support jumping to computed addresses).