c++ - 推力:如何有意避免将参数传递给算法?

标签 c++ cuda gpgpu thrust

假设我想做一个 thrust::reduce_by_key 但我不关心输出键是什么。有没有一种方法可以通过某种方式将空对象(可能是空指针)传递给该参数的算法,从而不会创建毫无意义的输出键列表,从而节省任何计算时间和内存分配?

thrust::reduce_by_key(
    keys_input.begin(),
    keys_input.end(),
    values_input.begin(),
    null, //What can go here, if anything at all?
    values_output.begin(),
    thrust::equal_to<int>(),
    thrust::plus<int>());

附加信息:也许有更好的方法来完成我想要完成的事情。本质上,我已经在 vector 中存储了一组缩减键,因此将它们存储在现有的一组缩减键上是多余的,这就是我不关心输出键的原因。

最佳答案

丢弃迭代器就是为此而设计的。

https://thrust.github.io/doc/classthrust_1_1discard__iterator.html

thrust::reduce_by_key(keys.begin(), keys.end(),
                      values.begin(),
                      thrust::make_discard_iterator(),
                      result.begin());

关于c++ - 推力:如何有意避免将参数传递给算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025719/

相关文章:

c++ - std::bind2nd 和 std::bind 与二维数组和结构数组

security - 不受信任的 GPGPU 代码(OpenCL 等) - 安全吗?有什么风险?

cuda - CUDA 中未对齐的地址

cuda - NVCC 在代码优化方面有多好?

C++ vector 指针

c++ - 正确读写串口Windows API

c++ - 如何从 C++ 中的矩阵 (Mat) 返回特定值的索引?

调用 cudaMemcpyToSymbol 时出现 cudaErrorInvalidValue 错误

c - Cuda by Example 中非常简单的教程的问题

cuda - 在内核运行时将数据传输到 GPU 以节省时间