Skip to content

Commit

Permalink
Moving engine to be shared pointer instead of dumb pointer (#26)
Browse files Browse the repository at this point in the history
* Moving engine to be shared pointer instead of dumb pointer

* Bumping version

* Forgot the single header

* Final touches
  • Loading branch information
gvegayon authored Oct 29, 2024
1 parent 6a93500 commit c1a1836
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 105 deletions.
33 changes: 15 additions & 18 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 58 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
80 changes: 37 additions & 43 deletions epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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<TData> fun);
void set_simulation_fun(LFMCMCSimFun<TData> fun);
void set_summary_fun(LFMCMCSummaryFun<TData> fun);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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<TData> fun);
void set_simulation_fun(LFMCMCSimFun<TData> fun);
void set_summary_fun(LFMCMCSummaryFun<TData> fun);
Expand All @@ -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();
Expand Down Expand Up @@ -1819,7 +1819,7 @@ inline void LFMCMC<TData>::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
Expand Down Expand Up @@ -1969,9 +1969,9 @@ inline void LFMCMC<TData>::seed(epiworld_fast_uint s) {
}

template<typename TData>
inline void LFMCMC<TData>::set_rand_engine(std::mt19937 & eng)
inline void LFMCMC<TData>::set_rand_engine(std::shared_ptr< std::mt19937 > & eng)
{
engine = &eng;
engine = eng;
}

template<typename TData>
Expand All @@ -1981,9 +1981,9 @@ inline void LFMCMC<TData>::set_rand_gamma(epiworld_double alpha, epiworld_double
}

template<typename TData>
inline std::mt19937 & LFMCMC<TData>::get_rand_endgine()
inline std::shared_ptr< std::mt19937 > & LFMCMC<TData>::get_rand_endgine()
{
return *engine;
return engine;
}

// Step 1: Simulate data
Expand Down Expand Up @@ -6339,7 +6339,7 @@ class Model {
std::vector< Entity<TSeq> > entities = {};
std::vector< Entity<TSeq> > 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -7615,12 +7615,6 @@ inline void Model<TSeq>::agents_empty_graph(

}

// template<typename TSeq>
// inline void Model<TSeq>::set_rand_engine(std::mt19937 & eng)
// {
// engine = std::make_shared< std::mt19937 >(eng);
// }

template<typename TSeq>
inline void Model<TSeq>::set_rand_gamma(epiworld_double alpha, epiworld_double beta)
{
Expand Down Expand Up @@ -7761,94 +7755,94 @@ inline void Model<TSeq>::set_backup()
// }

template<typename TSeq>
inline std::mt19937 & Model<TSeq>::get_rand_endgine()
inline std::shared_ptr< std::mt19937 > & Model<TSeq>::get_rand_endgine()
{
return engine;
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::runif() {
// CHECK_INIT()
return runifd(engine);
return runifd(*engine);
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::runif(epiworld_double a, epiworld_double b) {
// CHECK_INIT()
return runifd(engine) * (b - a) + a;
return runifd(*engine) * (b - a) + a;
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::rnorm() {
// CHECK_INIT()
return rnormd(engine);
return rnormd(*engine);
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::rnorm(epiworld_double mean, epiworld_double sd) {
// CHECK_INIT()
return rnormd(engine) * sd + mean;
return rnormd(*engine) * sd + mean;
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::rgamma() {
return rgammad(engine);
return rgammad(*engine);
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::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<typename TSeq>
inline epiworld_double Model<TSeq>::rexp() {
return rexpd(engine);
return rexpd(*engine);
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::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<typename TSeq>
inline epiworld_double Model<TSeq>::rlognormal() {
return rlognormald(engine);
return rlognormald(*engine);
}

template<typename TSeq>
inline epiworld_double Model<TSeq>::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<typename TSeq>
inline int Model<TSeq>::rbinom() {
return rbinomd(engine);
return rbinomd(*engine);
}

template<typename TSeq>
inline int Model<TSeq>::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<typename TSeq>
inline void Model<TSeq>::seed(size_t s) {
this->engine.seed(s);
this->engine->seed(s);
}

template<typename TSeq>
Expand Down Expand Up @@ -8238,7 +8232,7 @@ inline Model<TSeq> & Model<TSeq>::run(
this->ndays = ndays;

if (seed >= 0)
engine.seed(seed);
engine->seed(seed);

array_double_tmp.resize(std::max(
size(),
Expand Down Expand Up @@ -17037,7 +17031,7 @@ inline ModelSURV<TSeq>::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;

Expand Down
2 changes: 1 addition & 1 deletion include/epiworld/epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit c1a1836

Please sign in to comment.