简体   繁体   English

OpenCL的Groupby减少?

[英]Groupby reduction in OpenCL?

I want to implement a groupby reduction in OpenCL. 我想在OpenCL中实现groupby减少。 For example, the input 例如,输入

a1 a2 a3 b1 b2 c3 c4

would produce 会产生

a6 b3 c7

The C pseudocode looks like this: C伪代码如下所示:

int data[n][2], result[n][2], result_count = -1, 
    added = 0, group = data[0][0];
for (int i = 0; i < n; i++) { 
  if (group == data[i][0]) {
    added += data[i][1];
  } else {
    result[++result_count][0] = group;
    result[result_count][1] = added;
    group = data[i][0];
    added = 0;
  }
} 
return result, result_count;

The only standard algorithm I know which goes in this direction is parallel reduction; 我知道朝这个方向发展的唯一标准算法是并行约简。 however, it reduces to one number and not to a buffer of added values by group. 但是,它减少为一个数,而不是按组减少附加值的缓冲区。 I am not sure if parallel reduction could work with a dynamic result buffer (eg in local memory) and still be efficient in terms of performance. 我不确定并行缩减是否可以与动态结果缓冲区一起使用(例如在本地内存中),并且在性能方面仍然有效。

A solution by Hashing 哈希解决方案

Phase 1) A hashing scheme can be used to hash the group value to a location, then an atomic add can sum the contents of the second value. 阶段1)可以使用散列方案将组值散列到某个位置,然后原子加法可以将第二个值的内容求和。

Phase 2) A prefix sum scan algorithm pass over the hash table to compact it. 阶段2)前缀和扫描算法将哈希表传递给它以对其进行压缩。

Phase 3) Optionally sort the results 阶段3)(可选)对结果进行排序

A solution by Sorting 排序解决方案

Phase 1) Sort the data on the group value 阶段1)按组值对数据进行排序

Phase 2) Use a reduction to sum each group 阶段2)使用归约法对每个组求和

Phase 3) A prefix sum scan to compact the sums 阶段3)前缀和扫描以压缩和

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM