Search code examples
rrcpp

Is it possible to pass Rcpp functions to a child process?


I have defined a function using Rcpp and it runs in the current session. So I tried to use future package to compile the same function in each worker process, but I run into the error

Error: Error 1 occurred building shared library.

Is there an easy to way to share Rcpp functions without having to build a package?

See MWE below

library(Rcpp)
cppFunction('
            NumericVector test(Rcpp::NumericVector x) {
            return x;
            }')

# test if compiled successful
test(1:2)

lapply(list(1:2), test) # works

future_lapply(list(1:2), function(a) {
  library(Rcpp)
  cppFunction('
     NumericVector test(Rcpp::NumericVector x) {
     return x;
  }')
  test(a)
 })

Solution

  • This question is really equivalent to spreading Rcpp-based functions via, say, foreach. In short, you cannot serialize a Rcpp-based function and pass it along.

    They sit at a "random" local memory point. The only reliable way is to stick them in a package and have each worker load the package. That will work.