当值的集合为元组时,使用推力unique_by_key计算唯一元素的数量

时间:2019-02-05 10:56:53

标签: cuda key unique thrust

我正在尝试使用推力:::唯一键来查找唯一的一组值。但是,我使用元组的值。密钥的类型为int,元组为类型(float,int)。

我的以下代码可以很好地找到唯一值集。 但是我还需要计算唯一值的数量。

我已经看到了推力示例,但是当值是元组时,我无法声明输出迭代器类型

thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key(thrust::host, A, A + N, B);

我的代码和检测到唯一性后的输出如下。 请告诉我如何计算唯一元素的数量。

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/unique.h>


int main()
{
    const int N = 8;

        thrust::device_vector<int> keys1(N);
        thrust::device_vector<int> keys2(N);
        thrust::device_vector<float> value_keys(N);

        keys1[0] = 1; keys1[1] = 1; keys1[2] = 2; keys1[3] = 2; 
        keys1[4] = 2; keys1[5] = 3; keys1[6] = 3; keys1[7] = 3; 


        keys2[0] = 4; keys2[1] = 1; keys2[2] = 7; keys2[3] = 2; 
        keys2[4] = 6; keys2[5] = 8; keys2[6] = 3; keys2[7] = 5; 

        value_keys[0] = -0.01; value_keys[1] = 1.1; value_keys[2] = -0.07; value_keys[3] = 2.1; 
        value_keys[4] = 5.2; value_keys[5] = -0.08; value_keys[6] = 3.2; value_keys[7] = 5.1; 


        thrust::unique_by_key(thrust::device, keys1.begin(), keys1.end(), 
        thrust::make_zip_iterator(thrust::make_tuple(value_keys.begin(), keys2.begin())));


    std::cout << "Unique PAIRS:"<< std::endl;
    std::cout << "keys1, value_keys, keys2:" << std::endl;
    for (int i = 0; i < N; i++) {
        std::cout << keys1[i] <<'\t' << value_keys[i] <<'\t' << keys2[i] << std::endl;
    }

}

输出:

Unique PAIRS:
keys1, value_keys, keys2:
1   -0.01   4
2   -0.07   7
3   -0.08   8
2   2.1 2
2   5.2 6
3   -0.08   8
3   3.2 3
3   5.1 5

1 个答案:

答案 0 :(得分:2)

这是一种可能的方法:

您已经指出,thrust::unique_by_key返回pair的迭代器。推力对像元组一样,我们可以使用推力元组元素访问机制(thrust::get<...>)来检索该对中的各个成员。

因此,如果我们检索该对的第一个元素,则它对应于提供给thrust::unique_by_key算法的的结果结束迭代器。我们可以从中减去键的Begin迭代器,以检索结果中键的长度(与结果中值的长度相同)。

这是一个可行的示例:

$ cat t390.cu
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/unique.h>


int main()
{
    const int N = 8;

        thrust::device_vector<int> keys1(N);
        thrust::device_vector<int> keys2(N);
        thrust::device_vector<float> value_keys(N);

        keys1[0] = 1; keys1[1] = 1; keys1[2] = 2; keys1[3] = 2;
        keys1[4] = 2; keys1[5] = 3; keys1[6] = 3; keys1[7] = 3;


        keys2[0] = 4; keys2[1] = 1; keys2[2] = 7; keys2[3] = 2;
        keys2[4] = 6; keys2[5] = 8; keys2[6] = 3; keys2[7] = 5;

        value_keys[0] = -0.01; value_keys[1] = 1.1; value_keys[2] = -0.07; value_keys[3] = 2.1;
        value_keys[4] = 5.2; value_keys[5] = -0.08; value_keys[6] = 3.2; value_keys[7] = 5.1;


        auto end = thrust::unique_by_key(thrust::device, keys1.begin(), keys1.end(),
          thrust::make_zip_iterator(thrust::make_tuple(value_keys.begin(), keys2.begin())));
        int result_size = thrust::get<0>(end) - keys1.begin();

    std::cout << "Unique PAIRS:"<< std::endl;
    std::cout << "keys1, value_keys, keys2:" << std::endl;
    for (int i = 0; i < result_size; i++) {
        std::cout << keys1[i] <<'\t' << value_keys[i] <<'\t' << keys2[i] << std::endl;
    }

}
$ nvcc -o t390 t390.cu -std=c++11
$ ./t390
Unique PAIRS:
keys1, value_keys, keys2:
1       -0.01   4
2       -0.07   7
3       -0.08   8
$

如果您不想使用c ++ 11 auto,则可以通过自己显式定义返回的对来适应上面的示例。这是通过查看thrust::unique_by_key算法使用的两种输入迭代器类型并从中创建一对来确定的。这是一个示例:

$ cat t390.cu
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/unique.h>

typedef thrust::zip_iterator<thrust::tuple<thrust::device_vector<float>::iterator, thrust::device_vector<int>::iterator> > my_iter;
typedef thrust::pair<thrust::device_vector<int>::iterator, my_iter> my_pair;


int main()
{
    const int N = 8;

        thrust::device_vector<int> keys1(N);
        thrust::device_vector<int> keys2(N);
        thrust::device_vector<float> value_keys(N);

        keys1[0] = 1; keys1[1] = 1; keys1[2] = 2; keys1[3] = 2;
        keys1[4] = 2; keys1[5] = 3; keys1[6] = 3; keys1[7] = 3;


        keys2[0] = 4; keys2[1] = 1; keys2[2] = 7; keys2[3] = 2;
        keys2[4] = 6; keys2[5] = 8; keys2[6] = 3; keys2[7] = 5;

        value_keys[0] = -0.01; value_keys[1] = 1.1; value_keys[2] = -0.07; value_keys[3] = 2.1;
        value_keys[4] = 5.2; value_keys[5] = -0.08; value_keys[6] = 3.2; value_keys[7] = 5.1;


        my_pair end = thrust::unique_by_key(thrust::device, keys1.begin(), keys1.end(),
          thrust::make_zip_iterator(thrust::make_tuple(value_keys.begin(), keys2.begin())));
        int result_size = thrust::get<0>(end) - keys1.begin();

    std::cout << "Unique PAIRS:"<< std::endl;
    std::cout << "keys1, value_keys, keys2:" << std::endl;
    for (int i = 0; i < result_size; i++) {
        std::cout << keys1[i] <<'\t' << value_keys[i] <<'\t' << keys2[i] << std::endl;
    }

}
$ nvcc -o t390 t390.cu
$ ./t390
Unique PAIRS:
keys1, value_keys, keys2:
1       -0.01   4
2       -0.07   7
3       -0.08   8
$