Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
VariableMng.h
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/* VariableMng.h (C) 2000-2026 */
9/* */
10/* Class managing the list of meshes. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_IMPL_INTERNAL_VARIABLEMNG_H
13#define ARCANE_IMPL_INTERNAL_VARIABLEMNG_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/TraceAccessor.h"
18#include "arcane/utils/String.h"
19#include "arcane/utils/HashTableMap.h"
20#include "arcane/utils/List.h"
21#include "arcane/utils/Ref.h"
22
23#include "arcane/core/IVariableMng.h"
24#include "arcane/core/IVariableFilter.h"
25#include "arcane/core/VariableCollection.h"
26#include "arcane/core/internal/IVariableMngInternal.h"
27
28#include "arcane/accelerator/core/IAcceleratorMng.h"
29
30#include <map>
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Arcane
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
43class XmlNode;
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
55: public TraceAccessor
56, public IVariableMng
57{
58 friend class VariableIOWriterMng;
59 friend class VariableIOReaderMng;
60
62 {
63 public:
64
65 String m_local_name;
66 String m_family_name;
67 String m_mesh_name;
68 bool operator==(const VariableNameInfo& vni) const
69 {
70 return m_local_name == vni.m_local_name && m_family_name == vni.m_family_name && m_mesh_name == vni.m_mesh_name;
71 }
72
73 public:
74
75 Int32 hash() const
76 {
77 Int32 h = 0;
78 if (!m_mesh_name.null()) {
79 h = hash(h, m_mesh_name.localstr());
80 h = hash(h, "_");
81 }
82 if (!m_family_name.null()) {
83 h = hash(h, m_family_name.localstr());
84 h = hash(h, "_");
85 }
86 h = hash(h, m_local_name.localstr());
87 return h;
88 }
89
90 public:
91
92 static Int32 hash(Int32 h, const char* p)
93 {
94 if (!p)
95 return h;
96 for (; *p != '\0'; ++p)
97 h = (h << 5) - h + *p;
98 return h;
99 }
100 };
101
103 {
104 public:
105
106 typedef const VariableNameInfo& KeyTypeConstRef;
107 typedef VariableNameInfo& KeyTypeRef;
108 typedef VariableNameInfo KeyTypeValue;
109 typedef FalseType Printable;
110 typedef Int32 HashValueType;
111 static Int32 hashFunction(const VariableNameInfo& key)
112 {
113 // ensures the value is positive
114 return key.hash() & 0x7fffffff;
115 }
116 };
117
118 class InternalApi
119 : public IVariableMngInternal
120 {
121 public:
122
123 explicit InternalApi(VariableMng* v)
124 : m_variable_mng(v)
125 {}
126
127 public:
128
129 void build() override { m_variable_mng->build(); }
130 void initialize() override { m_variable_mng->initialize(); }
131 void removeAllVariables() override { m_variable_mng->removeAllVariables(); }
132 void removeAllShMemVariables() override { m_variable_mng->_removeAllShMemVariables(); }
133 void detachMeshVariables(IMesh* mesh) override { m_variable_mng->detachMeshVariables(mesh); }
134 void addVariableRef(VariableRef* var) override { m_variable_mng->addVariableRef(var); }
135 void removeVariableRef(VariableRef* var) override { m_variable_mng->removeVariableRef(var); }
136 void addVariable(IVariable* var) override { m_variable_mng->addVariable(var); }
137 void removeVariable(IVariable* var) override { m_variable_mng->removeVariable(var); }
138 void initializeVariables(bool is_continue) override { m_variable_mng->initializeVariables(is_continue); }
139 ISubDomain* internalSubDomain() const override { return m_variable_mng->_internalSubDomain(); }
140 IAcceleratorMng* acceleratorMng() const override { return m_variable_mng->m_accelerator_mng.get(); }
141 void setAcceleratorMng(Ref<IAcceleratorMng> v) override { m_variable_mng->m_accelerator_mng = v; }
142 void addAutoDestroyVariable(VariableRef* var) override { m_variable_mng->m_auto_create_variables.add(var); }
143
144 private:
145
146 VariableMng* m_variable_mng = nullptr;
147 };
148
149 public:
150
152
153 public:
154
155 explicit VariableMng(ISubDomain* sd);
156 ~VariableMng() override;
157
158 public:
159
160 void build() override;
161 void initialize() override;
162 void removeAllVariables() override;
163 void detachMeshVariables(IMesh* mesh) override;
164
165 public:
166
167 ISubDomain* subDomain() override { return m_sub_domain; }
168 IParallelMng* parallelMng() const override { return m_parallel_mng; }
170 IVariable* checkVariable(const VariableInfo& infos) override;
171 void addVariableRef(VariableRef* ref) override;
172 void addVariable(IVariable* var) override;
173 void removeVariableRef(VariableRef*) override;
174 void removeVariable(IVariable* var) override;
175 void dumpList(std::ostream&, IModule*) override;
176 void dumpList(std::ostream&) override;
177 void initializeVariables(bool) override;
179 void variables(VariableRefCollection, IModule*) override;
180 VariableCollection variables() override;
182 void notifyUsedVariableChanged() override { m_used_variables_changed = true; }
183 Real exportSize(const VariableCollection& vars) override;
184 IObservable* writeObservable() override { return m_write_observable; }
185 IObservable* readObservable() override { return m_read_observable; }
186 void writeVariables(IDataWriter*, const VariableCollection& vars) override;
188 void writeCheckpoint(ICheckpointWriter*) override;
189 void writePostProcessing(IPostProcessorWriter* writer) override;
190 void readVariables(IDataReader*, IVariableFilter*) override;
191 void readCheckpoint(ICheckpointReader*) override;
192 void readCheckpoint(const CheckpointReadInfo& infos) override;
193 IVariable* findVariable(const String& name) override;
194 IVariable* findMeshVariable(IMesh* mesh, const String& name) override;
195 IVariable* findVariableFullyQualified(const String& name) override;
196
197 void dumpStats(std::ostream& ostr, bool is_verbose) override;
198 void dumpStatsJSON(JSONWriter& writer) override;
199 IVariableUtilities* utilities() const override { return m_utilities; }
200
203 {
204 return m_on_variable_added;
205 }
206
209 {
210 return m_on_variable_removed;
211 }
213 ISubDomain* _internalSubDomain() const override { return m_sub_domain; }
214 IVariableMngInternal* _internalApi() override { return &m_internal_api; }
215
216 public:
217
218 static bool isVariableToSave(IVariable& var);
219
220 private:
221
223 using FullNameVariableMap = std::map<String, IVariable*>;
225 using FullNameVariablePair = FullNameVariableMap::value_type;
226
228 using VariableFactoryMap = std::map<String, IVariableFactory*>;
230 using VariableFactoryPair = VariableFactoryMap::value_type;
231
234 IParallelMng* m_parallel_mng = nullptr;
235 ITimeStats* m_time_stats = nullptr;
236 InternalApi m_internal_api{ this };
238 VariableList m_variables;
239 VariableList m_used_variables;
240 bool m_variables_changed = true;
241 bool m_used_variables_changed = true;
244 VNIMap m_vni_map;
245 IObservable* m_write_observable = nullptr;
246 IObservable* m_read_observable = nullptr;
249 List<IVariableFactory*> m_variable_factories;
252 VariableFactoryMap m_variable_factory_map;
253
255
256 Int64 m_nb_created_variable_reference = 0;
257 Int64 m_nb_created_variable = 0;
258 // Indicates in which module a variable is created
259 std::map<IVariable*, IModule*> m_variable_creation_modules;
260
261 IVariableUtilities* m_utilities = nullptr;
262 VariableIOWriterMng* m_variable_io_writer_mng = nullptr;
263 VariableIOReaderMng* m_variable_io_reader_mng = nullptr;
264 VariableSynchronizerMng* m_variable_synchronizer_mng = nullptr;
265
266 Ref<IAcceleratorMng> m_accelerator_mng;
267
268 private:
269
271 void _dumpVariable(const VariableRef& v, std::ostream& o);
272
273 static const char* _msgClassName() { return "Variable"; }
274 VariableRef* _createVariableFromType(const String& full_type,
275 const VariableBuildInfo& vbi);
276
277 void _removeAllShMemVariables();
278};
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
286class VariableIOWriterMng
287: public TraceAccessor
288{
289 private:
290
292 : public IVariableFilter
293 {
294 public:
295
296 bool applyFilter(IVariable& var) override
297 {
298 return VariableMng::isVariableToSave(var);
299 }
300 };
301
302 public:
303
304 explicit VariableIOWriterMng(VariableMng* vm);
305
306 public:
307
308 void writeCheckpoint(ICheckpointWriter* service);
309 void writePostProcessing(IPostProcessorWriter* post_processor);
310 void writeVariables(IDataWriter* writer, const VariableCollection& vars, bool use_hash);
311 void writeVariables(IDataWriter* writer, IVariableFilter* filter, bool use_hash);
312
313 private:
314
315 VariableMng* m_variable_mng = nullptr;
316 bool m_use_hash_v2 = false;
317
318 private:
319
320 void _writeVariables(IDataWriter* writer, const VariableCollection& vars, bool use_hash);
321 String _generateMetaData(const VariableCollection& vars, IHashAlgorithm* hash_algo);
322 void _generateVariablesMetaData(JSONWriter& json_writer, XmlNode variables_node,
323 const VariableCollection& vars, IHashAlgorithm* hash_algo);
324 void _generateMeshesMetaData(JSONWriter& json_writer, XmlNode meshes_node);
325 static const char* _msgClassName() { return "Variable"; }
326};
327
328/*---------------------------------------------------------------------------*/
329/*---------------------------------------------------------------------------*/
330
334class VariableIOReaderMng
335: public TraceAccessor
336{
337 public:
338
339 class IDataReaderWrapper;
341 class DataReaderWrapper;
343
344 public:
345
346 explicit VariableIOReaderMng(VariableMng* vm);
347
348 public:
349
350 void readCheckpoint(ICheckpointReader* service);
351 void readCheckpoint(const CheckpointReadInfo& infos);
352 void readVariables(IDataReader* reader, IVariableFilter* filter);
353
354 private:
355
356 VariableMng* m_variable_mng = nullptr;
357 bool m_is_use_json_metadata = true;
358
359 private:
360
361 void _readVariablesData(VariableReaderMng& var_read_mng, IDataReaderWrapper* reader);
363 void _checkHashFunction(const VariableMetaDataList& vmd_list);
364 void _createVariablesFromMetaData(const VariableMetaDataList& vmd_list);
365 void _readVariablesMetaData(VariableMetaDataList& vmd_list, JSONValue variables_json, const XmlNode& variables_node);
366 void _readMeshesMetaData(JSONValue meshes_json, const XmlNode& meshes_node);
367 void _buildFilteredVariableList(VariableReaderMng& vars_read_mng, IVariableFilter* filter);
368 void _finalizeReadVariables(const VariableList& vars_to_read);
369 static const char* _msgClassName() { return "Variable"; }
370};
371
372/*---------------------------------------------------------------------------*/
373/*---------------------------------------------------------------------------*/
374
375} // namespace Arcane
376
377/*---------------------------------------------------------------------------*/
378/*---------------------------------------------------------------------------*/
379
380#endif
Checkpoint reading information.
Hash table for associative arrays.
Interface for the protection/recovery reading service.
Interface of the checkpoint/recovery write service.
Interface for reading variable data.
Definition IDataReader.h:35
Interface for writing variable data.
Definition IDataWriter.h:45
Interface of a hashing algorithm.
Interface of a module.
Definition IModule.h:40
Interface of the parallelism manager for a subdomain.
Interface for a writer for post-processing information.
Interface of the subdomain manager.
Definition ISubDomain.h:75
Interface managing execution time statistics.
Definition ITimeStats.h:44
Functor of a filter applicable to variables.
Interface of the variable manager.
Variable manager interface.
Interface of the variable synchronization manager.
Interface providing utility functions on variables.
Interface of a variable.
Definition IVariable.h:40
Implementation of a collection of elements in vector form.
Reference to an instance.
View of an array of elements of type T.
Definition Span.h:635
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
ITraceMng * traceMng() const
Trace manager.
Parameters necessary for building a variable.
Interface to use IDataReader or IDataReader2.
Reading management for variables.
void _readMetaData(VariableMetaDataList &vmd_list, Span< const Byte > bytes)
Reading metadata.
void _checkHashFunction(const VariableMetaDataList &vmd_list)
Checks hash function values.
bool applyFilter(IVariable &var) override
Applies the filter to the variable var.
Writing management for variables.
void _generateMeshesMetaData(JSONWriter &json_writer, XmlNode meshes_node)
void writeVariables(IDataWriter *writer, const VariableCollection &vars, bool use_hash)
Information characterizing a variable.
void build() override
Constructs the instance members.
void initializeVariables(bool is_continue) override
Initializes the variables.
void addVariable(IVariable *var) override
Adds a variable.
void setAcceleratorMng(Ref< IAcceleratorMng > v) override
Sets the accelerator manager.
void removeVariableRef(VariableRef *var) override
Removes a reference to a variable.
void removeAllShMemVariables() override
void addAutoDestroyVariable(VariableRef *var) override
Adds the variable to the list of variables that are kept until the end of execution.
IAcceleratorMng * acceleratorMng() const override
Manager for accelerators.
void detachMeshVariables(IMesh *mesh) override
Detaches variables associated with the mesh mesh.
void initialize() override
Initializes the instance. The instance is not usable until this method has been called.
void removeVariable(IVariable *var) override
Removes a variable.
void addVariableRef(VariableRef *var) override
Adds a reference to a variable.
void removeAllVariables() override
Removes and destroys the variables managed by this manager.
ISubDomain * internalSubDomain() const override
Temporary internal function to retrieve the subdomain.
Variable manager.
Definition VariableMng.h:57
IVariable * checkVariable(const VariableInfo &infos) override
Checks a variable.
Real exportSize(const VariableCollection &vars) override
IObservable * writeObservable() override
Observable for variables being written.
IVariable * findVariableFullyQualified(const String &name) override
Returns the fully qualified variable named name or 0 if no such name exists.
void dumpList(std::ostream &, IModule *) override
Displays the list of variables managed by a module.
std::map< String, IVariableFactory * > VariableFactoryMap
Type of the list of variable factories by full name.
IVariableMngInternal * _internalApi() override
Internal Arcane API.
IObservable * readObservable() override
Observable for variables being read.
IVariableUtilities * utilities() const override
Interface of associated utility functions.
void _dumpVariable(const VariableRef &v, std::ostream &o)
Writes the value of variable v to the stream o.
void addVariable(IVariable *var) override
Adds a variable.
ISubDomain * m_sub_domain
Subdomain manager.
void dumpStatsJSON(JSONWriter &writer) override
Writes statistics with the writer writer.
Integer m_generate_name_id
Number used to generate a variable name.
List< VariableRef * > m_auto_create_variables
List of variables created automatically during a restart.
FullNameVariableMap m_full_name_variable_map
List of variables by full name.
void readVariables(IDataReader *, IVariableFilter *) override
Reads all variables.
void writeVariables(IDataWriter *, const VariableCollection &vars) override
Exports the variables.
void detachMeshVariables(IMesh *mesh) override
Detaches variables associated with the mesh mesh.
ITraceMng * traceMng() override
Message manager.
String generateTemporaryVariableName() override
Generates a name for a temporary variable.
void removeVariableRef(VariableRef *) override
Removes a reference to a variable.
ISubDomain * subDomain() override
Sub-domain manager.
IVariable * findVariable(const String &name) override
Returns the variable named name or 0 if no such name exists.
VariableFactoryMap::value_type VariableFactoryPair
Pair of the list of variables by full name.
EventObservable< const VariableStatusChangedEventArgs & > & onVariableAdded() override
Event sent when a variable is created.
VariableCollection usedVariables() override
List of used variables.
void removeVariable(IVariable *var) override
Removes a variable.
void build() override
Constructs the instance members.
void notifyUsedVariableChanged() override
Notifies the manager that a variable's state has changed.
void addVariableRef(VariableRef *ref) override
Adds a reference to a variable.
IVariable * findMeshVariable(IMesh *mesh, const String &name) override
Returns the mesh variable named name or 0 if no such name exists.
void initialize() override
Initializes the instance. The instance is not usable until this method has been called.
ISubDomain * _internalSubDomain() const override
void removeAllVariables() override
Removes and destroys the variables managed by this manager.
VariableRefList m_variables_ref
List of variables.
FullNameVariableMap::value_type FullNameVariablePair
Pair of the list of variables by full name.
void writeCheckpoint(ICheckpointWriter *) override
Writes variables for a checkpoint.
IParallelMng * parallelMng() const override
Associated parallelism manager.
void writePostProcessing(IPostProcessorWriter *writer) override
Writes variables for post-processing.
IVariableSynchronizerMng * synchronizerMng() const override
Interface of the variable synchronization manager.
std::map< String, IVariable * > FullNameVariableMap
Type of the list of variables by full name.
~VariableMng() override
Destroys the manager.
VariableCollection variables() override
List of variables.
void initializeVariables(bool) override
Initializes the variables.
EventObservable< const VariableStatusChangedEventArgs & > & onVariableRemoved() override
Event sent when a variable is destroyed.
void dumpStats(std::ostream &ostr, bool is_verbose) override
Writes statistics about variables to the stream ostr.
VariableMng(ISubDomain *sd)
Constructs a variable manager for the case pn.
void readCheckpoint(ICheckpointReader *) override
Reads all variables from a checkpoint.
Management of variable reading.
Reference to a variable.
Definition VariableRef.h:56
Variable synchronizer manager.
Node of a DOM tree.
Definition XmlNode.h:51
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Collection< VariableRef * > VariableRefCollection
Collection of variables.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
List< VariableRef * > VariableRefList
Array of variable references.
std::int32_t Int32
Signed integer type of 32 bits.
Structure equivalent to the boolean value true.