rrcpprcppparallel

Rcpp::Function in parellel for section


I am trying to parallelize for cycle computing fitness value of individuals. For this whole algorithm I am using Rcpp, but fitness function is passed from R.

So I am trying to do something like this:

#pragma omp parallel for
  for (int i = 0; i < population.size(); i++)
  {
    population[i].computeFitness(FitnessFunction);
  }

Where FitnessFunction is Rcpp::Function and computeFitness is just class function essentially assigning computed value to member variable.

void computeFitness(Rcpp::Function optFunction)
  {
    this->_fitness = Rcpp::as<double>(optFunction(this->_coords));
  }

But this crashes, because, as I now know, R is single-threaded and I cannot use any underlying R instances in parallel sections.

So is there any way to convert Rcpp::Function to either std::function, functor or something similar? Is there any other way to pass a function from R to Rcpp, that would allow me to parallelize computation of this fitness value?

This whole work is for creating parallel optimization package of Moth Search Algoritm for CRAN.

Basically same code in c++ with std::function works well. Rcpp code works fine without it being parallel.


Solution

  • Are you aware that Rcpp::Function() just calls an R function, and hence

    1. violates the principle you state of using the underlying R instance in (OpenMP or pthread) parallel code and
    2. of course also only runs at R speed as an R function?

    You can achieve the parallel calling of R code more easily at the R level.

    And if you want C++ speed in parallel you need to write C++ code that can be called in parallel -- see for example the RcppParallel package and its vignettet for introductory examples.