+#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</* computation unit name */ std::string, papi_process_data> 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<boost::char_separator<char>> Tokenizer;
+ boost::char_separator<char> 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<char> 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<char*>((*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, long long>(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