rc++17rcpprcppparallel

RcppParallel worker with additional arguments


This is my first try with the RcppParallel package and I have to work with C++17 (Ubuntu)

I tried to stay close to the ParallelFor example of the developers site, but I need an additional (non-iterative) argument for the worker threshold.

This is the my current code

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);
  }
};

However I always end up with the same compilation errors:

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);
                               ^

Any suggestions, how to fix this or alternatives, how to make it run with the required threshold parameter?


Solution

  • replacer is a function template, not a function, which means it cannot be used as a function object unless a specific instantiation is used, as otherwise template argument deduction fails.

    Additionally, as a member function, it would require an implicit object parameter to be callable.

    You could use a generic lambda expression instead:

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

    This way, even if replacer is overloaded or is a function template, this will work.

    Alternatively, remove replacer altogether, and use a lambda expression directly:

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