数组中每个段的推力CUDA计算索引

问题描述 投票:0回答:1

我有一个int数组,在我的应用程序中用作键。这已经排序。我想为每个唯一键分配一个从0开始的唯一索引。如何在cuda中使用推力呢?

    int* sorted_keys = {1, 1, 1, 13, 13, 13, 13, 13, 19, 19, 20}
    // Some thrust operation to get a new array as
    index_for_sorted_keys = {0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 3}

每个段可以是任意长度。

c++ cuda thrust
1个回答
0
投票

可能有很多方法可以做到。一种可能的方式:thrust::adjacent_difference()使用二进制运算符,当值相同时将生成0,在其他情况下则生成1,然后按thrust::adjacent_difference()

这是一个可行的示例:

a prefix sum
© www.soinside.com 2019 - 2024. All rights reserved.