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

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

本文关键字:自定义 平均值 检索 key by redy zip iterator 推力 元组      更新时间:2023-10-16

我要做的是通过 thrust::reduce_by_key获得键的平均值。我首先是sort_by_key,这可以通过连续的reduce_by_key进行分组。我用它来帮助我这么远。但是,我遇到了很多我无法理解的错误(这也是我第一次使用dred_by_key),我想不出一种更好的方法来执行此操作,而无需使用大量临时分配来(1)获取值按键,然后按键计数,(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

有人对如何解决这个问题有任何想法吗?还是链接?我阅读了此处使用的所有内容的文档,并且在尝试理解它时非常谨慎,但没有解决方案。谢谢!

更新
见埃里克的答案。结合他所说的,这是新的源代码。创建了一个op来处理元组。该代码不做的唯一一件事是在dred_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#ga6333d78d4cb2650624ec354C354C9ABD0C97F

最后一个参数binary_op应为类型

二进制函数是二进制函数的模型,二进制函数的result_type可转换为outputiterator2的value_type。

这意味着您的减少操作应该是

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

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

outputiterator2是输出迭代器的模型,inputiterator2的value_type可转换为outputiterator2的value_type。

因此,您需要两个结果阵列,一个用于键的总和,一个用于键。它们应将其拉链并用作values_output

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


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

从redy_by_key()输出,是两个还原向量的函数