我试图使用推力::通过唯一的密钥来找到独特的价值观。但是我使用的值的元组。关键是int类型,元组如果type(浮点,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
这是一种可能的方法:
正如你已经指出的那样,thrust::unique_by_key
返回迭代器的pair。在推进一对是像一个元组,我们可以使用推力元组元素访问机制(thrust::get<...>
)来检索对个人会员。
因此,如果我们检索对的第一个元素,这对应于结果结束迭代对供给到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>
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
$