From 59006c43707833328c3a2dfb583adce276cac4e6 Mon Sep 17 00:00:00 2001 From: "Hamada S. Badr" Date: Thu, 15 Feb 2024 09:13:11 -0500 Subject: [PATCH] Change default RNG to `boost::mixmax` Following https://github.com/stan-dev/stan/pull/3264 --- StanHeaders/R/stanFunction.R | 2 +- rstan/rstan/R/Rcpp_module_def.R | 34 ++++++------ rstan/rstan/R/doctor_cppcode.R | 8 +-- rstan/rstan/inst/include/exporter.h | 52 +++++++++---------- .../rstan/rcpp_module_def_for_rstan.hpp | 34 ++++++------ rstan/rstan/inst/include/rstan/stan_fit.hpp | 4 +- .../inst/include/rstan_next/stan_fit.hpp | 2 +- rstan/rstan/src/Module.cpp | 2 +- rstan/rstan/src/chains.cpp | 4 +- rstan/rstan/src/pointer-tools.cpp | 6 +-- rstan/rstan/src/stan_fit.cpp | 6 +-- 11 files changed, 77 insertions(+), 77 deletions(-) diff --git a/StanHeaders/R/stanFunction.R b/StanHeaders/R/stanFunction.R index 9703c1e1b..56351f20c 100644 --- a/StanHeaders/R/stanFunction.R +++ b/StanHeaders/R/stanFunction.R @@ -82,7 +82,7 @@ stanFunction <- function(function_name, ..., env = parent.frame(), rebuild = FAL incl <- c(incl, paste0('#include ', dQuote(create_rng))) code <- sub(") {", ", const int random_seed = 0) {", code, fixed = TRUE) code <- sub(" return ", - "boost::ecuyer1988 base_rng__ = stan::services::util::create_rng(random_seed, 0); return ", + "boost::random::mixmax base_rng__ = stan::services::util::create_rng(random_seed, 0); return ", code) code <- sub("); }", ", base_rng__); }", code, fixed = TRUE) } diff --git a/rstan/rstan/R/Rcpp_module_def.R b/rstan/rstan/R/Rcpp_module_def.R index e596c3579..924aeaea2 100644 --- a/rstan/rstan/R/Rcpp_module_def.R +++ b/rstan/rstan/R/Rcpp_module_def.R @@ -19,61 +19,61 @@ get_Rcpp_module_def_code <- function(model_name) { RCPP_MODULE <- ' RCPP_MODULE(stan_fit4%model_name%_mod) { - class_ >( + class_ >( "stan_fit4%model_name%") .constructor() .method( "call_sampler", - &rstan::stan_fit::call_sampler) + &rstan::stan_fit::call_sampler) .method( "param_names", - &rstan::stan_fit::param_names) + &rstan::stan_fit::param_names) .method("param_names_oi", &rstan::stan_fit::param_names_oi) + boost::random::mixmax>::param_names_oi) .method("param_fnames_oi", &rstan::stan_fit::param_fnames_oi) + boost::random::mixmax>::param_fnames_oi) .method( "param_dims", - &rstan::stan_fit::param_dims) + &rstan::stan_fit::param_dims) .method("param_dims_oi", &rstan::stan_fit::param_dims_oi) + boost::random::mixmax>::param_dims_oi) .method("update_param_oi", &rstan::stan_fit::update_param_oi) + boost::random::mixmax>::update_param_oi) .method("param_oi_tidx", &rstan::stan_fit::param_oi_tidx) + boost::random::mixmax>::param_oi_tidx) .method("grad_log_prob", &rstan::stan_fit::grad_log_prob) + boost::random::mixmax>::grad_log_prob) .method("log_prob", - &rstan::stan_fit::log_prob) + &rstan::stan_fit::log_prob) .method("unconstrain_pars", &rstan::stan_fit::unconstrain_pars) + boost::random::mixmax>::unconstrain_pars) .method("constrain_pars", &rstan::stan_fit::constrain_pars) + boost::random::mixmax>::constrain_pars) .method( "num_pars_unconstrained", &rstan::stan_fit::num_pars_unconstrained) + boost::random::mixmax>::num_pars_unconstrained) .method( "unconstrained_param_names", &rstan::stan_fit< - stan_model, boost::random::ecuyer1988>::unconstrained_param_names) + stan_model, boost::random::mixmax>::unconstrained_param_names) .method( "constrained_param_names", &rstan::stan_fit::constrained_param_names) + boost::random::mixmax>::constrained_param_names) .method("standalone_gqs", &rstan::stan_fit::standalone_gqs); + boost::random::mixmax>::standalone_gqs); } ' gsub("%model_name%", model_name, RCPP_MODULE) diff --git a/rstan/rstan/R/doctor_cppcode.R b/rstan/rstan/R/doctor_cppcode.R index 6074e55ca..4b2400b2d 100644 --- a/rstan/rstan/R/doctor_cppcode.R +++ b/rstan/rstan/R/doctor_cppcode.R @@ -64,7 +64,7 @@ doctor_cppcode <- function(stanc_ret, "boost_random_R base_rng__ = boost_random_R();") } else { # this could be dangerous to use with parallel chains protected$PRNG <- paste0(four_spaces, - "mutable boost::ecuyer1988 base_rng__;") + "mutable boost::random::mixmax base_rng__;") } if (check_logical_scalar_first(use_Rcout)) { @@ -121,7 +121,7 @@ doctor_cppcode <- function(stanc_ret, lines <- gsub("typename T_lp_accum__", "typename T_lp_accum__ = double", lines) lines <- gsub("Class RNG", paste0("Class RNG = ", ifelse(use_R_PRNG, "boost_random_R", - "boost::ecuyer1988")), lines) + "boost::random::mixmax")), lines) } if (check_logical_scalar_first(double_only)) { @@ -240,7 +240,7 @@ doctor_cppcode <- function(stanc_ret, "#include ") if (!use_R_PRNG) necessary_headers <- c(necessary_headers, - "#include ", + "#include ", "#include ") } @@ -478,7 +478,7 @@ doctor_cppcode <- function(stanc_ret, } # do not create base_rng__ in ctor_body - mark <- grep("boost::ecuyer1988 base_rng__ =", lines, fixed = TRUE) + mark <- grep("boost::random::mixmax base_rng__ =", lines, fixed = TRUE) lines <- lines[-c(mark:(mark + 2L))] # deal with constructor diff --git a/rstan/rstan/inst/include/exporter.h b/rstan/rstan/inst/include/exporter.h index 5d7751eaf..3c08564ca 100644 --- a/rstan/rstan/inst/include/exporter.h +++ b/rstan/rstan/inst/include/exporter.h @@ -2,19 +2,19 @@ #define RSTAN_EXPORTER_H #include -#include +#include #include namespace Rcpp { - SEXP wrap(boost::ecuyer1988 RNG); - SEXP wrap(boost::ecuyer1988& RNG); + SEXP wrap(boost::random::mixmax RNG); + SEXP wrap(boost::random::mixmax& RNG); SEXP wrap(std::ostream stream); - template <> boost::ecuyer1988 as(SEXP ptr_RNG); - template <> boost::ecuyer1988& as(SEXP ptr_RNG); + template <> boost::random::mixmax as(SEXP ptr_RNG); + template <> boost::random::mixmax& as(SEXP ptr_RNG); template <> std::ostream* as(SEXP ptr_stream); namespace traits { - template <> class Exporter; - template <> struct input_parameter; + template <> class Exporter; + template <> struct input_parameter; } } @@ -22,15 +22,15 @@ namespace Rcpp { #include namespace Rcpp { - SEXP wrap(boost::ecuyer1988 RNG){ - boost::ecuyer1988* ptr_RNG = &RNG; - Rcpp::XPtr Xptr_RNG(ptr_RNG); + SEXP wrap(boost::random::mixmax RNG){ + boost::random::mixmax* ptr_RNG = &RNG; + Rcpp::XPtr Xptr_RNG(ptr_RNG); return Xptr_RNG; } - SEXP wrap(boost::ecuyer1988& RNG){ - boost::ecuyer1988* ptr_RNG = &RNG; - Rcpp::XPtr Xptr_RNG(ptr_RNG); + SEXP wrap(boost::random::mixmax& RNG){ + boost::random::mixmax* ptr_RNG = &RNG; + Rcpp::XPtr Xptr_RNG(ptr_RNG); return Xptr_RNG; } @@ -40,15 +40,15 @@ namespace Rcpp { return Xptr_stream; } - template <> boost::ecuyer1988 as(SEXP ptr_RNG) { - Rcpp::XPtr ptr(ptr_RNG); - boost::ecuyer1988& RNG = *ptr; + template <> boost::random::mixmax as(SEXP ptr_RNG) { + Rcpp::XPtr ptr(ptr_RNG); + boost::random::mixmax& RNG = *ptr; return RNG; } - template <> boost::ecuyer1988& as(SEXP ptr_RNG) { - Rcpp::XPtr ptr(ptr_RNG); - boost::ecuyer1988& RNG = *ptr; + template <> boost::random::mixmax& as(SEXP ptr_RNG) { + Rcpp::XPtr ptr(ptr_RNG); + boost::random::mixmax& RNG = *ptr; return RNG; } @@ -59,18 +59,18 @@ namespace Rcpp { namespace traits { - template <> class Exporter { + template <> class Exporter { public: - Exporter( SEXP x ) : t(Rcpp::as(x)) {} - inline boost::ecuyer1988& get(){ return t ; } + Exporter( SEXP x ) : t(Rcpp::as(x)) {} + inline boost::random::mixmax& get(){ return t ; } private: - boost::ecuyer1988& t ; + boost::random::mixmax& t ; } ; template <> - struct input_parameter { - typedef typename Rcpp::ConstReferenceInputParameter type ; - //typedef typename boost::ecuyer1988& type ; + struct input_parameter { + typedef typename Rcpp::ConstReferenceInputParameter type ; + //typedef typename boost::random::mixmax& type ; }; } diff --git a/rstan/rstan/inst/include/rstan/rcpp_module_def_for_rstan.hpp b/rstan/rstan/inst/include/rstan/rcpp_module_def_for_rstan.hpp index 1c0ad627c..7fa695f0b 100644 --- a/rstan/rstan/inst/include/rstan/rcpp_module_def_for_rstan.hpp +++ b/rstan/rstan/inst/include/rstan/rcpp_module_def_for_rstan.hpp @@ -5,26 +5,26 @@ /* RCPP_MODULE(stan_fit4%model_name%_mod){ Rcpp::class_ >("stan_fit4%model_name%") + boost::random::mixmax> >("stan_fit4%model_name%") // .constructor() .constructor() // .constructor() - .method("call_sampler", &rstan::stan_fit::call_sampler) - .method("param_names", &rstan::stan_fit::param_names) - .method("param_names_oi", &rstan::stan_fit::param_names_oi) - .method("param_fnames_oi", &rstan::stan_fit::param_fnames_oi) - .method("param_dims", &rstan::stan_fit::param_dims) - .method("param_dims_oi", &rstan::stan_fit::param_dims_oi) - .method("update_param_oi",&rstan::stan_fit::update_param_oi) - .method("param_oi_tidx", &rstan::stan_fit::param_oi_tidx) - .method("grad_log_prob", &rstan::stan_fit::grad_log_prob) - .method("log_prob", &rstan::stan_fit::log_prob) - .method("unconstrain_pars", &rstan::stan_fit::unconstrain_pars) - .method("constrain_pars", &rstan::stan_fit::constrain_pars) - .method("num_pars_unconstrained", &rstan::stan_fit::num_pars_unconstrained) - .method("unconstrained_param_names", &rstan::stan_fit::unconstrained_param_names) - .method("constrained_param_names", &rstan::stan_fit::constrained_param_names) - .method("standalone_gqs", &rstan::stan_fit::standalone_gqs) + .method("call_sampler", &rstan::stan_fit::call_sampler) + .method("param_names", &rstan::stan_fit::param_names) + .method("param_names_oi", &rstan::stan_fit::param_names_oi) + .method("param_fnames_oi", &rstan::stan_fit::param_fnames_oi) + .method("param_dims", &rstan::stan_fit::param_dims) + .method("param_dims_oi", &rstan::stan_fit::param_dims_oi) + .method("update_param_oi",&rstan::stan_fit::update_param_oi) + .method("param_oi_tidx", &rstan::stan_fit::param_oi_tidx) + .method("grad_log_prob", &rstan::stan_fit::grad_log_prob) + .method("log_prob", &rstan::stan_fit::log_prob) + .method("unconstrain_pars", &rstan::stan_fit::unconstrain_pars) + .method("constrain_pars", &rstan::stan_fit::constrain_pars) + .method("num_pars_unconstrained", &rstan::stan_fit::num_pars_unconstrained) + .method("unconstrained_param_names", &rstan::stan_fit::unconstrained_param_names) + .method("constrained_param_names", &rstan::stan_fit::constrained_param_names) + .method("standalone_gqs", &rstan::stan_fit::standalone_gqs) ; } */ diff --git a/rstan/rstan/inst/include/rstan/stan_fit.hpp b/rstan/rstan/inst/include/rstan/stan_fit.hpp index dfba5d523..018348027 100644 --- a/rstan/rstan/inst/include/rstan/stan_fit.hpp +++ b/rstan/rstan/inst/include/rstan/stan_fit.hpp @@ -14,7 +14,7 @@ #include #include -#include // L'Ecuyer RNG +#include #include #include @@ -384,7 +384,7 @@ std::vector unconstrained_to_constrained(Model& model, const std::vector& params) { std::vector params_i; std::vector constrained_params; - boost::ecuyer1988 rng = stan::services::util::create_rng(random_seed, id); + boost::random::mixmax rng = stan::services::util::create_rng(random_seed, id); model.write_array(rng, const_cast&>(params), params_i, constrained_params); return constrained_params; diff --git a/rstan/rstan/inst/include/rstan_next/stan_fit.hpp b/rstan/rstan/inst/include/rstan_next/stan_fit.hpp index 24e645df2..89222d4a8 100644 --- a/rstan/rstan/inst/include/rstan_next/stan_fit.hpp +++ b/rstan/rstan/inst/include/rstan_next/stan_fit.hpp @@ -14,7 +14,7 @@ class stan_fit : public stan_fit_base { SEXP model_sexp_; Rcpp::XPtr model_xptr_; stan::model::model_base* model_; - boost::ecuyer1988 base_rng; + boost::random::mixmax base_rng; const std::vector names_; const std::vector > dims_; const unsigned int num_params_; diff --git a/rstan/rstan/src/Module.cpp b/rstan/rstan/src/Module.cpp index 8b80c55a1..ccb318646 100644 --- a/rstan/rstan/src/Module.cpp +++ b/rstan/rstan/src/Module.cpp @@ -102,7 +102,7 @@ write_array(stan::model::model_base* user_model, unsigned int random_seed = 0, unsigned int id = 0) { std::vector params_i; std::vector constrained_params; - boost::ecuyer1988 rng = stan::services::util::create_rng(random_seed, id); + boost::random::mixmax rng = stan::services::util::create_rng(random_seed, id); user_model->write_array(rng, params_r, params_i, constrained_params, include_tparams, include_gqs, &Rcpp::Rcout); return constrained_params; diff --git a/rstan/rstan/src/chains.cpp b/rstan/rstan/src/chains.cpp index d558208e7..3734b94a4 100644 --- a/rstan/rstan/src/chains.cpp +++ b/rstan/rstan/src/chains.cpp @@ -21,7 +21,7 @@ #include #include #include -#include // L'Ecuyer RNG +#include #include #include @@ -598,7 +598,7 @@ SEXP seq_permutation(SEXP conf) { boost::uintmax_t DISCARD_STRIDE = static_cast(1) << 50; int n = args.get_n(); int cid = args.get_chain_id() + args.get_chains(); - typedef boost::random::ecuyer1988 RNG; + typedef boost::random::mixmax RNG; RNG rng(args.get_seed()); rng.discard(DISCARD_STRIDE * (cid - 1)); Rcpp::IntegerVector x(n); diff --git a/rstan/rstan/src/pointer-tools.cpp b/rstan/rstan/src/pointer-tools.cpp index 4cd681e4c..abbadd67b 100644 --- a/rstan/rstan/src/pointer-tools.cpp +++ b/rstan/rstan/src/pointer-tools.cpp @@ -1,5 +1,5 @@ #include -#include +#include RcppExport SEXP get_stream_() { std::ostream* pstream(&Rcpp::Rcout); @@ -9,8 +9,8 @@ RcppExport SEXP get_stream_() { RcppExport SEXP get_rng_(SEXP seed) { int seed_ = Rcpp::as(seed); - boost::ecuyer1988* rng = new boost::ecuyer1988(seed_); - Rcpp::XPtr ptr(rng, true); + boost::random::mixmax* rng = new boost::random::mixmax(seed_); + Rcpp::XPtr ptr(rng, true); return ptr; } diff --git a/rstan/rstan/src/stan_fit.cpp b/rstan/rstan/src/stan_fit.cpp index 86860b0a4..ec647a382 100644 --- a/rstan/rstan/src/stan_fit.cpp +++ b/rstan/rstan/src/stan_fit.cpp @@ -13,7 +13,7 @@ #include #include -#include // L'Ecuyer RNG +#include #include #include @@ -373,7 +373,7 @@ std::vector unconstrained_to_constrained(stan::model::model_base* model, const std::vector& params) { std::vector params_i; std::vector constrained_params; - boost::ecuyer1988 rng = stan::services::util::create_rng(random_seed, id); + boost::random::mixmax rng = stan::services::util::create_rng(random_seed, id); model->write_array(rng, const_cast&>(params), params_i, constrained_params); return constrained_params; @@ -392,7 +392,7 @@ int command(stan_args& args, Rcpp::List& holder, const std::vector& qoi_idx, const std::vector& fnames_oi, - boost::ecuyer1988& base_rng) { + boost::random::mixmax& base_rng) { stan::math::init_threadpool_tbb();