xerus
a general purpose tensor library
performanceData.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include <string>
28 #include <vector>
29 
30 #include "misc/timeMeasure.h"
31 #include "misc/histogram.h"
32 
33 #include "basic.h"
34 #include "tensorNetwork.h"
35 
36 namespace xerus {
37  template<bool isOperator> class TTNetwork;
38 
39  typedef TTNetwork<false> TTTensor;
41 
44  public:
45  struct DataPoint {
47  size_t elapsedTime;
48  double residual;
49  double error;
51  size_t flags;
52 
53  DataPoint(const size_t _itrCount, const size_t _time, const value_t _residual, const value_t _error, const TensorNetwork::RankTuple _ranks, const size_t _flags)
54  : iterationCount(_itrCount), elapsedTime(_time), residual(_residual), error(_error), ranks(_ranks), flags(_flags) {}
55  };
56 
57  const bool active;
58 
60 
61  using ErrorFunction = std::function<double(const TTTensor&)>;
63 
64  size_t startTime;
65  size_t stopTime;
66  std::vector<DataPoint> data;
67 
68  std::string additionalInformation;
69 
70 
71  explicit PerformanceData(const bool _printProgress = false, const bool _active = true) :
72  active(_active), printProgress(_printProgress), startTime(~0ul), stopTime(~0ul) {}
73 
74  explicit PerformanceData(const ErrorFunction& _errorFunction, const bool _printProgress = false, const bool _active = true) :
75  active(_active), printProgress(_printProgress), errorFunction(_errorFunction), startTime(~0ul), stopTime(~0ul) {}
76 
77  void start() {
78  using ::xerus::misc::operator<<;
79  if (active) {
80  if(printProgress) {
81  std::stringstream ss(additionalInformation);
82  while (ss) {
83  std::string line;
84  std::getline(ss, line);
86  }
87  }
88  startTime = misc::uTime();
89  }
90  }
91 
92  void stop_timer() {
93  if (active) {
94  stopTime = misc::uTime();
95  }
96  }
97 
98  void continue_timer() {
99  if (active) {
100  size_t currtime = misc::uTime();
101  startTime += currtime - stopTime;
102  stopTime = ~0ul;
103  }
104  }
105 
106  void reset() {
107  if (active) {
108  data.clear();
109  additionalInformation.clear();
110  startTime = ~0ul;
111  stopTime = ~0ul;
112  }
113  }
114 
115  size_t get_elapsed_time() const {
116  return misc::uTime() - startTime;
117  }
118 
119  size_t get_runtime() const {
120  if (stopTime != ~0ul) {
121  return stopTime - startTime;
122  } else {
123  return misc::uTime() - startTime;
124  }
125  }
126 
127  void add(const size_t _itrCount, const value_t _residual, const TensorNetwork::RankTuple _ranks = TensorNetwork::RankTuple(), const size_t _flags = 0);
128 
129  void add(const size_t _itrCount, const value_t _residual, const TTTensor& _x, const size_t _flags = 0);
130 
131  void add(const value_t _residual, const TensorNetwork::RankTuple _ranks = TensorNetwork::RankTuple(), const size_t _flags = 0);
132 
133  void add(const value_t _residual, const TTTensor& _x, const size_t _flags = 0);
134 
135  operator bool() const {
136  return active;
137  }
138 
140  template<class T>
141  PerformanceData& operator<<(const T &_info) {
142  using ::xerus::misc::operator<<;
143  if (active) {
144  additionalInformation += misc::to_string(_info);
145  if(printProgress) {
147  }
148  }
149  return *this;
150  }
151 
152  void dump_to_file(const std::string &_fileName) const;
153 
154  misc::LogHistogram get_histogram(const value_t _base, bool _assumeConvergence = false) const;
155  };
156 
158 }
size_t get_elapsed_time() const
#define XERUS_LOG_SHORT(lvl,...)
logs the message msg with severity level lvl, omits the current file name and line number ...
Definition: namedLogger.h:162
TTNetwork< true > TTOperator
Specialized TensorNetwork class used to represent TTTensor and TToperators.
The main namespace of xerus.
Definition: basic.h:37
std::string additionalInformation
std::function< double(const TTTensor &)> ErrorFunction
Storage class for the performance data collected during an algorithm (typically iteration count...
TensorNetwork::RankTuple ranks
TTNetwork< false > TTTensor
std::vector< DataPoint > data
size_t uTime()
: Returns the time since epoche in microseconds.
Definition: timeMeasure.cpp:30
PerformanceData NoPerfData
Header file for shorthand notations that are xerus specific but used throughout the library...
double value_t
The type of values to be used by xerus.
Definition: basic.h:43
void dump_to_file(const std::string &_fileName) const
std::vector< size_t > RankTuple
: Represention of the ranks of a TensorNetwork.
Definition: tensorNetwork.h:45
size_t get_runtime() const
static XERUS_force_inline std::string to_string(const bool obj)
Definition: stringFromTo.h:41
Header file for the histogram classes.
PerformanceData & operator<<(const T &_info)
The pipe operator allows to add everything that can be converted to string to the additional informat...
PerformanceData(const bool _printProgress=false, const bool _active=true)
A logarithmic histogram, i.e. the size of all buckets is given by a constant factor [x - x*base) ...
Definition: histogram.h:37
DataPoint(const size_t _itrCount, const size_t _time, const value_t _residual, const value_t _error, const TensorNetwork::RankTuple _ranks, const size_t _flags)
ErrorFunction errorFunction
Header file for basic time measurement functionality.
Header file for the TensorNetwork class.
misc::LogHistogram get_histogram(const value_t _base, bool _assumeConvergence=false) const
void add(const size_t _itrCount, const value_t _residual, const TensorNetwork::RankTuple _ranks=TensorNetwork::RankTuple(), const size_t _flags=0)
PerformanceData(const ErrorFunction &_errorFunction, const bool _printProgress=false, const bool _active=true)