问题描述
|
我想在内核函数中定义一个函数,以使索引代码更清晰:
kernel void do_something (const int some_offset,const int other_offset,global float* buffer)
{
int index(int x,int y)
{
return some_offset+other_offset*x+y;
}
float value = buffer[index(1,2)];
...
}
否则,我必须在内核之外声明索引函数,并像
float value = buffer[index(1,2,some_offset,other_offset)];
这会使它变得更难看等等。有什么办法可以做到这一点?编译器给我一个错误,说:
OpenCL Compile Error: clBuildProgram Failed (CL_BUILD_PROGRAM_FAILURE).
Line 5: error: expected a \";\"
是否可以做我想做的事情,或者有其他方法可以实现相同目的?
谢谢!
解决方法
C不支持嵌套函数。但是,您的情况很简单,可以使用宏实现:
#define index(x,y) some_offset+other_offset*(x)+(y)
如果将更复杂的表达式(例如index(a+b,c)
)传递给它,则x和y周围的括号对于使宏执行所需的操作至关重要。