Antares Simulator
Power System Simulator
optimization_statistics.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #ifndef __SOLVER_UTILS_STATISTICS_H__
22 #define __SOLVER_UTILS_STATISTICS_H__
23 
24 #include <atomic>
25 #include <cmath>
26 #include <string>
27 
29 {
30 private:
31  std::atomic<long long> totalSolveTime;
32  std::atomic<unsigned int> nbSolve;
33 
34  std::atomic<long long> totalUpdateTime;
35  std::atomic<unsigned int> nbUpdate;
36 
37 public:
38  void reset()
39  {
40  totalSolveTime = 0;
41  nbSolve = 0;
42  totalUpdateTime = 0;
43  nbUpdate = 0;
44  }
45 
47  {
48  this->reset();
49  }
50 
52  totalSolveTime(rhs.totalSolveTime.load()),
53  nbSolve(rhs.nbSolve.load()),
54  totalUpdateTime(rhs.totalUpdateTime.load()),
55  nbUpdate(rhs.nbUpdate.load())
56  {
57  }
58 
60  OptimizationStatistics& operator=(const OptimizationStatistics&) = delete;
61  OptimizationStatistics& operator=(OptimizationStatistics&&) = delete;
62 
63  void add(const OptimizationStatistics& other)
64  {
65  totalSolveTime += other.totalSolveTime;
66  totalUpdateTime += other.totalUpdateTime;
67  nbSolve += other.nbSolve;
68  nbUpdate += other.nbUpdate;
69  }
70 
71  void addUpdateTime(long long updateTime)
72  {
73  totalUpdateTime += updateTime;
74  nbUpdate++;
75  }
76 
77  void addSolveTime(long long solveTime)
78  {
79  totalSolveTime += solveTime;
80  nbSolve++;
81  }
82 
83  unsigned int getNbUpdate() const
84  {
85  return nbUpdate;
86  }
87 
88  long long getTotalSolveTime() const
89  {
90  return totalSolveTime;
91  }
92 
93  long long getTotalUpdateTime() const
94  {
95  return totalUpdateTime;
96  }
97 
98  double getAverageUpdateTime() const
99  {
100  if (nbUpdate == 0)
101  {
102  return 0.0;
103  }
104  return ((double)totalUpdateTime) / nbUpdate;
105  }
106 
107  double getAverageSolveTime() const
108  {
109  if (nbSolve == 0)
110  {
111  return 0.0;
112  }
113  return ((double)totalSolveTime) / nbSolve;
114  }
115 
116  std::string toString() const
117  {
118  return "Average solve time: " + std::to_string(std::lround(getAverageSolveTime())) + " ms, "
119  + "average update time: " + std::to_string(std::lround(getAverageUpdateTime()))
120  + " ms";
121  }
122 };
123 
124 #endif
Definition: optimization_statistics.h:29