Recursion OpenCL与间接递归
Opencl不支持递归函数,但这是否也包括间接版本Recursion OpenCL与间接递归,recursion,opencl,Recursion,Opencl,Opencl不支持递归函数,但这是否也包括间接版本 void recursiveA(int *a,int b) // call this first to start recursion { a[b]=3; if(b<10) { recursiveB(a,b+1); // A calls B } } void recursiveB(int *a, int b) { a[b]=3; if(b<10)
void recursiveA(int *a,int b) // call this first to start recursion
{
a[b]=3;
if(b<10)
{
recursiveB(a,b+1); // A calls B
}
}
void recursiveB(int *a, int b)
{
a[b]=3;
if(b<10)
{
recursiveA(a,b+1); // B calls A while A still not finished before
// and entry point & arguments of A are corrupt ?
}
}
void recursiveA(int*a,int b)//首先调用此函数以开始递归
{
a[b]=3;
如果(bOpenCL不支持递归控制流,其中包括相互递归。因此,为了确保您的代码在您希望针对的每个平台上正常工作,您应该避免使用任何形式的递归,而是使用迭代方法编写算法
在实践中,OpenCL编译器可能能够很好地处理某些递归算法。例如,如果您的函数是尾部递归的,那么编译器可以通过应用标准尾部调用优化技术来生成非递归形式。我刚刚尝试了您发布的第二个递归代码段,它被多个应用程序接受OpenCL编译器。第一个代码段导致它们全部崩溃,这表明它们无法应用必要的转换来避免递归调用(尽管它们显然应该生成适当的错误消息,而不是崩溃)
因此,在一些OpenCL实现中,您可能可以避免使用简单的递归,但是为了在不同平台之间实现最大的可移植性,我强烈建议您避免使用递归
void recurse(int *a, int b)
{
a[b]=3;
if(b<10)
{
recurse(a,b+1); // some OpenCL devices does not have the ability so this is not
// possible in OpenCL
}
}