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 RUNTIME_PROGRESS_TOOL_VERSION "8"
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 << "[RuntimeProgressTool]: " << e.what() << '\n';
94 throw BadInput(static_cast<std::string>("[RuntimeProgressTool]: ")
95 + static_cast<std::string>(e.what()));
96 }
97}
98
99template <typename T>
101{
102 try
103 {
104 this->setSubmodules(all_submodules);
105 iteration_interval = this->ini_file_data.getDoubleParameters("iteration_interval");
106 total_time_steps =
107 std::ceil(this->sim->m_simulation_config.getDoubleParameters("time_max")
108 / this->sim->m_simulation_config.getDoubleParameters("time_delta"));
109 start_time = std::chrono::high_resolution_clock::now();
110 last_call_time = start_time;
111 }
112 catch (const BadInput& e)
113 {
114 std::cerr << "[RuntimeProgressTool]: " << e.what() << '\n';
115 throw BadInput(static_cast<std::string>("[RuntimeProgressTool]: ")
116 + static_cast<std::string>(e.what()));
117 }
118 return true;
119}
120
121template <typename T>
123{
124 if (param == "InitChain")
125 {
126 return init();
127 }
128 if (param == "PreTimeStepChain")
129 {
130 return preTimeStep();
131 }
132 if (param == "PostNonLinIterChain")
133 {
134 return postNonLinIter();
135 }
136 if (param == "PostTimeStepChain")
137 {
138 return postTimeStep();
139 }
140 if (param == "OutputChain")
141 {
142 return output();
143 }
144 return false;
145}
146
153template <typename T>
155{
156 // Using milliseconds as seconds can too quickly lead to time intervals of 0
157 // That's why multiplication by 1000 is done to still get the iterations per seconds.
158 std::chrono::time_point current_time{std::chrono::high_resolution_clock::now()};
159 std::chrono::duration passed_time_since_start{
160 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)};
161 std::chrono::duration passed_time_since_last_call{
162 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - last_call_time)};
163 // I'm using the ANSI escape code for color here, to highlight that this is a status related
164 // message and not outputs or errors. This only works on terminals supporting these escape
165 // codes.
166 std::cout << "\033[32m[RuntimeProgressTool]: Time steps: " << this->sim->time_step << "/"
167 << total_time_steps << "\033[0m\n";
168
169 if (passed_time_since_last_call.count() == 0)
170 {
171 // Time intervall chosen to small for the current simulation.
172 }
173 else
174 {
175 std::cout << "\033[32m[RuntimeProgressTool]: Simulation speed of "
176 << (iteration_interval * 1000 / passed_time_since_last_call.count())
177 << " it/s for the last interval. Simulation speed of "
178 << (this->sim->time_step * 1000 / passed_time_since_start.count())
179 << " it/s since start.\033[0m\n";
180 }
181 last_call_time = current_time;
182}
183
189template <typename T>
191{
192 if (this->sim->time_step % iteration_interval == 0)
193 {
194 _calculate_iteration_time();
195 }
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 checkProgress();
214 }
215 return true;
216}
217
218template <typename T>
220{
221 if (this->sim->my_rank != 0)
222 {
223 return true;
224 }
225 std::chrono::time_point current_time{std::chrono::high_resolution_clock::now()};
226 std::chrono::duration passed_time_since_start{
227 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)};
228 std::cout << "\033[32m[RuntimeProgress] Total runtime of "
229 << (float)(passed_time_since_start.count() / 1000.0)
230 << " seconds since start.\033[0m\n";
231 return true;
232}
233
234template <typename T>
236 ModuleFactory<T>::registerModule(getName(), createMethode);
237
238#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:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
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:508
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:45
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:75
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:219
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition RuntimeProgressTool.h:100
bool postTimeStep() override
Definition RuntimeProgressTool.h:209
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:122
static std::string getName()
Definition RuntimeProgressTool.h:74
bool preTimeStep() override
Definition RuntimeProgressTool.h:199
void checkProgress()
Calls progress update printing function when the given interval of time steps have passed.
Definition RuntimeProgressTool.h:190