Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicGenericReader.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/* BasicGenericReader.cc (C) 2000-2024 */
9/* */
10/* Lecture simple pour les protections/reprises. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/BasicReader.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/IDataCompressor.h"
19#include "arcane/utils/JSONReader.h"
20#include "arcane/utils/Ref.h"
21
22#include "arcane/core/IApplication.h"
23#include "arcane/core/IXmlDocumentHolder.h"
24#include "arcane/core/IIOMng.h"
25#include "arcane/core/IData.h"
26#include "arcane/core/ArcaneException.h"
27#include "arcane/core/ISerializedData.h"
28#include "arcane/core/XmlNodeList.h"
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace Arcane::impl
34{
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39BasicGenericReader::
40BasicGenericReader(IApplication* app, Int32 version, Ref<KeyValueTextReader> text_reader)
41: TraceAccessor(app->traceMng())
42, m_application(app)
43, m_text_reader(text_reader)
44, m_version(version)
45{
46}
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51void BasicGenericReader::
52initialize(const String& path, Int32 rank)
53{
54 // Dans le cas de la version 1 ou 2, on ne peut pas créer le KeyValueTextReader
55 // avant de lire les 'OwnMetaData' car ce sont ces dernières qui contiennent
56 // le numéro de version.
57
58 m_path = path;
59 m_rank = rank;
60
61 info(4) << "BasicGenericReader::initialize known_version=" << m_version;
62
63 ScopedPtrT<IXmlDocumentHolder> xdoc;
64
65 if (m_version >= 3) {
66 if (!m_text_reader.get())
67 ARCANE_FATAL("Null text reader");
68 String dc_name;
69 if (m_text_reader->dataCompressor().get())
70 dc_name = m_text_reader->dataCompressor()->name();
71 info(4) << "BasicGenericReader::initialize data_compressor=" << dc_name;
72
73 // Si on connait déjà la version et qu'elle est supérieure ou égale à 3
74 // alors les informations sont dans la base de données. Dans ce cas on lit
75 // directement les infos depuis cette base.
76 String main_filename = BasicReaderWriterCommon::_getBasicVariableFile(m_version, m_path, rank);
77 Int64 meta_data_size = 0;
78 String key_name = "Global:OwnMetadata";
79 m_text_reader->getExtents(key_name, Int64ArrayView(1, &meta_data_size));
80 UniqueArray<std::byte> bytes(meta_data_size);
81 m_text_reader->read(key_name, bytes);
82 info(4) << "Reading own metadata rank=" << rank << " from database";
83 xdoc = IXmlDocumentHolder::loadFromBuffer(bytes, "OwnMetadata", traceMng());
84 }
85 else {
86 StringBuilder filename = BasicReaderWriterCommon::_getOwnMetatadaFile(m_path, m_rank);
87 info(4) << "Reading own metadata rank=" << rank << " file=" << filename;
88 IApplication* app = m_application;
89 xdoc = app->ioMng()->parseXmlFile(filename);
90 }
91 XmlNode root = xdoc->documentNode().documentElement();
92 XmlNodeList variables_elem = root.children("variable-data");
93 String deflater_name = root.attrValue("deflater-service");
94 String hash_algorithm_name = root.attrValue("hash-algorithm-service");
95 String version_id = root.attrValue("version", false);
96 info(4) << "Infos from metadata deflater-service=" << deflater_name
97 << " hash-algorithm-service=" << hash_algorithm_name
98 << " version=" << version_id;
99 if (version_id.null() || version_id == "1")
100 // Version 1:
101 // - taille des dimensions sur 32 bits
102 m_version = 1;
103 else if (version_id == "2")
104 // Version 2:
105 // - taille des dimensions sur 64 bits
106 m_version = 2;
107 else if (version_id == "3")
108 // Version 3:
109 // - taille des dimensions sur 64 bits
110 // - 1 seul fichier pour toutes les meta-données
111 m_version = 3;
112 else
113 ARCANE_FATAL("Unsupported version '{0}' (max=3)", version_id);
114
115 Ref<IDataCompressor> deflater;
116 if (!deflater_name.null())
117 deflater = BasicReaderWriterCommon::_createDeflater(m_application, deflater_name);
118
119 Ref<IHashAlgorithm> hash_algorithm;
120 if (!hash_algorithm_name.null())
121 hash_algorithm = BasicReaderWriterCommon::_createHashAlgorithm(m_application, hash_algorithm_name);
122
123 // Si disponible, essaie de relire les informations des variables au format JSON
124 bool do_json = true;
125 String json_variables_elem = root.child("variables-data-json").value();
126 if (do_json && !json_variables_elem.empty()) {
127 JSONDocument json_doc;
128 json_doc.parse(json_variables_elem.bytes(), "Internal variables data");
129 JSONValue json_root = json_doc.root();
130 JSONValue json_vars = json_root.expectedChild("Variables");
131 for (JSONKeyValue kv : json_vars.keyValueChildren()) {
132 String var_full_name = kv.name();
133 m_variables_data_info.add(var_full_name, kv.value());
134 }
135 }
136 else {
137 for (const XmlNode& n : variables_elem) {
138 String var_full_name = n.attrValue("full-name");
139 m_variables_data_info.add(var_full_name, n);
140 }
141 }
142
143 if (!m_text_reader.get()) {
144 String main_filename = BasicReaderWriterCommon::_getBasicVariableFile(m_version, m_path, rank);
145 m_text_reader = makeRef(new KeyValueTextReader(traceMng(), main_filename, m_version));
146 }
147
148 // Il est possible qu'il y ait déjà un algorithme de compression spécifié.
149 // Il ne faut pas l'écraser si aucun n'est spécifié dans 'OwnMetadata'.
150 // (Normalement cela ne devrait pas arriver sauf incohérence).
151 if (deflater.get())
152 m_text_reader->setDataCompressor(deflater);
153 if (hash_algorithm.get())
154 m_text_reader->setHashAlgorithm(hash_algorithm);
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160Ref<VariableDataInfo> BasicGenericReader::
161_getVarInfo(const String& full_name)
162{
163 Ref<VariableDataInfo> vdi = m_variables_data_info.find(full_name);
164 if (!vdi.get())
165 ARCANE_THROW(ReaderWriterException,
166 "Can not find own metadata infos for data var={0} rank={1}", full_name, m_rank);
167 return vdi;
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173void BasicGenericReader::
174readData(const String& var_full_name, IData* data)
175{
176 KeyValueTextReader* reader = m_text_reader.get();
177 String vname = var_full_name;
178 Ref<VariableDataInfo> vdi = _getVarInfo(vname);
179 if (m_version < 3)
180 reader->setFileOffset(vdi->fileOffset());
181
182 eDataType data_type = vdi->baseDataType();
183 Int64 memory_size = vdi->memorySize();
184 Integer dimension_array_size = vdi->dimensionArraySize();
185 Int64 nb_element = vdi->nbElement();
186 Integer nb_dimension = vdi->nbDimension();
187 Int64 nb_base_element = vdi->nbBaseElement();
188 bool is_multi_size = vdi->isMultiSize();
189 UniqueArray<Int64> extents(dimension_array_size);
190 reader->getExtents(var_full_name, extents.view());
191 ArrayShape shape = vdi->shape();
192
193 Ref<ISerializedData> sd(arcaneCreateSerializedDataRef(data_type, memory_size, nb_dimension, nb_element,
194 nb_base_element, is_multi_size, extents, shape));
195
196 Int64 storage_size = sd->memorySize();
197 info(4) << " READ DATA storage_size=" << storage_size << " DATA=" << data;
198
199 data->allocateBufferForSerializedData(sd.get());
200
201 if (storage_size != 0)
202 reader->read(var_full_name, asWritableBytes(sd->writableBytes()));
203
204 data->assignSerializedData(sd.get());
205}
206
207/*---------------------------------------------------------------------------*/
208/*---------------------------------------------------------------------------*/
209
210void BasicGenericReader::
211readItemGroup(const String& group_full_name, Int64Array& written_unique_ids,
212 Int64Array& wanted_unique_ids)
213{
214 if (m_version >= 3) {
215 {
216 String written_uid_name = String("GroupWrittenUid:") + group_full_name;
217 Int64 nb_written_uid = 0;
218 m_text_reader->getExtents(written_uid_name, Int64ArrayView(1, &nb_written_uid));
219 written_unique_ids.resize(nb_written_uid);
220 m_text_reader->read(written_uid_name, asWritableBytes(written_unique_ids.span()));
221 }
222 {
223 String wanted_uid_name = String("GroupWantedUid:") + group_full_name;
224 Int64 nb_wanted_uid = 0;
225 m_text_reader->getExtents(wanted_uid_name, Int64ArrayView(1, &nb_wanted_uid));
226 wanted_unique_ids.resize(nb_wanted_uid);
227 m_text_reader->read(wanted_uid_name, asWritableBytes(wanted_unique_ids.span()));
228 }
229 return;
230 }
231
232 info(5) << "READ GROUP " << group_full_name;
233 String filename = BasicReaderWriterCommon::_getBasicGroupFile(m_path, group_full_name, m_rank);
234 std::ifstream reader(filename.localstr(), std::ios::in | std::ios::binary);
235
236 {
237 Integer nb_unique_id = 0;
238 binaryRead(reader,asWritableBytes(SmallSpan<Integer>(&nb_unique_id, 1)));
239 info(5) << "NB_WRITTEN_UNIQUE_ID = " << nb_unique_id;
240 written_unique_ids.resize(nb_unique_id);
241 binaryRead(reader,asWritableBytes(written_unique_ids.span()));
242 }
243
244 {
245 Integer nb_unique_id = 0;
246 binaryRead(reader,asWritableBytes(SmallSpan<Integer>(&nb_unique_id, 1)));
247 info(5) << "NB_WANTED_UNIQUE_ID = " << nb_unique_id;
248 wanted_unique_ids.resize(nb_unique_id);
249 binaryRead(reader,asWritableBytes(wanted_unique_ids.span()));
250 }
251}
252
253/*---------------------------------------------------------------------------*/
254/*---------------------------------------------------------------------------*/
255
256String BasicGenericReader::
257comparisonHashValue(const String& var_full_name) const
258{
259 Ref<VariableDataInfo> vdi = m_variables_data_info.find(var_full_name);
260 if (vdi.get())
261 return vdi->comparisonHashValue();
262 return {};
263}
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268} // namespace Arcane::impl
269
270/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Ref< ISerializedData > arcaneCreateSerializedDataRef(eDataType data_type, Int64 memory_size, Integer nb_dim, Int64 nb_element, Int64 nb_base_element, bool is_multi_size, Int64ConstArrayView dimensions)
Créé des données sérialisées.
ArrayView< Int64 > Int64ArrayView
Equivalent C d'un tableau à une dimension d'entiers 64 bits.
Definition UtilsTypes.h:609
eDataType
Type d'une donnée.
Definition DataTypes.h:39
Int32 Integer
Type représentant un entier.
void binaryRead(std::istream &istr, const Span< std::byte > &bytes)
Lit en binaire le contenu de bytes depuis le flot istr.
Definition ArrayView.cc:70
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.
detail::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.
Definition Span.h:916