From d57476a45fb47825786005b1b3bccdc71f8dbe31 Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Wed, 27 Sep 2023 17:32:45 -0600 Subject: [PATCH] Adding ncores --- barry.hpp | 220 +++++++++++------- include/barry/barry-macros.hpp | 2 +- include/barry/counters/network.hpp | 2 +- include/barry/model-meat.hpp | 4 +- include/barry/models/geese/flock-bones.hpp | 2 +- include/barry/models/geese/flock-meat.hpp | 6 +- include/barry/models/geese/geese-bones.hpp | 2 +- .../models/geese/geese-meat-likelihood.hpp | 3 +- include/barry/typedefs.hpp | 6 +- 9 files changed, 155 insertions(+), 92 deletions(-) diff --git a/barry.hpp b/barry.hpp index f35072c0a..0d44fb688 100644 --- a/barry.hpp +++ b/barry.hpp @@ -17,7 +17,7 @@ #include #include -#ifdef __OPENMP +#if defined(__OPENMP) || defined(_OPENMP) #include // Set the number of threads to match the number of cores @@ -117,7 +117,7 @@ namespace barry { #define BARRY_MAX_NUM_ELEMENTS static_cast< size_t >(std::numeric_limits< size_t >::max() /2u) #endif -#ifdef __OPENMP +#if defined(__OPENMP) || defined(_OPENMP) #define BARRY_WITH_OMP #include #endif @@ -570,7 +570,7 @@ inline double vec_inner_prod( ) { double res = 0.0; - #ifdef __OPENMP + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd reduction(+:res) #else #ifdef __GNUC__ @@ -617,6 +617,13 @@ inline double vec_inner_prod( #define BARRY_UNUSED(expr) do { (void)(expr); } while (0); +#if defined(_OPENMP) || defined(__OPENMP) +#define BARRY_NCORES_ARG(default) size_t ncores default +#else +#define BARRY_NCORES_ARG(default) size_t +#endif + + #endif /*////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -6356,7 +6363,7 @@ inline void Support 0u) { - #ifdef __OPENMP + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd #endif for (size_t n = 0u; n < n_counters; ++n) @@ -6479,7 +6486,7 @@ inline void Support 0u) { - #ifdef __OPENMP + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd #endif for (size_t n = 0u; n < n_counters; ++n) @@ -7295,33 +7302,38 @@ class Model { double likelihood( const std::vector & params, const size_t & i, - bool as_log = false + bool as_log = false, + BARRY_NCORES_ARG(=2) ); double likelihood( const std::vector & params, const Array_Type & Array_, int i = -1, - bool as_log = false + bool as_log = false, + BARRY_NCORES_ARG(=2) ); double likelihood( const std::vector & params, const std::vector & target_, const size_t & i, - bool as_log = false + bool as_log = false, + BARRY_NCORES_ARG(=2) ); double likelihood( const std::vector & params, const double * target_, const size_t & i, - bool as_log = false + bool as_log = false, + BARRY_NCORES_ARG(=2) ); double likelihood_total( const std::vector & params, - bool as_log = false + bool as_log = false, + BARRY_NCORES_ARG(=2) ); ///@} @@ -7486,9 +7498,8 @@ inline double update_normalizing_constant( { std::vector< double > resv(n); - #ifdef __OPENMP - omp_set_num_threads(omp_get_num_procs()); - #pragma omp parallel for + #if defined(__OPENMP) || defined(_OPENMP) + #pragma omp parallel for shared(resv) #else #ifdef __GNUC__ #ifndef __clang__ @@ -7502,7 +7513,7 @@ inline double update_normalizing_constant( double tmp = 0.0; const double * support_n = support + i * k + 1u; - #ifdef __OPENMP + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd reduction(+:tmp) #else #ifdef __GNUC__ @@ -7520,7 +7531,7 @@ inline double update_normalizing_constant( // Accumulate resv to a double res double res = 0.0; - #ifdef __OPENMP + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp parallel for simd reduction(+:res) #else #ifdef __GNUC__ @@ -7568,8 +7579,7 @@ inline double likelihood_( double numerator = 0.0; // Computing the numerator - #ifdef __OPENMP - omp_set_num_threads(omp_get_num_procs()); + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd reduction(+:numerator) #else #ifdef __GNUC__ @@ -7578,7 +7588,6 @@ inline double likelihood_( #endif #endif #endif - #pragma code_align(32) for (size_t j = 0u; j < params.size(); ++j) numerator += *(stats_target + j) * params[j]; @@ -7618,15 +7627,6 @@ inline double likelihood_( } -#define MODEL_TYPE() Model - -#define MODEL_TEMPLATE_ARGS() - -#define MODEL_TEMPLATE(a,b) \ - template MODEL_TEMPLATE_ARGS() inline a MODEL_TYPE()::b - template < typename Array_Type, typename Data_Counter_Type, @@ -7798,20 +7798,23 @@ inline Model & } -MODEL_TEMPLATE(void, store_psets)() noexcept { +template +inline void Model:: store_psets() noexcept { // if (with_pset) // throw std::logic_error("Powerset storage alreay activated."); with_pset = true; return; } -MODEL_TEMPLATE(std::vector< double >, gen_key)( +template +inline std::vector< double > Model:: gen_key( const Array_Type & Array_ ) { return this->counters->gen_hash(Array_); } -MODEL_TEMPLATE(void, add_counter)( +template +inline void Model:: add_counter( Counter & counter ) { @@ -7819,7 +7822,8 @@ MODEL_TEMPLATE(void, add_counter)( return; } -MODEL_TEMPLATE(void, add_counter)( +template +inline void Model:: add_counter( Counter_fun_type count_fun_, Counter_fun_type init_fun_, Data_Counter_Type data_ @@ -7835,7 +7839,8 @@ MODEL_TEMPLATE(void, add_counter)( } -MODEL_TEMPLATE(void, set_counters)( +template +inline void Model:: set_counters( Counters * counters_ ) { @@ -7852,7 +7857,8 @@ MODEL_TEMPLATE(void, set_counters)( } -MODEL_TEMPLATE(void, add_hasher)( +template +inline void Model:: add_hasher( Hasher_fun_type fun_ ) { @@ -7862,7 +7868,8 @@ MODEL_TEMPLATE(void, add_hasher)( //////////////////////////////////////////////////////////////////////////////// -MODEL_TEMPLATE(void, add_rule)( +template +inline void Model:: add_rule( Rule & rules ) { @@ -7871,7 +7878,8 @@ MODEL_TEMPLATE(void, add_rule)( } -MODEL_TEMPLATE(void, set_rules)( +template +inline void Model:: set_rules( Rules * rules_ ) { @@ -7888,7 +7896,8 @@ MODEL_TEMPLATE(void, set_rules)( //////////////////////////////////////////////////////////////////////////////// -MODEL_TEMPLATE(void, add_rule_dyn)( +template +inline void Model:: add_rule_dyn( Rule & rules_ ) { @@ -7896,7 +7905,8 @@ MODEL_TEMPLATE(void, add_rule_dyn)( return; } -MODEL_TEMPLATE(void, add_rule_dyn)( +template +inline void Model:: add_rule_dyn( Rule_fun_type rule_fun_, Data_Rule_Dyn_Type data_ ) { @@ -7910,7 +7920,8 @@ MODEL_TEMPLATE(void, add_rule_dyn)( } -MODEL_TEMPLATE(void, set_rules_dyn)( +template +inline void Model:: set_rules_dyn( Rules * rules_ ) { @@ -7924,10 +7935,10 @@ MODEL_TEMPLATE(void, set_rules_dyn)( } - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// -MODEL_TEMPLATE(size_t, add_array)( +template +inline size_t Model:: add_array( const Array_Type & Array_, bool force_new ) { @@ -8062,11 +8073,17 @@ MODEL_TEMPLATE(size_t, add_array)( } -MODEL_TEMPLATE(double, likelihood)( +template +inline double Model::likelihood( const std::vector & params, const size_t & i, - bool as_log + bool as_log, + BARRY_NCORES_ARG() ) { + + #if defined(__OPENMP) || defined(_OPENMP) + omp_set_num_threads(ncores); + #endif // Checking if the index exists if (i >= arrays2support.size()) @@ -8105,12 +8122,18 @@ MODEL_TEMPLATE(double, likelihood)( } -MODEL_TEMPLATE(double, likelihood)( +template +inline double Model::likelihood( const std::vector & params, const Array_Type & Array_, int i, - bool as_log + bool as_log, + BARRY_NCORES_ARG() ) { + + #if defined(__OPENMP) || defined(_OPENMP) + omp_set_num_threads(ncores); + #endif // Key of the support set to use int loc; @@ -8185,12 +8208,18 @@ MODEL_TEMPLATE(double, likelihood)( } -MODEL_TEMPLATE(double, likelihood)( +template +inline double Model::likelihood( const std::vector & params, const std::vector & target_, const size_t & i, - bool as_log + bool as_log, + BARRY_NCORES_ARG() ) { + + #if defined(__OPENMP) || defined(_OPENMP) + omp_set_num_threads(ncores); + #endif // Checking if the index exists if (i >= arrays2support.size()) @@ -8248,12 +8277,18 @@ MODEL_TEMPLATE(double, likelihood)( } -MODEL_TEMPLATE(double, likelihood)( +template +inline double Model::likelihood( const std::vector & params, const double * target_, const size_t & i, - bool as_log + bool as_log, + BARRY_NCORES_ARG() ) { + + #if defined(__OPENMP) || defined(_OPENMP) + omp_set_num_threads(ncores); + #endif // Checking if the index exists if (i >= arrays2support.size()) @@ -8318,10 +8353,16 @@ MODEL_TEMPLATE(double, likelihood)( } -MODEL_TEMPLATE(double, likelihood_total)( +template +inline double Model::likelihood_total( const std::vector & params, - bool as_log + bool as_log, + BARRY_NCORES_ARG() ) { + + #if defined(__OPENMP) || defined(_OPENMP) + omp_set_num_threads(ncores); + #endif size_t params_last_size = params_last.size(); @@ -8384,7 +8425,8 @@ MODEL_TEMPLATE(double, likelihood_total)( } -MODEL_TEMPLATE(double, get_norm_const)( +template +inline double Model:: get_norm_const( const std::vector & params, const size_t & i, bool as_log @@ -8420,7 +8462,8 @@ MODEL_TEMPLATE(double, get_norm_const)( } -MODEL_TEMPLATE(const std::vector< Array_Type > *, get_pset)( +template +inline const std::vector< Array_Type > * Model:: get_pset( const size_t & i ) { @@ -8432,7 +8475,8 @@ MODEL_TEMPLATE(const std::vector< Array_Type > *, get_pset)( } -MODEL_TEMPLATE(const std::vector< double > *, get_pset_stats)( +template +inline const std::vector< double > * Model:: get_pset_stats( const size_t & i ) { @@ -8443,7 +8487,8 @@ MODEL_TEMPLATE(const std::vector< double > *, get_pset_stats)( } -MODEL_TEMPLATE(void, print_stats)(size_t i) const +template +inline void Model:: print_stats(size_t i) const { if (i >= arrays2support.size()) @@ -8533,14 +8578,16 @@ inline void Model +inline size_t Model:: size() const noexcept { // INITIALIZED() return this->stats_target.size(); } -MODEL_TEMPLATE(size_t, size_unique)() const noexcept +template +inline size_t Model:: size_unique() const noexcept { // INITIALIZED() @@ -8548,7 +8595,8 @@ MODEL_TEMPLATE(size_t, size_unique)() const noexcept } -MODEL_TEMPLATE(size_t, nterms)() const noexcept +template +inline size_t Model:: nterms() const noexcept { if (transform_model_fun) @@ -8558,21 +8606,24 @@ MODEL_TEMPLATE(size_t, nterms)() const noexcept } -MODEL_TEMPLATE(size_t, nrules)() const noexcept +template +inline size_t Model:: nrules() const noexcept { return this->rules->size(); } -MODEL_TEMPLATE(size_t, nrules_dyn)() const noexcept +template +inline size_t Model:: nrules_dyn() const noexcept { return this->rules_dyn->size(); } -MODEL_TEMPLATE(size_t, support_size)() const noexcept +template +inline size_t Model:: support_size() const noexcept { // INITIALIZED() @@ -8584,7 +8635,8 @@ MODEL_TEMPLATE(size_t, support_size)() const noexcept } -MODEL_TEMPLATE(std::vector< std::string >, colnames)() const +template +inline std::vector< std::string > Model:: colnames() const { if (transform_model_fun) @@ -8679,7 +8731,8 @@ inline Array_Type Model +inline Array_Type Model:: sample( const Array_Type & Array_, const std::vector & params ) { @@ -8847,7 +8900,8 @@ MODEL_TEMPLATE(Array_Type, sample)( } -MODEL_TEMPLATE(double, conditional_prob)( +template +inline double Model:: conditional_prob( const Array_Type & Array_, const std::vector< double > & params, size_t i, @@ -8879,59 +8933,67 @@ MODEL_TEMPLATE(double, conditional_prob)( } -MODEL_TEMPLATE(const std::mt19937 *, get_rengine)() const { +template +inline const std::mt19937 * Model:: get_rengine() const { return this->rengine; } -template MODEL_TEMPLATE_ARGS() -inline Counters * MODEL_TYPE()::get_counters() { +template +inline Counters * Model::get_counters() { return this->counters; } -template MODEL_TEMPLATE_ARGS() -inline Rules * MODEL_TYPE()::get_rules() { +template +inline Rules * Model::get_rules() { return this->rules; } -template MODEL_TEMPLATE_ARGS() -inline Rules * MODEL_TYPE()::get_rules_dyn() { +template +inline Rules * Model::get_rules_dyn() { return this->rules_dyn; } -template MODEL_TEMPLATE_ARGS() +template inline Support * -MODEL_TYPE()::get_support_fun() { +Model::get_support_fun() { return &this->support_fun; } -MODEL_TEMPLATE(std::vector< std::vector< double > > *, get_stats_target)() +template +inline std::vector< std::vector< double > > * Model:: get_stats_target() { return &stats_target; } -MODEL_TEMPLATE(std::vector< std::vector< double > > *, get_stats_support)() +template +inline std::vector< std::vector< double > > * Model:: get_stats_support() { return &stats_support; } -MODEL_TEMPLATE(std::vector< size_t > *, get_arrays2support)() +template +inline std::vector< size_t > * Model:: get_arrays2support() { return &arrays2support; } -MODEL_TEMPLATE(std::vector< std::vector< Array_Type > > *, get_pset_arrays)() { +template +inline std::vector< std::vector< Array_Type > > * Model:: get_pset_arrays() { return &pset_arrays; } -MODEL_TEMPLATE(std::vector< std::vector > *, get_pset_stats)() { +template +inline std::vector< std::vector > * Model:: get_pset_stats() { return &pset_stats; } -MODEL_TEMPLATE(std::vector< std::vector > *, get_pset_probs)() { +template +inline std::vector< std::vector > * Model:: get_pset_probs() { return &pset_probs; } -MODEL_TEMPLATE(void, set_transform_model)( +template +inline void Model:: set_transform_model( std::function(double *,size_t)> fun, std::vector< std::string > names ) diff --git a/include/barry/barry-macros.hpp b/include/barry/barry-macros.hpp index 20b261e3b..c32f36c9c 100644 --- a/include/barry/barry-macros.hpp +++ b/include/barry/barry-macros.hpp @@ -12,7 +12,7 @@ #if defined(_OPENMP) || defined(__OPENMP) #define BARRY_NCORES_ARG(default) size_t ncores default #else -#define BARRY_NCORES_ARG(default) size_t +#define BARRY_NCORES_ARG(default) size_t ncores default #endif diff --git a/include/barry/counters/network.hpp b/include/barry/counters/network.hpp index e6b5b8ec8..642cf2981 100644 --- a/include/barry/counters/network.hpp +++ b/include/barry/counters/network.hpp @@ -673,7 +673,7 @@ inline void counter_ctriads(NetCounters * counters) // i->j->k->i double ans = 0.0; - #ifdef __OPENM + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd reduction(+:ans) #endif for (size_t k = 0u; k < Array.nrow(); ++k) diff --git a/include/barry/model-meat.hpp b/include/barry/model-meat.hpp index 202e2bbce..69fc5cf0e 100644 --- a/include/barry/model-meat.hpp +++ b/include/barry/model-meat.hpp @@ -914,7 +914,7 @@ inline double Model & par, bool as_log = false, bool use_reduced_sequence = true, - BARRY_NCORES_ARG(=1) + size_t ncores = 1u ); /** diff --git a/include/barry/models/geese/flock-meat.hpp b/include/barry/models/geese/flock-meat.hpp index 901981ab1..5ddae20c1 100644 --- a/include/barry/models/geese/flock-meat.hpp +++ b/include/barry/models/geese/flock-meat.hpp @@ -139,7 +139,7 @@ inline double Flock::likelihood_joint( const std::vector< double > & par, bool as_log, bool use_reduced_sequence, - BARRY_NCORES_ARG() + size_t ncores ) { @@ -150,14 +150,14 @@ inline double Flock::likelihood_joint( if (as_log) { for (auto& d : this->dat) - ans += d.likelihood(par, as_log, use_reduced_sequence); + ans += d.likelihood(par, as_log, use_reduced_sequence, ncores); } else { for (auto& d : this->dat) - ans *= d.likelihood(par, as_log, use_reduced_sequence); + ans *= d.likelihood(par, as_log, use_reduced_sequence, ncores); } diff --git a/include/barry/models/geese/geese-bones.hpp b/include/barry/models/geese/geese-bones.hpp index e8ead9cfb..9492dcac3 100644 --- a/include/barry/models/geese/geese-bones.hpp +++ b/include/barry/models/geese/geese-bones.hpp @@ -216,7 +216,7 @@ class Geese { const std::vector< double > & par, bool as_log = false, bool use_reduced_sequence = true, - BARRY_NCORES_ARG(= 1) + size_t ncores = 1u ); double likelihood_exhaust(const std::vector< double > & par); diff --git a/include/barry/models/geese/geese-meat-likelihood.hpp b/include/barry/models/geese/geese-meat-likelihood.hpp index a76b218b8..91bf84e40 100644 --- a/include/barry/models/geese/geese-meat-likelihood.hpp +++ b/include/barry/models/geese/geese-meat-likelihood.hpp @@ -7,7 +7,7 @@ inline double Geese::likelihood( const std::vector< double > & par, bool as_log, bool use_reduced_sequence, - BARRY_NCORES_ARG() + size_t ncores ) { INITIALIZED() @@ -150,6 +150,7 @@ inline double Geese::likelihood( par0, temp_stats, node.narray[s], + as_log, ncores ); } catch (std::exception & e) { diff --git a/include/barry/typedefs.hpp b/include/barry/typedefs.hpp index 28ae44b2c..1d4bbe004 100644 --- a/include/barry/typedefs.hpp +++ b/include/barry/typedefs.hpp @@ -256,7 +256,7 @@ inline bool vec_equal_approx( } ///@} -#ifdef __OPENM +#if defined(__OPENMP) || defined(_OPENMP) #pragma omp declare simd #endif template @@ -267,7 +267,7 @@ inline T vec_inner_prod( ) { double res = 0.0; - #ifdef __OPENM + #if defined(__OPENMP) || defined(_OPENMP) #pragma omp simd reduction(+:res) #else #ifdef __GNUC__ @@ -283,7 +283,7 @@ inline T vec_inner_prod( } -#ifdef __OPENM +#if defined(__OPENMP) || defined(_OPENMP) #pragma omp declare simd #endif template <>