Skip to content

Commit

Permalink
#2302: Add PAPI related information data structure and enable multipl…
Browse files Browse the repository at this point in the history
…exed event set
  • Loading branch information
pierrepebay committed Jul 17, 2024
1 parent b195e95 commit c8eb14a
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 124 deletions.
44 changes: 28 additions & 16 deletions examples/collection/do_flops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#include <iostream>

static constexpr std::size_t const default_nrow_object = 8;
static constexpr std::size_t const default_num_objs = 4;
static constexpr std::size_t const default_num_objs = 1;
static constexpr double const default_tol = 1.0e-02;
static constexpr std::size_t const default_flops_per_iter = 100000;

Expand All @@ -84,6 +84,16 @@ do_flops( int n )
dummy( ( void * ) &c );

Check notice on line 84 in examples/collection/do_flops.cc

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

examples/collection/do_flops.cc#L84

C-style pointer casting
}

double pi(uint64_t n) {
double sum = 0.0;
int sign = 1;
for (int i = 0; i < n; ++i) {
sum += sign/(2.0*i+1.0);
sign *= -1;
}
return 4.0*sum;
}

struct NodeObj {
bool is_finished_ = false;
void workFinishedHandler() { is_finished_ = true; }
Expand Down Expand Up @@ -135,21 +145,31 @@ struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {

void doIteration() {
iter_ += 1;
fmt::print("-- Starting Iteration --\n");

// vt::theContext()->getTask()->startPAPIMetrics();
vt::theContext()->getTask()->startPAPIMetrics();

do_flops(flopsPerIter_);
// ----------------------------------------------------------
// test non packed double precision floating point operations
// should result in ~4*n of these operations

// vt::theContext()->getTask()->stopPAPIMetrics();
// auto res = vt::theContext()->getTask()->getPAPIMetrics();
// for (auto [name, value] : res) {
// fmt::print(" {}: {}\n", name, value);
// }
double p;
p = pi(10000000);
fmt::print("pi: {}\n", p);
// ----------------------------------------------------------

auto proxy = this->getCollectionProxy();
proxy.reduce<&GenericWork::checkCompleteCB, vt::collective::MaxOp>(
proxy[0], 0.0
);

vt::theContext()->getTask()->stopPAPIMetrics();
std::unordered_map<std::string, uint64_t> res = vt::theContext()->getTask()->getPAPIMetrics();
for (auto [name, value] : res) {
fmt::print(" {}: {}\n", name, value);
}

fmt::print("-- Stopping Iteration --\n");
}

struct VecMsg : vt::CollectionMessage<GenericWork> {
Expand Down Expand Up @@ -187,8 +207,6 @@ struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {
return;
}

vt::theContext()->getTask()->startPAPIMetrics();

vt::IdxBase const myIdx = getIndex().x();
auto proxy = this->getCollectionProxy();

Expand All @@ -204,12 +222,6 @@ struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {
myIdx
);
}

vt::theContext()->getTask()->stopPAPIMetrics();
auto res = vt::theContext()->getTask()->getPAPIMetrics();
for (auto [name, value] : res) {
fmt::print(" {}: {}\n", name, value);
}
}

void init() {
Expand Down
11 changes: 3 additions & 8 deletions src/vt/context/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,10 @@ Context::Context([[maybe_unused]] bool const is_interop, MPI_Comm comm) {
if (retval != PAPI_VER_CURRENT)
handle_papi_error(retval);

/* Check for possible failures */
if (retval < 0)
/* Enable and initialize multiplex support */
retval = PAPI_multiplex_init();
if (retval != PAPI_OK)
handle_papi_error(retval);

/* Print PAPI Version */
fprintf(stdout, "PAPI Version Number\n");
fprintf(stdout, "MAJOR: %d\n", PAPI_VERSION_MAJOR(retval));
fprintf(stdout, "MINOR: %d\n", PAPI_VERSION_MINOR(retval));
fprintf(stdout, "REVISION: %d\n", PAPI_VERSION_REVISION(retval));
}

Context::~Context() {
Expand Down
46 changes: 8 additions & 38 deletions src/vt/context/runnable_context/lb_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,13 @@ void LBData::start(TimeType time) {
}
}

void LBData::startPAPIMetrics() {
/* -- PAPI START -- */
/* Start counting events in the Event Set */
papi_retval_ = PAPI_start(EventSet_);
if (papi_retval_ != PAPI_OK)
handle_papi_error(papi_retval_, "LBData start: Starting counting events in the Event Set: ");

start_real_cycles_ = PAPI_get_real_cyc();
start_real_usec_ = PAPI_get_real_usec();
start_virt_cycles_ = PAPI_get_virt_cyc();
start_virt_usec_ = PAPI_get_virt_usec();

/* ---------------- */
}

void LBData::finish(TimeType time) {
// record end 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_);
if (papi_retval_ != PAPI_OK)
handle_papi_error(papi_retval_, "LBData finish: Stoping the counting of events in the Event Set: ");

end_real_cycles_ = PAPI_get_real_cyc();
end_real_usec_ = PAPI_get_real_usec();
end_virt_cycles_ = PAPI_get_virt_cyc();
end_virt_usec_ = PAPI_get_virt_usec();

/* -------------- */
}

void LBData::send(elm::ElementIDStruct dest, MsgSizeType bytes) {
if (should_instrument_) {
lb_data_->sendToEntity(dest, cur_elm_id_, bytes);
Expand All @@ -108,15 +78,15 @@ typename LBData::ElementIDStruct const& LBData::getCurrentElementID() const {
return cur_elm_id_;
}

std::unordered_map<std::string, double> LBData::getPAPIMetrics() {
std::unordered_map<std::string, double> papi_metrics = {};
for (size_t i = 0; i < native_events_.size(); i++) {
papi_metrics[native_events_[i]] = papi_values_[i];
std::unordered_map<std::string, uint64_t> LBData::getPAPIMetrics() {
std::unordered_map<std::string, uint64_t> papi_metrics = {};
for (size_t i = 0; i < papiData_->native_events.size(); i++) {
papi_metrics[papiData_->native_events[i]] = papiData_->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_;
papi_metrics[std::string("real_time")] = papiData_->end_real_usec - papiData_->start_real_usec;
papi_metrics[std::string("real_cycles")] = papiData_->end_real_cycles - papiData_->start_real_cycles;
papi_metrics[std::string("virt_time")] = papiData_->end_virt_usec - papiData_->start_virt_usec;
papi_metrics[std::string("virt_cycles")] = papiData_->end_virt_cycles - papiData_->start_virt_cycles;
// for (auto [name, value] : papi_metrics) {
// fmt::print("{}: {}\n", name, value);
// }
Expand Down
45 changes: 8 additions & 37 deletions src/vt/context/runnable_context/lb_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include "vt/vrt/collection/balance/lb_common.h"
#include "vt/elm/elm_lb_data.fwd.h"
#include "vt/context/runnable_context/papi_data.h"

#include <papi.h>

Expand All @@ -60,12 +61,6 @@ struct LBData {
using ElementIDStruct = elm::ElementIDStruct;
using ElementLBData = elm::ElementLBData;

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);
}

LBData() = default;

/**
Expand All @@ -86,29 +81,9 @@ struct LBData {
LBData(ElementLBData* in_lb_data, ElementIDStruct const& in_elm_id)
: lb_data_(in_lb_data),
cur_elm_id_(in_elm_id),
should_instrument_(true)
{
/* Create the PAPI Event Set */
papi_retval_ = PAPI_create_eventset(&EventSet_);
if (papi_retval_ != PAPI_OK) {
printf("LBData Constructor 2: Creating the PAPI Event Set: PAPI error %d: %s\n", papi_retval_, PAPI_strerror(papi_retval_));
exit(1);
}

for (const auto& event_name : native_events_) {
int native = 0x0;
papi_retval_ = PAPI_event_name_to_code(event_name.c_str(), &native);
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_));
exit(1);
}
}
}
should_instrument_(true),
papiData_(std::make_unique<PAPIData>())
{ }

/**
* \brief Return whether time is required
Expand Down Expand Up @@ -147,33 +122,29 @@ struct LBData {
/**
* \brief Start PAPI metrics map for the running context
*/
void startPAPIMetrics();
void startPAPIMetrics() { papiData_->start(); }

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

/**
* \brief Get the current PAPI metrics map for the running context
*
* \return the PAPI metrics map
*/
std::unordered_map<std::string, double> getPAPIMetrics();
std::unordered_map<std::string, uint64_t> getPAPIMetrics();

private:
ElementLBData* lb_data_ = nullptr; /**< Element LB data */
ElementIDStruct cur_elm_id_ = {}; /**< Current element ID */
bool should_instrument_ = false; /**< Whether we are instrumenting */
int EventSet_ = PAPI_NULL;
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<std::string> native_events_ = {"instructions", "cache-misses", "fp_arith_inst_retired.scalar_double"};
long_long papi_values_[3];
std::unique_ptr<PAPIData> papiData_;
};

}} /* end namespace vt::ctx */
Expand Down
24 changes: 2 additions & 22 deletions src/vt/context/runnable_context/lb_data.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,11 @@ template <typename ElmT, typename MsgT>
LBData::LBData(ElmT* in_elm, MsgT* msg)
: lb_data_(&in_elm->getLBData()),
cur_elm_id_(in_elm->getElmID()),
should_instrument_(msg->lbLiteInstrument())
should_instrument_(msg->lbLiteInstrument()),
papiData_(std::make_unique<PAPIData>())
{
// record the communication LB data right away!
theCollection()->recordLBData(in_elm, msg);

/* Create the PAPI Event Set */
papi_retval_ = PAPI_create_eventset(&EventSet_);
if (papi_retval_ != PAPI_OK) {
printf("LBData Constructor 1: Creating the PAPI Event Set: PAPI error %d: %s\n", papi_retval_, PAPI_strerror(papi_retval_));
exit(1);
}

for (const auto& event_name : native_events_) {
int native = 0x0;
papi_retval_ = PAPI_event_name_to_code(event_name.c_str(), &native);
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_));
exit(1);
}
}
}

}} /* end namespace vt::ctx */
Expand Down
Loading

0 comments on commit c8eb14a

Please sign in to comment.