MoCSI API Reference
Loading...
Searching...
No Matches
RuntimeProgressTool.h
Go to the documentation of this file.
1#ifndef RUNTIME_PROGRESS_TOOL_H
2#define RUNTIME_PROGRESS_TOOL_H
3
4#define VERSION "6"
5
6#include <chrono>
7#include <iostream>
8#include <memory>
9#include <string>
10#include <valarray>
11
12#include "../../src/GenericManagingModule.h"
13#include "../../src/ModuleFactory.h"
14
39template <typename T>
41{
42 private:
43 static bool m_registered;
44 int iteration_interval;
45 int total_time_steps;
46 std::string m_ini_filepath{"tools/RuntimeProgressTool.ini"};
47 std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
48 std::chrono::time_point<std::chrono::high_resolution_clock> last_call_time;
49
50 void _calculate_iteration_time();
51
52 public:
54 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
55 bool exec(std::string_view param) override; // main functionality of the module
56
57 bool init() override { return true; };
58 bool preTimeStep() override;
59 bool postNonLinIter() override { return true; };
60 bool postTimeStep() override;
61 bool output() override;
62
63 void checkProgress();
64
65 std::vector<std::string> getChainInsertion() const override
66 {
67 return this->ini_file_data.getStringVectorParameters("chain_insertion");
68 };
69
70 static std::shared_ptr<GenericManagingModule<T>> createMethode(SimulationClassBase<T>* sim)
71 {
72 return std::make_shared<RuntimeProgressTool<T>>(sim);
73 };
74 static std::string getName() { return "RuntimeProgressTool"; };
75 std::string_view getNameLocal() const override { return "RuntimeProgressTool"; };
76};
77
78// ================= Implementation =================
79
80template <typename T>
83{
84 try
85 {
86 std::string ini_folder_path{
87 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
88 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
90 }
91 catch (const BadInput& e)
92 {
93 std::cerr << e.what() << '\n';
94 }
95}
96
97template <typename T>
99{
100 this->setSubmodules(all_submodules);
101 iteration_interval = this->ini_file_data.getDoubleParameters("iteration_interval");
102 total_time_steps =
103 std::ceil(this->sim->m_simulation_config.getDoubleParameters("time_max")
104 / this->sim->m_simulation_config.getDoubleParameters("time_delta"));
105 start_time = std::chrono::high_resolution_clock::now();
106 last_call_time = start_time;
107
108 return true;
109}
110
111template <typename T>
113{
114 if (param == "InitChain")
115 {
116 return init();
117 }
118 if (param == "PreTimeStepChain")
119 {
120 return preTimeStep();
121 }
122 if (param == "PostNonLinIterChain")
123 {
124 return postNonLinIter();
125 }
126 if (param == "PostTimeStepChain")
127 {
128 return postTimeStep();
129 }
130 if (param == "OutputChain")
131 {
132 return output();
133 }
134 return false;
135}
136
143template <typename T>
145{
146 // Using milliseconds as seconds can too quickly lead to time intervals of 0
147 // That's why multiplication by 1000 is done to still get the iterations per seconds.
148 std::chrono::time_point current_time{std::chrono::high_resolution_clock::now()};
149 std::chrono::duration passed_time_since_start{
150 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)};
151 std::chrono::duration passed_time_since_last_call{
152 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - last_call_time)};
153 // I'm using the ANSI escape code for color here, to highlight that this is a status related
154 // message and not outputs or errors. This only works on terminals supporting these escape
155 // codes.
156 std::cout << "\033[32m[RuntimeProgressTool] Time steps: " << this->sim->time_step << "/"
157 << total_time_steps << "\033[0m\n";
158
159 if (passed_time_since_last_call.count() == 0)
160 {
161 // Time intervall chosen to small for the current simulation.
162 }
163 else
164 {
165 std::cout << "\033[32m[RuntimeProgress] Simulation speed of "
166 << (iteration_interval * 1000 / passed_time_since_last_call.count())
167 << " it/s for the last interval. Simulation speed of "
168 << (this->sim->time_step * 1000 / passed_time_since_start.count())
169 << " it/s since start.\033[0m\n";
170 }
171 last_call_time = current_time;
172}
173
179template <typename T>
181{
182 if (this->sim->time_step % iteration_interval == 0)
183 {
184 _calculate_iteration_time();
185 }
186}
187
188template <typename T>
190{
191 if (this->sim->my_rank == 0)
192 {
193 checkProgress();
194 }
195 return true;
196}
197
198template <typename T>
200{
201 if (this->sim->my_rank == 0)
202 {
203 checkProgress();
204 }
205 return true;
206}
207
208template <typename T>
210{
211 if (this->sim->my_rank != 0)
212 {
213 return true;
214 }
215 std::chrono::time_point current_time{std::chrono::high_resolution_clock::now()};
216 std::chrono::duration passed_time_since_start{
217 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)};
218 std::cout << "\033[32m[RuntimeProgress] Total runtime of "
219 << (float)(passed_time_since_start.count() / 1000.0)
220 << " seconds since start.\033[0m\n";
221 return true;
222}
223
224template <typename T>
226 ModuleFactory<T>::registerModule(getName(), createMethode);
227
228#endif // RUNTIME_PROGRESS_TOOL_H
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
Abstract base class for the managing modules. Managing modules are the highest tier of modules and ar...
Definition GenericManagingModule.h:29
InputManager ini_file_data
Definition GenericManagingModule.h:38
SimulationClassBase< T > * sim
Definition GenericManagingModule.h:31
std::vector< std::string > m_generic_submodules
Definition GenericManagingModule.h:36
std::vector< std::string > getStringVectorParameters(const std::string &key) const
Definition InputManager.cpp:503
void loadUserInput(const std::string &user_ini_file_path)
Public function to load and merge user supplied ini files with the already stored data.
Definition InputManager.cpp:42
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:68
Monitors and reports the simulation progress and iterations per second.
Definition RuntimeProgressTool.h:41
bool init() override
Definition RuntimeProgressTool.h:57
bool postNonLinIter() override
Definition RuntimeProgressTool.h:59
bool output() override
Definition RuntimeProgressTool.h:209
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition RuntimeProgressTool.h:98
bool postTimeStep() override
Definition RuntimeProgressTool.h:199
RuntimeProgressTool(SimulationClassBase< T > *sim)
Definition RuntimeProgressTool.h:81
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Definition RuntimeProgressTool.h:70
std::string_view getNameLocal() const override
Definition RuntimeProgressTool.h:75
std::vector< std::string > getChainInsertion() const override
Definition RuntimeProgressTool.h:65
bool exec(std::string_view param) override
Definition RuntimeProgressTool.h:112
static std::string getName()
Definition RuntimeProgressTool.h:74
bool preTimeStep() override
Definition RuntimeProgressTool.h:189
void checkProgress()
Calls progress update printing function when the given interval of time steps have passed.
Definition RuntimeProgressTool.h:180