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

Cuda 当值集为元组时,使用推力unique_by_键计算唯一元素的数量,cuda,key,unique,thrust,Cuda,Key,Unique,Thrust,我正在尝试使用推力::unique by键来查找唯一的值集。但是,我使用一个元组作为值。键的类型是int,tuple if类型是(float,int) 下面的代码可以很好地找到唯一值集但是,我还需要计算唯一值的数量。 我已经看到了推力示例,但是当值是元组时,我无法声明输出迭代器类型 thrust::pair<int*,int*> new_end; new_end = thrust::unique_by_key(thrust::host, A, A + N, B); 以下是一种可能的

我正在尝试使用推力::unique by键来查找唯一的值集。但是,我使用一个元组作为值。键的类型是int,tuple if类型是(float,int)

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

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

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

以下是一种可能的方法:

正如您已经指出的,返回一组迭代器。推力中的一对类似于元组,我们可以使用推力元组元素访问机制()来检索对中的单个成员

因此,如果我们检索对的第一个元素,这对应于提供给
asch::unique\u 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
$

以下是一种可能的方法:

正如您已经指出的,返回一组迭代器。推力中的一对类似于元组,我们可以使用推力元组元素访问机制()来检索对中的单个成员

因此,如果我们检索对的第一个元素,这对应于提供给
asch::unique\u 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。由于一些现有的软件限制,我不能使用C++11。所以我不能用汽车。我需要声明迭代器,而不是使用auto。
$ 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
$
$ 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
$