Passing a function as an argument in OpenCL

谁都会走 提交于 2021-01-27 19:52:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!