| 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 | // Counter.h --
 | 
|---|
| 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>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Common/Stats/Base.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #if defined( NO_STATISTICS )
 | 
|---|
| 24 |         #define NO_COUNTER_STATISTICS
 | 
|---|
| 25 | #endif
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace Stats {
 | 
|---|
| 28 |         namespace Counters {
 | 
|---|
| 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:
 | 
|---|
| 39 |                                 inline void operator++() {}
 | 
|---|
| 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;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                         extern Base::TreeTop top;
 | 
|---|
| 69 | 
 | 
|---|
| 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) {}
 | 
|---|
| 74 | 
 | 
|---|
| 75 |                                 virtual void print(std::ostream & os) override { os << ""; }
 | 
|---|
| 76 |                         protected:
 | 
|---|
| 77 |                                 virtual ~CounterGroup() = default;
 | 
|---|
| 78 |                         };
 | 
|---|
| 79 | 
 | 
|---|
| 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) {}
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                                 virtual void print(std::ostream & os) override { os << count; }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |                                 inline void operator++()             { if(!enabled) return; count++;        }
 | 
|---|
| 88 |                                 inline void operator++(int)          { if(!enabled) return; count++;        }
 | 
|---|
| 89 |                                 inline void operator+=(size_t value) { if(!enabled) return; count += value; }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |                         protected:
 | 
|---|
| 92 |                                 virtual ~SimpleCounter() = default;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |                         private:
 | 
|---|
| 95 |                                 size_t count = 0;
 | 
|---|
| 96 |                         };
 | 
|---|
| 97 | 
 | 
|---|
| 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{} {}
 | 
|---|
| 103 | 
 | 
|---|
| 104 |                                 virtual void print(std::ostream & os) { os << sum / count; }
 | 
|---|
| 105 | 
 | 
|---|
| 106 |                                 inline void push(T value) { if(!enabled) return; sum += value; count++; }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |                         protected:
 | 
|---|
| 109 |                                 virtual ~AverageCounter() = default;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |                         private:
 | 
|---|
| 112 |                                 T sum;
 | 
|---|
| 113 |                                 size_t count = 1;
 | 
|---|
| 114 |                         };
 | 
|---|
| 115 | 
 | 
|---|
| 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{} {}
 | 
|---|
| 121 | 
 | 
|---|
| 122 |                                 virtual void print(std::ostream & os) { os << max; }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |                                 inline void push(T value) { if(!enabled) return; max = std::max(max, value); }
 | 
|---|
| 125 | 
 | 
|---|
| 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
 | 
|---|
| 145 |         }
 | 
|---|
| 146 | }
 | 
|---|