From 00f2c5b6561123a982739db42384a6a3e157c51c Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Mon, 2 Oct 2023 11:35:32 -0600 Subject: [PATCH] Barrier for make-sense optimization --- include/barry/model-bones.hpp | 5 ++++- include/barry/model-meat.hpp | 9 +++++++-- include/barry/models/geese/flock-meat.hpp | 2 +- include/barry/models/geese/geese-meat-likelihood.hpp | 6 +++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/barry/model-bones.hpp b/include/barry/model-bones.hpp index e454b86b3..4f32a7b23 100644 --- a/include/barry/model-bones.hpp +++ b/include/barry/model-bones.hpp @@ -132,7 +132,10 @@ class Model { * set of parameters. It will also update the `normalizing_constants` member * variable. */ - void update_normalizing_constants(const std::vector< double > & params); + void update_normalizing_constants( + const std::vector< double > & params, + BARRY_NCORES_ARG(=1) + ); void set_rengine(std::mt19937 * rengine_, bool delete_ = false) { diff --git a/include/barry/model-meat.hpp b/include/barry/model-meat.hpp index 7aa8893ac..313a56b94 100644 --- a/include/barry/model-meat.hpp +++ b/include/barry/model-meat.hpp @@ -152,11 +152,16 @@ template < typename Data_Rule_Dyn_Type > inline void Model::update_normalizing_constants( - const std::vector< double > & params + const std::vector< double > & params, + size_t ncores ) { + + // Barrier to make sure paralelization makes sense + if ((ncores > 1u) && (stats_support.size() < 1000u)) + ncores = 1u; #if defined(__OPENMP) || defined(_OPENMP) - #pragma omp parallel for firstprivate(params) \ + #pragma omp parallel for firstprivate(params) num_threads(ncores) \ shared(stats_support, normalizing_constants, first_calc_done) #endif for (size_t i = 0u; i < stats_support.size(); ++i) diff --git a/include/barry/models/geese/flock-meat.hpp b/include/barry/models/geese/flock-meat.hpp index 11e1f2993..bd3297464 100644 --- a/include/barry/models/geese/flock-meat.hpp +++ b/include/barry/models/geese/flock-meat.hpp @@ -148,7 +148,7 @@ inline double Flock::likelihood_joint( double ans = as_log ? 0.0: 1.0; std::vector< double > par0(par.begin(), par.end() - nfunctions); - model.update_normalizing_constants(par0); + model.update_normalizing_constants(par0, ncores); if (as_log) { diff --git a/include/barry/models/geese/geese-meat-likelihood.hpp b/include/barry/models/geese/geese-meat-likelihood.hpp index d7fa7877c..fdae06552 100644 --- a/include/barry/models/geese/geese-meat-likelihood.hpp +++ b/include/barry/models/geese/geese-meat-likelihood.hpp @@ -26,7 +26,7 @@ inline double Geese::likelihood( // Updating normalizing constants if (!no_update_normalizing_constant) - model->update_normalizing_constants(par0); + model->update_normalizing_constants(par0, ncores); // Following the prunning sequence const std::vector< size_t > & preseq = use_reduced_sequence ? @@ -65,6 +65,10 @@ inline double Geese::likelihood( std::vector< std::vector< size_t > > & locations = pset_loc[ arrays2support->operator[](array_id) ]; + + // Making sure parallelization makes sense + if (psets.size() < 1000) + ncores = 1u; // Summation over all possible values of X const auto & node_offspring = node.offspring;