[Rcpp-devel] thread safety RNG
Dirk Eddelbuettel
edd at debian.org
Fri Oct 21 18:11:21 CEST 2022
Hi Konrad,
I have been meaning to look at ast2ast for some time now but alas no time
yet...
On 21 October 2022 at 12:15, konrad wrote:
| my package ast2ast includes currently some random number generation
| functions (e.g. R::dunif) within the C++ code.
|
| So my question is whether the RNG functions in the namespace 'R' are
| thread safe?
It's been a while since I worked that corner but my initial reactions are
- nope sadly they are not (AFAIK)
- have you seen what 'Writing R Extensions' says on RNG state?
- this is why we have streaming / multi-threaded RNGs on CRAN
| Moreover, I would like to know how to handle the functions GetRNGstate()
| and PutRNGstate(). I guess that these functions modify a global
| variable. Thus, it is necessary to lock these functions with a mutex.
Don't you need a mutex for all of R anyway? (Depending on whether you can in
or out I guess.) Put differently, 'all of R' is in a mutex as R is
(generally) single-threaded (with a few local threaded overlays in places).
| As a side note ast2ast translates R to C++ code. This code is thread
| safe in the sense that no static methods are implemented or that memory
| is associated with functions or global variables. Therefore, it would
| also be cool if the RNGs could abe used in parallel.
Maybe start by seeing what Writing R Extension recommends and experiment with
an even narrow layer of just C to exclude any Rcpp side effect (there should
not be any but smaller sandboxen help on exploration I have found).
Lastly, RcppParallel is a little bit in this space to by offering very
explicit use of TBB / tinythread as does RcppThread by Thomas (but I am less
familiar with it).
Cheers, Dirk
--
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list