| 1 | /**
|
|---|
| 2 | * @file timer.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue Sep 6 16:17:40 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Class to measure timings.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifdef HAVE_CONFIG_H
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | #ifdef HAVE_MPI
|
|---|
| 15 | #include <mpi.h>
|
|---|
| 16 | #endif
|
|---|
| 17 |
|
|---|
| 18 | #include <iostream>
|
|---|
| 19 | #include <limits>
|
|---|
| 20 | #include <sstream>
|
|---|
| 21 |
|
|---|
| 22 | #include "base/helper.hpp"
|
|---|
| 23 | #include "base/timer.hpp"
|
|---|
| 24 | #include "comm/comm.hpp"
|
|---|
| 25 | #include "thirdparty/pugixml/pugixml.hpp"
|
|---|
| 26 | #include "mg.hpp"
|
|---|
| 27 |
|
|---|
| 28 | using namespace VMG;
|
|---|
| 29 |
|
|---|
| 30 | std::map<std::string, TimerData> Timer::td;
|
|---|
| 31 |
|
|---|
| 32 | void Timer::Start(std::string event)
|
|---|
| 33 | {
|
|---|
| 34 | #ifdef HAVE_MPI
|
|---|
| 35 | #ifdef DEBUG_MEASURE_TIME
|
|---|
| 36 | std::map<std::string, TimerData>::iterator iter = td.find(event);
|
|---|
| 37 | if (iter == td.end())
|
|---|
| 38 | iter = td.insert(std::make_pair(event, TimerData())).first;
|
|---|
| 39 |
|
|---|
| 40 | iter->second.time_start = MPI_Wtime();
|
|---|
| 41 | #endif
|
|---|
| 42 | #endif
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void Timer::Stop(std::string event)
|
|---|
| 46 | {
|
|---|
| 47 | #ifdef HAVE_MPI
|
|---|
| 48 | #ifdef DEBUG_MEASURE_TIME
|
|---|
| 49 | double time_end = MPI_Wtime();
|
|---|
| 50 |
|
|---|
| 51 | std::map<std::string, TimerData>::iterator iter = td.find(event);
|
|---|
| 52 |
|
|---|
| 53 | if (time_end - iter->second.time_start < std::numeric_limits<double>::min())
|
|---|
| 54 | ++(iter->second.warning);
|
|---|
| 55 |
|
|---|
| 56 | ++(iter->second.total);
|
|---|
| 57 |
|
|---|
| 58 | iter->second.duration += time_end - iter->second.time_start;
|
|---|
| 59 | #endif
|
|---|
| 60 | #endif
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void Timer::Clear()
|
|---|
| 64 | {
|
|---|
| 65 | td.clear();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | pugi::xml_node Timer::ToXMLNode()
|
|---|
| 69 | {
|
|---|
| 70 | std::map<std::string, TimerData>::iterator iter;
|
|---|
| 71 |
|
|---|
| 72 | pugi::xml_document doc;
|
|---|
| 73 | pugi::xml_node node_process = doc.append_child("Process");
|
|---|
| 74 | pugi::xml_attribute att_rank = node_process.append_attribute("Rank");
|
|---|
| 75 | att_rank.set_value(MG::GetComm()->GlobalRank());
|
|---|
| 76 |
|
|---|
| 77 | pugi::xml_node node_timings = node_process.append_child("Timings");
|
|---|
| 78 |
|
|---|
| 79 | for (iter=Timer::td.begin(); iter!=Timer::td.end(); ++iter) {
|
|---|
| 80 |
|
|---|
| 81 | pugi::xml_node node_entry = node_timings.append_child("Sample");
|
|---|
| 82 | node_entry.append_attribute("Name").set_value(Helper::ToString(iter->first).c_str());
|
|---|
| 83 |
|
|---|
| 84 | pugi::xml_node node_duration = node_entry.append_child("Duration");
|
|---|
| 85 | pugi::xml_node node_duration_data = node_duration.append_child(pugi::node_pcdata);
|
|---|
| 86 | node_duration_data.set_value(Helper::ToString(iter->second.duration).c_str());
|
|---|
| 87 |
|
|---|
| 88 | pugi::xml_node node_warnings = node_entry.append_child("Warnings");
|
|---|
| 89 | pugi::xml_node node_warnings_data = node_warnings.append_child(pugi::node_pcdata);
|
|---|
| 90 | node_warnings_data.set_value(Helper::ToString(iter->second.warning).c_str());
|
|---|
| 91 |
|
|---|
| 92 | pugi::xml_node node_total = node_entry.append_child("Total");
|
|---|
| 93 | pugi::xml_node node_total_data = node_total.append_child(pugi::node_pcdata);
|
|---|
| 94 | node_total_data.set_value(Helper::ToString(iter->second.total).c_str());
|
|---|
| 95 |
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return node_process;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | std::string Timer::ToString()
|
|---|
| 102 | {
|
|---|
| 103 | pugi::xml_node node = Timer::ToXMLNode();
|
|---|
| 104 | std::stringstream str;
|
|---|
| 105 | node.print(str);
|
|---|
| 106 | return str.str();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void Timer::Print()
|
|---|
| 110 | {
|
|---|
| 111 | #ifdef DEBUG_MEASURE_TIME
|
|---|
| 112 | std::map<std::string, TimerData>::const_iterator iter;
|
|---|
| 113 | Comm& comm = *MG::GetComm();
|
|---|
| 114 |
|
|---|
| 115 | if (comm.GlobalRank() == 0) {
|
|---|
| 116 | comm.PrintStringOnce("Running times:");
|
|---|
| 117 | for (iter=Timer::td.begin(); iter!=Timer::td.end(); ++iter)
|
|---|
| 118 | comm.PrintStringOnce(" %s: %es", iter->first.c_str(), iter->second.duration);
|
|---|
| 119 | }
|
|---|
| 120 | #endif
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | std::ostream& VMG::operator<<(std::ostream& out, const Timer&)
|
|---|
| 124 | {
|
|---|
| 125 | return out << Timer::ToString();
|
|---|
| 126 | }
|
|---|