问题
Is it possible to pass a function pointer to a kernel in OpenCL 1.2? I know it can be done in C, but I don't know how to do it in OpenCL's C.
Edit: I would like to do the same thing that is described in this post: How do you pass a function as a parameter in C?, but to a kernel.
Previously, I have used inline functions to call them from a kernel, but I want the function to be a parameter instead of hard coded in.
回答1:
Short:
OpenCL's C != C, consider it as a syntactical help that most of it looks like c, but it does not cover all language detail and extends it to some point and it doesn't supply that (See Spec at Page 233, 6.9 a: https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf)
Thus, you cannot do that.
Detailed:
Passing function pointers in C is a way of structuring your code in order to optimize logic. Kernels on the other hand are not the point to structure things, rather then a point to optimize every line you code to its maximum possible efficiency. In the end, everything will be combined into a single program that every thread will execute and every function you call will be inlined (You do not have a call stack as on the CPU).
So what you want in that case, is to rethink your program logic that you wanted to implement as in C, in order to optimize it for one or multiple kernel calls, eliminating the function pointer approach. Optimizing it for your function pointer approach will end up in either readability problems, performance problems or code smell.
回答2:
U can pass any data into Kernel but you cannot call that function from kernel.
来源:https://stackoverflow.com/questions/51249952/passing-a-function-as-an-argument-in-opencl