[R-sig-hpc] R package with multi-threaded C

Dirk Eddelbuettel edd at debian.org
Sat May 9 20:56:41 CEST 2015

On 9 May 2015 at 20:38, Guillaume Chapron wrote:
| Hello,
| I am trying to develop a R package that calls a multi-threaded C function and I am struggling on how to do this properly. The C program I originally wrote (prior to thinking about building it into a R package) uses Grand Central Dispatch library (for multi-threading on a Mac) and GSL (for RNG). Importantly, each thread has its own seed (the program runs Monte Carlo simulations). 
| Now that I am looking at modifying this to be included in a R package, I am not sure how to proceed with having a parallel and portable C code using the RNG seed from R. In the book R Programming for Bioinformatics by Gentleman (2008), it says p. 201:
| "The decision to have these functions (GetRNGstate and SetRNGstate) manipulate a global variable, .Random.seed, is slightly unfortunate as it makes it somewhat more difficult to manage several different random number streams simultaneously."
| Has anyone been able to do this or is there a package implementing something similar? I could always have the C code single-threaded but then simulations are going to be quite long so I am really keen on a multi-thread approach.

Yes and yes. People have done / are doing this a lot.  

You correctly identified the issue with the RNGs -- so you need stream-aware
and/or multithreading-ready RNGs.  Which exist on CRAN in a few packages --
have a look at the Task View for HPC where I think I mention some.


http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

More information about the R-sig-hpc mailing list