Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
BasicCheckpointService.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/* BasicCheckpointService.cc (C) 2000-2024 */
9/* */
10/* Basic protection/recovery service. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/BasicReader.h"
15#include "arcane/std/internal/BasicWriter.h"
16
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/OStringStream.h"
19#include "arcane/utils/PlatformUtils.h"
20
21#include "arcane/core/IXmlDocumentHolder.h"
22#include "arcane/core/IParallelMng.h"
23#include "arcane/core/CheckpointService.h"
24#include "arcane/core/Directory.h"
25#include "arcane/core/IParallelReplication.h"
26#include "arcane/core/IVariableUtilities.h"
27#include "arcane/core/VerifierService.h"
28#include "arcane/core/IVariableMng.h"
29#include "arcane/core/CheckpointInfo.h"
30
31#include "arcane/std/ArcaneBasicCheckpoint_axl.h"
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36namespace Arcane
37{
38using namespace Arcane::impl;
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
46class ArcaneBasicCheckpointService
48{
49 public:
50
51 struct MetaData
52 {
53 int m_version = -1;
54 static MetaData parse(const String& meta_data, ITraceMng* tm)
55 {
56 auto doc_ptr = IXmlDocumentHolder::loadFromBuffer(meta_data.bytes(), "MetaData", tm);
57 ScopedPtrT<IXmlDocumentHolder> xml_doc(doc_ptr);
58 XmlNode root = xml_doc->documentNode().documentElement();
59 Integer version = root.attr("version").valueAsInteger();
60 if (version != 1)
61 ARCANE_THROW(ReaderWriterException, "Bad checkpoint metadata version '{0}' (expected 1)", version);
62 MetaData md;
63 md.m_version = version;
64 return md;
65 }
66 };
67
68 public:
69
70 explicit ArcaneBasicCheckpointService(const ServiceBuildInfo& sbi)
72 , m_write_index(0)
73 , m_writer(nullptr)
74 , m_reader(nullptr)
75 {}
76 IDataWriter* dataWriter() override { return m_writer; }
77 IDataReader* dataReader() override { return m_reader; }
78
79 void notifyBeginWrite() override;
80 void notifyEndWrite() override;
81 void notifyBeginRead() override;
82 void notifyEndRead() override;
83 void close() override {}
84 String readerServiceName() const override { return "ArcaneBasicCheckpointReader"; }
85
86 private:
87
88 Integer m_write_index;
89 BasicWriter* m_writer;
90 BasicReader* m_reader;
91
92 private:
93
94 String _defaultFileName()
95 {
96 info() << "USE DEFAULT FILE NAME index=" << currentIndex();
97 IParallelReplication* pr = subDomain()->parallelMng()->replication();
98
99 String buf = "arcanedump";
100 if (pr->hasReplication()) {
101 buf = buf + "_r";
102 buf = buf + pr->replicationRank();
103 }
104 info() << "FILE_NAME is " << buf;
105 return buf;
106 }
107 Directory _defaultDirectory()
108 {
109 return Directory(baseDirectoryName());
110 }
111};
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
122class ArcaneBasic2CheckpointService
123: public ArcaneBasicCheckpointService
124{
125 public:
126
127 explicit ArcaneBasic2CheckpointService(const ServiceBuildInfo& sbi)
128 : ArcaneBasicCheckpointService(sbi)
129 {}
130 String readerServiceName() const override { return "ArcaneBasic2CheckpointReader"; }
131};
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
138{
139 String meta_data_str = readerMetaData();
140 MetaData md = MetaData::parse(meta_data_str, traceMng());
141
142 info() << " GET META DATA READER " << readerMetaData()
143 << " version=" << md.m_version
144 << " filename=" << fileName();
145
146 String filename = fileName();
147 if (filename.null()) {
148 Directory dump_dir(_defaultDirectory());
149 filename = dump_dir.file(_defaultFileName());
150 setFileName(filename);
151 }
152 filename = filename + "_n" + currentIndex();
153 info() << " READ CHECKPOINT FILENAME = " << filename;
154 IParallelMng* pm = subDomain()->parallelMng();
155 IApplication* app = subDomain()->application();
156 bool want_parallel = false;
157 m_reader = new BasicReader(app, pm, A_NULL_RANK, filename, want_parallel);
158 m_reader->initialize();
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
166{
167 delete m_reader;
168 m_reader = nullptr;
169}
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
176{
177 auto open_mode = BasicReaderWriterCommon::OpenModeAppend;
178 Integer write_index = checkpointTimes().size();
179 --write_index;
180 if (write_index == 0)
181 open_mode = BasicReaderWriterCommon::OpenModeTruncate;
182
183 String filename = fileName();
184 if (filename.null()) {
185 Directory dump_dir(_defaultDirectory());
186 filename = dump_dir.file(_defaultFileName());
187 setFileName(filename);
188 }
189 filename = filename + "_n" + write_index;
190
191 Int32 version = 2;
192 Ref<IDataCompressor> data_compressor;
193 if (options()) {
194 version = options()->formatVersion();
195 // Only use compression from version 3 onwards because it is
196 // incompatible with older versions
197 if (version >= 3) {
198 data_compressor = options()->dataCompressor.instanceRef();
199 }
200 }
201
202 info() << "Writing checkpoint with 'ArcaneBasicCheckpointService'"
203 << " version=" << version
204 << " filename='" << filename << "'\n";
205
207
208 IParallelMng* pm = subDomain()->parallelMng();
209 IApplication* app = subDomain()->application();
210 bool want_parallel = pm->isParallel();
211 want_parallel = false;
212 m_writer = new BasicWriter(app, pm, filename, open_mode, version, want_parallel);
213 m_writer->setDataCompressor(data_compressor);
214 m_writer->initialize();
215}
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
222{
223 OStringStream ostr;
224 ostr() << "<infos";
225 const int meta_data_version = 1;
226 ostr() << " version='" << meta_data_version << "'";
227 ostr() << "/>\n";
228 setReaderMetaData(ostr.str());
229 ++m_write_index;
230 delete m_writer;
231 m_writer = nullptr;
232}
233
234/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
236
240class ArcaneBasic2CheckpointReaderService
241: public AbstractService
242, public ICheckpointReader2
243{
244 public:
245
246 explicit ArcaneBasic2CheckpointReaderService(const ServiceBuildInfo& sbi)
247 : AbstractService(sbi)
248 , m_application(sbi.application())
249 , m_reader(nullptr)
250 {}
251 IDataReader2* dataReader() override { return m_reader; }
252
253 void notifyBeginRead(const CheckpointReadInfo& cri) override;
254 void notifyEndRead() override;
255
256 private:
257
258 IApplication* m_application;
259 BasicReader* m_reader;
260
261 private:
262
263 String _defaultFileName(const CheckpointInfo& ci)
264 {
265 Integer index = ci.checkpointIndex();
266 bool has_replication = ci.nbReplication() > 1;
267 Int32 replication_rank = ci.replicationRank();
268 info() << "USE DEFAULT FILE NAME index=" << index;
269
270 String buf = "arcanedump";
271 if (has_replication) {
272 buf = buf + "_r";
273 buf = buf + replication_rank;
274 }
275 info() << "FILE_NAME is " << buf;
276 return buf;
277 }
278};
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
285{
286 const CheckpointInfo& ci = cri.checkpointInfo();
287 String reader_meta_data_str = ci.readerMetaData();
288 auto md = ArcaneBasicCheckpointService::MetaData::parse(reader_meta_data_str, traceMng());
289
290 info() << "Basic2CheckpointReader GET META DATA READER " << reader_meta_data_str
291 << " version=" << md.m_version;
292
293 Directory dump_dir(ci.directory());
294 String filename = dump_dir.file(_defaultFileName(ci));
295 filename = filename + "_n" + ci.checkpointIndex();
296 ;
297 info() << " READ CHECKPOINT FILENAME = " << filename;
298 IParallelMng* pm = cri.parallelMng();
299 bool want_parallel = false;
300 m_reader = new BasicReader(m_application, pm, ci.subDomainRank(), filename, want_parallel);
301 m_reader->initialize();
302}
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
309{
310 delete m_reader;
311 m_reader = nullptr;
312}
313
314/*---------------------------------------------------------------------------*/
315/*---------------------------------------------------------------------------*/
316
318 ServiceProperty("ArcaneBasicCheckpointWriter", ST_SubDomain | ST_CaseOption),
320
322 ServiceProperty("ArcaneBasicCheckpointReader", ST_SubDomain),
324
326 ServiceProperty("ArcaneBasic2CheckpointWriter", ST_SubDomain | ST_CaseOption),
328
330 ServiceProperty("ArcaneBasic2CheckpointReader", ST_Application),
332
333/*---------------------------------------------------------------------------*/
334/*---------------------------------------------------------------------------*/
335
336} // End namespace Arcane
337
338/*---------------------------------------------------------------------------*/
339/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_SERVICE_INTERFACE(ainterface)
Macro to declare an interface when registering a service.
AbstractService(const ServiceBuildInfo &)
Constructor from a ServiceBuildInfo.
ArcaneArcaneBasicCheckpointObject(const Arcane::ServiceBuildInfo &sbi)
Constructeur.
CaseOptionsArcaneBasicCheckpoint * options() const
Options du jeu de données du service.
void notifyBeginRead(const CheckpointReadInfo &cri) override
Notifies that a protection will be read with information from checkpoint_info.
void notifyEndRead() override
Notifies the end of reading a protection.
IDataReader2 * dataReader() override
Returns the data reader associated with this protection reader.
Basic protection/recovery (version 2).
String readerServiceName() const override
Name of the reader service associated with this writer.
Basic protection/recovery (version 1).
String readerServiceName() const override
Name of the reader service associated with this writer.
void notifyEndRead() override
Notifies that a protection has just been read.
void notifyBeginRead() override
Notifies that a protection will be read with current parameters.
void notifyBeginWrite() override
Notifies that a checkpoint is going to be written with the current parameters.
IDataReader * dataReader() override
Returns the associated reader.
void notifyEndWrite() override
Notifies that a checkpoint has just been written.
IDataWriter * dataWriter() override
Returns the associated writer.
void close() override
Closes the checkpoints.
Information about a checkpoint.
Checkpoint reading information.
String readerMetaData() const override
Metadata for the reader associated with this writer.
String baseDirectoryName() const override
Name of the checkpoint base directory.
void setReaderMetaData(const String &s) override
Metadata associated with this reader.
void setFileName(const String &file_name) override
Sets the name of the checkpoint file.
RealConstArrayView checkpointTimes() const override
Checkpoint times.
String fileName() const override
Name of the checkpoint file.
constexpr Integer size() const noexcept
Number of elements in the array.
Class managing a directory.
Definition Directory.h:36
String file(const String &file_name) const override
Returns the full path of the file file_name in the directory.
Definition Directory.cc:120
Application interface.
Interface for the protection/recovery reading service (V2).
Interface for the protection/recovery reading service.
Interface of the checkpoint/recovery write service.
Interface for reading data of a variable (Version 2).
Interface for reading variable data.
Definition IDataReader.h:35
Interface for writing variable data.
Definition IDataWriter.h:45
Interface of the parallelism manager for a subdomain.
virtual IParallelReplication * replication() const =0
Replication information.
virtual bool isParallel() const =0
Returns true if the execution is parallel.
Brief information on parallel subdomain replication.
virtual bool hasReplication() const =0
Indicates if replication is active.
virtual Int32 replicationRank() const =0
Rank in the replication (from 0 to nbReplication()-1).
virtual IParallelMng * parallelMng()=0
Returns the parallelism manager.
static IXmlDocumentHolder * loadFromBuffer(Span< const Byte > buffer, const String &name, ITraceMng *tm)
Loads an XML document.
Definition DomUtils.cc:425
Output stream linked to a String.
Exception in a reader or writer.
Reference to an instance.
Encapsulation of an automatically destructing pointer.
Definition ScopedPtr.h:44
IApplication * application() const
Access to the associated IApplication.
Structure containing the information to create a service.
Service creation properties.
bool null() const
Returns true if the string is null.
Definition String.cc:306
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
Node of a DOM tree.
Definition XmlNode.h:51
XmlNode attr(const String &name, bool throw_exception=false) const
Returns the attribute of name name.
Definition XmlNode.cc:257
Integer valueAsInteger(bool throw_exception=false) const
Node value converted to integer.
Definition XmlNode.cc:441
Simple read/write.
Definition BasicWriter.h:38
#define ARCANE_REGISTER_SERVICE(aclass, a_service_property,...)
Macro for registering a service.
bool recursiveCreateDirectory(const String &dir_name)
Create a directory.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
@ ST_Application
The service is used at the application level.
@ ST_CaseOption
The service is used at the dataset level.
@ ST_SubDomain
The service is used at the subdomain level.
std::int32_t Int32
Signed integer type of 32 bits.