Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
BasicGenericWriter.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/* BasicGenericWriter.cc (C) 2000-2026 */
9/* */
10/* Simple writing for checkpoints/recovery. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/BasicWriter.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/IDataCompressor.h"
18#include "arcane/utils/JSONWriter.h"
19#include "arcane/utils/Ref.h"
20
21#include "arcane/core/IXmlDocumentHolder.h"
22#include "arcane/core/IParallelMng.h"
23#include "arcane/core/IIOMng.h"
24#include "arcane/core/IApplication.h"
25#include "arcane/core/ISerializedData.h"
26#include "arcane/core/IRessourceMng.h"
27#include "arcane/core/ItemGroup.h"
28
29#include <fstream>
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34namespace Arcane::impl
35{
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40BasicGenericWriter::
41BasicGenericWriter(IApplication* app, Int32 version,
42 Ref<KeyValueTextWriter> text_writer)
43: TraceAccessor(app->traceMng())
44, m_application(app)
45, m_version(version)
46, m_text_writer(text_writer)
47{
48}
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
53void BasicGenericWriter::
54initialize(const String& path, Int32 rank)
55{
56 if (!m_text_writer)
57 ARCANE_FATAL("Null text writer");
58 m_path = path;
59 m_rank = rank;
60}
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
65void BasicGenericWriter::
66writeData(const String& var_full_name, const ISerializedData* sdata,
67 const String& comparison_hash, bool is_save_values)
68{
69 //TODO: Verify that initialize() has been called.
70 auto var_data_info = m_variables_data_info.add(var_full_name, sdata);
71 KeyValueTextWriter* writer = m_text_writer.get();
72 var_data_info->setFileOffset(writer->fileOffset());
73 var_data_info->setComparisonHashValue(comparison_hash);
74 info(4) << " SDATA name=" << var_full_name << " nb_element=" << sdata->nbElement()
75 << " dim=" << sdata->nbDimension() << " datatype=" << sdata->baseDataType()
76 << " nb_basic_element=" << sdata->nbBaseElement()
77 << " is_multi=" << sdata->isMultiSize()
78 << " dimensions_size=" << sdata->extents().size()
79 << " memory_size=" << sdata->memorySize()
80 << " bytes_size=" << sdata->constBytes().size();
81
82 if (is_save_values) {
83 const void* ptr = sdata->constBytes().data();
84
85 // If the variable is a two-dimensional array type, save the
86 // sizes of the second dimension per element.
87 Int64ConstArrayView extents = sdata->extents();
88 writer->setExtents(var_full_name, extents);
89
90 // Now, save the values if necessary
91 Int64 nb_base_element = sdata->nbBaseElement();
92 if (nb_base_element != 0 && ptr) {
93 writer->write(var_full_name, asBytes(sdata->constBytes()));
94 }
95 }
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101void BasicGenericWriter::
102writeItemGroup(const String& group_full_name, SmallSpan<const Int64> written_unique_ids,
103 SmallSpan<const Int64> wanted_unique_ids)
104{
105 if (m_version >= 3) {
106 // Save the group information to the database (key, value)
107 {
108 String written_uid_name = String("GroupWrittenUid:") + group_full_name;
109 Int64 nb_written_uid = written_unique_ids.size();
110 m_text_writer->setExtents(written_uid_name, Int64ConstArrayView(1, &nb_written_uid));
111 m_text_writer->write(written_uid_name, asBytes(written_unique_ids));
112 }
113 {
114 String wanted_uid_name = String("GroupWantedUid:") + group_full_name;
115 Int64 nb_wanted_uid = wanted_unique_ids.size();
116 m_text_writer->setExtents(wanted_uid_name, Int64ConstArrayView(1, &nb_wanted_uid));
117 m_text_writer->write(wanted_uid_name, asBytes(wanted_unique_ids));
118 }
119 return;
120 }
121
122 String filename = BasicReaderWriterCommon::_getBasicGroupFile(m_path, group_full_name, m_rank);
123 ofstream writer(filename.localstr(), std::ios::out | std::ios::binary);
124
125 // Save the list of written unique_ids
126 {
127 Integer nb_unique_id = written_unique_ids.size();
128 binaryWrite(writer, asBytes(Span<const Int32>(&nb_unique_id, 1)));
129 binaryWrite(writer, asBytes(written_unique_ids));
130 }
131
132 // Save the list of desired unique_ids by this subdomain
133 {
134 Integer nb_unique_id = wanted_unique_ids.size();
135 binaryWrite(writer, asBytes(Span<const Int32>(&nb_unique_id, 1)));
136 binaryWrite(writer, asBytes(wanted_unique_ids));
137 }
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143void BasicGenericWriter::
144endWrite()
145{
146 IApplication* app = m_application;
147 ScopedPtrT<IXmlDocumentHolder> xdoc(app->ressourceMng()->createXmlDocument());
148 XmlNode doc = xdoc->documentNode();
149 XmlElement root(doc, "variables-data");
150 const IDataCompressor* dc = m_text_writer->dataCompressor().get();
151 if (dc) {
152 root.setAttrValue("deflater-service", dc->name());
153 root.setAttrValue("min-compress-size", String::fromNumber(dc->minCompressSize()));
154 }
155 root.setAttrValue("version", String::fromNumber(m_version));
156 JSONWriter jsw;
157 {
158 JSONWriter::Object main_object(jsw);
159 JSONWriter::Object json_vars(jsw, "Variables");
160 for (const auto& i : m_variables_data_info) {
161 Ref<VariableDataInfo> vdi = i.second;
162 XmlNode e = root.createAndAppendElement("variable-data");
163 e.setAttrValue("full-name", vdi->fullName());
164 vdi->write(e, jsw);
165 }
166 }
167 if (m_version >= 3) {
168 // Adds the saving of metadata in JSON format
169 XmlElement json_var(root, "variables-data-json", jsw.getBuffer());
170
171 // Save the metadata in the database.
172 UniqueArray<Byte> bytes;
173 xdoc->save(bytes);
174 Int64 length = bytes.length();
175 String key_name = "Global:OwnMetadata";
176 m_text_writer->setExtents(key_name, Int64ConstArrayView(1, &length));
177 m_text_writer->write(key_name, asBytes(bytes.span()));
178 }
179 else {
180 String filename = BasicReaderWriterCommon::_getOwnMetatadaFile(m_path, m_rank);
181 app->ioMng()->writeXmlFile(xdoc.get(), filename);
182 }
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188} // namespace Arcane::impl
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of a serialized data.
virtual eDataType baseDataType() const =0
Data type.
virtual Integer nbDimension() const =0
Dimension. 0 for a scalar, 1 for a 1D array, ...
virtual Int64 memorySize() const =0
Indicates the number of bytes that must be allocated to store or read the data.
virtual Int64 nbBaseElement() const =0
Number of base elements.
virtual Int64ConstArrayView extents() const =0
Array containing the number of elements for each dimension.
virtual Int64 nbElement() const =0
Number of elements.
virtual Span< const Byte > constBytes() const =0
Serialized values.
virtual bool isMultiSize() const =0
Indicates if it is a multi-size array. (only relevant if nbDimension()>1).
View of an array of elements of type T.
Definition Span.h:803
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:325
TraceMessage info() const
Flow for an information message.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:537
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:325
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
Definition UtilsTypes.h:474
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1030
void binaryWrite(std::ostream &ostr, const Span< const std::byte > &bytes)
Writes the content of bytes to the stream ostr in binary format.
Definition ArrayView.cc:93
std::int32_t Int32
Signed integer type of 32 bits.