I had similar needs and found the simplest thing to do was to make the
function on each worker node...

library(inline)
library(parallel)

# A silly Rcpp function
silly.src <- '
  int x = as<int>(i);
  double y = 5.6;
  NumericVector j(1);
  j[0] =  x + y;
  return j;
'

M <- detectCores()
cl <- makeCluster( M )

# Load up Rcpp in each node.
clusterEvalQ( cl, require( inline ) )

# Pass over the source.
clusterExport( cl, 'silly.src', .GlobalEnv )

# Build it and keep the output for troubleshooting.
captured <- clusterEvalQ( cl, capture.output(
  silly <- cxxfunction( signature( i = "int" ),
                        body = silly.src,
                        plugin = "Rcpp",
                        verbose= TRUE ) ) )

# Use an explicit 'function' in the 'par' commands.
res <- parSapply(cl, 1:10, function(x) { silly(x) } )

cat( unlist( captured ), sep='\n' )
print( res )

stopCluster(cl)
rm(cl)

rm( captured )
rm( res )


-- 
Sincerely,
Thell
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to