循环中的OpenCL clEnqueueNDRangeKernel

时间:2015-09-12 06:58:20

标签: opencl

我正在使用Mac上的opencl 1.2开发一个简单的radix-2 FFT算法。我正在尝试在笔记本电脑中使用HD 5000图形

我的主机代码是这样的:

gws=4;
lws=1;

for (cur_iter=0; cur_iter <= 2; cur_iter++){
    ret = clSetKernelArg(r2kernel, 3, sizeof(cl_int), (void *)&cur_iter);
     printf("iter %d \n", cur_iter);
    ret = clEnqueueNDRangeKernel(command_queue, r2kernel, 1, NULL, &gws, &lws, 0, NULL, &kernelDone);
    // printf("ret %d \n", ret);
    ret = clWaitForEvents(1, &kernelDone);
    // printf("ret %d \n", ret);
}

cur_iter表示FFT的当前阶段。我的内核代码是这样的:

kernel void radix2(global float2 * x, global float2 * w,int iter, int cur_iter)
{

int gid = get_global_id(0); // number of threads

int butterflySize = 1 << (iter-cur_iter-1);
int butterflyGrpDist = 1 << (iter-cur_iter);
int butterflyGrpBase = (gid >> (iter-cur_iter-1))*(butterflyGrpDist);
int butterflyGrpOffset = gid & (butterflySize-1);

int a = butterflyGrpBase + butterflyGrpOffset;
int b = a + butterflySize;

 printf("gid %d pass %d, %d, %d ,total iter %d \n", gid,cur_iter,a,b,iter);


float2 u0 = x[a];

float2 u1 = x[b];

float2 tmp;

 DFT2(u0,u1,tmp);

int waddr=butterflyGrpOffset<<cur_iter;

float2 twiddle = w[waddr];

MUL(u1,twiddle,tmp);



x[a] = u0;

x[b] = u1;

}

我在内核中打印出了gid和cur_iter。我期望在每次迭代时获得4个内核(用于8点FFT)。但我得到的就像这样

iter 0 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
iter 1 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
gid 0 pass 1, 0, 2 ,total iter 3 
gid 1 pass 1, 1, 3 ,total iter 3 
gid 2 pass 1, 4, 6 ,total iter 3 
gid 3 pass 1, 5, 7 ,total iter 3 
iter 2 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
gid 0 pass 1, 0, 2 ,total iter 3 
gid 1 pass 1, 1, 3 ,total iter 3 
gid 2 pass 1, 4, 6 ,total iter 3 
gid 3 pass 1, 5, 7 ,total iter 3 
gid 0 pass 0, 0, 4 ,total iter 3 
gid 1 pass 0, 1, 5 ,total iter 3 
gid 2 pass 0, 2, 6 ,total iter 3 
gid 3 pass 0, 3, 7 ,total iter 3 
gid 0 pass 1, 0, 2 ,total iter 3 
gid 1 pass 1, 1, 3 ,total iter 3 
gid 2 pass 1, 4, 6 ,total iter 3 
gid 3 pass 1, 5, 7 ,total iter 3 
gid 0 pass 2, 0, 1 ,total iter 3 
gid 2 pass 2, 4, 5 ,total iter 3 
gid 3 pass 2, 6, 7 ,total iter 3 
gid 1 pass 2, 2, 3 ,total iter 3 

这意味着在每次迭代时,传递给我内核的cur_iter总是从零开始,内核启动的实例也是错误的,即使它的值是2或3。我想知道为什么。任何形式的帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

看起来 printf 缓冲区正在为每次迭代重新输出(每次迭代都包含以前打印过的所有内容,再加上更多。没有办法你在传递中多次获得相同的gid。如果你使用clFinish而不是clWaitForEvents它会改变什么吗?