Arcane  4.2.3.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 VariableRef* createVariableFromType(const String& full_type, const VariableBuildInfo& vbi) override { return m_variable_mng->_createVariableFromType(full_type, vbi); }
144
145 private:
146
147 VariableMng* m_variable_mng = nullptr;
148 };
149
150 public:
151
153
154 public:
155
156 explicit VariableMng(ISubDomain* sd);
157 ~VariableMng() override;
158
159 public:
160
161 void build() override;
162 void initialize() override;
163 void removeAllVariables() override;
164 void detachMeshVariables(IMesh* mesh) override;
165
166 public:
167
168 ISubDomain* subDomain() override { return m_sub_domain; }
169 IParallelMng* parallelMng() const override { return m_parallel_mng; }
171 IVariable* checkVariable(const VariableInfo& infos) override;
172 void addVariableRef(VariableRef* ref) override;
173 void addVariable(IVariable* var) override;
174 void removeVariableRef(VariableRef*) override;
175 void removeVariable(IVariable* var) override;
176 void dumpList(std::ostream&, IModule*) override;
177 void dumpList(std::ostream&) override;
178 void initializeVariables(bool) override;
180 void variables(VariableRefCollection, IModule*) override;
181 VariableCollection variables() override;
183 void notifyUsedVariableChanged() override { m_used_variables_changed = true; }
184 Real exportSize(const VariableCollection& vars) override;
185 IObservable* writeObservable() override { return m_write_observable; }
186 IObservable* readObservable() override { return m_read_observable; }
187 void writeVariables(IDataWriter*, const VariableCollection& vars) override;
189 void writeCheckpoint(ICheckpointWriter*) override;
190 void writePostProcessing(IPostProcessorWriter* writer) override;
191 void readVariables(IDataReader*, IVariableFilter*) override;
192 void readCheckpoint(ICheckpointReader*) override;
193 void readCheckpoint(const CheckpointReadInfo& infos) override;
194 IVariable* findVariable(const String& name) override;
195 IVariable* findMeshVariable(IMesh* mesh, const String& name) override;
196 IVariable* findVariableFullyQualified(const String& name) override;
197
198 void dumpStats(std::ostream& ostr, bool is_verbose) override;
199 void dumpStatsJSON(JSONWriter& writer) override;
200 IVariableUtilities* utilities() const override { return m_utilities; }
201
204 {
205 return m_on_variable_added;
206 }
207
210 {
211 return m_on_variable_removed;
212 }
214 ISubDomain* _internalSubDomain() const override { return m_sub_domain; }
215 IVariableMngInternal* _internalApi() override { return &m_internal_api; }
216
217 public:
218
219 static bool isVariableToSave(IVariable& var);
220
221 private:
222
224 using FullNameVariableMap = std::map<String, IVariable*>;
226 using FullNameVariablePair = FullNameVariableMap::value_type;
227
229 using VariableFactoryMap = std::map<String, IVariableFactory*>;
231 using VariableFactoryPair = VariableFactoryMap::value_type;
232
235 IParallelMng* m_parallel_mng = nullptr;
236 ITimeStats* m_time_stats = nullptr;
237 InternalApi m_internal_api{ this };
239 VariableList m_variables;
240 VariableList m_used_variables;
241 bool m_variables_changed = true;
242 bool m_used_variables_changed = true;
245 VNIMap m_vni_map;
246 IObservable* m_write_observable = nullptr;
247 IObservable* m_read_observable = nullptr;
250 List<IVariableFactory*> m_variable_factories;
253 VariableFactoryMap m_variable_factory_map;
254
256
257 Int64 m_nb_created_variable_reference = 0;
258 Int64 m_nb_created_variable = 0;
259 // Indicates in which module a variable is created
260 std::map<IVariable*, IModule*> m_variable_creation_modules;
261
262 IVariableUtilities* m_utilities = nullptr;
263 VariableIOWriterMng* m_variable_io_writer_mng = nullptr;
264 VariableIOReaderMng* m_variable_io_reader_mng = nullptr;
265 VariableSynchronizerMng* m_variable_synchronizer_mng = nullptr;
266
267 Ref<IAcceleratorMng> m_accelerator_mng;
268
269 private:
270
272 void _dumpVariable(const VariableRef& v, std::ostream& o);
273
274 static const char* _msgClassName() { return "Variable"; }
275 VariableRef* _createVariableFromType(const String& full_type,
276 const VariableBuildInfo& vbi);
277
278 void _removeAllShMemVariables();
279};
280
281/*---------------------------------------------------------------------------*/
282/*---------------------------------------------------------------------------*/
283
287class VariableIOWriterMng
288: public TraceAccessor
289{
290 private:
291
293 : public IVariableFilter
294 {
295 public:
296
297 bool applyFilter(IVariable& var) override
298 {
299 return VariableMng::isVariableToSave(var);
300 }
301 };
302
303 public:
304
305 explicit VariableIOWriterMng(VariableMng* vm);
306
307 public:
308
309 void writeCheckpoint(ICheckpointWriter* service);
310 void writePostProcessing(IPostProcessorWriter* post_processor);
311 void writeVariables(IDataWriter* writer, const VariableCollection& vars, bool use_hash);
312 void writeVariables(IDataWriter* writer, IVariableFilter* filter, bool use_hash);
313
314 private:
315
316 VariableMng* m_variable_mng = nullptr;
317 bool m_use_hash_v2 = false;
318
319 private:
320
321 void _writeVariables(IDataWriter* writer, const VariableCollection& vars, bool use_hash);
322 String _generateMetaData(const VariableCollection& vars, IHashAlgorithm* hash_algo);
323 void _generateVariablesMetaData(JSONWriter& json_writer, XmlNode variables_node,
324 const VariableCollection& vars, IHashAlgorithm* hash_algo);
325 void _generateMeshesMetaData(JSONWriter& json_writer, XmlNode meshes_node);
326 static const char* _msgClassName() { return "Variable"; }
327};
328
329/*---------------------------------------------------------------------------*/
330/*---------------------------------------------------------------------------*/
331
335class VariableIOReaderMng
336: public TraceAccessor
337{
338 public:
339
340 class IDataReaderWrapper;
342 class DataReaderWrapper;
344
345 public:
346
347 explicit VariableIOReaderMng(VariableMng* vm);
348
349 public:
350
351 void readCheckpoint(ICheckpointReader* service);
352 void readCheckpoint(const CheckpointReadInfo& infos);
353 void readVariables(IDataReader* reader, IVariableFilter* filter);
354
355 private:
356
357 VariableMng* m_variable_mng = nullptr;
358 bool m_is_use_json_metadata = true;
359
360 private:
361
362 void _readVariablesData(VariableReaderMng& var_read_mng, IDataReaderWrapper* reader);
364 void _checkHashFunction(const VariableMetaDataList& vmd_list);
365 void _createVariablesFromMetaData(const VariableMetaDataList& vmd_list);
366 void _readVariablesMetaData(VariableMetaDataList& vmd_list, JSONValue variables_json, const XmlNode& variables_node);
367 void _readMeshesMetaData(JSONValue meshes_json, const XmlNode& meshes_node);
368 void _buildFilteredVariableList(VariableReaderMng& vars_read_mng, IVariableFilter* filter);
369 void _finalizeReadVariables(const VariableList& vars_to_read);
370 static const char* _msgClassName() { return "Variable"; }
371};
372
373/*---------------------------------------------------------------------------*/
374/*---------------------------------------------------------------------------*/
375
376} // namespace Arcane
377
378/*---------------------------------------------------------------------------*/
379/*---------------------------------------------------------------------------*/
380
381#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:78
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:633
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.
VariableRef * createVariableFromType(const String &full_type, const VariableBuildInfo &vbi) override
Method allowing to create a variable with a VariableBuildInfo. Useful to create the same variable fro...
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.