OpenCL - 执行缩减的方法

时间:2017-01-24 04:30:19

标签: c synchronization opencl atomic reduction

following post开始,我尝试实现数组的减少 使用此内核代码:

 #pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable

__kernel void sumGPU ( __global const long *input, 
               __global long *finalSum
               )
 {
  uint local_id = get_local_id(0);
  uint group_size = get_local_size(0);

  // Temporary local value
  local long tempInput;

  tempInput = input[local_id];

  // Variable for final sum 
  local long totalSumIntegerPart[1];

  // Initialize sums
  if (local_id==0)
    totalSumIntegerPart[0] = 0;

  // Compute atom_add into each workGroup 
  barrier(CLK_LOCAL_MEM_FENCE);

  atom_add(&totalSumIntegerPart[0], tempInput);

  barrier(CLK_LOCAL_MEM_FENCE);

  // Perform sum of each workGroup sum
  if (local_id==(get_local_size(0)-1))
    atom_add(finalSum, totalSumIntegerPart[0]);

}                   

finalSum的值不是预期值(我最初将input数组设置为:

 for (i=0; i<nWorkItems; i++)
    input[i] = i+1;

所以,我希望nWorkItems = 1024finalSum = nWorkItems*(nWorkItems+1)/2=524800

实际上,我得到finalSum = 16384

我通过sizeWorkGroup = 16nWorkItems = 1024来获得此结果。

奇怪的是,对于sizeWorkGroup = 32nWorkItems = 1024,我得到了另一个值:finalSum = 32768

我不理解最后一条指令(应该计算每个部分和的总和,即每个工作组):

// Perform sum of each workGroup sum
  if (local_id==(get_local_size(0)-1))
    atom_add(finalSum, totalSumIntegerPart[0]);

事实上,我认为指令atom_add(finalSum, totalSumIntegerPart[0]);将独立于local_id if condition

最重要的是必须执行此指令&#34; number of workGroups&#34; times(假设finalSum是所有工作组之间的共享值,不是吗?)。

所以我想我可以替换:

// Perform sum of each workGroup sum
  if (local_id==(get_local_size(0)-1))
    atom_add(finalSum, totalSumIntegerPart[0]);

通过

 // Perform sum of each workGroup sum
      if (local_id==0)
        atom_add(finalSum, totalSumIntegerPart[0]);

任何人都可以使用我的参数(sizeWorkGroup = 16nWorkItems = 1024)帮助找到正确的值,即finalSum等于524800

或者说我为什么最后的总和不能很好地执行?

更新:

这里是following link上的内核代码(它与我的略有不同,因为atom_add这里只为每个工作项增加1):

kernel void AtomicSum(global int* sum)

{
 local int tmpSum[1]; 
 if(get_local_id(0)==0){
 tmpSum[0]=0;}

barrier(CLK_LOCAL_MEM_FENCE);         
atomic_add(&tmpSum[0],1);         
barrier(CLK_LOCAL_MEM_FENCE);    

if(get_local_id(0)==(get_local_size(0)-1)){
  atomic_add(sum,tmpSum[0]);
 }

}

这是一个有效的内核代码,我的意思是,它会产生很好的结果吗?

也许一个解决方案可以放在我的内核代码的开头:

unsigned int tid = get_local_id(0);
unsigned int gid = get_global_id(0);
unsigned int localSize = get_local_size(0);
// load one tile into local memory
int idx = i * localSize + tid;
localInput[tid] = input[idx];

我将测试它并随时通知你。

由于

1 个答案:

答案 0 :(得分:1)

这一行错了:

tempInput = input[local_id];

应该是:

tempInput = input[get_global_id(0)];

您总是在汇总输入的第一个区域,这与您奇怪的结果一致。为什么它取决于工作组规模的参数。

16*16*64 = 16384
32*32*32 = 32768

您的代码也可以简化一下:

  uint local_id = get_local_id(0);

  // Variable for final sum 
  local long totalSumIntegerPart;

  // Initialize sums
  if (local_id==0)
    totalSumIntegerPart = 0;

  // Compute atom_add into each workGroup 
  barrier(CLK_LOCAL_MEM_FENCE);    
  atom_add(&totalSumIntegerPart, input[get_global_id(0)]);    
  barrier(CLK_LOCAL_MEM_FENCE);

  // Perform sum of each workGroup sum
  if (local_id==0)
    atom_add(finalSum, totalSumIntegerPart);

我不会滥用原子,因为它们不是最有效的减少方法。使用适当的缩减方法,您可以获得10倍的速度。但是,它可以作为PoC或学习本地内存和CL。