thrust::exclusive_scan_by_key 意外行为

thrust::exclusive_scan_by_key unexpected behavior

int data[ 10 ] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int keys[ 10 ] = { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
thrust::exclusive_scan_by_key( keys, keys + 10, data, data );

根据 Thrust Site 中的示例,我预计 0,0,1,1,2,2,3,3,4,4,但得到的却是 0,0,0,0,0,0,0,0,0;是错误,还是某处定义了此行为?

更重要的是,假设这不是错误,有没有办法轻松实现这种效果?

我认为您不了解 scan_by_key 的作用。来自 documentation:

"Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise"

scan_by_key 要求您的键数组使用连续值标记不同的段:

keys: 0 0 0 1 1 1 0 0 0 1 1 1
seg#: 0 0 0 1 1 1 2 2 2 3 3 3

thrust 比较 相邻 键来确定段。

您的密钥正在生成这样的分段映射:

int keys[ 10 ] = { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
          seg#:    0  1  2  3  4  5  6  7  8  9

由于您进行的是独占扫描,因此此类分段图(无论数据如何)的正确答案将全为零。

不完全清楚你想要实现什么"this effect",但你可能想通过键操作进行背靠背稳定排序,颠倒键和值的意义,重新排列这个将段(即键 1 和键 2)组合在一起的数据。