0

I need to be able to do something like this in OpenCL, where to my kernel I'll pass an index, and that index represents what function I need currently (C++ example):

int a(int _i) {
    return +_i;
}

int b(int _i) {
    return -_i;
}

decltype(a) * FUNCS[2] = { a, b };

int main() {
    FUNCS[0](9);
}

I basically want to create n local functions in the kernel file along with the main kernel, and being able to choose what to use based on the index I'll pass globally.

Is this possible?

Frax
  • 143
  • 7
  • What about std::function? – folibis Aug 18 '22 at 15:05
  • There are no function pointers in OpenCL. You have to emulate it with if statements. – t.niese Aug 18 '22 at 15:10
  • @t.niese Is it the only way? I have a lot of functions that may call themselves too, so I would pass a lot of indices to decide which functions I need, I cant do that with ifs, I would have to create all combinations... – Frax Aug 18 '22 at 15:12
  • @Frax See related/dupe [Does opencl support Function Pointers?](https://stackoverflow.com/questions/7391166/does-opencl-support-function-pointers). Also see [How best to implement function pointers in a C++ dialect that does not support it?](https://stackoverflow.com/questions/59870562/how-best-to-implement-function-pointers-in-a-c-dialect-that-does-not-support-i) that says: *"OpenCL doesn't support function pointers"*. And also [Passing a function as an argument in OpenCL](https://stackoverflow.com/questions/51249952/passing-a-function-as-an-argument-in-opencl). – Jason Aug 18 '22 at 15:13
  • @JasonLiam Looks like it can't be done then, Ifs and Switches are impraticable for my situation, too many functions to be called. – Frax Aug 18 '22 at 15:18
  • @Frax At least you do have a workaround, though impracticable in your case. – Jason Aug 18 '22 at 15:19
  • @JasonLiam Thanks a lot. Is there a reason why in 2022 still there's no support for this (Will there ever be)? – Frax Aug 18 '22 at 15:21
  • 1
    @Frax I don't know why there is still no support for this. You can ask OPENCL developers the same [here](https://www.khronos.org/opencl/). In the above [link](https://www.khronos.org/opencl/) there is a tab called *Developers* under which you'll find a menu item called *"Groups and forums"* where a query could be made. You're welcome :) – Jason Aug 18 '22 at 15:59

0 Answers0