| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // Time.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Thierry Delisle
|
|---|
| 10 | // Created On : Mon Mar 04 15:16:07 2019
|
|---|
| 11 | // Last Modified By :
|
|---|
| 12 | // Last Modified On :
|
|---|
| 13 | // Update Count :
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Time.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert>
|
|---|
| 19 | #include <chrono>
|
|---|
| 20 | #include <cstdint>
|
|---|
| 21 | #include <cstring>
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 | #include <iomanip>
|
|---|
| 24 | #include <stack>
|
|---|
| 25 |
|
|---|
| 26 | namespace Stats {
|
|---|
| 27 | namespace Time {
|
|---|
| 28 | # if !defined(NO_TIME_STATISTICS)
|
|---|
| 29 | extern bool enabled;
|
|---|
| 30 |
|
|---|
| 31 | Base::TreeTop top;
|
|---|
| 32 |
|
|---|
| 33 | class TimerNode : public Base::Tree<top> {
|
|---|
| 34 | public:
|
|---|
| 35 | TimerNode(const char * const name )
|
|---|
| 36 | : Base::Tree<top>(name)
|
|---|
| 37 | {}
|
|---|
| 38 |
|
|---|
| 39 | TimerNode(const char * const name, Base::Tree<top> * parent)
|
|---|
| 40 | : Base::Tree<top>(name, parent)
|
|---|
| 41 |
|
|---|
| 42 | {}
|
|---|
| 43 |
|
|---|
| 44 | virtual void print(std::ostream & os) override {
|
|---|
| 45 | assert(finished);
|
|---|
| 46 | std::chrono::duration<double> diff = end - begin;
|
|---|
| 47 | os << diff.count();
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void start() {
|
|---|
| 51 | begin = std::chrono::high_resolution_clock::now();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void finish() {
|
|---|
| 55 | end = std::chrono::high_resolution_clock::now();
|
|---|
| 56 | finished = true;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | protected:
|
|---|
| 60 | virtual ~TimerNode() = default;
|
|---|
| 61 |
|
|---|
| 62 | private:
|
|---|
| 63 | bool finished = false;
|
|---|
| 64 |
|
|---|
| 65 | typedef std::chrono::time_point<std::chrono::high_resolution_clock> point_t;
|
|---|
| 66 | point_t begin;
|
|---|
| 67 | point_t end;
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | std::stack<TimerNode *> nodes;
|
|---|
| 71 |
|
|---|
| 72 | void StartBlock(const char * const name) {
|
|---|
| 73 | if(!enabled) return;
|
|---|
| 74 | auto node = nodes.empty()
|
|---|
| 75 | ? new TimerNode(name)
|
|---|
| 76 | : new TimerNode(name, nodes.top());
|
|---|
| 77 |
|
|---|
| 78 | nodes.push(node);
|
|---|
| 79 | node->start();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void StopBlock() {
|
|---|
| 83 | if(!enabled) return;
|
|---|
| 84 | nodes.top()->finish();
|
|---|
| 85 | nodes.pop();
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void print() {
|
|---|
| 89 | if(!top.head) return;
|
|---|
| 90 | size_t nc = 0;
|
|---|
| 91 | Base::ForAll(top, 0, [&](Base::TreeImpl * node, size_t level) {
|
|---|
| 92 | nc = std::max(nc, (4 * level) + std::strlen(node->name));
|
|---|
| 93 | });
|
|---|
| 94 |
|
|---|
| 95 | const char * const title = "Timing Results";
|
|---|
| 96 | size_t nct = nc + 14;
|
|---|
| 97 | std::cerr << std::string(nct, '=') << std::endl;
|
|---|
| 98 | std::cerr << std::string((nct - std::strlen(title)) / 2, ' ');
|
|---|
| 99 | std::cerr << title << std::endl;
|
|---|
| 100 | std::cerr << std::string(nct, '-') << std::endl;
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | Base::ForAll(top, 0, [&](Base::TreeImpl * node, size_t level) {
|
|---|
| 104 | std::cerr << std::string(level * 4, ' ');
|
|---|
| 105 | std::cerr << node->name;
|
|---|
| 106 | std::cerr << std::string(nc - ((level * 4) + std::strlen(node->name)), ' ');
|
|---|
| 107 | std::cerr << " | ";
|
|---|
| 108 | std::cerr << std::setw(9);
|
|---|
| 109 | node->print(std::cerr);
|
|---|
| 110 | std::cerr << " |";
|
|---|
| 111 | std::cerr << '\n';
|
|---|
| 112 | }, true);
|
|---|
| 113 |
|
|---|
| 114 | std::cerr << std::string(nct, '-') << std::endl;
|
|---|
| 115 | }
|
|---|
| 116 | # endif
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|