[Rcpp-devel] Inline and Parallel Computing

Marie Auger-Methe marie.augermethe at gmail.com
Mon Apr 23 18:48:42 CEST 2012


Thanks Dirk and Thell,

I tried something similar to Thell's option before, but without the 
clusterEvalQ and the explicit function in parSapply, and it didn't work. 
Your version however works!
Making a package also looks like it's working!

Marie

On 22/04/2012 11:15 PM, Thell Fowler wrote:
> 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 at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120423/7aca53ed/attachment.html>


More information about the Rcpp-devel mailing list