lambdarcpprcppparallel

How to pass lambda function in a c++ functor?


I am pretty new at Rccp and Rccpparallel and I have trouble figuring out where I made mistake. So I want to create a function that do a power element wise in a matrix in parallel. I am following rcppParallel examples.

On one core the code compiles and works fine, but when I try to pass n to functor below I got the following error.

capture of non-variable "Power::n"
"this" was not captured for this lambda function
invalid use of non-static data member "Power::n"

If I swap n in functor below it compiles and works fine. What am i missing? R code:

library(Rcpp)
library(RcppParallel)
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
sourceCpp("lambdaPower.cpp")

lambdaPower.cpp

#include <Rcpp.h>
using namespace Rcpp;

#include <cmath>
#include <algorithm>

// [[Rcpp::export]]
NumericMatrix matrixPower(NumericMatrix orig, double n)
{

    // allocate the matrix we will return
    NumericMatrix mat(orig.nrow(), orig.ncol());

    // transform it
    std::transform(orig.begin(), orig.end(), mat.begin(), [n](double x) { return pow(x, n); });

    // return the new matrix
    return mat;
}

// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
using namespace RcppParallel;

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

    // destination matrix
    RMatrix<double> output;

    //power
    double n;


    // initialize with source and destination
    Power(const NumericMatrix input, NumericMatrix output, double n)
        : input(input), output(output), n(n){}
    // take the n power 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,
                       [n](double x) { return pow(x,n); }); // why n doesn work?
                    // If i swap n with fixed number it compiles and works.
                    // [](double x) { return pow(x,2); }); compiles and works
    }
};

// [[Rcpp::export]]
NumericMatrix parallelMatrixPower(NumericMatrix x, double n)
{

    // allocate the output matrix
    NumericMatrix output(x.nrow(), x.ncol());

    // power functor (pass input and output matrixes)
    Power power(x, output, n);

    // call parallelFor to do the work
    parallelFor(0, x.length(), power);

    // return the output matrix
    return output;
}

Thanks a lot.


Solution

  • You code compiles if you copy n into the scope where the lambda is defined:

    ....
      void operator()(std::size_t begin, std::size_t end)
      {
        auto _n = n;
        std::transform(input.begin() + begin,
                       input.begin() + end,
                       output.begin() + begin,
                       [_n](double x) { return pow(x,_n); });
      }
    ....
    

    I am not really good at explaining this, but you can read the details in "Item 31: Avoid default capture modes" of "Effective Modern C++" by Scott Meyers.

    BTW, instead of Sys.setenv("PKG_CXXFLAGS"="-std=c++11") in the R code I would use // [[Rcpp::plugins(cpp11)]] in the C++ code.