OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Statistics.h
Go to the documentation of this file.
1 //
2 // Class Statistics
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #ifndef __STATISTICS_H__
22 #define __STATISTICS_H__
23 
24 #include <map>
25 #include <string>
26 #include <iostream>
27 
28 template<typename T>
29 class Statistics {
30 
31 public:
32 
33  Statistics(std::string name) : stat_name_(name) {}
35 
36  void registerStatistic(std::string name, T initial_value = 0) {
37  std::pair<statistics_iterator_t, bool> statistic_position;
38  statistic_position = statistics_.insert(std::pair<std::string, T>(name, initial_value));
39 
40  if(statistic_position.second == false)
41  std::cout << "Statistic " << statistic_position.first->second << " already exists!" << std::endl;
42  }
43 
44  void changeStatisticBy(std::string name, T change_by_value) {
45  statistics_iterator_t name_at;
46  name_at = statistics_.find(name);
47 
48  if(name_at != statistics_.end())
49  statistics_[name] += change_by_value;
50  else
51  std::cout << "Statistic " << name << " not registered!" << std::endl;
52  }
53 
54  T getStatisticValue(std::string name) {
55  return statistics_[name];
56  }
57 
58  void dumpStatistics() {
59  std::cout << "Statistics: " << stat_name_ << std::endl;
60 
61  T sum = 0;
62  for(std::pair<std::string, T> stat : statistics_) {
63  sum += stat.second;
64  std::cout << "\t" << stat.first << " = " << stat.second << std::endl;
65  }
66 
67  std::cout << "_________________________" << std::endl;
68  std::cout << "Total: " << sum << std::endl;
69  }
70 
71  void dumpStatistics(std::ostringstream &stream) {
72  stream << "Statistics: " << stat_name_ << std::endl;
73 
74  T sum = 0;
75  for (std::pair<std::string, T> stat : statistics_) {
76  sum += stat.second;
77  stream << "\t" << stat.first << " = " << stat.second << std::endl;
78  }
79 
80  stream << "_________________________" << std::endl;
81  stream << "Total: " << sum << std::endl;
82  }
83 
84 
85 private:
86 
87  typedef typename std::map<std::string, T> statistics_t;
89 
91  std::string stat_name_;
92 
93 };
94 
95 #endif
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
std::string::iterator iterator
Definition: MSLang.h:16
std::string stat_name_
Definition: Statistics.h:91
Statistics(std::string name)
Definition: Statistics.h:33
T getStatisticValue(std::string name)
Definition: Statistics.h:54
void registerStatistic(std::string name, T initial_value=0)
Definition: Statistics.h:36
std::map< std::string, T >::iterator statistics_iterator_t
Definition: Statistics.h:88
std::map< std::string, T > statistics_t
Definition: Statistics.h:87
statistics_t statistics_
Definition: Statistics.h:90
void dumpStatistics()
Definition: Statistics.h:58
void dumpStatistics(std::ostringstream &stream)
Definition: Statistics.h:71
void changeStatisticBy(std::string name, T change_by_value)
Definition: Statistics.h:44