Skip to content

Commit

Permalink
Barrier for make-sense optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Oct 2, 2023
1 parent d6c4555 commit 00f2c5b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion include/barry/model-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
9 changes: 7 additions & 2 deletions include/barry/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,16 @@ template <
typename Data_Rule_Dyn_Type
>
inline void Model<Array_Type, Data_Counter_Type, Data_Rule_Type, Data_Rule_Dyn_Type>::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)
Expand Down
2 changes: 1 addition & 1 deletion include/barry/models/geese/flock-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
6 changes: 5 additions & 1 deletion include/barry/models/geese/geese-meat-likelihood.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 00f2c5b

Please sign in to comment.