Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
VariableScalar.cc
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/* VariableScalar.cc (C) 2000-2025 */
9/* */
10/* Scalar variable. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/VariableScalar.h"
15
16#include "arcane/utils/NotSupportedException.h"
17#include "arcane/utils/ArgumentException.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/TraceInfo.h"
20#include "arcane/utils/Ref.h"
21#include "arcane/utils/ITraceMng.h"
22
23#include "arcane/core/VariableDiff.h"
24#include "arcane/core/VariableBuildInfo.h"
25#include "arcane/core/VariableInfo.h"
26#include "arcane/core/IApplication.h"
27#include "arcane/core/IVariableMng.h"
28#include "arcane/core/IItemFamily.h"
29#include "arcane/core/IVariableSynchronizer.h"
30#include "arcane/core/IDataReader.h"
31#include "arcane/core/ItemGroup.h"
32#include "arcane/core/IDataFactoryMng.h"
33#include "arcane/core/IParallelMng.h"
34#include "arcane/core/IMesh.h"
35#include "arcane/core/VariableComparer.h"
36#include "arcane/core/internal/IVariableInternal.h"
37#include "arcane/core/internal/IVariableMngInternal.h"
38
39#include "arcane/core/datatype/DataStorageBuildInfo.h"
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44namespace Arcane
45{
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50template <class DataType>
52: public VariableDiff<DataType>
53{
54 typedef VariableDataTypeTraitsT<DataType> VarDataTypeTraits;
55 typedef typename VariableDiff<DataType>::DiffInfo DiffInfo;
56
57 public:
58
59 //TODO: a simplifier car recopie de ArrayVariableDiff
60 // but for scalar variables there is only one value and no associated group.
63 const VariableComparerArgs& compare_args)
64 {
65 const bool compare_ghost = compare_args.isCompareGhost();
66 ItemGroup group = var->itemGroup();
67 if (group.null())
68 return {};
69 IMesh* mesh = group.mesh();
70 if (!mesh)
71 return {};
72 ITraceMng* msg = mesh->traceMng();
73 IParallelMng* pm = mesh->parallelMng();
74
75 GroupIndexTable* group_index_table = (var->isPartial()) ? group.localIdToIndex().get() : nullptr;
76
77 int nb_diff = 0;
78 bool compare_failed = false;
79 Integer ref_size = ref.size();
80 ENUMERATE_ITEM (i, group) {
81 const Item& item = *i;
82 if (!item.isOwn() && !compare_ghost)
83 continue;
84 Integer index = item.localId();
85 if (group_index_table) {
86 index = (*group_index_table)[index];
87 if (index < 0)
88 continue;
89 }
90
91 DataType diff = DataType();
92 if (index >= ref_size) {
93 ++nb_diff;
94 compare_failed = true;
95 }
96 else {
97 DataType dref = ref[index];
98 DataType dcurrent = current[index];
99 if (VarDataTypeTraits::verifDifferent(dref, dcurrent, diff, true)) {
100 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, item, NULL_ITEM_ID));
101 ++nb_diff;
102 }
103 }
104 }
105 if (compare_failed) {
106 Int32 sid = pm->commRank();
107 const String& var_name = var->name();
108 msg->pinfo() << "Processor " << sid << " : "
109 << " Unable to compare : elements numbers are different !"
110 << " for the variable " << var_name << " ref_size=" << ref_size;
111 }
112 if (nb_diff != 0)
113 this->_sortAndDump(var, pm, compare_args);
114
115 return VariableComparerResults(nb_diff);
116 }
117
119 checkReplica(IVariable* var, const DataType& var_value,
120 const VariableComparerArgs& compare_args)
121 {
122 IParallelMng* replica_pm = var->_internalApi()->replicaParallelMng();
123 if (!replica_pm)
124 return {};
125 const int max_print = compare_args.maxPrint();
126 // Calls the correct specialization to ensure the template type has
127 // the reduction.
128 using ReduceType = typename VariableDataTypeTraitsT<DataType>::HasReduceMinMax;
129 if constexpr (std::is_same<TrueType, ReduceType>::value)
130 return _checkReplica2(replica_pm, var_value);
131 ARCANE_UNUSED(replica_pm);
132 ARCANE_UNUSED(var);
133 ARCANE_UNUSED(var_value);
134 ARCANE_UNUSED(max_print);
135 throw NotSupportedException(A_FUNCINFO);
136 }
137
138 private:
139
141 _checkReplica2(IParallelMng* pm, const DataType& var_value)
142 {
143 Int32 nb_rank = pm->commSize();
144 if (nb_rank == 1)
145 return {};
146
147 DataType max_value = pm->reduce(Parallel::ReduceMax, var_value);
148 DataType min_value = pm->reduce(Parallel::ReduceMin, var_value);
149
150 Integer nb_diff = 0;
151 DataType diff = DataType();
152 if (VarDataTypeTraits::verifDifferent(min_value, max_value, diff, true)) {
153 this->m_diffs_info.add(DiffInfo(min_value, max_value, diff, 0, NULL_ITEM_ID));
154 ++nb_diff;
155 }
156 return VariableComparerResults(nb_diff);
157 }
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165
166template <typename T> VariableScalarT<T>::
168: Variable(v, info)
169, m_value(nullptr)
170{
171 IDataFactoryMng* df = v.dataFactoryMng();
172 DataStorageBuildInfo storage_build_info(v.traceMng());
173 String storage_full_type = info.storageTypeInfo().fullName();
174 Ref<IData> data = df->createSimpleDataRef(storage_full_type, storage_build_info);
175 m_value = dynamic_cast<ValueDataType*>(data.get());
176 _setData(makeRef(m_value));
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182template <typename T> auto VariableScalarT<T>::
183getReference(const VariableBuildInfo& vb, const VariableInfo& vi) -> ThatClass*
184{
185 ThatClass* true_ptr = 0;
186 IVariableMng* vm = vb.variableMng();
187 IVariable* var = vm->checkVariable(vi);
188 if (var)
189 true_ptr = dynamic_cast<ThatClass*>(var);
190 else {
191 true_ptr = new ThatClass(vb, vi);
192 vm->_internalApi()->addVariable(true_ptr);
193 }
194 ARCANE_CHECK_PTR(true_ptr);
195 return true_ptr;
196}
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
201template <typename T> VariableScalarT<T>* VariableScalarT<T>::
203{
204 if (!var)
205 throw ArgumentException(A_FUNCINFO, "null variable");
206 ThatClass* true_ptr = dynamic_cast<ThatClass*>(var);
207 if (!true_ptr)
208 ARCANE_FATAL("Can not build a reference from variable {0}", var->name());
209 return true_ptr;
210}
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
214
215// Use a Helper function to specialize the call in the
216// case of the 'Byte' type because ArrayVariableDiff::checkReplica() uses
217// a Min/Max reduction and this does not exist in MPI for the Byte type.
218namespace
219{
220 template <typename T> VariableComparerResults
221 _checkIfSameOnAllReplicaHelper(IVariable* var, const T& value,
222 const VariableComparerArgs& compare_args)
223 {
225 return csa.checkReplica(var, value, compare_args);
226 }
227
228 // Specialization for the 'Byte' type which does not support reductions.
230 _checkIfSameOnAllReplicaHelper(IVariable* var, const Byte& value,
231 const VariableComparerArgs& compare_args)
232 {
233 Integer int_value = value;
235 return csa.checkReplica(var, int_value, compare_args);
236 }
237} // namespace
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
243_compareVariable(const VariableComparerArgs& compare_args)
244{
245 switch (compare_args.compareMode()) {
247
248 if (itemKind() == IK_Particle)
249 return {};
250 IDataReader* reader = compare_args.dataReader();
251 ARCANE_CHECK_POINTER(reader);
252 T from(value());
253 T ref = T();
254 Ref<IScalarDataT<T>> ref_data(m_value->cloneTrueEmptyRef());
255 reader->read(this, ref_data.get());
256 ref = ref_data->value();
257 ConstArrayView<T> from_array(1, &from);
258 ConstArrayView<T> ref_array(1, &ref);
260 VariableComparerResults r = csa.check(this, ref_array, from_array, compare_args);
261 return r;
262 }
264 return {};
266 VariableComparerResults r = _checkIfSameOnAllReplicaHelper(this, value(), compare_args);
267 return r;
268 }
269 }
270 ARCANE_FATAL("Invalid value for compare mode '{0}'", (int)compare_args.compareMode());
271}
272
273/*---------------------------------------------------------------------------*/
274/*---------------------------------------------------------------------------*/
275
276template <typename T> void VariableScalarT<T>::
277print(std::ostream& o) const
278{
279 o << m_value->value();
280}
281
282/*---------------------------------------------------------------------------*/
283/*---------------------------------------------------------------------------*/
284
285template <typename T> void VariableScalarT<T>::
287{
288 // Nothing to do for scalar variables
289}
290
291/*---------------------------------------------------------------------------*/
292/*---------------------------------------------------------------------------*/
293
294template <typename T> void VariableScalarT<T>::
296{
297 // Nothing to do for scalar variables
298 ARCANE_UNUSED(local_ids);
299}
300
301/*---------------------------------------------------------------------------*/
302/*---------------------------------------------------------------------------*/
303
304template <typename T> Real VariableScalarT<T>::
305allocatedMemory() const
306{
307 return static_cast<Real>(sizeof(T));
308}
309
310/*---------------------------------------------------------------------------*/
311/*---------------------------------------------------------------------------*/
312
313template <typename T> void VariableScalarT<T>::
315{
316 ARCANE_UNUSED(source);
317 ARCANE_UNUSED(destination);
318}
319
320/*---------------------------------------------------------------------------*/
321/*---------------------------------------------------------------------------*/
322
323template <typename T> void VariableScalarT<T>::
325 Int32ConstArrayView second_source,
326 Int32ConstArrayView destination)
327{
328 ARCANE_UNUSED(first_source);
329 ARCANE_UNUSED(second_source);
330 ARCANE_UNUSED(destination);
331}
332
333/*---------------------------------------------------------------------------*/
334/*---------------------------------------------------------------------------*/
335
336template <typename T> void VariableScalarT<T>::
337compact(Int32ConstArrayView new_to_old_ids)
338{
339 ARCANE_UNUSED(new_to_old_ids);
340}
341
342/*---------------------------------------------------------------------------*/
343/*---------------------------------------------------------------------------*/
344
345template <typename T> void VariableScalarT<T>::
347{
348}
349
350/*---------------------------------------------------------------------------*/
351/*---------------------------------------------------------------------------*/
352
353template <typename T> void VariableScalarT<T>::
354setIsSynchronized(const ItemGroup& group)
355{
356 ARCANE_UNUSED(group);
357}
358
359/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
361
362template <typename DataType> void
364swapValues(ThatClass& rhs)
365{
366 _checkSwapIsValid(&rhs);
367 m_value->swapValues(rhs.m_value);
368 // References must be updated for this variable and \a rhs.
369 syncReferences();
370 rhs.syncReferences();
371}
372
373/*---------------------------------------------------------------------------*/
374/*---------------------------------------------------------------------------*/
375
376ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableScalarT);
377template class VariableScalarT<String>;
378
379/*---------------------------------------------------------------------------*/
380/*---------------------------------------------------------------------------*/
381
382} // End namespace Arcane
383
384/*---------------------------------------------------------------------------*/
385/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define ENUMERATE_ITEM(name, group)
Generic enumerator for a node group.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Information to construct an instance of 'IData'.
Interface for reading variable data.
Definition IDataReader.h:35
virtual void read(IVariable *var, IData *data)=0
Reads the data data of the variable var.
Interface of the parallelism manager for a subdomain.
virtual Int32 commRank() const =0
Rank of this instance in the communicator.
virtual Int32 commSize() const =0
Number of instances in the communicator.
virtual char reduce(eReduceType rt, char v)=0
Performs a reduction of type rt on the real v and returns the value.
virtual TraceMessage pinfo()=0
Stream for a parallel information message.
Variable manager interface.
virtual IVariable * checkVariable(const VariableInfo &infos)=0
Checks a variable.
virtual IVariableMngInternal * _internalApi()=0
Internal Arcane API.
Interface of a variable.
Definition IVariable.h:40
virtual bool isPartial() const =0
Indicates if the variable is partial.
virtual ItemGroup itemGroup() const =0
Associated mesh group.
virtual String name() const =0
Variable name.
virtual IVariableInternal * _internalApi()=0
Internal Arcane API.
Mesh entity group.
Definition ItemGroup.h:51
SharedPtrT< GroupIndexTable > localIdToIndex() const
Table of local ids to a position for all entities in the group.
Definition ItemGroup.h:312
bool null() const
true means the group is the null group
Definition ItemGroup.h:75
IMesh * mesh() const
Mesh to which this group belongs (0 for the null group).
Definition ItemGroup.h:131
Base class for a mesh element.
Definition Item.h:84
constexpr Int32 localId() const
Local identifier of the entity in the processor subdomain.
Definition Item.h:233
constexpr bool isOwn() const
true if the entity belongs to the subdomain
Definition Item.h:267
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
TraceMessage info() const
Flow for an information message.
Parameters necessary for building a variable.
Arguments for VariableComparer methods.
Results of a comparison operation.
Information characterizing a variable.
IData * data() override
Data associated with the variable.
void copyItemsValues(Int32ConstArrayView source, Int32ConstArrayView destination) override
Copies the values of entities numbered source into entities numbered destination.
VariableScalarT(const VariableBuildInfo &v, const VariableInfo &vi)
Constructs a variable based on the reference v.
void setIsSynchronized() override
Indicates that the variable is synchronized.
void synchronize() override
Synchronizes the variable.
void copyItemsMeanValues(Int32ConstArrayView first_source, Int32ConstArrayView second_source, Int32ConstArrayView destination) override
Copies the mean values of entities numbered first_source and second_source into entities numbered des...
VariableComparerResults _compareVariable(const VariableComparerArgs &compare_args) final
Comparison of values between variables.
void print(std::ostream &o) const override
Prints the variable's values to the stream o.
void compact(Int32ConstArrayView new_to_old_ids) override
Compresses the variable's values.
Real allocatedMemory() const override
Memory size (in Bytes) used by the variable.
Variable(const VariableBuildInfo &v, const VariableInfo &vi)
Creates a variable linked to the reference v.
Definition Variable.cc:344
void _setData(const Ref< IData > &data)
Positions the data.
Definition Variable.cc:918
eItemKind itemKind() const override
Kind of mesh entities on which the variable is based.
Definition Variable.cc:873
@ ReduceMin
Minimum of values.
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
@ SameOnAllReplica
Checks that the variable values are the same on all replicas.
@ Same
Compares with a reference.
@ Sync
Checks that the variable is synchronized.
@ IK_Particle
Particle mesh entity.
double Real
Type representing a real number.
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.