0

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)
kennyB
  • 1,963
  • 3
  • 17
  • 22
  • 2
    Put it in a package. – nrussell Jun 24 '15 at 19:20
  • 1
    It works fine on my machine (Windows 8.1, R 3.2.0, Rcpp 0.11.6. Looking at http://stackoverflow.com/questions/6074310/snow-inline-rcpp another option would be to put your function into a local package. – Nick Kennedy Jun 24 '15 at 19:20

1 Answers1

0

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

kennyB
  • 1,963
  • 3
  • 17
  • 22