简体   繁体   English

使用Thrust减少计数

[英]Count reduction using Thrust

Given some input keys and values, I am trying to count how many consecutive values with the same key exist. 给定一些输入键和值,我试图计算存在多少具有相同键的连续值。 I will give an example to make this more clear. 我将举一个例子来说明这一点。

Input keys: { 1, 4, 4, 4, 2, 2, 1 } 输入键: { 1, 4, 4, 4, 2, 2, 1 }

Input values: { 9, 8, 7, 6, 5, 4, 3 } 输入值: { 9, 8, 7, 6, 5, 4, 3 }

Expected output keys: { 1, 4, 2, 1 } 预期的输出键: { 1, 4, 2, 1 } 1,4,2,1 { 1, 4, 2, 1 }

Expected output values: { 1, 3, 2, 1 } 预期输出值: { 1, 3, 2, 1 } 1,3,2,1 { 1, 3, 2, 1 }

I am trying to solve this problem on a GPU using CUDA. 我试图使用CUDA在GPU上解决这个问题。 The reduction capabilities of the Thrust library seemed like a good solution for this and I got to the following: Thrust库的减少功能似乎是一个很好的解决方案,我得到了以下内容:

#include <thrust/reduce.h>
#include <thrust/functional.h>

struct count_functor : public thrust::binary_function<int, int, int>
{
    __host__ __device__
        int operator()(int input, int counter)
    {
        return counter + 1;
    }
};

const int N = 7;
int A[N] = { 1, 4, 4, 4, 2, 2, 1 }; // input keys
int B[N] = { 9, 8, 7, 6, 5, 4, 3 }; // input values
int C[N];                         // output keys
int D[N];                         // output values

thrust::pair<int*, int*> new_end;
thrust::equal_to<int> binary_pred;
count_functor binary_op;
new_end = thrust::reduce_by_key(A, A + N, B, C, D, binary_pred, binary_op);
for (int i = 0; i < new_end.first - C; i++) {
    std::cout << C[i] << " - " << D[i] << "\n";
}

This code is pretty similar to an example from the Thrust documentation . 此代码与Thrust文档中的示例非常相似。 However, instead of the plus operation, I am trying to count. 但是,我试图计算,而不是plus操作。 The output from this code is the following: 此代码的输出如下:

1 - 9
4 - 7
2 - 5
1 - 3

However, I would expected the second column to contain the values 1, 3, 2, 1 . 但是,我希望第二列包含值1, 3, 2, 1 I think the counts are off because the reduction starts with the first value it finds and does not apply the operator until it has a second value, but I am not sure this is the case. 我认为计数是关闭的,因为减少从它找到的第一个值开始,并且在它有第二个值之前不应用运算符,但我不确定是这种情况。

Am I overlooking something about the reduce_by_key function that could solve this problem or should I use a completely different function to achieve what I want? 我是否忽略了一些可以解决这个问题的reduce_by_key函数,或者我应该使用一个完全不同的函数来实现我想要的东西?

For your use case you don't need the values of B , the values of D are only dependent on the values of A . 对于您的用例,您不需要B的值, D的值仅取决于A的值。

In order to count how many consecutive values are in A you can supply a thrust::constant_iterator as the input values and apply thrust::reduce_by_key : 为了计算A有多少个连续值,你可以提供一个thrust::constant_iterator作为输入值并应用thrust::reduce_by_key

#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <iostream>
#include <thrust/iterator/constant_iterator.h>

int main()
{
const int N = 7;
int A[N] = { 1, 4, 4, 4, 2, 2, 1 }; 
int C[N];
int D[N];

thrust::pair<int*, int*> new_end;
thrust::equal_to<int> binary_pred;
thrust::plus<int> binary_op;
new_end = thrust::reduce_by_key(A, A + N, thrust::make_constant_iterator(1), C, D, binary_pred, binary_op);

for (int i = 0; i < new_end.first - C; i++) {
    std::cout << C[i] << " - " << D[i] << "\n";
}
return 0;
}

output 产量

1 - 1
4 - 3
2 - 2
1 - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM