I am trying to use thrust::unique by key to find unique set of values. However I am using a tuple for the values. The key is of type int, the tuple if type (float, int).
My code as below works fine to find the set of unique values. However I also need to count the number of unique values.
I have seen the thrust example but I am unable to declare the output iterator type when the value is a tuple
thrust::pair<int*,int*> new_end;
new_end = thrust::unique_by_key(thrust::host, A, A + N, B);
My code and output after detecting unique is as below. kindly tell me how to count the number of unique elements.
#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;
}
}
Output:
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