[Rcpp-devel] Inline and Parallel Computing

Dirk Eddelbuettel edd at debian.org
Mon Apr 23 00:50:05 CEST 2012


On 22 April 2012 at 17:15, Thell Fowler wrote:
| I had similar needs and found the simplest thing to do was to make the function
| on each worker node...

That was one of the suggested approaches, yes.  It also means you're
compiling N times, and that you N nodes all have all dev tools.

It works, but I'd rather write a package.  But thanks for sending a full
example. 

Dirk
 
| 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 at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com


More information about the Rcpp-devel mailing list