Skip to content

Commit

Permalink
#2302: Fix to comply with style guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrepebay committed Dec 12, 2024
1 parent 367341f commit 796e2a0
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 83 deletions.
4 changes: 2 additions & 2 deletions examples/collection/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ set(
transpose
4d_collection
)
if ((vt_papi_enabled AND vt_papi_found))
if (vt_papi_enabled AND vt_papi_found)
list(APPEND COLLECTION_EXAMPLES do_flops_papi)
endif()
if ((vt_perf_enabled AND vt_perf_found))
if (vt_perf_enabled AND vt_perf_found)
list(APPEND COLLECTION_EXAMPLES do_flops_perf)
endif()

Expand Down
6 changes: 0 additions & 6 deletions examples/collection/do_flops_papi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
//@HEADER
*/

#include <vt/transport.h>

#include <cstdlib>
#include <cassert>
#include <iostream>

/// [Do Flops example]

#include <vt/transport.h>
Expand Down
8 changes: 8 additions & 0 deletions src/vt/context/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ struct RunnableNew {};

#include <mpi.h>

#if vt_check_enabled(papi)
# include <papi.h>
void handle_papi_error (int retval)
{
vtAbort(fmt::format("PAPI error {}: {}\n", retval, PAPI_strerror(retval)));
}
#endif

// This cannot use the normal debug_print macros because they rely on context
// being live to print contextual information
#define DEBUG_VT_CONTEXT 0
Expand Down
10 changes: 0 additions & 10 deletions src/vt/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ struct RunnableNew;
#if vt_check_enabled(trace_enabled)
# include "vt/trace/trace_common.h"
#endif
#if vt_check_enabled(papi)
# include <papi.h>
#endif

namespace vt { namespace ctx {

Expand Down Expand Up @@ -127,13 +124,6 @@ struct Context : runtime::component::Component<Context> {

std::string name() override { return "Context"; }

#if vt_check_enabled(papi)
void handle_papi_error (int retval)
{
vtAbort(fmt::format("PAPI error {}: {}\n", retval, PAPI_strerror(retval)));
}
#endif

template <typename SerializerT>
void serialize(SerializerT& s) {
s | thisNode_
Expand Down
10 changes: 5 additions & 5 deletions src/vt/context/runnable_context/lb_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ typename LBData::ElementIDStruct const& LBData::getCurrentElementID() const {

#if vt_check_enabled(papi)
std::unordered_map<std::string, uint64_t> LBData::getPAPIMetrics() {
std::unordered_map<std::string, uint64_t> papi_metrics = papiData_->events;
papi_metrics["real_time"] = papiData_->end_real_usec - papiData_->start_real_usec;
papi_metrics["real_cycles"] = papiData_->end_real_cycles - papiData_->start_real_cycles;
papi_metrics["virt_time"] = papiData_->end_virt_usec - papiData_->start_virt_usec;
papi_metrics["virt_cycles"] = papiData_->end_virt_cycles - papiData_->start_virt_cycles;
std::unordered_map<std::string, uint64_t> papi_metrics = papi_data_->events;
papi_metrics["real_time"] = papi_data_->end_real_usec - papi_data_->start_real_usec;
papi_metrics["real_cycles"] = papi_data_->end_real_cycles - papi_data_->start_real_cycles;
papi_metrics["virt_time"] = papi_data_->end_virt_usec - papi_data_->start_virt_usec;
papi_metrics["virt_cycles"] = papi_data_->end_virt_cycles - papi_data_->start_virt_cycles;
return papi_metrics;
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/vt/context/runnable_context/lb_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct LBData {
should_instrument_(true)
{
#if vt_check_enabled(papi)
papiData_ = std::make_unique<PAPIData>();
papi_data_ = std::make_unique<PAPIData>();
#endif
}

Expand Down Expand Up @@ -127,15 +127,15 @@ struct LBData {
/**
* \brief Start PAPI metrics map for the running context
*/
void startPAPIMetrics() { papiData_->start(); }
void startPAPIMetrics() { papi_data_->start(); }

/**
* \brief Stop PAPI metrics map for the running context
*
* \note has to be called after startPAPIMetrics
*
*/
void stopPAPIMetrics() { papiData_->stop(); }
void stopPAPIMetrics() { papi_data_->stop(); }

/**
* \brief Get the current PAPI metrics map for the running context
Expand All @@ -150,7 +150,7 @@ struct LBData {
ElementIDStruct cur_elm_id_ = {}; /**< Current element ID */
bool should_instrument_ = false; /**< Whether we are instrumenting */
#if vt_check_enabled(papi)
std::unique_ptr<PAPIData> papiData_;
std::unique_ptr<PAPIData> papi_data_;
#endif
};

Expand Down
2 changes: 1 addition & 1 deletion src/vt/context/runnable_context/lb_data.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ LBData::LBData(ElmT* in_elm, MsgT* msg)
should_instrument_(msg->lbLiteInstrument())
{
#if vt_check_enabled(papi)
papiData_ = std::make_unique<PAPIData>();
papi_data_ = std::make_unique<PAPIData>();
#endif
// record the communication LB data right away!
theCollection()->recordLBData(in_elm, msg);
Expand Down
37 changes: 23 additions & 14 deletions src/vt/context/runnable_context/papi_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@
// *****************************************************************************
//@HEADER
*/


#include "vt/context/runnable_context/papi_data.h"

#if vt_check_enabled(papi)

namespace vt { namespace ctx {

PAPIData::PAPIData()
: EventSet(PAPI_NULL), retval(0),
: event_set(PAPI_NULL), retval(0),
start_real_cycles(0), end_real_cycles(0), start_real_usec(0), end_real_usec(0),
start_virt_cycles(0), end_virt_cycles(0), start_virt_usec(0), end_virt_usec(0)
start_virt_cycles(0), end_virt_cycles(0), start_virt_usec(0), end_virt_usec(0)
{
const char* env_p = getenv("VT_EVENTS");
char const* env_p = getenv("VT_EVENTS");

if (env_p == nullptr) {
events["PAPI_TOT_INS"] = 0;
Expand All @@ -62,32 +66,34 @@ PAPIData::PAPIData()
}
}

retval = PAPI_create_eventset(&EventSet);
if (retval != PAPI_OK)
retval = PAPI_create_eventset(&event_set);
if (retval != PAPI_OK) {
handle_error("PAPIData Constructor: Couldn't create an event set: ");
}

// Add instructions before multiplexing (needed https://github.com/icl-utk-edu/papi/wiki/PAPI-Multiplexing)
retval = PAPI_add_event(EventSet, PAPI_TOT_INS);
if (retval != PAPI_OK)
retval = PAPI_add_event(event_set, PAPI_TOT_INS);
if (retval != PAPI_OK) {
handle_error("PAPIData Constructor: Couldn't add instructions to event set: ");
}

retval = PAPI_set_multiplex(EventSet);
retval = PAPI_set_multiplex(event_set);
if (retval != PAPI_OK) {
handle_error("PAPIData Constructor: Couldn't convert event set to multiplexed: ");
}

for (const auto& event : events) {
for (auto const& event : events) {
// Skip adding instructions to the event set since it was already
// added before making the event set multiplexed.
if (event.first == "PAPI_TOT_INS") {
continue;
}
int native = 0x0;
int native = 0;
retval = PAPI_event_name_to_code(event.first.c_str(), &native);
if (retval != PAPI_OK) {
handle_error(fmt::format("Couldn't event_name_to_code for {}: ", event.first.c_str()));
}
retval = PAPI_add_event(EventSet, native);
retval = PAPI_add_event(event_set, native);
if (retval != PAPI_OK) {
handle_error(fmt::format("Couldn't add {} to the PAPI Event Set: ", event.first.c_str()));
}
Expand All @@ -99,9 +105,10 @@ void PAPIData::handle_error(const std::string &info) const {
}

void PAPIData::start() {
retval = PAPI_start(EventSet);
if (retval != PAPI_OK)
retval = PAPI_start(event_set);
if (retval != PAPI_OK) {
handle_error("PAPIData start: Starting counting events in the Event Set: ");
}

start_real_cycles = PAPI_get_real_cyc();
start_real_usec = PAPI_get_real_usec();
Expand All @@ -112,7 +119,7 @@ void PAPIData::start() {
void PAPIData::stop() {
std::vector<long long> aligned_values(events.size(), 0);

retval = PAPI_stop(EventSet, aligned_values.data());
retval = PAPI_stop(event_set, aligned_values.data());
if (retval != PAPI_OK) {
handle_error("PAPIData stop: Stopping the counting of events in the Event Set: ");
}
Expand All @@ -134,3 +141,5 @@ void PAPIData::stop() {
end_virt_usec = PAPI_get_virt_usec();
}
}} /* end namespace vt::ctx */

#endif
6 changes: 5 additions & 1 deletion src/vt/context/runnable_context/papi_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
#if !defined INCLUDED_VT_CONTEXT_RUNNABLE_CONTEXT_PAPI_DATA_H
#define INCLUDED_VT_CONTEXT_RUNNABLE_CONTEXT_PAPI_DATA_H


#include "vt/config.h"

#if vt_check_enabled(papi)
#include <papi.h>

#include <vector>
Expand All @@ -62,7 +64,7 @@ namespace vt { namespace ctx {
* \brief Structure for storing Performance API (PAPI) related data structures
*/
struct PAPIData {
int EventSet = PAPI_NULL;
int event_set = PAPI_NULL;
int retval = PAPI_OK;
uint64_t start_real_cycles = 0, end_real_cycles = 0, start_real_usec = 0, end_real_usec = 0;
uint64_t start_virt_cycles = 0, end_virt_cycles = 0, start_virt_usec = 0, end_virt_usec = 0;
Expand All @@ -76,4 +78,6 @@ struct PAPIData {

}} /* end namespace vt::ctx */

#endif

#endif /*INCLUDED_VT_CONTEXT_RUNNABLE_CONTEXT_PAPI_DATA_H*/
2 changes: 1 addition & 1 deletion src/vt/metrics/example_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

namespace vt { namespace metrics {

const std::unordered_map<std::string, std::pair<uint64_t,uint64_t>> example_event_map = {
std::unordered_map<std::string, std::pair<uint64_t,uint64_t>> const example_event_map = {
{"cycles", std::make_pair(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES)},
{"instructions", std::make_pair(PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS)},
{"cache_references", std::make_pair(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES)},
Expand Down
55 changes: 17 additions & 38 deletions src/vt/metrics/perf_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ namespace vt { namespace metrics {
PerfData::PerfData()
: event_map_(example_event_map)
{
const char* env_p = getenv("VT_EVENTS");
char const* env_p = getenv("VT_EVENTS");

// Check if the environment variable is set
if (env_p == nullptr) {
event_names_.push_back("instructions");
}
else {
} else {
std::string env_str(env_p);
std::stringstream ss(env_str);
std::string item;
Expand All @@ -65,18 +64,15 @@ PerfData::PerfData()
}
}

for (const auto &event_name : event_names_)
{
if (event_map_.find(event_name) == event_map_.end())
{
for (const auto &event_name : event_names_) {
if (event_map_.find(event_name) == event_map_.end()) {
cleanupBeforeAbort();
vtAbort("Event name isn't in known perf events map: " + event_name);
}
}

// Initialize perf events once and store file descriptors
for (const auto &event_name : event_names_)
{
for (const auto &event_name : event_names_) {
struct perf_event_attr pe = {};
pe.type = event_map_.at(event_name).first;
pe.size = sizeof(struct perf_event_attr);
Expand All @@ -92,8 +88,7 @@ PerfData::PerfData()
}

int fd = perfEventOpen(&pe, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
if (fd == -1)
{
if (fd == -1) {
cleanupBeforeAbort();
vtAbort("Error opening perf event: " + std::string(strerror(errno)));
}
Expand All @@ -102,32 +97,25 @@ PerfData::PerfData()
}
}

PerfData::~PerfData()
{
for (int fd : event_fds_)
{
if (fd != -1)
{
PerfData::~PerfData() {
for (int fd : event_fds_) {
if (fd != -1) {
close(fd);
}
}
}

void PerfData::startTaskMeasurement()
{
for (int fd : event_fds_)
{
void PerfData::startTaskMeasurement() {
for (int fd : event_fds_) {
if (fd != -1) {
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
}
}
}

void PerfData::stopTaskMeasurement()
{
for (int fd : event_fds_)
{
void PerfData::stopTaskMeasurement() {
for (int fd : event_fds_) {
if (fd != -1) {
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
}
Expand Down Expand Up @@ -182,25 +170,16 @@ void PerfData::startup() { event_map_ = example_event_map; }

std::string PerfData::name() { return "PerfData"; }

template <typename SerializerT>
void PerfData::serialize(SerializerT& s) {
s | event_map_;
}

void PerfData::cleanupBeforeAbort()
{
for (int fd : event_fds_)
{
if (fd != -1)
{
void PerfData::cleanupBeforeAbort() {
for (int fd : event_fds_) {
if (fd != -1) {
close(fd);
}
}
event_fds_.clear();
}

long PerfData::perfEventOpen(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
{
long PerfData::perfEventOpen(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) {
return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
}

Expand Down
6 changes: 5 additions & 1 deletion src/vt/metrics/perf_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ struct PerfData: runtime::component::Component<PerfData>
std::string name() override;

template <typename SerializerT>
void serialize(SerializerT& s);
void PerfData::serialize(SerializerT& s) {
s | event_map_
| event_names_
| event_fds_;
}

private:
std::unordered_map<std::string, std::pair<uint64_t,uint64_t>> event_map_;
Expand Down

0 comments on commit 796e2a0

Please sign in to comment.