From 86b505a493ace79acb8262ef6a264abc99ef4fc9 Mon Sep 17 00:00:00 2001 From: Pierre Pebay Date: Wed, 10 Jul 2024 11:03:19 -0600 Subject: [PATCH] #2302: Refine access to PAPI events and use preset events --- src/vt/context/runnable_context/lb_data.cc | 21 +++++++++-- src/vt/context/runnable_context/lb_data.h | 35 ++++++++++++------- .../context/runnable_context/lb_data.impl.h | 14 +++----- src/vt/runnable/runnable.cc | 2 +- src/vt/runnable/runnable.h | 15 +++++++- 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/vt/context/runnable_context/lb_data.cc b/src/vt/context/runnable_context/lb_data.cc index 3deeb0c280..4020aa5b5a 100644 --- a/src/vt/context/runnable_context/lb_data.cc +++ b/src/vt/context/runnable_context/lb_data.cc @@ -51,7 +51,9 @@ void LBData::start(TimeType time) { if (should_instrument_) { lb_data_->start(time); } +} +void LBData::startPAPIMetrics() { /* -- PAPI START -- */ /* Start counting events in the Event Set */ papi_retval_ = PAPI_start(EventSet_); @@ -71,7 +73,9 @@ void LBData::finish(TimeType time) { if (should_instrument_) { lb_data_->stop(time); } +} +void LBData::stopPAPIMetrics() { /* -- PAPI STOP -- */ /* Stop the counting of events in the Event Set */ papi_retval_ = PAPI_stop(EventSet_, papi_values_); @@ -104,11 +108,22 @@ typename LBData::ElementIDStruct const& LBData::getCurrentElementID() const { return cur_elm_id_; } -std::unordered_map LBData::getPAPIMetrics() const { +std::unordered_map LBData::getPAPIMetrics() { std::unordered_map papi_metrics = {}; - for (size_t i = 0; i < native_events_.size(); i++) { - papi_metrics[native_events_[i]] = papi_values_[i]; + char event_code_str[PAPI_MAX_STR_LEN]; + for (size_t i = 0; i < events_.size(); i++) { + papi_retval_ = PAPI_event_code_to_name(events_[i], event_code_str); + if (papi_retval_ != PAPI_OK) + handle_papi_error(papi_retval_, "LBData getPAPIMetrics: couldn't get name from event code: "); + papi_metrics[std::string(event_code_str)] = papi_values_[i]; } + papi_metrics[std::string("real_time")] = end_real_usec_ - start_real_usec_; + papi_metrics[std::string("real_cycles")] = end_real_cycles_ - start_real_cycles_; + papi_metrics[std::string("virt_time")] = end_virt_usec_ - start_virt_usec_; + papi_metrics[std::string("virt_cycles")] = end_virt_cycles_ - start_virt_cycles_; + // for (auto [name, value] : papi_metrics) { + // fmt::print("{}: {}\n", name, value); + // } return papi_metrics; } diff --git a/src/vt/context/runnable_context/lb_data.h b/src/vt/context/runnable_context/lb_data.h index 94b9b5f55d..6e6021132f 100644 --- a/src/vt/context/runnable_context/lb_data.h +++ b/src/vt/context/runnable_context/lb_data.h @@ -60,7 +60,7 @@ struct LBData { using ElementIDStruct = elm::ElementIDStruct; using ElementLBData = elm::ElementLBData; - void handle_papi_error (int retval, std::string info) + void handle_papi_error (int retval, std::string info) const { printf("%s: PAPI error %d: %s\n", info.c_str(), retval, PAPI_strerror(retval)); exit(1); @@ -95,16 +95,12 @@ struct LBData { exit(1); } - for (const auto& event_name : native_events_) { - int native = 0x0; - papi_retval_ = PAPI_event_name_to_code(event_name.c_str(), &native); + for (const auto& event : events_) { + papi_retval_ = PAPI_add_event(EventSet_, event); + char event_code_str[PAPI_MAX_STR_LEN]; if (papi_retval_ != PAPI_OK) { - printf("LBData Constructor 2: Couldn't event_name_to_code for %s: PAPI error %d: %s\n",event_name.c_str(), papi_retval_, PAPI_strerror(papi_retval_)); - exit(1); - } - papi_retval_ = PAPI_add_event(EventSet_, native); - if (papi_retval_ != PAPI_OK) { - printf("LBData Constructor 2: Couldn't add %s to the PAPI Event Set: PAPI error %d: %s\n",event_name.c_str(), papi_retval_, PAPI_strerror(papi_retval_)); + PAPI_event_code_to_name(event, event_code_str); + printf("LBData Constructor 2: Couldn't add %s: PAPI error %d: %s\n", event_code_str, papi_retval_, PAPI_strerror(papi_retval_)); exit(1); } } @@ -144,12 +140,25 @@ struct LBData { */ ElementIDStruct const& getCurrentElementID() const; + /** + * \brief Start PAPI metrics map for the running context + */ + void startPAPIMetrics(); + + /** + * \brief Stop PAPI metrics map for the running context + * + * \note has to be called after startPAPIMetrics + * + */ + void stopPAPIMetrics(); + /** * \brief Get the current PAPI metrics map for the running context * * \return the PAPI metrics map */ - std::unordered_map getPAPIMetrics() const; + std::unordered_map getPAPIMetrics(); private: ElementLBData* lb_data_ = nullptr; /**< Element LB data */ @@ -159,8 +168,8 @@ struct LBData { int papi_retval_; long long start_real_cycles_, end_real_cycles_, start_real_usec_, end_real_usec_; long long start_virt_cycles_, end_virt_cycles_, start_virt_usec_, end_virt_usec_; - std::vector native_events_ = {"instructions", "cache-misses", "fp_arith_inst_retired.scalar_double"}; - long_long papi_values_[3]; + std::vector events_ = {PAPI_L1_DCM, PAPI_TOT_INS}; + long_long papi_values_[5]; }; }} /* end namespace vt::ctx */ diff --git a/src/vt/context/runnable_context/lb_data.impl.h b/src/vt/context/runnable_context/lb_data.impl.h index e9831218e7..61bc76141d 100644 --- a/src/vt/context/runnable_context/lb_data.impl.h +++ b/src/vt/context/runnable_context/lb_data.impl.h @@ -70,16 +70,12 @@ LBData::LBData(ElmT* in_elm, MsgT* msg) exit(1); } - for (const auto& event_name : native_events_) { - int native = 0x0; - papi_retval_ = PAPI_event_name_to_code(event_name.c_str(), &native); + for (const auto& event : events_) { + papi_retval_ = PAPI_add_event(EventSet_, event); + char event_code_str[PAPI_MAX_STR_LEN]; if (papi_retval_ != PAPI_OK) { - printf("LBData Constructor 1: Couldn't event_name_to_code for %s: PAPI error %d: %s\n",event_name.c_str(), papi_retval_, PAPI_strerror(papi_retval_)); - exit(1); - } - papi_retval_ = PAPI_add_event(EventSet_, native); - if (papi_retval_ != PAPI_OK) { - printf("LBData Constructor 1: Couldn't add %s to the PAPI Event Set: PAPI error %d: %s\n",event_name.c_str(), papi_retval_, PAPI_strerror(papi_retval_)); + PAPI_event_code_to_name(event, event_code_str); + printf("LBData Constructor 1: Couldn't add %s: PAPI error %d: %s\n", event_code_str, papi_retval_, PAPI_strerror(papi_retval_)); exit(1); } } diff --git a/src/vt/runnable/runnable.cc b/src/vt/runnable/runnable.cc index 9add98befc..ff06007ec4 100644 --- a/src/vt/runnable/runnable.cc +++ b/src/vt/runnable/runnable.cc @@ -199,7 +199,7 @@ void RunnableNew::run() { #endif } -std::unordered_map RunnableNew::getPAPIMetrics() const { +std::unordered_map RunnableNew::getPAPIMetrics() { std::unordered_map result = {}; if (contexts_.has_lb) { diff --git a/src/vt/runnable/runnable.h b/src/vt/runnable/runnable.h index dfdf744ae3..48e300c815 100644 --- a/src/vt/runnable/runnable.h +++ b/src/vt/runnable/runnable.h @@ -321,12 +321,25 @@ struct RunnableNew { */ BaseMsgType* getMsg() const { return msg_.get(); } + /** + * \brief Start PAPI metrics map for the running context + */ + void startPAPIMetrics() { contexts_.lb.startPAPIMetrics(); } + + /** + * \brief Stop PAPI metrics map for the running context + * + * \note has to be called after startPAPIMetrics + * + */ + void stopPAPIMetrics() { contexts_.lb.stopPAPIMetrics(); } + /** * \brief Get the dictionnary of PAPI metrics associated with the runnable * * \return the dictionnary */ - std::unordered_map getPAPIMetrics() const; + std::unordered_map getPAPIMetrics(); #if vt_check_enabled(fcontext) /**