在OpenCL中将函数作为参数传递 [英] Passing a function as an argument in OpenCL

查看:97
本文介绍了在OpenCL中将函数作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将函数指针传递给OpenCL 1.2中的内核?我知道它可以在C语言中完成,但是我不知道如何在OpenCL的C语言中完成.

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.

我想做这篇文章中描述的相同的事情:

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.

推荐答案

简短:

OpenCL的C!= C,可以认为它是一种语法帮助,它的大部分外观类似于c,但是它并未涵盖所有语言细节,并且将其扩展到一定程度,并且没有提供(参见233页的规格),6.9 a: https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf )

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)

因此,您不能那样做.

详细:

在C中传递函数指针是一种结构化代码以优化逻辑的方法.另一方面,内核不是构造事物的关键,而是优化您编写的每一行以使其最大可能效率的关键.最后,将所有内容组合到一个程序中,每个线程将执行该程序,并且内联您调用的每个函数(您没有像CPU那样的调用堆栈).

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).

因此,在这种情况下,您想要重新考虑要在C中实现的程序逻辑,以便针对一个或多个内核调用对其进行优化,从而消除了函数指针方法.针对您的函数指针方法进行优化会导致可读性问题,性能问题或代码异味

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.

这篇关于在OpenCL中将函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆