Arcane  4.1.12.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-2024 */
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/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::impl
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38BasicGenericWriter::
39BasicGenericWriter(IApplication* app, Int32 version,
40 Ref<KeyValueTextWriter> text_writer)
41: TraceAccessor(app->traceMng())
42, m_application(app)
43, m_version(version)
44, m_text_writer(text_writer)
45{
46}
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51void BasicGenericWriter::
52initialize(const String& path, Int32 rank)
53{
54 if (!m_text_writer)
55 ARCANE_FATAL("Null text writer");
56 m_path = path;
57 m_rank = rank;
58}
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
63void BasicGenericWriter::
64writeData(const String& var_full_name, const ISerializedData* sdata,
65 const String& comparison_hash, bool is_save_values)
66{
67 //TODO: Verify that initialize() has been called.
68 auto var_data_info = m_variables_data_info.add(var_full_name, sdata);
69 KeyValueTextWriter* writer = m_text_writer.get();
70 var_data_info->setFileOffset(writer->fileOffset());
71 var_data_info->setComparisonHashValue(comparison_hash);
72 info(4) << " SDATA name=" << var_full_name << " nb_element=" << sdata->nbElement()
73 << " dim=" << sdata->nbDimension() << " datatype=" << sdata->baseDataType()
74 << " nb_basic_element=" << sdata->nbBaseElement()
75 << " is_multi=" << sdata->isMultiSize()
76 << " dimensions_size=" << sdata->extents().size()
77 << " memory_size=" << sdata->memorySize()
78 << " bytes_size=" << sdata->constBytes().size();
79
80 if (is_save_values) {
81 const void* ptr = sdata->constBytes().data();
82
83 // If the variable is a two-dimensional array type, save the
84 // sizes of the second dimension per element.
85 Int64ConstArrayView extents = sdata->extents();
86 writer->setExtents(var_full_name, extents);
87
88 // Now, save the values if necessary
89 Int64 nb_base_element = sdata->nbBaseElement();
90 if (nb_base_element != 0 && ptr) {
91 writer->write(var_full_name, asBytes(sdata->constBytes()));
92 }
93 }
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99void BasicGenericWriter::
100writeItemGroup(const String& group_full_name, SmallSpan<const Int64> written_unique_ids,
101 SmallSpan<const Int64> wanted_unique_ids)
102{
103 if (m_version >= 3) {
104 // Save the group information to the database (key, value)
105 {
106 String written_uid_name = String("GroupWrittenUid:") + group_full_name;
107 Int64 nb_written_uid = written_unique_ids.size();
108 m_text_writer->setExtents(written_uid_name, Int64ConstArrayView(1, &nb_written_uid));
109 m_text_writer->write(written_uid_name, asBytes(written_unique_ids));
110 }
111 {
112 String wanted_uid_name = String("GroupWantedUid:") + group_full_name;
113 Int64 nb_wanted_uid = wanted_unique_ids.size();
114 m_text_writer->setExtents(wanted_uid_name, Int64ConstArrayView(1, &nb_wanted_uid));
115 m_text_writer->write(wanted_uid_name, asBytes(wanted_unique_ids));
116 }
117 return;
118 }
119
120 String filename = BasicReaderWriterCommon::_getBasicGroupFile(m_path, group_full_name, m_rank);
121 ofstream writer(filename.localstr(), std::ios::out | std::ios::binary);
122
123 // Save the list of written unique_ids
124 {
125 Integer nb_unique_id = written_unique_ids.size();
126 binaryWrite(writer, asBytes(Span<const Int32>(&nb_unique_id, 1)));
127 binaryWrite(writer, asBytes(written_unique_ids));
128 }
129
130 // Save the list of desired unique_ids by this subdomain
131 {
132 Integer nb_unique_id = wanted_unique_ids.size();
133 binaryWrite(writer, asBytes(Span<const Int32>(&nb_unique_id, 1)));
134 binaryWrite(writer, asBytes(wanted_unique_ids));
135 }
136}
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
141void BasicGenericWriter::
142endWrite()
143{
144 IApplication* app = m_application;
145 ScopedPtrT<IXmlDocumentHolder> xdoc(app->ressourceMng()->createXmlDocument());
146 XmlNode doc = xdoc->documentNode();
147 XmlElement root(doc, "variables-data");
148 const IDataCompressor* dc = m_text_writer->dataCompressor().get();
149 if (dc) {
150 root.setAttrValue("deflater-service", dc->name());
151 root.setAttrValue("min-compress-size", String::fromNumber(dc->minCompressSize()));
152 }
153 root.setAttrValue("version", String::fromNumber(m_version));
154 JSONWriter jsw;
155 {
156 JSONWriter::Object main_object(jsw);
157 JSONWriter::Object json_vars(jsw, "Variables");
158 for (const auto& i : m_variables_data_info) {
159 Ref<VariableDataInfo> vdi = i.second;
160 XmlNode e = root.createAndAppendElement("variable-data");
161 e.setAttrValue("full-name", vdi->fullName());
162 vdi->write(e, jsw);
163 }
164 }
165 if (m_version >= 3) {
166 // Adds the saving of metadata in JSON format
167 XmlElement json_var(root, "variables-data-json", jsw.getBuffer());
168
169 // Save the metadata in the database.
170 UniqueArray<Byte> bytes;
171 xdoc->save(bytes);
172 Int64 length = bytes.length();
173 String key_name = "Global:OwnMetadata";
174 m_text_writer->setExtents(key_name, Int64ConstArrayView(1, &length));
175 m_text_writer->write(key_name, asBytes(bytes.span()));
176 }
177 else {
178 String filename = BasicReaderWriterCommon::_getOwnMetatadaFile(m_path, m_rank);
179 app->ioMng()->writeXmlFile(xdoc.get(), filename);
180 }
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186} // namespace Arcane::impl
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
#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:805
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
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:539
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
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:480
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:1032
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.