推力:reduce_by_key 将 zip_iterator(元组)传递到自定义函子中以按键检索平均值

Thrust: reduce_by_key passing zip_iterator(tuple) into custom functor to retrieve average by key

我想做的是通过 thrust::reduce_by_key 按键获取值的平均值。我首先 sort_by_key 并且按 reduce_by_key 的连续键分组工作得很好。我使用 this 帮助我走到这一步。但是,我遇到了很多我无法理解的错误(这也是我第一次使用 reduce_by_key),而且我想不出更好的方法来执行此操作而不使用大量临时分配来 (1) get按键值的总和然后按键计数和(2)将两者除以求平均值。

input keys:   1,   1,   1,  2,   3,   5,  5,  2
input values: 120, 477, 42, 106, 143, 53, 83, 24

expected output values: 213, 65, 143, 68

我有以下自定义仿函数:

struct GetAverage
{
    template<typename Tuple>
    __host__ __device__
    int operator()(const Tuple& t)
    {
        //SumByKey / CountByKey
        return thrust::get<0>(t) / thrust::get<1>(t);
    }
};

从下面的代码调用仿函数,位于 main()

thrust::device_vector<unsigned int> tempKey(8);
thrust::device_vector<unsigned int> tempValue(8);

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

tempValue[0] = 120;
tempValue[1] = 477;
tempValue[2] = 42;
tempValue[3] = 106;
tempValue[4] = 143;
tempValue[5] = 53;
tempValue[6] = 83;
tempValue[7] = 24;

thrust::sort_by_key(tempKey.begin(), tempKey.end(), tempValue.begin());

thrust::equal_to<int> binary_pred;
thrust::reduce_by_key(
    tempKey.begin(),
    tempKey.end(),
    thrust::make_zip_iterator(
        thrust::make_tuple(
            tempValue.begin(),
            thrust::make_constant_iterator(1)
        )
    ), //values_first; Should go into GetAverage() custom functor as a zipped tuple <tempValue, 1>
    tempKey.begin(), //keys_output; Should be returning the unique keys
    tempValue.begin(), //values_output; Should be returning the average by key 
    binary_pred,
    GetAverage()
);

错误示例:
-no instance of function template "GetAverage::operator()" matches the argument list
-no operator "=" matches these operands
-no suitable conversion function from "InputValueType" to "TemporaryType" exists
-no suitable conversion function from "thrust::detail::tuple_of_iterator_references<thrust::device_reference<int>, int, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>" to "TemporaryType" exists

有没有人知道如何解决这个问题?还是链接?我阅读了此处使用的所有内容的文档,并尽可能仔细地尝试理解它,但没有解决方案。谢谢!

更新
请参阅埃里克的回答。结合他说的,这是新的源代码。创建了一个操作来处​​理元组的加号。这段代码唯一没有做的是在 reduce_by_key 调用之后,应该在结果上使用 thrust::transform 来通过总和除以计数来获得平均值。

// --- Defining key tuple type
typedef thrust::tuple<int, int> Tuple;

/* PLUS OPERATOR BETWEEN TUPLES */
struct TuplePlus
{
    __host__ __device__
    Tuple operator ()(const Tuple& lhs, const Tuple& rhs)
    {
        return thrust::make_tuple(
            thrust::get<0>(lhs) + thrust::get<0>(rhs),
            thrust::get<1>(lhs) + thrust::get<1>(rhs)
        );
    }
};

里面 main() 我现在有以下内容。

thrust::equal_to<int> binary_pred;
thrust::reduce_by_key(
    tempKey.begin(),
    tempKey.end(),
    thrust::make_zip_iterator(
        thrust::make_tuple(
            tempValue.begin(),
            thrust::make_constant_iterator(1)
        )
    ), //values_first; goes in as a zipped up tuple <value, 1>
    tempKey.begin(), //keys_output
    thrust::make_zip_iterator(
        thrust::make_tuple(
            tempValue.begin(),
            tempCount.begin()
        )
    ), //values_output; ZipIterator<Sum, Count> by key
    binary_pred,
    TuplePlus()
);

有两个问题。

元组序列缩减的结果应该是一个元组而不是 int。根据文档

https://thrust.github.io/doc/group__reductions.html#ga633d78d4cb2650624ec354c9abd0c97f

最后一个参数binary_op应该是

类型

BinaryFunction is a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator2's value_type.

这意味着你的归约操作应该类似于

struct GetSum
{
  template<typename Tuple>
  __host__ __device__
  Tuple operator()(const Tuple& a, construction Tuple& b)
  {
    ...
  }
}

另一方面,在归约阶段,你只能计算总和而不能有效地计算平均值。这意味着您的 values_output 也应该是一个与 values_first 类型相同的 zip 迭代器。

OutputIterator2 is a model of Output Iterator and InputIterator2's value_type is convertible to OutputIterator2's value_type.

因此您需要两个结果数组,一个用于按键求和,一个用于按键计数。它们应该压缩在一起并用作 values_output .

然后您需要另一个 thrust::transform 来计算最终结果 - 按键平均。


您也可以试试@RobertCrovella 提出的方法,它使用单个 thrust::reduce_by_key 来计算平均值。