14#include "arcane/impl/internal/VariableMng.h"
16#include "arcane/utils/Deleter.h"
17#include "arcane/utils/StringBuilder.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/PlatformUtils.h"
20#include "arcane/utils/JSONWriter.h"
21#include "arcane/utils/OStringStream.h"
22#include "arcane/utils/Math.h"
24#include "arcane/core/ArcaneException.h"
25#include "arcane/core/VarRefEnumerator.h"
26#include "arcane/core/IModule.h"
27#include "arcane/core/ISubDomain.h"
28#include "arcane/core/IObservable.h"
29#include "arcane/core/VariableInfo.h"
30#include "arcane/core/VariableBuildInfo.h"
31#include "arcane/core/VariableFactoryRegisterer.h"
32#include "arcane/core/IVariableFactory.h"
33#include "arcane/core/ItemGroup.h"
34#include "arcane/core/IMesh.h"
35#include "arcane/core/MeshHandle.h"
36#include "arcane/core/IParallelMng.h"
37#include "arcane/core/ITimeLoopMng.h"
38#include "arcane/core/IEntryPoint.h"
39#include "arcane/core/Properties.h"
40#include "arcane/core/VariableStatusChangedEventArgs.h"
42#include "arcane/impl/VariableUtilities.h"
43#include "arcane/impl/internal/VariableSynchronizerMng.h"
81, m_vni_map(2000, true)
85, m_variable_io_writer_mng(new VariableIOWriterMng(this))
86, m_variable_io_reader_mng(new VariableIOReaderMng(this))
102 delete m_variable_synchronizer_mng;
104 delete m_variable_io_reader_mng;
105 delete m_variable_io_writer_mng;
108 m_write_observable->detachAllObservers();
109 m_read_observable->detachAllObservers();
111 delete m_write_observable;
112 delete m_read_observable;
114 m_variable_factories.each(
Deleter());
125 VariableRef::setTraceCreation(
true);
134 m_time_stats = m_parallel_mng->timeStats();
141 if (m_variable_factory_map.find(full_name) != m_variable_factory_map.end()) {
142 ARCANE_FATAL(
"VariableFactoryMap already contains a factory for the same type '{0}'",
146 m_variable_factories.add(vf);
147 info(5) <<
"Add variable factory kind=" << vff->
itemKind()
151 <<
" full_name=" << full_name
156 m_variable_synchronizer_mng->initialize();
175 remaining_vars.
add(v);
180 const bool has_remaining_vars = !remaining_vars.
empty();
181 if (has_remaining_vars && is_check)
182 pwarning() <<
"The following variables are still referenced: "
184 <<
" (set the environment variable ARCANE_TRACE_VARIABLE_CREATION"
185 <<
" to get the stack trace)";
186 bool has_trace = VariableRef::hasTraceCreation();
188 for (
const auto& i : remaining_vars) {
191 info() <<
" variable name=" << var->
name()
201 if (has_remaining_vars) {
206 for (
const auto& i : remaining_vars)
208 remaining_refs.
add(*ivar);
210 r->unregisterVariable();
242 ++m_nb_created_variable_reference;
265 info(5) <<
"Add variable"
266 <<
" name=" << var->
name()
267 <<
" full_name=" << full_name
272 vni.m_local_name = var->
name();
275 m_vni_map.add(vni, var);
278 m_variables_changed =
true;
279 m_used_variables_changed =
true;
280 ++m_nb_created_variable;
284 module = ep->module();
285 m_variable_creation_modules.insert(std::make_pair(var, module));
287 m_on_variable_added.notify(eargs);
305 m_on_variable_removed.notify(eargs);
309 if (!var_group.
null())
315 vni.m_local_name = var->
name();
318 m_vni_map.remove(vni);
320 m_variables_changed =
true;
321 m_used_variables_changed =
true;
322 m_variable_creation_modules.erase(var);
347 ARCANE_FATAL(
"Mesh variable has the same name that a global variable (name={0})", check_name);
354 ARCANE_FATAL(
"Global variable has the same name that a mesh variable (name={0})", check_name);
363 VNIMap::Data* var_data = m_vni_map.lookup(vni);
368 var = var_data->value();
416 mesh_name = mesh_name +
"_";
463 info() <<
"** GENERATED NAME =" << name;
473 o <<
" ** VariableMng::Variable list\n";
476 if ((*ivar)->module() != c)
489 o <<
" ** VariableMng::Variable list\n";
498 mem_used += i.second->allocatedMemory();
499 o <<
" ** VariableMng::Allocated memory : " << mem_used;
510 o <<
" ** Variable: " << &var <<
" : ";
512 o << var.
name() <<
" = ";
514 o <<
" (Type " << var.
dataType() <<
")\n";
523 ARCANE_UNUSED(is_continue);
525 info() <<
"Initialisation des variables";
530 if (module && !module->
used())
535 var_ref->setUsed(
true);
548 if ((*ivar)->module() == c)
560 if (m_variables_changed) {
561 m_variables_changed =
false;
564 m_variables.add(i.second);
576 if (m_used_variables_changed) {
577 m_used_variables_changed =
false;
578 m_used_variables.clear();
582 m_used_variables.add(var);
585 return m_used_variables;
600 if (
mesh && !
mesh->properties()->getBool(
"dump"))
611 m_variable_io_writer_mng->writeCheckpoint(service);
620 m_variable_io_writer_mng->writePostProcessing(post_processor);
629 m_variable_io_writer_mng->writeVariables(writer, vars,
false);
638 m_variable_io_writer_mng->writeVariables(writer, filter,
false);
647 auto i = m_variable_factory_map.find(full_type);
648 if (i == m_variable_factory_map.end())
649 ARCANE_FATAL(
"No factory to create variable name={0} type={1}", vbi.name(), full_type);
652 info(5) <<
"Automatic creation of the variable"
653 <<
" name=" << vbi.name()
654 <<
" family=" << vbi.itemFamilyName()
655 <<
" type=" << full_type
673 m_variable_io_reader_mng->readCheckpoint(service);
682 m_variable_io_reader_mng->readCheckpoint(infos);
691 m_variable_io_reader_mng->readVariables(reader, filter);
714 for (VariableCollection::Enumerator i(vars); ++i;) {
731 return m_variable_synchronizer_mng;
749dumpStats(std::ostream& ostr,
bool is_verbose)
752 ostr <<
"\nMemory statistics for variables:\n";
753 ostr <<
" Number of created variables: " << m_nb_created_variable <<
'\n';
754 ostr <<
" Number of created variables reference: " << m_nb_created_variable_reference <<
'\n';
756 ostr <<
" Number of currently reference: " <<
m_variables_ref.count() <<
'\n';
761 Real mem_used[NB_ITEM_KIND];
762 Real mem_used_array = 0.;
763 Real mem_used_particle = 0.;
764 Real mem_used_link = 0.;
765 Real total_mem_used = 0.;
769 for (
Integer i = 0; i < NB_ITEM_KIND; ++i) {
781 typedef std::map<IModule*, std::set<IVariable*>> ModuleVariableMap;
782 std::set<IVariable*> variables_with_module;
785 ModuleVariableMap modules_variables;
800 var_module = m_variable_creation_modules[var];
802 variables_with_module.insert(var);
803 modules_variables[var_module].insert(var);
816 memory_sorted_variables.
add(var);
819 if (variables_with_module.find(var) == variables_with_module.end())
820 modules_variables[0].insert(var);
824 total_mem_used += mem;
826 ostr <<
"Var: <" << var->
name() <<
"> Kind=" <<
itemKindName(ik) <<
" Mem=" << mem <<
'\n';
837 mem_used_particle += mem;
841 mem_used_array += mem;
847 ostr <<
"Memory repartition by module:\n";
866 for (ModuleVariableMap::const_iterator imodvar = modules_variables.begin();
867 imodvar != modules_variables.end(); ++imodvar) {
868 IModule* module = imodvar->first;
869 Real private_mem_used = 0.0;
870 Real shared_mem_used = 0.0;
871 for (std::set<IVariable*>::const_iterator i = imodvar->second.begin();
872 i != imodvar->second.end(); ++i) {
877 private_mem_used += mem_used2;
879 shared_mem_used += mem_used2;
882 <<
" Mem=" << mem_used <<
" private?=" << is_private <<
'\n';
884 String module_name =
"None";
886 module_name =
module->name();
887 Real module_mem_used = private_mem_used + shared_mem_used;
890 <<
Trace::Width(12) << String::fromNumber(private_mem_used / 1e6, 3)
891 <<
Trace::Width(12) << String::fromNumber(shared_mem_used / 1e6, 3)
892 <<
Trace::Width(12) << String::fromNumber(module_mem_used / 1e6, 3)
893 <<
Trace::Width(7) << String::fromNumber(100.0 * module_mem_used / total_mem_used, 1) <<
"%"
894 <<
Trace::Width(12) << String::fromNumber(module_mem_used / ((
Real)nb_cell * 1000.0), 2)
902 <<
Trace::Width(12) << String::fromNumber(total_mem_used / 1e6, 3)
904 <<
Trace::Width(13) << String::fromNumber(total_mem_used / ((
Real)nb_cell * 1000.0), 2)
908 for (
Integer i = 0; i < NB_ITEM_KIND; ++i) {
910 <<
" Memory=" << mem_used[i] <<
'\n';
912 ostr <<
"Variable Particle N=" << nb_var_particle
913 <<
" Memory=" << mem_used_particle <<
'\n';
914 ostr <<
"Variable Link N=" << nb_var_link
915 <<
" Memory=" << mem_used_link <<
'\n';
916 ostr <<
"Variable Array N=" << nb_var_array
917 <<
" Memory=" << mem_used_array <<
'\n';
918 ostr <<
"Variable Total N=" << total_nb_var
919 <<
" Memory=" << total_mem_used <<
'\n';
922 std::sort(std::begin(memory_sorted_variables), std::end(memory_sorted_variables),
925 Integer nb_var_to_display = memory_sorted_variables.
size();
927 nb_var_to_display =
math::min(nb_var_to_display, 15);
928 ostr <<
"\nBiggest variables (D=Dump, E=Exchange R=Restore):\n";
935 for (
Integer i = 0; i < nb_var_to_display; ++i) {
936 IVariable* var = memory_sorted_variables[i];
946 is_no_exchange =
true;
948 properties += (is_no_exchange) ? pr_false : pr_true;
952 <<
Trace::Width(14) << String::fromNumber(mem_used2 / 1e3, 3)
965 writer.writeKey(
"Variables");
973 writer.write(
"Used", var->
isUsed());
975 writer.write(
"Name", var->
name());
980 writer.write(
"Mesh", var->
meshName());
983 writer.write(
"AllocatedMemory", mem);
993_removeAllShMemVariables()
1007 for (IVariable* var : shmem_vars) {
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer size() const
Number of elements in the vector.
bool empty() const
Capacity (number of allocated elements) of the vector.
void add(ConstReferenceType val)
Adds element val to the end of the array.
Exception for an invalid partial variable item group name.
Exception for an invalid variable kind/type.
Checkpoint reading information.
Integer count() const
Number of elements in the collection.
Utility class for destroying objects allocated by new.
Interface for the protection/recovery reading service.
Interface of the checkpoint/recovery write service.
Interface for reading variable data.
Interface for writing variable data.
Interface of a module entry point.
virtual CellGroup allCells()=0
Group of all cells.
virtual bool used() const =0
true if the module is used.
Interface of an observable.
Interface for a writer for post-processing information.
Interface of the subdomain manager.
virtual void checkId(const String &where, const String &id)=0
Checks if an identifier is valid.
virtual IMesh * defaultMesh()=0
Default mesh.
virtual ITimeLoopMng * timeLoopMng()=0
Returns the time loop manager.
virtual IEntryPoint * currentEntryPoint()=0
Returns the entry point currently being executed or 0 if there is none.
Interface for a variable factory.
virtual const String & fullTypeName() const =0
Full name of the variable type.
Functor of a filter applicable to variables.
Variable manager interface.
Interface of the variable synchronization manager.
virtual Real allocatedMemory() const =0
Memory size (in Bytes) used by the variable.
virtual eDataType dataType() const =0
Data type managed by the variable (Real, Integer, ...).
virtual String meshName() const =0
Name of the associated mesh (null if none).
virtual void setUsed(bool v)=0
Sets the usage state of the variable.
virtual String itemFamilyName() const =0
Name of the associated family (null if none).
virtual String fullName() const =0
Full variable name (with family prefix).
@ PNoExchange
Indicates that the variable should not be exchanged.
@ PTemporary
Indicates that the variable is temporary.
@ PInShMem
Indicates that the variable must be allocated in shared memory.
@ PNoRestore
Indicates that the variable should not be restored.
@ PNoDump
Indicates that the variable should not be saved.
virtual int property() const =0
Returns the properties of the variable.
virtual eItemKind itemKind() const =0
Kind of mesh entities on which the variable is based.
virtual Integer nbReference() const =0
Number of references on this variable.
virtual Integer dimension() const =0
Dimension of the variable.
virtual bool isUsed() const =0
Usage state of the variable.
virtual ItemGroup itemGroup() const =0
Associated mesh group.
virtual Integer nbElement() const =0
Number of elements of the variable.
virtual String itemGroupName() const =0
Name of the associated entity group.
virtual MeshHandle meshHandle() const =0
Mesh associated with the variable.
virtual String name() const =0
Variable name.
void detachObserver(const void *ref)
Detaches an observer.
ItemGroupImpl * internal() const
Returns the group implementation.
Integer size() const
Number of elements in the group.
bool null() const
true means the group is the null group
IMesh * mesh() const
Mesh to which this group belongs (0 for the null group).
IMesh * _internalMeshOrNull() const
Output stream linked to a String.
Unicode character string constructor.
Unicode character string.
bool null() const
Returns true if the string is null.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flow for a debug message.
TraceMessage info() const
Flow for an information message.
TraceMessage pwarning() const
Formatting the stream by length.
1D data vector with value semantics (STL style).
Parameters necessary for building a variable.
Variable factory registrar.
Integer dimension() const
Dimension of the variable created by this factory.
IVariableFactory * createFactory()
Creates a factory for this variable type.
Integer multiTag() const
Tag indicating the multiple type (0 if not multiple, 1 if multiple, 2 if multiple deprecated).
eDataType dataType() const
Data type of the variable created by this factory.
eItemKind itemKind() const
Kind of data variables of the variable created by this factory.
VariableFactoryRegisterer * nextVariableFactory() const
Next VariableFactory (0 if the last).
Information characterizing a variable.
bool isPartial() const
Indicates if the variable is partial.
const String & itemFamilyName() const
Name of the entity family to which the variable is associated.
const String & localName() const
Name of the variable.
eItemKind itemKind() const
Mesh entity type.
const String & itemGroupName() const
Name of the entity group to which the variable is associated.
Integer dimension() const
Dimension.
eDataType dataType() const
Element type.
const String & meshName() const
Name of the mesh to which the variable is associated.
const String & fullName() const
Full name of the variable (associated with the family).
IVariable * checkVariable(const VariableInfo &infos) override
Checks a variable.
Real exportSize(const VariableCollection &vars) override
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.
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.
VariableCollection usedVariables() override
List of used variables.
void removeVariable(IVariable *var) override
Removes a variable.
void build() override
Constructs the instance members.
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.
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.
~VariableMng() override
Destroys the manager.
VariableCollection variables() override
List of variables.
void initializeVariables(bool) override
Initializes the variables.
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.
virtual eDataType dataType() const
Variable type (Real, Integer, ...).
virtual void print(std::ostream &o) const
Prints the variable value.
virtual IModule * module() const
Module associated with the variable (or nullptr, if none).
IVariable * variable() const
Associated variable.
String name() const
Variable name.
const String & assignmentStackTrace() const
Call stack at the time of assigning this instance.
Variable sorter based on used memory size.
Arguments of the event notifying a change in the state of a variable.
@ Removed
Variable removed.
Variable synchronizer manager.
Utility functions for variables.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool arcaneIsCheck()
True if running in check mode.
Collection< VariableRef * > VariableRefCollection
Collection of variables.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
eItemKind
Mesh entity type.
@ IK_Particle
Particle mesh entity.
@ IK_Node
Node mesh entity.
@ IK_Cell
Cell mesh entity.
@ IK_Unknown
Unknown or uninitialized mesh entity.
@ IK_Face
Face mesh entity.
@ IK_DoF
Degree of Freedom mesh entity.
@ IK_Edge
Edge mesh entity.
const char * itemKindName(eItemKind kind)
Entity kind name.
double Real
Type representing a real number.
const char * dataTypeName(eDataType type)
Data type name.