问题描述
是否可以在OpenCL中使用C样式函数指针?
Is there a way to use C style function pointers in OpenCL?
换句话说,我想用几个值以及指向OpenCL函数的指针填充OpenCL结构.我不是在谈论从CPU功能转到GPU功能,而是在谈论从GPU功能转到GPU功能.
In other words, I'd like to fill out a OpenCL struct with several values, as well as pointers to a OpenCL function. I'm not talking about going from a CPU function to a GPU function, I'm talking about going from a GPU function to a GPU function.
这可能吗?
-编辑--
如果没有,有办法解决吗?在CUDA中,我们具有对象继承,而在4.0中,我们甚至具有虚函数.我发现实现这样的运行时分派的唯一途径是诉诸if语句,这将使丑陋变得非常快.
If not, it there a way around this? In CUDA we have object inheritance, and in 4.0 we even have virtual functions. About the only way I can find to implement a runtime dispatch like this is to resort to if statements, and that will get ugly really fast.
推荐答案
来自 OpenCL 1.1规范:
第6.8节(限制)(a):
Section 6.8 (Restrictions) (a):
- 程序中声明为指针的内核函数的参数 必须使用__global,__ constant或__local限定符声明.
- A 用__constant,__ local或__global限定符声明的指针 只能分配给使用__constant声明的指针, __local或 分别是__global限定词.
- 指向函数的指针不正确 允许.
- Arguments to kernel functions declared in a program that are pointers must be declared with the __global, __constant or __local qualifier.
- A pointer declared with the __constant, __local or __global qualifier can only be assigned to a pointer declared with the __constant, __local or __global qualifier respectively.
- Pointers to functions are not allowed.
我为此使用的通常解决方法是使用宏.邪恶,但目前不可避免.所以我通常会得到类似以下的结果:
The usual work around I use for this is with macros. Evil, but currently inescapable. So I typically end up with something like:
#define FEATURENAME_START impl1_start
#define FEATURENAME_END impl1_end
然后我将其在编译时注入内核,或者将其作为参数传递给OpenCL编译器.从通常意义上讲,它不是完全运行时,但是从主机的角度来看,即使不是设备,也可以是运行时.
I then either inject this into the kernel at compilation time or pass it as an argument to the OpenCL compiler. It's not quite runtime in the usual sense, but it can be runtime from the Host's perspective still, even if not the device.
这篇关于opencl是否支持功能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!