From c1a18365337e7488c0f5dff0d9408da20a04035b Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Tue, 29 Oct 2024 12:31:02 -0600 Subject: [PATCH] Moving engine to be shared pointer instead of dumb pointer (#26) * Moving engine to be shared pointer instead of dumb pointer * Bumping version * Forgot the single header * Final touches --- .vscode/c_cpp_properties.json | 33 ++++---- .vscode/settings.json | 59 +++++++++++++- epiworld.hpp | 80 +++++++++---------- include/epiworld/epiworld.hpp | 2 +- include/epiworld/math/lfmcmc/lfmcmc-bones.hpp | 12 +-- include/epiworld/math/lfmcmc/lfmcmc-meat.hpp | 10 +-- include/epiworld/model-bones.hpp | 6 +- include/epiworld/model-meat.hpp | 36 ++++----- include/epiworld/models/surveillance.hpp | 2 +- include/epiworld/random_graph.hpp | 6 +- tests/00-lfmcmc.cpp | 6 +- 11 files changed, 147 insertions(+), 105 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index dc4abb941..272cb77ba 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,19 +1,16 @@ { - "configurations": [ - { - "name": "osx", - "includePath": [ - "${workspaceFolder}/**", - "/usr/local/include", - "include/epiworld" - ], - "defines": [], - "compilerPath": "/usr/bin/g++", - "cStandard": "gnu17", - "cppStandard": "c++17", - "intelliSenseMode": "${default}" - } - ], - "version": 4 -} - + "configurations": [ + { + "name": "macos-clang-arm64", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-arm64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 29e864021..2e7368749 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -79,5 +79,62 @@ "language": "quarto", "scheme": "file" } - ] + ], + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/epiworld.hpp b/epiworld.hpp index abb67187e..b59af62f5 100644 --- a/epiworld.hpp +++ b/epiworld.hpp @@ -19,7 +19,7 @@ /* Versioning */ #define EPIWORLD_VERSION_MAJOR 0 #define EPIWORLD_VERSION_MINOR 4 -#define EPIWORLD_VERSION_PATCH 0 +#define EPIWORLD_VERSION_PATCH 1 static const int epiworld_version_major = EPIWORLD_VERSION_MAJOR; static const int epiworld_version_minor = EPIWORLD_VERSION_MINOR; @@ -1178,7 +1178,7 @@ class LFMCMC { private: // Random number sampling - std::mt19937 * engine = nullptr; + std::shared_ptr< std::mt19937 > engine = nullptr; std::shared_ptr< std::uniform_real_distribution<> > runifd = std::make_shared< std::uniform_real_distribution<> >(0.0, 1.0); @@ -1190,7 +1190,7 @@ class LFMCMC { std::make_shared< std::gamma_distribution<> >(); // Process data - TData * observed_data; + TData observed_data; // Information about the size of the problem size_t n_samples; @@ -1253,10 +1253,10 @@ class LFMCMC { ); LFMCMC() {}; - LFMCMC(TData & observed_data_) : observed_data(&observed_data_) {}; + LFMCMC(const TData & observed_data_) : observed_data(observed_data_) {}; ~LFMCMC() {}; - void set_observed_data(TData & observed_data_) {observed_data = &observed_data_;}; + void set_observed_data(const TData & observed_data_) {observed_data = observed_data_;}; void set_proposal_fun(LFMCMCProposalFun fun); void set_simulation_fun(LFMCMCSimFun fun); void set_summary_fun(LFMCMCSummaryFun fun); @@ -1268,8 +1268,8 @@ class LFMCMC { * @param eng */ ///@{ - void set_rand_engine(std::mt19937 & eng); - std::mt19937 & get_rand_endgine(); + void set_rand_engine(std::shared_ptr< std::mt19937 > & eng); + std::shared_ptr< std::mt19937 > & get_rand_endgine(); void seed(epiworld_fast_uint s); void set_rand_gamma(epiworld_double alpha, epiworld_double beta); epiworld_double runif(); @@ -1455,7 +1455,7 @@ class LFMCMC { private: // Random number sampling - std::mt19937 * engine = nullptr; + std::shared_ptr< std::mt19937 > engine = nullptr; std::shared_ptr< std::uniform_real_distribution<> > runifd = std::make_shared< std::uniform_real_distribution<> >(0.0, 1.0); @@ -1467,7 +1467,7 @@ class LFMCMC { std::make_shared< std::gamma_distribution<> >(); // Process data - TData * observed_data; + TData observed_data; // Information about the size of the problem size_t n_samples; @@ -1530,10 +1530,10 @@ class LFMCMC { ); LFMCMC() {}; - LFMCMC(TData & observed_data_) : observed_data(&observed_data_) {}; + LFMCMC(const TData & observed_data_) : observed_data(observed_data_) {}; ~LFMCMC() {}; - void set_observed_data(TData & observed_data_) {observed_data = &observed_data_;}; + void set_observed_data(const TData & observed_data_) {observed_data = observed_data_;}; void set_proposal_fun(LFMCMCProposalFun fun); void set_simulation_fun(LFMCMCSimFun fun); void set_summary_fun(LFMCMCSummaryFun fun); @@ -1545,8 +1545,8 @@ class LFMCMC { * @param eng */ ///@{ - void set_rand_engine(std::mt19937 & eng); - std::mt19937 & get_rand_endgine(); + void set_rand_engine(std::shared_ptr< std::mt19937 > & eng); + std::shared_ptr< std::mt19937 > & get_rand_endgine(); void seed(epiworld_fast_uint s); void set_rand_gamma(epiworld_double alpha, epiworld_double beta); epiworld_double runif(); @@ -1819,7 +1819,7 @@ inline void LFMCMC::run( params_now = params_init; // Computing the baseline sufficient statistics - summary_fun(observed_stats, *observed_data, this); + summary_fun(observed_stats, observed_data, this); n_statistics = observed_stats.size(); // Reserving size @@ -1969,9 +1969,9 @@ inline void LFMCMC::seed(epiworld_fast_uint s) { } template -inline void LFMCMC::set_rand_engine(std::mt19937 & eng) +inline void LFMCMC::set_rand_engine(std::shared_ptr< std::mt19937 > & eng) { - engine = ŋ + engine = eng; } template @@ -1981,9 +1981,9 @@ inline void LFMCMC::set_rand_gamma(epiworld_double alpha, epiworld_double } template -inline std::mt19937 & LFMCMC::get_rand_endgine() +inline std::shared_ptr< std::mt19937 > & LFMCMC::get_rand_endgine() { - return *engine; + return engine; } // Step 1: Simulate data @@ -6339,7 +6339,7 @@ class Model { std::vector< Entity > entities = {}; std::vector< Entity > entities_backup = {}; - std::mt19937 engine; + std::shared_ptr< std::mt19937 > engine = std::make_shared< std::mt19937 >(); std::uniform_real_distribution<> runifd = std::uniform_real_distribution<> (0.0, 1.0); @@ -6485,8 +6485,8 @@ class Model { * @param s Seed */ ///@{ - void set_rand_engine(std::mt19937 & eng); - std::mt19937 & get_rand_endgine(); + void set_rand_engine(std::shared_ptr< std::mt19937 > & eng); + std::shared_ptr< std::mt19937 > & get_rand_endgine(); void seed(size_t s); void set_rand_norm(epiworld_double mean, epiworld_double sd); void set_rand_unif(epiworld_double a, epiworld_double b); @@ -7615,12 +7615,6 @@ inline void Model::agents_empty_graph( } -// template -// inline void Model::set_rand_engine(std::mt19937 & eng) -// { -// engine = std::make_shared< std::mt19937 >(eng); -// } - template inline void Model::set_rand_gamma(epiworld_double alpha, epiworld_double beta) { @@ -7761,7 +7755,7 @@ inline void Model::set_backup() // } template -inline std::mt19937 & Model::get_rand_endgine() +inline std::shared_ptr< std::mt19937 > & Model::get_rand_endgine() { return engine; } @@ -7769,86 +7763,86 @@ inline std::mt19937 & Model::get_rand_endgine() template inline epiworld_double Model::runif() { // CHECK_INIT() - return runifd(engine); + return runifd(*engine); } template inline epiworld_double Model::runif(epiworld_double a, epiworld_double b) { // CHECK_INIT() - return runifd(engine) * (b - a) + a; + return runifd(*engine) * (b - a) + a; } template inline epiworld_double Model::rnorm() { // CHECK_INIT() - return rnormd(engine); + return rnormd(*engine); } template inline epiworld_double Model::rnorm(epiworld_double mean, epiworld_double sd) { // CHECK_INIT() - return rnormd(engine) * sd + mean; + return rnormd(*engine) * sd + mean; } template inline epiworld_double Model::rgamma() { - return rgammad(engine); + return rgammad(*engine); } template inline epiworld_double Model::rgamma(epiworld_double alpha, epiworld_double beta) { auto old_param = rgammad.param(); rgammad.param(std::gamma_distribution<>::param_type(alpha, beta)); - epiworld_double ans = rgammad(engine); + epiworld_double ans = rgammad(*engine); rgammad.param(old_param); return ans; } template inline epiworld_double Model::rexp() { - return rexpd(engine); + return rexpd(*engine); } template inline epiworld_double Model::rexp(epiworld_double lambda) { auto old_param = rexpd.param(); rexpd.param(std::exponential_distribution<>::param_type(lambda)); - epiworld_double ans = rexpd(engine); + epiworld_double ans = rexpd(*engine); rexpd.param(old_param); return ans; } template inline epiworld_double Model::rlognormal() { - return rlognormald(engine); + return rlognormald(*engine); } template inline epiworld_double Model::rlognormal(epiworld_double mean, epiworld_double shape) { auto old_param = rlognormald.param(); rlognormald.param(std::lognormal_distribution<>::param_type(mean, shape)); - epiworld_double ans = rlognormald(engine); + epiworld_double ans = rlognormald(*engine); rlognormald.param(old_param); return ans; } template inline int Model::rbinom() { - return rbinomd(engine); + return rbinomd(*engine); } template inline int Model::rbinom(int n, epiworld_double p) { auto old_param = rbinomd.param(); rbinomd.param(std::binomial_distribution<>::param_type(n, p)); - epiworld_double ans = rbinomd(engine); + epiworld_double ans = rbinomd(*engine); rbinomd.param(old_param); return ans; } template inline void Model::seed(size_t s) { - this->engine.seed(s); + this->engine->seed(s); } template @@ -8238,7 +8232,7 @@ inline Model & Model::run( this->ndays = ndays; if (seed >= 0) - engine.seed(seed); + engine->seed(seed); array_double_tmp.resize(std::max( size(), @@ -17037,7 +17031,7 @@ inline ModelSURV::ModelSURV( // How many will we find std::binomial_distribution<> bdist(m->size(), m->par("Surveilance prob.")); - int nsampled = bdist(m->get_rand_endgine()); + int nsampled = bdist(*m->get_rand_endgine()); int to_go = nsampled + 1; diff --git a/include/epiworld/epiworld.hpp b/include/epiworld/epiworld.hpp index cfdee7a4a..398bf6c70 100644 --- a/include/epiworld/epiworld.hpp +++ b/include/epiworld/epiworld.hpp @@ -19,7 +19,7 @@ /* Versioning */ #define EPIWORLD_VERSION_MAJOR 0 #define EPIWORLD_VERSION_MINOR 4 -#define EPIWORLD_VERSION_PATCH 0 +#define EPIWORLD_VERSION_PATCH 1 static const int epiworld_version_major = EPIWORLD_VERSION_MAJOR; static const int epiworld_version_minor = EPIWORLD_VERSION_MINOR; diff --git a/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp b/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp index 6c494b95f..60a896f3d 100755 --- a/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp +++ b/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp @@ -116,7 +116,7 @@ class LFMCMC { private: // Random number sampling - std::mt19937 * engine = nullptr; + std::shared_ptr< std::mt19937 > engine = nullptr; std::shared_ptr< std::uniform_real_distribution<> > runifd = std::make_shared< std::uniform_real_distribution<> >(0.0, 1.0); @@ -128,7 +128,7 @@ class LFMCMC { std::make_shared< std::gamma_distribution<> >(); // Process data - TData * observed_data; + TData observed_data; // Information about the size of the problem size_t n_samples; @@ -191,10 +191,10 @@ class LFMCMC { ); LFMCMC() {}; - LFMCMC(TData & observed_data_) : observed_data(&observed_data_) {}; + LFMCMC(const TData & observed_data_) : observed_data(observed_data_) {}; ~LFMCMC() {}; - void set_observed_data(TData & observed_data_) {observed_data = &observed_data_;}; + void set_observed_data(const TData & observed_data_) {observed_data = observed_data_;}; void set_proposal_fun(LFMCMCProposalFun fun); void set_simulation_fun(LFMCMCSimFun fun); void set_summary_fun(LFMCMCSummaryFun fun); @@ -206,8 +206,8 @@ class LFMCMC { * @param eng */ ///@{ - void set_rand_engine(std::mt19937 & eng); - std::mt19937 & get_rand_endgine(); + void set_rand_engine(std::shared_ptr< std::mt19937 > & eng); + std::shared_ptr< std::mt19937 > & get_rand_endgine(); void seed(epiworld_fast_uint s); void set_rand_gamma(epiworld_double alpha, epiworld_double beta); epiworld_double runif(); diff --git a/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp b/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp index 688f4fd03..3984df2ae 100755 --- a/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp +++ b/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp @@ -228,7 +228,7 @@ inline void LFMCMC::run( params_now = params_init; // Computing the baseline sufficient statistics - summary_fun(observed_stats, *observed_data, this); + summary_fun(observed_stats, observed_data, this); n_statistics = observed_stats.size(); // Reserving size @@ -378,9 +378,9 @@ inline void LFMCMC::seed(epiworld_fast_uint s) { } template -inline void LFMCMC::set_rand_engine(std::mt19937 & eng) +inline void LFMCMC::set_rand_engine(std::shared_ptr< std::mt19937 > & eng) { - engine = ŋ + engine = eng; } template @@ -390,9 +390,9 @@ inline void LFMCMC::set_rand_gamma(epiworld_double alpha, epiworld_double } template -inline std::mt19937 & LFMCMC::get_rand_endgine() +inline std::shared_ptr< std::mt19937 > & LFMCMC::get_rand_endgine() { - return *engine; + return engine; } // Step 1: Simulate data diff --git a/include/epiworld/model-bones.hpp b/include/epiworld/model-bones.hpp index cf454d463..d4055094a 100644 --- a/include/epiworld/model-bones.hpp +++ b/include/epiworld/model-bones.hpp @@ -140,7 +140,7 @@ class Model { std::vector< Entity > entities = {}; std::vector< Entity > entities_backup = {}; - std::mt19937 engine; + std::shared_ptr< std::mt19937 > engine = std::make_shared< std::mt19937 >(); std::uniform_real_distribution<> runifd = std::uniform_real_distribution<> (0.0, 1.0); @@ -286,8 +286,8 @@ class Model { * @param s Seed */ ///@{ - void set_rand_engine(std::mt19937 & eng); - std::mt19937 & get_rand_endgine(); + void set_rand_engine(std::shared_ptr< std::mt19937 > & eng); + std::shared_ptr< std::mt19937 > & get_rand_endgine(); void seed(size_t s); void set_rand_norm(epiworld_double mean, epiworld_double sd); void set_rand_unif(epiworld_double a, epiworld_double b); diff --git a/include/epiworld/model-meat.hpp b/include/epiworld/model-meat.hpp index f04f79dac..5df3befb5 100644 --- a/include/epiworld/model-meat.hpp +++ b/include/epiworld/model-meat.hpp @@ -674,12 +674,6 @@ inline void Model::agents_empty_graph( } -// template -// inline void Model::set_rand_engine(std::mt19937 & eng) -// { -// engine = std::make_shared< std::mt19937 >(eng); -// } - template inline void Model::set_rand_gamma(epiworld_double alpha, epiworld_double beta) { @@ -820,7 +814,7 @@ inline void Model::set_backup() // } template -inline std::mt19937 & Model::get_rand_endgine() +inline std::shared_ptr< std::mt19937 > & Model::get_rand_endgine() { return engine; } @@ -828,86 +822,86 @@ inline std::mt19937 & Model::get_rand_endgine() template inline epiworld_double Model::runif() { // CHECK_INIT() - return runifd(engine); + return runifd(*engine); } template inline epiworld_double Model::runif(epiworld_double a, epiworld_double b) { // CHECK_INIT() - return runifd(engine) * (b - a) + a; + return runifd(*engine) * (b - a) + a; } template inline epiworld_double Model::rnorm() { // CHECK_INIT() - return rnormd(engine); + return rnormd(*engine); } template inline epiworld_double Model::rnorm(epiworld_double mean, epiworld_double sd) { // CHECK_INIT() - return rnormd(engine) * sd + mean; + return rnormd(*engine) * sd + mean; } template inline epiworld_double Model::rgamma() { - return rgammad(engine); + return rgammad(*engine); } template inline epiworld_double Model::rgamma(epiworld_double alpha, epiworld_double beta) { auto old_param = rgammad.param(); rgammad.param(std::gamma_distribution<>::param_type(alpha, beta)); - epiworld_double ans = rgammad(engine); + epiworld_double ans = rgammad(*engine); rgammad.param(old_param); return ans; } template inline epiworld_double Model::rexp() { - return rexpd(engine); + return rexpd(*engine); } template inline epiworld_double Model::rexp(epiworld_double lambda) { auto old_param = rexpd.param(); rexpd.param(std::exponential_distribution<>::param_type(lambda)); - epiworld_double ans = rexpd(engine); + epiworld_double ans = rexpd(*engine); rexpd.param(old_param); return ans; } template inline epiworld_double Model::rlognormal() { - return rlognormald(engine); + return rlognormald(*engine); } template inline epiworld_double Model::rlognormal(epiworld_double mean, epiworld_double shape) { auto old_param = rlognormald.param(); rlognormald.param(std::lognormal_distribution<>::param_type(mean, shape)); - epiworld_double ans = rlognormald(engine); + epiworld_double ans = rlognormald(*engine); rlognormald.param(old_param); return ans; } template inline int Model::rbinom() { - return rbinomd(engine); + return rbinomd(*engine); } template inline int Model::rbinom(int n, epiworld_double p) { auto old_param = rbinomd.param(); rbinomd.param(std::binomial_distribution<>::param_type(n, p)); - epiworld_double ans = rbinomd(engine); + epiworld_double ans = rbinomd(*engine); rbinomd.param(old_param); return ans; } template inline void Model::seed(size_t s) { - this->engine.seed(s); + this->engine->seed(s); } template @@ -1297,7 +1291,7 @@ inline Model & Model::run( this->ndays = ndays; if (seed >= 0) - engine.seed(seed); + engine->seed(seed); array_double_tmp.resize(std::max( size(), diff --git a/include/epiworld/models/surveillance.hpp b/include/epiworld/models/surveillance.hpp index 6dca7e4ea..8b1a2889a 100644 --- a/include/epiworld/models/surveillance.hpp +++ b/include/epiworld/models/surveillance.hpp @@ -227,7 +227,7 @@ inline ModelSURV::ModelSURV( // How many will we find std::binomial_distribution<> bdist(m->size(), m->par("Surveilance prob.")); - int nsampled = bdist(m->get_rand_endgine()); + int nsampled = bdist(*m->get_rand_endgine()); int to_go = nsampled + 1; diff --git a/include/epiworld/random_graph.hpp b/include/epiworld/random_graph.hpp index dfe6883dc..af9f4a5de 100644 --- a/include/epiworld/random_graph.hpp +++ b/include/epiworld/random_graph.hpp @@ -14,7 +14,7 @@ class RandGraph { RandGraph(int N_) : N(N_) {}; void init(int s); - void set_rand_engine(std::mt19937 & e); + void set_rand_engine(std::shared_ptr< std::mt19937 > & e); epiworld_double runif(); }; @@ -35,10 +35,10 @@ inline void RandGraph::init(int s) { } -inline void RandGraph::set_rand_engine(std::mt19937 & e) +inline void RandGraph::set_rand_engine(std::shared_ptr< std::mt19937 > & e) { - engine = std::make_shared< std::mt19937 >(e); + engine = e; } diff --git a/tests/00-lfmcmc.cpp b/tests/00-lfmcmc.cpp index a83e3d453..4f1dfcea6 100644 --- a/tests/00-lfmcmc.cpp +++ b/tests/00-lfmcmc.cpp @@ -44,13 +44,13 @@ void summary_fun(vec_double & res, const vec_double & p, LFMCMC * m) EPIWORLD_TEST_CASE("LFMCMC", "[Basic example]") { - std::mt19937 rand; - rand.seed(91231); + auto rand = std::make_shared(); + rand->seed(91231); std::normal_distribution rnorm(5, 1.5); vec_double obsdata; for (size_t i = 0u; i < 50000; ++i) - obsdata.push_back(rnorm(rand)); + obsdata.push_back(rnorm(*rand)); LFMCMC< vec_double > model(obsdata); model.set_rand_engine(rand);