推力:redy_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
进行分组。我用它来帮助我这么远。但是,我遇到了很多我无法理解的错误(这也是我第一次使用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()输出,是两个还原向量的函数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 自定义创建QFuture
- 推力:redy_by_key将zip_iterator(元组)传递到自定义函子中,以通过键检索平均值