| [8f74a6a] | 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 | //
 | 
|---|
| [675716e] | 7 | // Counter.h --
 | 
|---|
| [8f74a6a] | 8 | //
 | 
|---|
 | 9 | // Author           : Thierry Delisle
 | 
|---|
 | 10 | // Created On       : Thu Feb 28 12::05:10 2019
 | 
|---|
 | 11 | // Last Modified By :
 | 
|---|
 | 12 | // Last Modified On :
 | 
|---|
 | 13 | // Update Count     :
 | 
|---|
 | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #pragma once
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include <cstdint>
 | 
|---|
 | 19 | #include <iostream>
 | 
|---|
| [8e70823] | 20 | 
 | 
|---|
 | 21 | #include "Common/Stats/Base.h"
 | 
|---|
| [8f74a6a] | 22 | 
 | 
|---|
| [1cb7fab2] | 23 | #if defined( NO_STATISTICS )
 | 
|---|
 | 24 |         #define NO_COUNTER_STATISTICS
 | 
|---|
 | 25 | #endif
 | 
|---|
 | 26 | 
 | 
|---|
| [8f74a6a] | 27 | namespace Stats {
 | 
|---|
 | 28 |         namespace Counters {
 | 
|---|
| [1cb7fab2] | 29 | #               if defined(NO_COUNTERS_STATISTICS)
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 |                         static inline void print() {}
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 |                         class CounterGroup {
 | 
|---|
 | 34 |                         public:
 | 
|---|
 | 35 |                         };
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 |                         class SimpleCounter {
 | 
|---|
 | 38 |                         public:
 | 
|---|
| [b8665e3] | 39 |                                 inline void operator++() {}
 | 
|---|
| [1cb7fab2] | 40 |                                 inline void operator++(int) {}
 | 
|---|
 | 41 |                                 inline void operator+=(size_t) {}
 | 
|---|
 | 42 |                         };
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 |                         template<typename T>
 | 
|---|
 | 45 |                         class AverageCounter {
 | 
|---|
 | 46 |                         public:
 | 
|---|
 | 47 |                                 inline void push(T value) {}
 | 
|---|
 | 48 |                         };
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 |                         template<typename T>
 | 
|---|
 | 51 |                         class MaxCounter {
 | 
|---|
 | 52 |                         public:
 | 
|---|
 | 53 |                                 inline void push(T value) {}
 | 
|---|
 | 54 |                         };
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |                         template<typename counter_t>
 | 
|---|
 | 57 |                         counter_t * build(const char * const name) {
 | 
|---|
 | 58 |                                 return nullptr;
 | 
|---|
 | 59 |                         }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |                         template<typename counter_t>
 | 
|---|
 | 62 |                         counter_t * build(const char * const name, Base::Tree<top> * parent) {
 | 
|---|
 | 63 |                                         return nullptr;
 | 
|---|
 | 64 |                         }
 | 
|---|
 | 65 | #               else
 | 
|---|
 | 66 |                         extern bool enabled;
 | 
|---|
| [8f74a6a] | 67 | 
 | 
|---|
| [1cb7fab2] | 68 |                         extern Base::TreeTop top;
 | 
|---|
| [8f74a6a] | 69 | 
 | 
|---|
| [1cb7fab2] | 70 |                         class CounterGroup : public Base::Tree<top> {
 | 
|---|
 | 71 |                         public:
 | 
|---|
 | 72 |                                 CounterGroup(const char * const name ) : Base::Tree<top>(name) {}
 | 
|---|
 | 73 |                                 CounterGroup(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
 | 
|---|
| [8f74a6a] | 74 | 
 | 
|---|
| [1cb7fab2] | 75 |                                 virtual void print(std::ostream & os) override { os << ""; }
 | 
|---|
 | 76 |                         protected:
 | 
|---|
 | 77 |                                 virtual ~CounterGroup() = default;
 | 
|---|
 | 78 |                         };
 | 
|---|
| [8f74a6a] | 79 | 
 | 
|---|
| [1cb7fab2] | 80 |                         class SimpleCounter : public Base::Tree<top> {
 | 
|---|
 | 81 |                         public:
 | 
|---|
 | 82 |                                 SimpleCounter(const char * const name ) : Base::Tree<top>(name) {}
 | 
|---|
 | 83 |                                 SimpleCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
 | 
|---|
| [8e70823] | 84 | 
 | 
|---|
| [1cb7fab2] | 85 |                                 virtual void print(std::ostream & os) override { os << count; }
 | 
|---|
| [8f74a6a] | 86 | 
 | 
|---|
| [b8665e3] | 87 |                                 inline void operator++()             { if(!enabled) return; count++;        }
 | 
|---|
| [1cb7fab2] | 88 |                                 inline void operator++(int)          { if(!enabled) return; count++;        }
 | 
|---|
 | 89 |                                 inline void operator+=(size_t value) { if(!enabled) return; count += value; }
 | 
|---|
| [8f74a6a] | 90 | 
 | 
|---|
| [1cb7fab2] | 91 |                         protected:
 | 
|---|
 | 92 |                                 virtual ~SimpleCounter() = default;
 | 
|---|
| [8f74a6a] | 93 | 
 | 
|---|
| [1cb7fab2] | 94 |                         private:
 | 
|---|
 | 95 |                                 size_t count = 0;
 | 
|---|
 | 96 |                         };
 | 
|---|
| [8e70823] | 97 | 
 | 
|---|
| [1cb7fab2] | 98 |                         template<typename T>
 | 
|---|
 | 99 |                         class AverageCounter : public Base::Tree<top> {
 | 
|---|
 | 100 |                         public:
 | 
|---|
 | 101 |                                 AverageCounter(const char * const name ) : Base::Tree<top>(name), sum{} {}
 | 
|---|
 | 102 |                                 AverageCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), sum{} {}
 | 
|---|
| [8f74a6a] | 103 | 
 | 
|---|
| [1cb7fab2] | 104 |                                 virtual void print(std::ostream & os) { os << sum / count; }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |                                 inline void push(T value) { if(!enabled) return; sum += value; count++; }
 | 
|---|
| [8f74a6a] | 107 | 
 | 
|---|
| [1cb7fab2] | 108 |                         protected:
 | 
|---|
 | 109 |                                 virtual ~AverageCounter() = default;
 | 
|---|
| [8f74a6a] | 110 | 
 | 
|---|
| [1cb7fab2] | 111 |                         private:
 | 
|---|
 | 112 |                                 T sum;
 | 
|---|
 | 113 |                                 size_t count = 1;
 | 
|---|
 | 114 |                         };
 | 
|---|
| [351c519] | 115 | 
 | 
|---|
| [1cb7fab2] | 116 |                         template<typename T>
 | 
|---|
 | 117 |                         class MaxCounter : public Base::Tree<top> {
 | 
|---|
 | 118 |                         public:
 | 
|---|
 | 119 |                                 MaxCounter(const char * const name ) : Base::Tree<top>(name), max{} {}
 | 
|---|
 | 120 |                                 MaxCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), max{} {}
 | 
|---|
| [351c519] | 121 | 
 | 
|---|
| [1cb7fab2] | 122 |                                 virtual void print(std::ostream & os) { os << max; }
 | 
|---|
| [351c519] | 123 | 
 | 
|---|
| [1cb7fab2] | 124 |                                 inline void push(T value) { if(!enabled) return; max = std::max(max, value); }
 | 
|---|
| [351c519] | 125 | 
 | 
|---|
| [1cb7fab2] | 126 |                         protected:
 | 
|---|
 | 127 |                                 virtual ~MaxCounter() = default;
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |                         private:
 | 
|---|
 | 130 |                                 T max;
 | 
|---|
 | 131 |                         };
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |                         template<typename counter_t>
 | 
|---|
 | 134 |                         counter_t * build(const char * const name) {
 | 
|---|
 | 135 |                                 if(!enabled) return nullptr;
 | 
|---|
 | 136 |                                 return new counter_t(name);
 | 
|---|
 | 137 |                         }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |                         template<typename counter_t>
 | 
|---|
 | 140 |                         counter_t * build(const char * const name, Base::Tree<top> * parent) {
 | 
|---|
 | 141 |                                 if(!enabled) return nullptr;
 | 
|---|
 | 142 |                                 return new counter_t(name, parent);
 | 
|---|
 | 143 |                         }
 | 
|---|
 | 144 | #               endif
 | 
|---|
| [8f74a6a] | 145 |         }
 | 
|---|
 | 146 | }
 | 
|---|