[Rcpp-devel] Inline and Parallel Computing
Thell Fowler
tbfowler4 at gmail.com
Mon Apr 23 00:15:37 CEST 2012
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120422/93a7505d/attachment.html>
More information about the Rcpp-devel
mailing list