Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
ArcaneCheckpointModule.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ArcaneCheckpointModule.cc (C) 2000-2026 */
9/* */
10/* Module managing protections/restorations. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/utils/ScopedPtr.h"
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/Convert.h"
19
20#include "arcane/core/ISubDomain.h"
21#include "arcane/core/EntryPoint.h"
22#include "arcane/core/Timer.h"
23#include "arcane/core/ITimeHistoryMng.h"
24#include "arcane/core/ModuleFactory.h"
25#include "arcane/core/ServiceUtils.h"
26#include "arcane/core/ICheckpointWriter.h"
27#include "arcane/core/ICheckpointMng.h"
28#include "arcane/core/Directory.h"
29#include "arcane/core/IParallelMng.h"
30
31#include "arcane/core/OutputChecker.h"
32#include "arcane/std/ArcaneCheckpoint_axl.h"
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
46class ArcaneCheckpointModule
47: public ArcaneArcaneCheckpointObject
48{
49 public:
50
51 ArcaneCheckpointModule(const ModuleBuilder& cb);
52 ~ArcaneCheckpointModule();
53
54 public:
55
56 virtual VersionInfo versionInfo() const { return VersionInfo(0, 9, 1); }
57
58 public:
59
60 virtual void checkpointCheckAndWriteData();
61 virtual void checkpointStartInit();
62 virtual void checkpointInit();
63 virtual void checkpointExit();
64
65 private:
66
67 OutputChecker m_output_checker;
68 Timer* m_checkpoint_timer;
69 ICheckpointWriter* m_checkpoint_writer;
70 String m_checkpoint_dirname;
71
72 private:
73
74 void _doCheckpoint(bool save_history);
75 void _dumpStats();
76 void _getCheckpointService();
77 void _setDirectoryName();
78 bool _checkHasOutput();
79};
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84ARCANE_REGISTER_MODULE_ARCANECHECKPOINT(ArcaneCheckpointModule);
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89ArcaneCheckpointModule::
90ArcaneCheckpointModule(const ModuleBuildInfo& mb)
92, m_output_checker(mb.subDomain(), "CheckpointRestart")
93, m_checkpoint_timer(0)
94, m_checkpoint_writer(0)
95, m_checkpoint_dirname(".")
96{
97 m_checkpoint_timer = new Timer(mb.subDomain(), "Checkpoint", Timer::TimerReal);
98 m_output_checker.assignIteration(&m_next_iteration, &options()->period);
99 m_output_checker.assignGlobalTime(&m_next_global_time, &options()->frequency);
100 m_output_checker.assignCPUTime(&m_next_cpu_time, &options()->frequencyCpu);
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
106ArcaneCheckpointModule::
107~ArcaneCheckpointModule()
108{
109 delete m_checkpoint_timer;
110}
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115void ArcaneCheckpointModule::
116checkpointInit()
117{
118 // Unlike other output types, the CPU time for the next output must be reset
119 // to zero because the CPU time used belongs to the current execution.
120 m_next_cpu_time = options()->frequencyCpu();
121 info() << " -------------------------------------------";
122 info() << "| PROTECTION-REPRISE |";
123 info() << " -------------------------------------------";
124 info() << " ";
125 //info() << " Uses the service '" << options()->checkpointServiceName()
126 // << "' for protections";
127 info() << " ";
128 m_output_checker.initialize();
129 info() << " ";
130 // If protections are active, check that the specified service
131 // exists
132 if (options()->doDumpAtEnd()) {
133 info() << "Protection required at the end of computations";
134 _getCheckpointService();
135 }
136}
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
141bool ArcaneCheckpointModule::
142_checkHasOutput()
143{
144 Real old_time = m_global_old_time();
145 Real current_time = m_global_time();
146 Integer iteration = m_global_iteration();
148
149 bool do_output = m_output_checker.check(old_time, current_time, iteration, cpu_used);
150 return do_output;
151}
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156void ArcaneCheckpointModule::
157checkpointStartInit()
158{
159 m_next_global_time = 0.;
161 m_next_cpu_time = 0;
162
163 // Initializes the output checker. This must be done here if we
164 // want to save things at iteration 1.
165 _checkHasOutput();
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
177{
178 if (!options()->doDumpAtEnd())
179 return;
180
181 _doCheckpoint(false);
182 _dumpStats();
183
184 if (m_checkpoint_writer)
185 m_checkpoint_writer->close();
186 m_checkpoint_writer = 0;
187}
188
189/*---------------------------------------------------------------------------*/
190/*---------------------------------------------------------------------------*/
191
192void ArcaneCheckpointModule::
193_dumpStats()
194{
195 // Displays execution statistics
196 Real total_time = m_checkpoint_timer->totalTime();
197 info() << "Total time spent in protection output (second): " << total_time;
198 Integer nb_time = m_checkpoint_timer->nbActivated();
199 if (nb_time != 0)
200 info() << "Average time per output (second): " << total_time / nb_time
201 << " (for " << nb_time << " outputs";
202}
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
207void ArcaneCheckpointModule::
208_setDirectoryName()
209{
210 Directory export_dir = subDomain()->storageDirectory();
211 if (export_dir.path().null())
212 export_dir = subDomain()->exportDirectory();
213
214 Directory output_directory = Directory(export_dir, "protection");
215 IParallelMng* pm = parallelMng();
216 if (pm->isMasterIO())
217 output_directory.createDirectory();
218 pm->barrier();
219
220 m_checkpoint_dirname = output_directory.path();
221}
222
223/*---------------------------------------------------------------------------*/
224/*---------------------------------------------------------------------------*/
225
232_doCheckpoint(bool save_history)
233{
234 {
235 Timer::Sentry sentry(m_checkpoint_timer);
236 Timer::Phase tp(subDomain(), TP_InputOutput);
237 if (!m_checkpoint_writer)
238 _getCheckpointService();
239 if (m_checkpoint_writer) {
240 Integer nb_checkpoint = m_checkpoints_time.size();
241 m_checkpoints_time.resize(nb_checkpoint + 1);
242 Real checkpoint_time = m_global_time();
243 m_checkpoints_time[nb_checkpoint] = checkpoint_time;
244 m_checkpoint_writer->setCheckpointTimes(m_checkpoints_time);
245 m_checkpoint_writer->setBaseDirectoryName(m_checkpoint_dirname);
246 info() << "**** Protection active at time " << checkpoint_time
247 << " directory=" << m_checkpoint_dirname
248 << " number " << nb_checkpoint << " ******";
249 subDomain()->checkpointMng()->writeDefaultCheckpoint(m_checkpoint_writer);
250 }
251 }
252 info() << "Checkpoint write time (second): "
253 << m_checkpoint_timer->lastActivationTime();
254
255 // Saves histories
256 if (save_history)
258}
259
260/*---------------------------------------------------------------------------*/
261/*---------------------------------------------------------------------------*/
262
263void ArcaneCheckpointModule::
264_getCheckpointService()
265{
266 ICheckpointWriter* checkpoint = options()->checkpointService();
267 if (!checkpoint) {
268 StringUniqueArray valid_values;
269 options()->checkpointService.getAvailableNames(valid_values);
270 pfatal() << "Protections required but protection/restore service selected ("
271 << options()->checkpointService.serviceName() << ") not available "
272 << "(valid values: " << String::join(", ", valid_values) << ")";
273 }
274 m_checkpoint_writer = checkpoint;
275 _setDirectoryName();
276}
277
278/*---------------------------------------------------------------------------*/
279/*---------------------------------------------------------------------------*/
280
287{
288 bool do_output = _checkHasOutput();
289 if (!do_output)
290 return;
291
292 info() << "Protection required.";
293 // TEMPORARY:
294 // Unlike end-of-execution checkpointing, this occurs at the end of the
295 // time loop, but before the iteration number is incremented. If we resume
296 // at this time, the iteration number will be incorrect. To correct this
297 // problem, we increment this number before the checkpoint and reset it
298 // to the correct value afterward.
299 // SDC: this problem has not been observed. It seems to me that it is the
300 // current iteration number that must be saved. Changed for a restart issue
301 // in an IFPEN application (internally Bugzilla 778.
302 // m_global_iteration = m_global_iteration() + 1;
303 _doCheckpoint(true);
304 // m_global_iteration = m_global_iteration() - 1;
305}
306
307/*---------------------------------------------------------------------------*/
308/*---------------------------------------------------------------------------*/
309
310} // End namespace Arcane
311
312/*---------------------------------------------------------------------------*/
313/*---------------------------------------------------------------------------*/
Generation de la classe de base du Module.
CaseOptionsArcaneCheckpoint * options() const
Options du jeu de données du module.
Arcane::VariableScalarInteger m_next_iteration
Variables du module.
ISubDomain * subDomain() const override
Sub-domain associated with the module.
IParallelMng * parallelMng() const override
Message passing parallelism manager.
virtual void checkpointCheckAndWriteData()
Checks if a checkpoint should be performed at this moment and performs it if necessary.
virtual VersionInfo versionInfo() const
Module version.
void _doCheckpoint(bool save_history)
Performs a checkpoint.
virtual void checkpointExit()
End-of-calculation operations.
VariableScalarInt32 m_global_iteration
Current iteration.
VariableScalarReal m_global_cpu_time
CPU time used (in seconds).
VariableScalarReal m_global_time
Current time.
VariableScalarReal m_global_old_time
Time previous to the current time.
virtual void writeDefaultCheckpoint(ICheckpointWriter *writer)=0
Writes a checkpoint using the writer.
Interface of the checkpoint/recovery write service.
virtual const IDirectory & storageDirectory() const =0
Base directory for exports requiring archiving.
virtual ICheckpointMng * checkpointMng() const =0
Protection manager.
virtual ITimeHistoryMng * timeHistoryMng()=0
Returns the history manager.
virtual const IDirectory & exportDirectory() const =0
Base directory for exports.
virtual void dumpHistory(bool is_verbose)=0
Saves the history.
Manages outputs based on physical time, CPU time, or a number of iterations.
Positions the phase of the currently executing action.
Definition Timer.h:142
Sentinel for the timer. The sentinel associated with a timer allows it to be triggered upon its const...
Definition Timer.h:90
Management of a timer.
Definition Timer.h:63
Integer nbActivated() const
Returns the number of times the timer has been activated.
Definition Timer.h:246
@ TimerReal
Timer using real time.
Definition Timer.h:77
Real totalTime() const
Returns the total time (in seconds) spent in the timer.
Definition Timer.h:240
TraceMessage pfatal() const
Flow for a parallel fatal error message.
TraceMessage info() const
Flow for an information message.
Information about a version.
Definition VersionInfo.h:47
Integer toInteger(Real r)
Converts a Real to Integer.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
UniqueArray< String > StringUniqueArray
Dynamic 1D array of strings.
Definition UtilsTypes.h:353