Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to the global RNG #41

Closed
rstub opened this issue Apr 24, 2020 · 4 comments · Fixed by #58
Closed

Provide access to the global RNG #41

rstub opened this issue Apr 24, 2020 · 4 comments · Fixed by #58
Labels
enhancement New feature or request

Comments

@rstub
Copy link
Member

rstub commented Apr 24, 2020

For some applications it makes sense to use a RNG directly. It is of course always possible to generate a new RNG, but that has its own independent state. It would be more convenient, if one could get access to (a shared pointer holding) the global RNG.

@rstub rstub added the enhancement New feature or request label Apr 24, 2020
@hsloot

This comment has been minimized.

@hsloot
Copy link
Contributor

hsloot commented Dec 6, 2020

I revisited the issue and noticed that my previous suggestion was not helpful. I came up with an alternative, simple approach (see https://github.com/hsloot/dqrng) using an Rcpp::XPtr to expose the underlying raw pointer to the RNG. This seems to work (also in parallel clusters). Benchmarks show that this approach comes with little to no overhead (see https://github.com/hsloot/dqrng/blob/master/performance/global_rng.md).

If you like the approach, I could provide a PR.

Notes on the implementation

The following method can be used to expose the raw pointer to the global RNG in an Rcpp::XPtr.

// src/dqrng.cpp
...
// after rng is initialized
// [[Rcpp::export(rng = false)]]
SEXP get_sxprng() {
  return Rcpp::XPtr<dqrng::rng64_t::element_type>(rng.get(), false);
}

This creates bindings via the Rcpp interface in dqrng_RcppExports.h. Using this method directly would be a little difficult, so I created a wrapper method intended to be used by others in a separate header dqrng_get_rng.h.

// inst/include/dqrng_get_rng.h
#ifndef DQRNG_GET_RNG_H
#define DQRNG_GET_RNG_H 1

#include <Rcpp.h>
#include "dqrng.h"
#include "dqrng_generator.h"

namespace dqrng {

dqrng::rng64_t::element_type* get_rng() {
	return Rcpp::XPtr<dqrng::rng64_t::element_type>(dqrng::get_sxprng()).get();
}

}

#endif // DQRNG_GET_RNG_H

Alternatives

I am not sure if it is also possible to directly expose the shared_ptr or the raw pointer without going through the Rcpp::XPtr; however, I could not make this to work.

@rstub
Copy link
Member Author

rstub commented Sep 4, 2023

Hi @hsloot.
Sorry for the late reply. I had missed your comments, but came to a similar solution when I started playing around with it, c.f. https://github.com/daqana/dqrng/tree/access-rng. Would you still be interested in providing a PR?

@hsloot
Copy link
Contributor

hsloot commented Sep 15, 2023

I tried merging both approaches and cleaning it up a bit; see #58.

@rstub rstub closed this as completed in #58 Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants