From ad11907f9221fee26cbe223d667a33744278f46b Mon Sep 17 00:00:00 2001 From: Christian Heinrich Date: Wed, 29 Jun 2016 18:40:44 +0200 Subject: [PATCH] [SMPI] Added PAPI counters functionality to SMPI. SMPI now supports PAPI via the 'smpi/papi-events' configuration option. It is still highly experimental and process-/thread-based counters are not yet supported, i.e., there is only one global papi-counter-set. Activate it via '--cfg=smpi/papi-events:default:COUNTER1:COUNTER2:COUNTER3' --- src/smpi/instr_smpi.cpp | 23 +++++++- src/smpi/private.hpp | 6 +- src/smpi/smpi_bench.cpp | 56 +++++++++++++++++- src/smpi/smpi_global.cpp | 123 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 197 insertions(+), 11 deletions(-) diff --git a/src/smpi/instr_smpi.cpp b/src/smpi/instr_smpi.cpp index 4079b4cd81..8b1dac5faf 100644 --- a/src/smpi/instr_smpi.cpp +++ b/src/smpi/instr_smpi.cpp @@ -5,10 +5,11 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "private.h" +#include "private.hpp" #include -#include -#include #include +#include +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI"); @@ -208,7 +209,23 @@ void TRACE_smpi_init(int rank) } xbt_assert(father!=nullptr, "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__); - PJ_container_new(str, INSTR_SMPI, father); +#if HAVE_PAPI + container_t container = +#endif + PJ_container_new(str, INSTR_SMPI, father); +#if HAVE_PAPI + papi_counter_t counters = smpi_process_papi_counters(); + + for (auto& it : counters) { + /** + * Check whether this variable already exists or not. Otherwise, it will be created + * multiple times but only the last one would be used... + */ + if (PJ_type_get_or_null(it.first.c_str(), container->type) == nullptr) { + PJ_type_variable_new(it.first.c_str(), NULL, container->type); + } + } +#endif } void TRACE_smpi_finalize(int rank) diff --git a/src/smpi/private.hpp b/src/smpi/private.hpp index 3e60d85fbe..0181dac05a 100644 --- a/src/smpi/private.hpp +++ b/src/smpi/private.hpp @@ -17,11 +17,11 @@ */ #define TOPAGE(addr) (void *)(((unsigned long)(addr) / xbt_pagesize) * xbt_pagesize) -#ifdef HAVE_PAPI +#if HAVE_PAPI typedef std::vector> papi_counter_t; -XBT_PRIVATE papi_counter_t& smpi_process_counter_data(); -XBT_PRIVATE int smpi_process_event_set(); +XBT_PRIVATE papi_counter_t& smpi_process_papi_counters(); +XBT_PRIVATE int smpi_process_papi_event_set(); #endif extern std::unordered_map location2speedup; diff --git a/src/smpi/smpi_bench.cpp b/src/smpi/smpi_bench.cpp index e9b4773355..3128889670 100644 --- a/src/smpi/smpi_bench.cpp +++ b/src/smpi/smpi_bench.cpp @@ -32,6 +32,10 @@ #include #include +#if HAVE_PAPI +#include +#endif + #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON #endif @@ -239,18 +243,54 @@ void smpi_bench_begin(void) if (MC_is_active() || MC_record_replay_is_active()) return; +#if HAVE_PAPI + if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') { + int event_set = smpi_process_papi_event_set(); + // PAPI_start sets everything to 0! See man(3) PAPI_start + if (PAPI_LOW_LEVEL_INITED == PAPI_is_initialized()) { + if (PAPI_start(event_set) != PAPI_OK) { + // TODO This needs some proper handling. + XBT_CRITICAL("Could not start PAPI counters.\n"); + xbt_die("Error."); + } + } + } +#endif xbt_os_threadtimer_start(smpi_process_timer()); } void smpi_bench_end(void) { - if (MC_is_active() || MC_record_replay_is_active()) return; double speedup = 1; xbt_os_timer_t timer = smpi_process_timer(); xbt_os_threadtimer_stop(timer); + +#if HAVE_PAPI + /** + * An MPI function has been called and now is the right time to update + * our PAPI counters for this process. + */ + if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') { + papi_counter_t& counter_data = smpi_process_papi_counters(); + int event_set = smpi_process_papi_event_set(); + std::vector event_values = std::vector(counter_data.size()); + + if (PAPI_stop(event_set, &event_values[0]) != PAPI_OK) { // Error + XBT_CRITICAL("Could not stop PAPI counters.\n"); + xbt_die("Error."); + } else { + for (unsigned int i = 0; i < counter_data.size(); i++) { + counter_data[i].second += event_values[i]; + // XBT_DEBUG("[%i] PAPI: Counter %s: Value is now %lli (got increment by %lli\n", smpi_process_index(), + // counter_data[i].first.c_str(), counter_data[i].second, event_values[i]); + } + } + } +#endif + if (smpi_process_get_sampling()) { XBT_CRITICAL("Cannot do recursive benchmarks."); XBT_CRITICAL("Are you trying to make a call to MPI within a SMPI_SAMPLE_ block?"); @@ -274,6 +314,20 @@ void smpi_bench_end(void) smpi_execute(xbt_os_timer_elapsed(timer)/speedup); } +#if HAVE_PAPI + if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0' && TRACE_smpi_is_enabled()) { + char container_name[INSTR_DEFAULT_STR_SIZE]; + smpi_container(smpi_process_index(), container_name, INSTR_DEFAULT_STR_SIZE); + container_t container = PJ_container_get(container_name); + papi_counter_t& counter_data = smpi_process_papi_counters(); + + for (auto& pair : counter_data) { + new_pajeSetVariable(surf_get_clock(), container, + PJ_type_get(/* countername */ pair.first.c_str(), container->type), pair.second); + } + } +#endif + smpi_total_benched_time += xbt_os_timer_elapsed(timer); } diff --git a/src/smpi/smpi_global.cpp b/src/smpi/smpi_global.cpp index d140c1aeeb..db6e796b2d 100644 --- a/src/smpi/smpi_global.cpp +++ b/src/smpi/smpi_global.cpp @@ -28,6 +28,16 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (ke #include #include /* trim_right / trim_left */ +#if HAVE_PAPI +#include "papi.h" +const char* papi_default_config_name = "default"; + +struct papi_process_data { + papi_counter_t counter_data; + int event_set; +}; + +#endif std::unordered_map location2speedup; typedef struct s_smpi_process_data { @@ -50,6 +60,11 @@ typedef struct s_smpi_process_data { xbt_bar_t finalization_barrier; int return_value; smpi_trace_call_location_t* trace_call_loc; +#if HAVE_PAPI + /** Contains hardware data as read by PAPI **/ + int papi_event_set; + papi_counter_t papi_counter_data; +#endif } s_smpi_process_data_t; static smpi_process_data_t *process_data = nullptr; @@ -294,6 +309,20 @@ xbt_mutex_t smpi_process_remote_mailboxes_mutex(int index) return data->mailboxes_mutex; } +#if HAVE_PAPI +int smpi_process_papi_event_set(void) +{ + smpi_process_data_t data = smpi_process_data(); + return data->papi_event_set; +} + +papi_counter_t& smpi_process_papi_counters(void) +{ + smpi_process_data_t data = smpi_process_data(); + return data->papi_counter_data; +} +#endif + xbt_os_timer_t smpi_process_timer() { smpi_process_data_t data = smpi_process_data(); @@ -445,14 +474,83 @@ void smpi_global_init() } } +#if HAVE_PAPI + // This map holds for each computation unit (such as "default" or "process1" etc.) + // the configuration as given by the user (counter data as a pair of (counter_name, counter_counter)) + // and the (computed) event_set. + std::map units2papi_setup; + + if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') { + if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT) + XBT_ERROR("Could not initialize PAPI library; is it correctly installed and linked?" + " Expected version is %i", + PAPI_VER_CURRENT); + + typedef boost::tokenizer> Tokenizer; + boost::char_separator separator_units(";"); + std::string str = std::string(xbt_cfg_get_string("smpi/papi-events")); + Tokenizer tokens(str, separator_units); + + // Iterate over all the computational units. This could be + // processes, hosts, threads, ranks... You name it. I'm not exactly + // sure what we will support eventually, so I'll leave it at the + // general term "units". + for (auto& unit_it : tokens) { + boost::char_separator separator_events(":"); + Tokenizer event_tokens(unit_it, separator_events); + + int event_set = PAPI_NULL; + if (PAPI_create_eventset(&event_set) != PAPI_OK) { + // TODO: Should this let the whole simulation die? + XBT_CRITICAL("Could not create PAPI event set during init."); + } + + // NOTE: We cannot use a map here, as we must obey the order of the counters + // This is important for PAPI: We need to map the values of counters back + // to the event_names (so, when PAPI_read() has finished)! + papi_counter_t counters2values; + + // Iterate over all counters that were specified for this specific + // unit. + // Note that we need to remove the name of the unit + // (that could also be the "default" value), which always comes first. + // Hence, we start at ++(events.begin())! + for (Tokenizer::iterator events_it = ++(event_tokens.begin()); events_it != event_tokens.end(); events_it++) { + + int event_code = PAPI_NULL; + char* event_name = const_cast((*events_it).c_str()); + if (PAPI_event_name_to_code(event_name, &event_code) == PAPI_OK) { + if (PAPI_add_event(event_set, event_code) != PAPI_OK) { + XBT_ERROR("Could not add PAPI event '%s'. Skipping.", event_name); + continue; + } else { + XBT_DEBUG("Successfully added PAPI event '%s' to the event set.", event_name); + } + } else { + XBT_CRITICAL("Could not find PAPI event '%s'. Skipping.", event_name); + continue; + } + + counters2values.push_back( + // We cannot just pass *events_it, as this is of type const basic_string + std::make_pair(std::string(*events_it), 0)); + } + + std::string unit_name = *(event_tokens.begin()); + papi_process_data config = {.counter_data = std::move(counters2values), .event_set = event_set}; + + units2papi_setup.insert(std::make_pair(unit_name, std::move(config))); + } + } +#endif if (process_count == 0){ process_count = SIMIX_process_count(); smpirun=1; } smpi_universe_size = process_count; - process_data = xbt_new0(smpi_process_data_t, process_count); + process_data = new smpi_process_data_t[process_count]; for (i = 0; i < process_count; i++) { - process_data[i] = xbt_new(s_smpi_process_data_t, 1); + process_data[i] = new s_smpi_process_data_t; process_data[i]->argc = nullptr; process_data[i]->argv = nullptr; process_data[i]->mailbox = simcall_mbox_create(get_mailbox_name(name, i)); @@ -472,6 +570,23 @@ void smpi_global_init() if (xbt_cfg_get_boolean("smpi/trace-call-location")) { process_data[i]->trace_call_loc = xbt_new(smpi_trace_call_location_t, 1); } + +#if HAVE_PAPI + if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') { + // TODO: Implement host/process/thread based counters. This implementation + // just always takes the values passed via "default", like this: + // "default:COUNTER1:COUNTER2:COUNTER3;". + auto it = units2papi_setup.find(papi_default_config_name); + if (it != units2papi_setup.end()) { + process_data[i]->papi_event_set = it->second.event_set; + process_data[i]->papi_counter_data = it->second.counter_data; + XBT_DEBUG("Setting PAPI set for process %i", i); + } else { + process_data[i]->papi_event_set = PAPI_NULL; + XBT_DEBUG("No PAPI set for process %i", i); + } + } +#endif } //if the process was launched through smpirun script we generate a global mpi_comm_world //if not, we let MPI_COMM_NULL, and the comm world will be private to each mpi instance @@ -512,9 +627,9 @@ void smpi_global_destroy() if (xbt_cfg_get_boolean("smpi/trace-call-location")) { xbt_free(process_data[i]->trace_call_loc); } - xbt_free(process_data[i]); + delete process_data[i]; } - xbt_free(process_data); + delete process_data; process_data = nullptr; if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){ -- 2.20.1