[R-pkg-devel] Multi-threaded C with R's RNG

Dirk Eddelbuettel edd at debian.org
Sat Nov 28 18:24:03 CET 2015


On 28 November 2015 at 13:40, Tom Snijders wrote:
| On Fri, 27 Nov 2015 19:48:04 +0100, Guillaume Chapron carnivorescience at gmail.com<mailto:carnivorescience at gmail.com> wrote:
| 
| 
| 
| | Has anyone written a package with a C code that uses R's RNG but in a multi-threaded way (e.g. to parallelize Monte Carlo simulations)? I do not have the knowledge to figure out myself how to solved this and would need to see an example.
| 
| 
| 
| This is done in RSiena, using the package parallel. The programming was done by Ruth Ripley. There is some documentation in the package RSienaTest in \doc\RSIENAspec.tex and \doc\RSienaDeveloper.tex (search in these files for 'processes'); RSienaTest is not at CRAN but can be obtained from http://r-forge.r-project.org/R/?group_id=461; with respect to this use of random number streams, RSiena and RSienaTest are identical.

I just glanced at its Random.h and Random.cpp.  One could split hairs and say
that its use of unif_rand() and subsequent _modifications within RSiena_
itself keep the calculation away from R's core.

See Writing R Extensions and the difference between using the RNG via
unif_rand (as also discussed in using the optional external math library that
buildable from R sources) and using the full-blown d/p/q/r functions.
Presumably when people ask for 'R random numbers in streams' they want the
full richness of the various distributions available for random draws.

But your point is well taken: the basic uniform sample can be accessed, and
hence be seeded from R before calling.   More complicated (parallel) setup
may still require extrade code.

There are a few CRAN packages doing that.  Too bad we don't have a nice
survey paper.  I could do with one now...

Dirk

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



More information about the R-package-devel mailing list