Discussion:
[Rcpp-devel] Distribution functions threadsafe in RcppParallel?
Murray Efford
2018-04-16 01:41:27 UTC
Permalink
I read in the RcppParallel blurb "The code that you write within parallel
workers should not call the R or Rcpp API in any fashion", which is
admirably clear. However, it leaves me without threadsafe access to
distribution functions (dpois, dbinom etc.). In practice, so far, these R
API calls seem to work for me, but can they be trusted? Is there an
alternative?

(It seems this question must have arisen before, but I haven't found an
answer)

Murray
Dirk Eddelbuettel
2018-04-16 03:33:29 UTC
Permalink
On 16 April 2018 at 13:41, Murray Efford wrote:
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?

That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).

As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.

But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...

Dirk

| (It seems this question must have arisen before, but I haven't found an
| answer)
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Murray Efford
2018-04-16 03:56:19 UTC
Permalink
Thanks. This is all happening inside a package for CRAN, so I would rather
avoid more complexity and potential platform-dependence, but I also cannot
afford for it to break unpredictably (or otherwise).
Murray
Post by Dirk Eddelbuettel
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?
That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).
As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.
But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...
Dirk
| (It seems this question must have arisen before, but I haven't found an
| answer)
--
JJ Allaire
2018-04-16 11:04:53 UTC
Permalink
Here it's recommended that you simply read the source code to figure out if
memory allocations or stack checking are done:
https://rcppcore.github.io/RcppParallel/#api_restrictions
Post by Murray Efford
Thanks. This is all happening inside a package for CRAN, so I would rather
avoid more complexity and potential platform-dependence, but I also cannot
afford for it to break unpredictably (or otherwise).
Murray
Post by Dirk Eddelbuettel
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?
That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).
As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.
But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...
Dirk
| (It seems this question must have arisen before, but I haven't found an
| answer)
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Yixuan Qiu
2018-04-16 16:53:27 UTC
Permalink
For your information, here is a header-only library for distribution
functions: https://github.com/kthohr/stats

The API seems to be designed to mimic the R API.


Best,
Yixuan
Post by Murray Efford
Thanks. This is all happening inside a package for CRAN, so I would rather
avoid more complexity and potential platform-dependence, but I also cannot
afford for it to break unpredictably (or otherwise).
Murray
Post by Dirk Eddelbuettel
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?
That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).
As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.
But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...
Dirk
| (It seems this question must have arisen before, but I haven't found an
| answer)
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Yixuan Qiu <***@gmail.com <***@cos.name>>
Department of Statistics,
Purdue University
Balamuta, James Joseph
2018-04-16 17:34:49 UTC
Permalink
Greetings and Salutations,

There is also the boost header libraries that you can use:

https://www.boost.org/doc/libs/1_67_0/doc/html/boost_random.html

Gallery example: http://gallery.rcpp.org/articles/timing-normal-rngs/

If you need parallel draws, see the sitmo engine: https://github.com/coatless/sitmo

Also, you may wish to consider the rTRNG library: https://github.com/miraisolutions/rTRNG

Sincerely,

JJB

From: <rcpp-devel-***@lists.r-forge.r-project.org> on behalf of Yixuan Qiu <***@gmail.com>
Date: Monday, April 16, 2018 at 11:53 AM
To: Murray Efford <***@gmail.com>
Cc: "rcpp-***@lists.r-forge.r-project.org" <rcpp-***@lists.r-forge.r-project.org>
Subject: Re: [Rcpp-devel] Distribution functions threadsafe in RcppParallel?

For your information, here is a header-only library for distribution functions: https://github.com/kthohr/stats
The API seems to be designed to mimic the R API.

Best,
Yixuan

2018-04-15 23:56 GMT-04:00 Murray Efford <***@gmail.com<mailto:***@gmail.com>>:
Thanks. This is all happening inside a package for CRAN, so I would rather avoid more complexity and potential platform-dependence, but I also cannot afford for it to break unpredictably (or otherwise).
Murray

On Mon, Apr 16, 2018 at 3:33 PM, Dirk Eddelbuettel <***@debian.org<mailto:***@debian.org>> wrote:

On 16 April 2018 at 13:41, Murray Efford wrote:
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?

That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).

As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.

But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...

Dirk

| (It seems this question must have arisen before, but I haven't found an
| answer)
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org<mailto:***@debian.org>


_______________________________________________
Rcpp-devel mailing list
Rcpp-***@lists.r-forge.r-project.org<mailto:Rcpp-***@lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Yixuan Qiu <***@gmail.com<mailto:***@cos.name>>
Department of Statistics,
Purdue University
Murray Efford
2018-04-16 21:44:31 UTC
Permalink
Thanks for all these good suggestions. StatsLib as suggested by Yixuan
seems especially straightforward. I may yet find that dpois and dbinom from
R are threadsafe.
Murray

On Tue, Apr 17, 2018 at 5:34 AM, Balamuta, James Joseph <
Post by Balamuta, James Joseph
Greetings and Salutations,
https://www.boost.org/doc/libs/1_67_0/doc/html/boost_random.html
Gallery example: http://gallery.rcpp.org/articles/timing-normal-rngs/
https://github.com/coatless/sitmo
Also, you may wish to consider the rTRNG library: https://github.com/
miraisolutions/rTRNG
Sincerely,
JJB
*Date: *Monday, April 16, 2018 at 11:53 AM
*Subject: *Re: [Rcpp-devel] Distribution functions threadsafe in
RcppParallel?
For your information, here is a header-only library for distribution
functions: https://github.com/kthohr/stats
The API seems to be designed to mimic the R API.
Best,
Yixuan
Thanks. This is all happening inside a package for CRAN, so I would rather
avoid more complexity and potential platform-dependence, but I also cannot
afford for it to break unpredictably (or otherwise).
Murray
| I read in the RcppParallel blurb "The code that you write within parallel
| workers should not call the R or Rcpp API in any fashion", which is
| admirably clear. However, it leaves me without threadsafe access to
| distribution functions (dpois, dbinom etc.). In practice, so far, these R
| API calls seem to work for me, but can they be trusted? Is there an
| alternative?
That's a fair question. They may work, as they are also exposed / available
via the standalone R math library (see Writing R Extensions).
As such, they may not required memory allocations or other interactions with
the R process and hence "not call R ... in any fashion" per the above.
But we can't say for sure. If you want to be safe, maybe stick to
equivalent functions from a non-R source: C++11, Boost, ...
Dirk
| (It seems this question must have arisen before, but I haven't found an
| answer)
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Department of Statistics,
Purdue University
Loading...