很难理解你有什么问题,如果你在谈论内核参数,你应该至少有一个内核参数,没有内核参数的内核是没有用的,因为 OpenCL 提供了基于数据的并行性,如果你不'没有任何数据你没有任何并行性你可以在一个 cpu 线程上执行你的内核......
如果您对维度有疑问,即您需要 4 个或更多维度,但 OpenCL 提供了 3 个维度,那么您应该执行以下操作:
// Assuming that you have only a,b,c,d
// and 'amount of work' = 10 * 15 * 05 * 15
int index = get_global_id(0);
int d = index % 15; index /= 15;
int c = index % 05; index /= 05;
int b = index % 15; index /= 15;
int a = index % 10; index /= 10;
#etc (do something with a,b,c,d)
最后一件事,试着让你的程序尽可能的扁平化,OpenCL 不喜欢很多循环和分支逻辑,试着手动解开你的循环而不是:
// if it is possible to render some constant into the OpenCL code,
// than try to expand it as much as possible
for (int i = 0; i < 4; i++) // The constant is 4
{
float x = sin(3.14 * i + ...);
float y = cos(x + ....);
x[i] = a * i * x + y ....;
}
这样写:
float x;
float y;
x = sin(3.14 * 0 + ...);
y = cos(x + ....);
x[0] = a * 0 * x + y ....;
x = sin(3.14 * 1 + ...);
y = cos(x + ....);
x[1] = a * 1 * x + y ....;
x = sin(3.14 * 2 + ...);
y = cos(x + ....);
x[2] = a * 2 * x + y ....;
x = sin(3.14 * 3 + ...);
y = cos(x + ....);
x[3] = a * 3 * x + y ....;
越平坦越好!我说的是合理的扩展,如果循环中有 1024 个循环,那么所有这些循环都是不合理的。在这种情况下,您应该将其扩展为 2 或 4 或 8 或 16 个周期,这将导致您拥有 512 或 256 或 128 或 64 个循环周期,这可以为您带来巨大的性能提升...