Skip to content

Commit

Permalink
Change default RNG to boost::mixmax
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbadr committed Feb 15, 2024
1 parent 6591339 commit 59006c4
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 77 deletions.
2 changes: 1 addition & 1 deletion StanHeaders/R/stanFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 17 additions & 17 deletions rstan/rstan/R/Rcpp_module_def.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,61 @@ get_Rcpp_module_def_code <- function(model_name) {
RCPP_MODULE <-
'
RCPP_MODULE(stan_fit4%model_name%_mod) {
class_<rstan::stan_fit<stan_model, boost::random::ecuyer1988> >(
class_<rstan::stan_fit<stan_model, boost::random::mixmax> >(
"stan_fit4%model_name%")
.constructor<SEXP, SEXP, SEXP>()
.method(
"call_sampler",
&rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler)
&rstan::stan_fit<stan_model, boost::random::mixmax>::call_sampler)
.method(
"param_names",
&rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names)
&rstan::stan_fit<stan_model, boost::random::mixmax>::param_names)
.method("param_names_oi",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::param_names_oi)
boost::random::mixmax>::param_names_oi)
.method("param_fnames_oi",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::param_fnames_oi)
boost::random::mixmax>::param_fnames_oi)
.method(
"param_dims",
&rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims)
&rstan::stan_fit<stan_model, boost::random::mixmax>::param_dims)
.method("param_dims_oi",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::param_dims_oi)
boost::random::mixmax>::param_dims_oi)
.method("update_param_oi",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::update_param_oi)
boost::random::mixmax>::update_param_oi)
.method("param_oi_tidx",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::param_oi_tidx)
boost::random::mixmax>::param_oi_tidx)
.method("grad_log_prob",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::grad_log_prob)
boost::random::mixmax>::grad_log_prob)
.method("log_prob",
&rstan::stan_fit<stan_model, boost::random::ecuyer1988>::log_prob)
&rstan::stan_fit<stan_model, boost::random::mixmax>::log_prob)
.method("unconstrain_pars",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::unconstrain_pars)
boost::random::mixmax>::unconstrain_pars)
.method("constrain_pars",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::constrain_pars)
boost::random::mixmax>::constrain_pars)
.method(
"num_pars_unconstrained",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::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<stan_model,
boost::random::ecuyer1988>::constrained_param_names)
boost::random::mixmax>::constrained_param_names)
.method("standalone_gqs",
&rstan::stan_fit<stan_model,
boost::random::ecuyer1988>::standalone_gqs);
boost::random::mixmax>::standalone_gqs);
}
'
gsub("%model_name%", model_name, RCPP_MODULE)
Expand Down
8 changes: 4 additions & 4 deletions rstan/rstan/R/doctor_cppcode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -240,7 +240,7 @@ doctor_cppcode <- function(stanc_ret,
"#include <boost/exception/all.hpp>")
if (!use_R_PRNG)
necessary_headers <- c(necessary_headers,
"#include <boost/random/additive_combine.hpp>",
"#include <boost/random/mixmax.hpp>",
"#include <boost/random/linear_congruential.hpp>")

}
Expand Down Expand Up @@ -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
Expand Down
52 changes: 26 additions & 26 deletions rstan/rstan/inst/include/exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
#define RSTAN_EXPORTER_H

#include <RcppCommon.h>
#include <boost/random/additive_combine.hpp>
#include <boost/random/mixmax.hpp>
#include <iostream>

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<boost::ecuyer1988&>;
template <> struct input_parameter<boost::ecuyer1988&>;
template <> class Exporter<boost::random::mixmax&>;
template <> struct input_parameter<boost::random::mixmax&>;
}
}


#include <Rcpp.h>

namespace Rcpp {
SEXP wrap(boost::ecuyer1988 RNG){
boost::ecuyer1988* ptr_RNG = &RNG;
Rcpp::XPtr<boost::ecuyer1988> Xptr_RNG(ptr_RNG);
SEXP wrap(boost::random::mixmax RNG){
boost::random::mixmax* ptr_RNG = &RNG;
Rcpp::XPtr<boost::random::mixmax> Xptr_RNG(ptr_RNG);
return Xptr_RNG;
}

SEXP wrap(boost::ecuyer1988& RNG){
boost::ecuyer1988* ptr_RNG = &RNG;
Rcpp::XPtr<boost::ecuyer1988> Xptr_RNG(ptr_RNG);
SEXP wrap(boost::random::mixmax& RNG){
boost::random::mixmax* ptr_RNG = &RNG;
Rcpp::XPtr<boost::random::mixmax> Xptr_RNG(ptr_RNG);
return Xptr_RNG;
}

Expand All @@ -40,15 +40,15 @@ namespace Rcpp {
return Xptr_stream;
}

template <> boost::ecuyer1988 as(SEXP ptr_RNG) {
Rcpp::XPtr<boost::ecuyer1988> ptr(ptr_RNG);
boost::ecuyer1988& RNG = *ptr;
template <> boost::random::mixmax as(SEXP ptr_RNG) {
Rcpp::XPtr<boost::random::mixmax> ptr(ptr_RNG);
boost::random::mixmax& RNG = *ptr;
return RNG;
}

template <> boost::ecuyer1988& as(SEXP ptr_RNG) {
Rcpp::XPtr<boost::ecuyer1988> ptr(ptr_RNG);
boost::ecuyer1988& RNG = *ptr;
template <> boost::random::mixmax& as(SEXP ptr_RNG) {
Rcpp::XPtr<boost::random::mixmax> ptr(ptr_RNG);
boost::random::mixmax& RNG = *ptr;
return RNG;
}

Expand All @@ -59,18 +59,18 @@ namespace Rcpp {


namespace traits {
template <> class Exporter<boost::ecuyer1988&> {
template <> class Exporter<boost::random::mixmax&> {
public:
Exporter( SEXP x ) : t(Rcpp::as<boost::ecuyer1988&>(x)) {}
inline boost::ecuyer1988& get(){ return t ; }
Exporter( SEXP x ) : t(Rcpp::as<boost::random::mixmax&>(x)) {}
inline boost::random::mixmax& get(){ return t ; }
private:
boost::ecuyer1988& t ;
boost::random::mixmax& t ;
} ;

template <>
struct input_parameter<boost::ecuyer1988&> {
typedef typename Rcpp::ConstReferenceInputParameter<boost::ecuyer1988&> type ;
//typedef typename boost::ecuyer1988& type ;
struct input_parameter<boost::random::mixmax&> {
typedef typename Rcpp::ConstReferenceInputParameter<boost::random::mixmax&> type ;
//typedef typename boost::random::mixmax& type ;
};
}

Expand Down
34 changes: 17 additions & 17 deletions rstan/rstan/inst/include/rstan/rcpp_module_def_for_rstan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
/*
RCPP_MODULE(stan_fit4%model_name%_mod){
Rcpp::class_<rstan::stan_fit<stan_model,
boost::random::ecuyer1988> >("stan_fit4%model_name%")
boost::random::mixmax> >("stan_fit4%model_name%")
// .constructor<Rcpp::List>()
.constructor<SEXP, SEXP, SEXP>()
// .constructor<SEXP, SEXP>()
.method("call_sampler", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler)
.method("param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names)
.method("param_names_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names_oi)
.method("param_fnames_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_fnames_oi)
.method("param_dims", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims)
.method("param_dims_oi", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims_oi)
.method("update_param_oi",&rstan::stan_fit<stan_model, boost::random::ecuyer1988>::update_param_oi)
.method("param_oi_tidx", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_oi_tidx)
.method("grad_log_prob", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::grad_log_prob)
.method("log_prob", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::log_prob)
.method("unconstrain_pars", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::unconstrain_pars)
.method("constrain_pars", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::constrain_pars)
.method("num_pars_unconstrained", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::num_pars_unconstrained)
.method("unconstrained_param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::unconstrained_param_names)
.method("constrained_param_names", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::constrained_param_names)
.method("standalone_gqs", &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::standalone_gqs)
.method("call_sampler", &rstan::stan_fit<stan_model, boost::random::mixmax>::call_sampler)
.method("param_names", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_names)
.method("param_names_oi", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_names_oi)
.method("param_fnames_oi", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_fnames_oi)
.method("param_dims", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_dims)
.method("param_dims_oi", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_dims_oi)
.method("update_param_oi",&rstan::stan_fit<stan_model, boost::random::mixmax>::update_param_oi)
.method("param_oi_tidx", &rstan::stan_fit<stan_model, boost::random::mixmax>::param_oi_tidx)
.method("grad_log_prob", &rstan::stan_fit<stan_model, boost::random::mixmax>::grad_log_prob)
.method("log_prob", &rstan::stan_fit<stan_model, boost::random::mixmax>::log_prob)
.method("unconstrain_pars", &rstan::stan_fit<stan_model, boost::random::mixmax>::unconstrain_pars)
.method("constrain_pars", &rstan::stan_fit<stan_model, boost::random::mixmax>::constrain_pars)
.method("num_pars_unconstrained", &rstan::stan_fit<stan_model, boost::random::mixmax>::num_pars_unconstrained)
.method("unconstrained_param_names", &rstan::stan_fit<stan_model, boost::random::mixmax>::unconstrained_param_names)
.method("constrained_param_names", &rstan::stan_fit<stan_model, boost::random::mixmax>::constrained_param_names)
.method("standalone_gqs", &rstan::stan_fit<stan_model, boost::random::mixmax>::standalone_gqs)
;
}
*/
4 changes: 2 additions & 2 deletions rstan/rstan/inst/include/rstan/stan_fit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/random/additive_combine.hpp> // L'Ecuyer RNG
#include <boost/random/mixmax.hpp>
#include <boost/random/uniform_real_distribution.hpp>

#include <rstan/io/rlist_ref_var_context.hpp>
Expand Down Expand Up @@ -384,7 +384,7 @@ std::vector<double> unconstrained_to_constrained(Model& model,
const std::vector<double>& params) {
std::vector<int> params_i;
std::vector<double> 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<std::vector<double>&>(params), params_i,
constrained_params);
return constrained_params;
Expand Down
2 changes: 1 addition & 1 deletion rstan/rstan/inst/include/rstan_next/stan_fit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class stan_fit : public stan_fit_base {
SEXP model_sexp_;
Rcpp::XPtr<stan::model::model_base> model_xptr_;
stan::model::model_base* model_;
boost::ecuyer1988 base_rng;
boost::random::mixmax base_rng;
const std::vector<std::string> names_;
const std::vector<std::vector<unsigned int> > dims_;
const unsigned int num_params_;
Expand Down
2 changes: 1 addition & 1 deletion rstan/rstan/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ write_array(stan::model::model_base* user_model,
unsigned int random_seed = 0, unsigned int id = 0) {
std::vector<int> params_i;
std::vector<double> 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;
Expand Down
4 changes: 2 additions & 2 deletions rstan/rstan/src/chains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/random/additive_combine.hpp> // L'Ecuyer RNG
#include <boost/random/mixmax.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <fstream>

Expand Down Expand Up @@ -598,7 +598,7 @@ SEXP seq_permutation(SEXP conf) {
boost::uintmax_t DISCARD_STRIDE = static_cast<boost::uintmax_t>(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);
Expand Down
6 changes: 3 additions & 3 deletions rstan/rstan/src/pointer-tools.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Rcpp.h>
#include <boost/random/additive_combine.hpp>
#include <boost/random/mixmax.hpp>

RcppExport SEXP get_stream_() {
std::ostream* pstream(&Rcpp::Rcout);
Expand All @@ -9,8 +9,8 @@ RcppExport SEXP get_stream_() {

RcppExport SEXP get_rng_(SEXP seed) {
int seed_ = Rcpp::as<int>(seed);
boost::ecuyer1988* rng = new boost::ecuyer1988(seed_);
Rcpp::XPtr<boost::ecuyer1988> ptr(rng, true);
boost::random::mixmax* rng = new boost::random::mixmax(seed_);
Rcpp::XPtr<boost::random::mixmax> ptr(rng, true);
return ptr;
}

Expand Down
6 changes: 3 additions & 3 deletions rstan/rstan/src/stan_fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/random/additive_combine.hpp> // L'Ecuyer RNG
#include <boost/random/mixmax.hpp>
#include <boost/random/uniform_real_distribution.hpp>

#include <rstan/io/rlist_ref_var_context.hpp>
Expand Down Expand Up @@ -373,7 +373,7 @@ std::vector<double> unconstrained_to_constrained(stan::model::model_base* model,
const std::vector<double>& params) {
std::vector<int> params_i;
std::vector<double> 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<std::vector<double>&>(params), params_i,
constrained_params);
return constrained_params;
Expand All @@ -392,7 +392,7 @@ int command(stan_args& args,
Rcpp::List& holder,
const std::vector<size_t>& qoi_idx,
const std::vector<std::string>& fnames_oi,
boost::ecuyer1988& base_rng) {
boost::random::mixmax& base_rng) {

stan::math::init_threadpool_tbb();

Expand Down

0 comments on commit 59006c4

Please sign in to comment.