Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicGenericWriter.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/* BasicGenericWriter.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/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,
66{
67 //TODO: Verifier que initialize() a bien été appelé.
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 // Si la variable est de type tableau à deux dimensions, sauve les
84 // tailles de la deuxième dimension par élément.
85 Int64ConstArrayView extents = sdata->extents();
86 writer->setExtents(var_full_name, extents);
87
88 // Maintenant, sauve les valeurs si necessaire
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::
102{
103 if (m_version >= 3) {
104 // Sauve les informations du groupe la base de données (clé,valeur)
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 // Sauve la liste des unique_ids écrits
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 // Sauve la liste des unique_ids souhaités par ce sous-domaine
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 // Ajoute la sauvegarde des meta-données au format JSON
167 XmlElement json_var(root, "variables-data-json", jsw.getBuffer());
168
169 // Sauve les méta-données dans la base de données.
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/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189} // namespace Arcane::impl
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Interface d'une donnée sérialisée.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Vue constante d'un tableau de type T.
Chaîne de caractères unicode.
ConstArrayView< Int64 > Int64ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 64 bits.
Definition UtilsTypes.h:638
Int32 Integer
Type représentant un entier.
detail::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets non modifiables.
Definition Span.h:881
void binaryWrite(std::ostream &ostr, const Span< const std::byte > &bytes)
Ecrit en binaire le contenu de bytes sur le flot ostr.
Definition ArrayView.cc:60