Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicWriter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* BasicWriter.cc (C) 2000-2024 */
9/* */
10/* Ecriture simple pour les protections/reprises. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/BasicWriter.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/PlatformUtils.h"
19#include "arcane/utils/JSONWriter.h"
20#include "arcane/utils/IDataCompressor.h"
21#include "arcane/utils/MemoryView.h"
22#include "arcane/utils/Ref.h"
23#include "arcane/utils/IHashAlgorithm.h"
24
25#include "arcane/core/IParallelMng.h"
26#include "arcane/core/ItemGroup.h"
27#include "arcane/core/IVariable.h"
28#include "arcane/core/IItemFamily.h"
29#include "arcane/core/IData.h"
30#include "arcane/core/internal/IVariableInternal.h"
31
32#include "arcane/std/internal/ParallelDataWriter.h"
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane::impl
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43BasicWriter::
44BasicWriter(IApplication* app, IParallelMng* pm, const String& path,
45 eOpenMode open_mode, Int32 version, bool want_parallel)
46: BasicReaderWriterCommon(app, pm, path, open_mode)
47, m_want_parallel(want_parallel)
48, m_version(version)
49{
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55void BasicWriter::
56initialize()
57{
58 _checkNoInit();
59
60 Int32 rank = m_parallel_mng->commRank();
61 if (m_open_mode == OpenModeTruncate && m_parallel_mng->isMasterIO())
62 platform::recursiveCreateDirectory(m_path);
63 m_parallel_mng->barrier();
64 String filename = _getBasicVariableFile(m_version, m_path, rank);
65 m_text_writer = makeRef(new KeyValueTextWriter(traceMng(), filename, m_version));
66 m_text_writer->setDataCompressor(m_data_compressor);
67 m_text_writer->setHashAlgorithm(m_hash_algorithm);
68
69 // Permet de surcharger le service utilisé pour la compression par une
70 // variable d'environnement si aucun n'est positionné
71 if (!m_data_compressor.get()) {
72 String data_compressor_name = platform::getEnvironmentVariable("ARCANE_DEFLATER");
73 if (!data_compressor_name.null()) {
74 data_compressor_name = data_compressor_name + "DataCompressor";
75 auto bc = _createDeflater(m_application, data_compressor_name);
76 info() << "Use data_compressor from environment variable ARCANE_DEFLATER name=" << data_compressor_name;
77 m_data_compressor = bc;
78 m_text_writer->setDataCompressor(bc);
79 }
80 }
81
82 // Idem pour le service de calcul de hash
83 if (!m_hash_algorithm.get()) {
84 String hash_algorithm_name = platform::getEnvironmentVariable("ARCANE_HASHALGORITHM");
85 if (hash_algorithm_name.null())
86 hash_algorithm_name = "SHA3_256";
87 else
88 info() << "Use hash algorithm from environment variable ARCANE_HASHALGORITHM name=" << hash_algorithm_name;
89 hash_algorithm_name = hash_algorithm_name + "HashAlgorithm";
90 auto v = _createHashAlgorithm(m_application, hash_algorithm_name);
91 m_hash_algorithm = v;
92 m_text_writer->setHashAlgorithm(v);
93 }
94
95 // Pour test, permet de spécifier un service pour le calcul du hash global.
96 if (!m_compare_hash_algorithm.get()) {
97 String algo_name = platform::getEnvironmentVariable("ARCANE_COMPAREHASHALGORITHM");
98 if (!algo_name.empty()) {
99 info() << "Use global hash algorithm from environment variable ARCANE_COMPAREHASHALGORITHM name=" << algo_name;
100 algo_name = algo_name + "HashAlgorithm";
101 auto v = _createHashAlgorithm(m_application, algo_name);
102 m_compare_hash_algorithm = v;
103 }
104 }
105
106 m_global_writer = new BasicGenericWriter(m_application, m_version, m_text_writer);
107 if (m_verbose_level > 0)
108 info() << "** OPEN MODE = " << m_open_mode;
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114void BasicWriter::
115_checkNoInit()
116{
117 if (m_is_init)
118 ARCANE_FATAL("initialize() has already been called");
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124Ref<ParallelDataWriter> BasicWriter::
125_getWriter(IVariable* var)
126{
127 return m_parallel_data_writers.getOrCreateWriter(var->itemGroup());
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133void BasicWriter::
134_directWriteVal(IVariable* var, IData* data)
135{
136 info(4) << "DIRECT WRITE VAL v=" << var->fullName();
137
138 IData* write_data = data;
139 Int64ConstArrayView written_unique_ids;
140 Int64UniqueArray wanted_unique_ids;
141 Int64UniqueArray sequential_written_unique_ids;
142
143 Ref<IData> allocated_write_data;
144 const bool is_mesh_variable = (var->itemKind() != IK_Unknown);
145 if (is_mesh_variable) {
146 ItemGroup group = var->itemGroup();
147 if (m_want_parallel) {
148 Ref<ParallelDataWriter> writer = _getWriter(var);
149 written_unique_ids = writer->sortedUniqueIds();
150 allocated_write_data = writer->getSortedValues(data);
151 write_data = allocated_write_data.get();
152 }
153 else {
154 // TODO vérifier que les uniqueId() sont bien triés.
155 // Normalement c'est toujours le cas.
156 _fillUniqueIds(group, sequential_written_unique_ids);
157 written_unique_ids = sequential_written_unique_ids.view();
158 }
159 // Ecrit les informations du groupe si c'est la première fois qu'on accède à ce groupe.
160 if (m_written_groups.find(group) == m_written_groups.end()) {
161 info(5) << "WRITE GROUP " << group.name();
162 const IItemFamily* item_family = group.itemFamily();
163 const String& gname = group.name();
164 String group_full_name = item_family->fullName() + "_" + gname;
165 _fillUniqueIds(group, wanted_unique_ids);
166 if (m_is_save_values)
167 m_global_writer->writeItemGroup(group_full_name, written_unique_ids, wanted_unique_ids.view());
168 m_written_groups.insert(group);
169 }
170 }
171
172 Ref<ISerializedData> sdata(write_data->createSerializedDataRef(false));
173 String compare_hash;
174 if (is_mesh_variable) {
175 compare_hash = _computeCompareHash(var, write_data);
176 }
177 m_global_writer->writeData(var->fullName(), sdata.get(), compare_hash, m_is_save_values);
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
194String BasicWriter::
195_computeCompareHash(IVariable* var, IData* write_data)
196{
197 IHashAlgorithm* hash_algo = m_compare_hash_algorithm.get();
198 if (!hash_algo)
199 return {};
200 return var->_internalApi()->computeComparisonHashCollective(hash_algo, write_data);
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206void BasicWriter::
207write(IVariable* var, IData* data)
208{
209 if (var->isPartial()) {
210 info() << "** WARNING: partial variable not implemented in BasicWriter";
211 return;
212 }
213 _directWriteVal(var, data);
214}
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
219void BasicWriter::
220setMetaData(const String& meta_data)
221{
222 // Dans la version 3, les méta-données de la protection sont dans la
223 // base de données.
224 if (m_version >= 3) {
225 Span<const Byte> bytes = meta_data.utf8();
226 Int64 length = bytes.length();
227 String key_name = "Global:CheckpointMetadata";
228 m_text_writer->setExtents(key_name, Int64ConstArrayView(1, &length));
229 m_text_writer->write(key_name, asBytes(bytes));
230 }
231 else {
232 Int32 my_rank = m_parallel_mng->commRank();
233 String filename = _getMetaDataFileName(my_rank);
234 std::ofstream ofile(filename.localstr());
235 meta_data.writeBytes(ofile);
236 }
237}
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242void BasicWriter::
243beginWrite(const VariableCollection& vars)
244{
245 ARCANE_UNUSED(vars);
246 Int32 my_rank = m_parallel_mng->commRank();
247 m_global_writer->initialize(m_path, my_rank);
248}
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
252
253void BasicWriter::
254_endWriteV3()
255{
256 const Int64 nb_part = m_parallel_mng->commSize();
257
258 // Sauvegarde les informations au format JSON
260
261 {
263 jsw.writeKey(_getArcaneDBTag());
264 {
266 jsw.write("Version", (Int64)m_version);
267 jsw.write("NbPart", nb_part);
268 jsw.write("HasValues", m_is_save_values);
269
271 Int64 data_compressor_min_size = 0;
272 if (m_data_compressor.get()) {
273 data_compressor_name = m_data_compressor->name();
274 data_compressor_min_size = m_data_compressor->minCompressSize();
275 }
276 jsw.write("DataCompressor", data_compressor_name);
277 jsw.write("DataCompressorMinSize", String::fromNumber(data_compressor_min_size));
278
279 // Sauve le nom de l'algorithme de hash
280 {
281 String name;
282 if (m_hash_algorithm.get())
283 name = m_hash_algorithm->name();
284 jsw.write("HashAlgorithm", name);
285 }
286
287 // Sauve le nom de l'algorithme de hash pour les comparaisons
288 {
289 String name;
290 if (m_compare_hash_algorithm.get())
291 name = m_compare_hash_algorithm->name();
292 jsw.write("ComparisonHashAlgorithm", name);
293 }
294 }
295 }
296
297 StringBuilder filename = m_path;
298 filename += "/arcane_acr_db.json";
299 String fn = filename.toString();
300 std::ofstream ofile(fn.localstr());
301 ofile << jsw.getBuffer();
302}
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307void BasicWriter::
308endWrite()
309{
310 const IParallelMng* pm = m_parallel_mng;
311 if (pm->isMasterIO()) {
312 if (m_version >= 3) {
313 _endWriteV3();
314 }
315 else {
316 Int64 nb_part = pm->commSize();
317 StringBuilder filename = m_path;
318 filename += "/infos.txt";
319 String fn = filename.toString();
320 std::ofstream ofile(fn.localstr());
321 ofile << nb_part << '\n';
322 }
323 }
324 m_global_writer->endWrite();
325}
326
327/*---------------------------------------------------------------------------*/
328/*---------------------------------------------------------------------------*/
329
330} // namespace Arcane::impl
331
332/*---------------------------------------------------------------------------*/
333/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Interface d'une donnée.
Definition IData.h:33
Interface d'un algorithme de hashage.
virtual bool isMasterIO() const =0
true si l'instance est un gestionnaire maître des entrées/sorties.
virtual Int32 commSize() const =0
Nombre d'instance dans le communicateur.
Interface d'une variable.
Definition IVariable.h:54
Ecrivain au format JSON.
Definition JSONWriter.h:33
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Collection de variables.
Vue constante d'un tableau de type T.
Chaîne de caractères unicode.
const char * localstr() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
Definition String.cc:227
UniqueArray< Int64 > Int64UniqueArray
Tableau dynamique à une dimension d'entiers 64 bits.
Definition UtilsTypes.h:513
@ IK_Unknown
Entité de maillage de genre inconnu ou non initialisé
ConstArrayView< Int64 > Int64ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 64 bits.
Definition UtilsTypes.h:638
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.