From 19ac4f91105d03762e71d8af6c60e33030d7f254 Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Mon, 2 Oct 2023 17:02:53 -0600 Subject: [PATCH] Removing extra mem allocation+copy in geese::likelihood --- include/barry/model-meat.hpp | 2 +- .../models/geese/geese-meat-likelihood.hpp | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/barry/model-meat.hpp b/include/barry/model-meat.hpp index 11bbbd408..78b00dcbd 100644 --- a/include/barry/model-meat.hpp +++ b/include/barry/model-meat.hpp @@ -101,7 +101,7 @@ inline double likelihood_( double numerator = 0.0; // Computing the numerator - for (size_t j = 0u; j < params.size(); ++j) + for (size_t j = 0u; j < n_params; ++j) numerator += *(stats_target + j) * params[j]; if (!log_) diff --git a/include/barry/models/geese/geese-meat-likelihood.hpp b/include/barry/models/geese/geese-meat-likelihood.hpp index 27616d4e8..ca55f9d60 100644 --- a/include/barry/models/geese/geese-meat-likelihood.hpp +++ b/include/barry/models/geese/geese-meat-likelihood.hpp @@ -14,7 +14,7 @@ inline void pset_loop( const std::vector> & states, const std::vector< PhyloArray > & psets, const std::vector & psets_stats, - std::vector< std::vector< size_t > > & locations, + const std::vector< std::vector< size_t > > & locations, const std::vector & node_offspring ) { @@ -73,17 +73,11 @@ inline void pset_loop( if (off_mult < 0.0) return; - // Multiplying by P(x|x_n), the transition probability - std::vector< double > temp_stats; - temp_stats.reserve(par0.size()); - for (auto p = 0u; p < par0.size(); ++p) - temp_stats.push_back(psets_stats[par0.size() * n + p]); - // Use try catch in the following line try { off_mult *= barry::likelihood_( - &temp_stats[0u], + &psets_stats[par0.size() * n], par0, norm_const_i, par0.size(), @@ -223,8 +217,13 @@ inline double Geese::likelihood( // Setting the probability at the node node.subtree_prob[s] = 0.0; + auto & nsp = node.subtree_prob[s]; + #if defined(_OPENMP) || defined(__OPENMP) + #pragma omp simd reduction(+:nsp) + #endif for (size_t n = 0u; n < psets.size(); ++n) - node.subtree_prob[s] += totprob_n[n]; + nsp += totprob_n[n]; + }