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

Drop legacy RNG interface #129

Merged
merged 8 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restore deleted functions
richfitz committed Dec 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 82e6bf3c73fe1cc58b9fa782cc0a04ca88450c4c
4 changes: 4 additions & 0 deletions R/cpp11.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions R/random.R
Original file line number Diff line number Diff line change
@@ -156,6 +156,21 @@ monty_rng_long_jump <- function(state, n = 1) {
}


## This was used previously, we have a variant of this in dust2, as
## well.
monty_rng_distributed_state <- function(seed = NULL, n_nodes = 1L) {
rng <- monty_rng_create(seed = seed)
ret <- vector("list", n_nodes)
for (i in seq_len(n_nodes)) {
ret[[i]] <- monty_rng_state(rng)
if (i < n_nodes) {
monty_rng_long_jump(rng)
}
}
ret
}


##' @export
print.monty_rng_state <- function(x, ...) {
cli::cli_h1("<monty_rng_state>")
8 changes: 8 additions & 0 deletions src/cpp11.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/random.cpp
Original file line number Diff line number Diff line change
@@ -337,7 +337,22 @@ cpp11::doubles monty_random_sample_n_4(Fn fn, size_t n_samples,
return r_y;
}


template <typename T>
SEXP monty_rng_alloc(cpp11::sexp r_seed, int n_streams, bool deterministic) {
auto seed = monty::random::r::as_rng_seed<typename T::rng_state>(r_seed);
T *rng = new T(n_streams, seed, deterministic);
return cpp11::external_pointer<T>(rng);
}


// Real functions that we export
[[cpp11::register]]
SEXP monty_rng_alloc(cpp11::sexp r_seed, int n_streams, bool deterministic) {
return monty_rng_alloc<default_rng64>(r_seed, n_streams, deterministic);
}


[[cpp11::register]]
cpp11::sexp cpp_monty_rng_state(cpp11::sexp ptr) {
auto * rng = safely_read_externalptr<default_rng64>(ptr, "state");