sourceCpp() with parallel

970 Views Asked by At

Using sourceCpp doesn't seem to work with parallel. How can I call an Rcpp function on a parallel cluster?

My example:

A test.cpp file:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
}

And an R file:

library(parallel)
library(Rcpp)
sourceCpp("test.cpp")
timesTwo(1)
# [1] 2

cl <- makeCluster(2)
clusterEvalQ(cl, {
  library(Rcpp)
  sourceCpp("test.cpp")
  timesTwo(1)
})
# Error in checkForRemoteErrors(lapply(cl, recvResult)) : 
# 2 nodes produced errors; first error: Error 1 occurred building shared library.
stopCluster(cl)
1

There are 1 best solutions below

0
On

This seems to work now that I've put the functions into a package.