【问题标题】:RcppParallel worker with additional arguments带有附加参数的 RcppParallel worker
【发布时间】:2020-06-04 07:27:12
【问题描述】:

这是我第一次尝试使用 RcppParallel 包,我必须使用 C++17 (Ubuntu)

我试图靠近开发者网站的 ParallelFor 示例,但我需要为工作人员 threshold 提供一个额外的(非迭代)参数。

这是我当前的代码

struct ReplaceWorker : public Worker
{
  // source matrix
  const RMatrix<double> input;

  // destination matrix
  RMatrix<double> output;

  // threshold
  double th;

  // initialize with source and destination
  ReplaceWorker(const NumericMatrix input, NumericMatrix output, double threshold) 
    : input(input), output(output), th(threshold) {}

  // replace function
  template<typename T>
  double replacer(const T &x){
    if(x < th){
      return(0);
    } else {
      return(1);
    }
  }

  // take the square root of the range of elements requested
  void operator()(std::size_t begin, std::size_t end) {
    std::transform(input.begin() + begin, 
                   input.begin() + end, 
                   output.begin() + begin, 
                   replacer);
  }
};

但是我总是以同样的编译错误结束:

usr/include/c++/7/bits/stl_algo.h:4295:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
        transform(_InputIterator __first, _InputIterator __last,
        ^~~~~~~~~
   /usr/include/c++/7/bits/stl_algo.h:4295:5: note:   template argument deduction/substitution failed:
   network_edge_strength.cpp:173:28: note:   couldn't deduce template parameter ‘_UnaryOperation’
                       replacer);
                               ^
/usr/include/c++/7/bits/stl_algo.h:4332:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
        transform(_InputIterator1 __first1, _InputIterator1 __last1,
        ^~~~~~~~~
   /usr/include/c++/7/bits/stl_algo.h:4332:5: note:   template argument deduction/substitution failed:
   network_edge_strength.cpp:173:28: note:   candidate expects 5 arguments, 4 provided
                       replacer);
                               ^

任何建议,如何解决此问题或替代方案,如何使其以所需的threshold 参数运行?

【问题讨论】:

    标签: r c++17 rcpp rcppparallel


    【解决方案1】:

    replacer 是函数模板,不是函数,这意味着它不能用作函数对象,除非使用特定的实例化,否则模板参数推导失败。

    另外,作为成员函数,它需要一个隐式对象参数才能被调用。

    您可以改用通用 lambda 表达式:

    std::transform(/* [...] */, [this] (const auto& x) { return replacer(x); });
    

    这样,即使replacer 被重载或者是一个函数模板,这也可以工作。

    或者,完全删除 replacer,直接使用 lambda 表达式:

    std::transform(/* [...] */, [this] (const auto& x) { return x < th ? 0 : 1; });
    

    【讨论】:

    • 谢谢!您的第二个版本似乎有效,但前提是我输入 [this] 而不是 [th]
    • @DanielR 是的,我的错。
    猜你喜欢
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多