14#include "arcane/utils/NotSupportedException.h"
15#include "arcane/utils/ArgumentException.h"
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/TraceInfo.h"
18#include "arcane/utils/Ref.h"
19#include "arcane/utils/MemoryAccessInfo.h"
20#include "arcane/utils/MemoryAllocator.h"
21#include "arccore/common/AlignedMemoryAllocator.h"
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"
37#include "arcane/core/datatype/DataTracer.h"
38#include "arcane/core/datatype/DataTypeTraits.h"
39#include "arcane/core/datatype/DataStorageBuildInfo.h"
41#include "arcane/core/VariableArray.h"
42#include "arcane/core/RawCopy.h"
44#include "arcane/core/internal/IDataInternal.h"
45#include "arcane/core/internal/IVariableMngInternal.h"
46#include "arcane/core/internal/IVariableInternal.h"
57template <
class DataType>
63 static constexpr bool IsNumeric = std::is_same_v<typename VarDataTypeTraits::IsNumeric, TrueType>;
64 using NormType =
typename VarDataTypeTraits::NormType;
72 const bool compare_ghost = compare_args.isCompareGhost();
74 return _checkAsArray(var, ref, current, compare_args);
88 bool compare_failed =
false;
95 NormType local_norm_max = {};
97 if constexpr (IsNumeric) {
99 if (is_use_local_norm) {
103 if (!item.
isOwn() && !compare_ghost)
106 if (group_index_table) {
107 index = (*group_index_table)[index];
111 if (index >= ref_size) {
115 DataType dref = ref[index];
116 NormType norm_max = VarDataTypeTraits::normeMax(dref);
117 if (norm_max > local_norm_max) {
118 local_norm_max = norm_max;
127 if (!item.
isOwn() && !compare_ghost)
130 if (group_index_table) {
131 index = (*group_index_table)[index];
135 DataType diff = DataType();
136 if (index >= ref_size) {
138 compare_failed =
true;
141 DataType dref = ref[index];
142 DataType dcurrent = current[index];
143 bool is_diff = _computeDifference(dref, dcurrent, diff, local_norm_max, diff_method);
145 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, item, NULL_ITEM_ID));
150 if (compare_failed) {
153 msg->
pinfo() <<
"Processor " << sid <<
" : "
154 <<
"comparison impossible because the number of the elements is different "
155 <<
" for the variable " << var_name <<
" ref_size=" << ref_size;
158 this->_sortAndDump(var, pm, compare_args);
172 if constexpr (std::is_same<TrueType, ReduceType>::value)
173 return _checkReplica2(replica_pm, var, var_value, compare_args);
175 ARCANE_UNUSED(replica_pm);
177 ARCANE_UNUSED(var_value);
178 ARCANE_UNUSED(compare_args);
192 bool compare_failed =
false;
199 NormType local_norm_max = {};
201 if constexpr (IsNumeric) {
203 if (is_use_local_norm) {
205 for (
Integer index = 0; index < current_size; ++index) {
206 if (index >= ref_size) {
210 DataType dref = ref[index];
211 typename VarDataTypeTraits::NormType norm_max = VarDataTypeTraits::normeMax(dref);
212 if (norm_max > local_norm_max) {
213 local_norm_max = norm_max;
220 for (
Integer index = 0; index < current_size; ++index) {
221 DataType diff = DataType();
222 if (index >= ref_size) {
224 compare_failed =
true;
227 DataType dref = ref[index];
228 DataType dcurrent = current[index];
229 if (_computeDifference(dref, dcurrent, diff, local_norm_max, diff_method)) {
230 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, index, NULL_ITEM_ID));
235 if (compare_failed) {
238 msg->
pinfo() <<
"Processor " << sid <<
" : "
239 <<
" comparison impossible because the number of elements is different"
240 <<
" for the variable " << var_name <<
" ref_size=" << ref_size;
243 this->_sortAndDump(var, pm, compare_args);
258 if (max_size != min_size) {
260 msg->
info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
261 <<
" because the number of elements is not the same on all the replica "
262 <<
" min=" << min_size <<
" max=" << max_size;
271 for (
Integer index = 0; index < size; ++index) {
272 DataType diff = DataType();
273 DataType min_val = min_values[index];
274 DataType max_val = max_values[index];
275 if (VarDataTypeTraits::verifDifferent(min_val, max_val, diff,
true)) {
276 this->m_diffs_info.add(DiffInfo(min_val, max_val, diff, index, NULL_ITEM_ID));
281 this->_sortAndDump(var, pm, compare_args);
285 bool _computeDifference(
const DataType& dref,
const DataType& dcurrent, DataType& diff,
286 const NormType& local_norm_max,
289 bool is_diff =
false;
290 switch (diff_method) {
292 is_diff = VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true);
295 is_diff = VarDataTypeTraits::verifDifferentNorm(dref, dcurrent, diff, local_norm_max,
true);
315 String storage_full_type =
info.storageTypeInfo().fullName();
316 Ref<IData> data = df->createSimpleDataRef(storage_full_type, storage_build_info);
317 m_value =
dynamic_cast<ValueDataType*
>(
data.get());
333template <
typename T> VariableArrayT<T>* VariableArrayT<T>::
334getReference(
const VariableBuildInfo& vb,
const VariableInfo& vi)
338 ThatClass* true_ptr =
nullptr;
342 true_ptr =
dynamic_cast<ThatClass*
>(var);
344 true_ptr =
new ThatClass(vb, vi);
345 vm->_internalApi()->addVariable(true_ptr);
347 ARCANE_CHECK_PTR(true_ptr);
359 auto* true_ptr =
dynamic_cast<ThatClass*
>(var);
361 ARCANE_FATAL(
"Can not build a reference from variable {0}", var->name());
369print(std::ostream& o)
const
373 o <<
"(dimension=" << size <<
") ";
393 itemGroup().synchronizer()->synchronize(
this);
410 itemGroup().synchronizer()->synchronize(
this, local_ids);
422 Real v2 = (
Real)(m_value->view().size());
439 return csa.checkReplica(var, values, compare_args);
443 VariableComparerResults
444 _checkIfSameOnAllReplicaHelper(IVariable* var, ConstArrayView<Byte> values,
445 const VariableComparerArgs& compare_args)
448 UniqueArray<Integer> int_values(size);
449 for (
Integer i = 0; i < size; ++i)
450 int_values[i] = values[i];
451 ArrayVariableDiff<Integer> csa;
452 return csa.checkReplica(var, int_values, compare_args);
462 switch (compare_args.compareMode()) {
473 reader->
read(
this, ref_data.
get());
483 ValueType& data_values = m_value->_internal()->_internalDeprecatedValue();
489 data_values.
copy(ref_array);
497 ARCANE_FATAL(
"Invalid value for compare mode '{0}'", (
int)compare_args.compareMode());
526 error() << String::format(
"Can not convert the string '{0}' to type '{1}'",
552 <<
") of the group `" << group.
name() <<
"' does not match "
563 ARCANE_ASSERT(source.
size() == destination.
size(),
564 (
"Impossible to copy: source and destination of different sizes !"));
567 for (
Integer i = 0; i < size; ++i)
568 value[destination[i]] = value[source[i]];
580 ARCANE_ASSERT((first_source.
size() == destination.
size()) && (second_source.
size() == destination.
size()),
581 (
"Impossible to copy: source and destination of different sizes !"));
584 for (
Integer i = 0; i < size; ++i) {
585 value[destination[i]] = (T)((value[first_source[i]] + value[second_source[i]]) / 2);
611 m_value->resize(new_size);
614 for (
Integer i = 0; i < new_size; ++i)
615 current_value.setAt(i, old_value.
at(new_to_old_ids[i]));
618 for (
Integer i = 0; i < new_size; ++i)
639 ARCANE_UNUSED(group);
648 Int32 new_size = resize_args.newSize();
649 Int32 nb_additional_element = resize_args.nbAdditionalCapacity();
650 bool use_no_init = resize_args.isUseNoInit();
652 auto* value_internal = m_value->_internal();
655 const bool is_collective_allocator = value_internal->_internalDeprecatedValue().allocator()->isCollective();
656 if (is_collective_allocator) {
657 value_internal->reserve(new_size + nb_additional_element);
659 else if (nb_additional_element != 0) {
660 Integer capacity = value_internal->capacity();
661 if (new_size > capacity)
662 value_internal->reserve(new_size + nb_additional_element);
668 Integer current_size = m_value->view().size();
672 value_internal->dispose();
675 value_internal->_internalDeprecatedValue().resizeNoInit(new_size);
677 value_internal->resize(new_size);
678 if (new_size > current_size) {
681 for (
Integer i = current_size; i < new_size; ++i)
687 if (use_nan || use_nan2) {
696 if (m_value->view().size() < value_internal->capacity()) {
697 value_internal->shrink();
707 Int64 capacity = value_internal->capacity();
718 _resize(VariableResizeArgs(n, nb_additional));
727 m_value->_internal()->shrink();
737 return m_value->_internal()->capacity();
743template <
typename DataType>
void VariableArrayT<DataType>::
744fill(
const DataType& value)
746 m_value->view().fill(value);
755 ARCANE_UNUSED(group);
762template <
typename DataType>
void
766 _checkSwapIsValid(&rhs);
769 m_value->swapValues(rhs.m_value);
772 rhs.syncReferences();
785 bool is_ok = (first_source.
size() == dsize) && (second_source.
size() == dsize);
787 ARCANE_FATAL(
"Unable to copy: source and destination of different sizes !");
791 for (
Integer i = 0; i < size; ++i)
792 value[destination[i]] = value[first_source[i]];
802 return m_value->_internal()->_internalDeprecatedValue();
808ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableArrayT);
809template class VariableArrayT<String>;
#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_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
static constexpr Integer simdAlignment()
Alignment for structures using vectorization.
Exception when an argument is invalid.
Modifiable view of an array of type T.
T & at(Int64 i)
Element at index i. Always checks for overflows.
void copy(Span< const T > rhs)
Copies the values from rhs into the instance.
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.
virtual void read(IVariable *var, IData *data)=0
Reads the data data of the variable var.
Interface of an entity family.
virtual IVariableSynchronizer * allItemsSynchronizer()=0
Synchronizer on all entities of the family.
Interface of the parallelism manager for a subdomain.
virtual ITraceMng * traceMng() const =0
Trace manager.
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.
virtual TraceMessage info()=0
Stream for an information message.
virtual IParallelMng * parallelMng() const =0
Associated parallelism manager.
virtual IVariable * checkVariable(const VariableInfo &infos)=0
Checks a variable.
virtual void synchronize(IVariable *var)=0
Synchronizes the variable var in blocking mode.
virtual eItemKind itemKind() const =0
Kind of mesh entities on which the variable is based.
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.
virtual IVariableMng * variableMng() const =0
Variable manager associated with the variable.
const String & name() const
Group name.
SharedPtrT< GroupIndexTable > localIdToIndex() const
Table of local ids to a position for all entities in the group.
IItemFamily * itemFamily() const
Entity family to which this group belongs (0 for the null group).
eItemKind itemKind() const
Group kind. This is the kind of its elements.
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).
Base class for a mesh element.
constexpr Int32 localId() const
Local identifier of the entity in the processor subdomain.
constexpr bool isOwn() const
true if the entity belongs to the subdomain
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
Unicode character string.
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flow for a debug message.
TraceMessage info() const
Flow for an information message.
TraceMessage error() const
Flow for an error message.
1D data vector with value semantics (STL style).
VariableArrayT(const VariableBuildInfo &v, const VariableInfo &vi)
Construit une variable basée sur la référence v.
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...
void shrinkMemory() override
Frees any additional memory allocated for the data.
void compact(Int32ConstArrayView old_to_new_ids) override
Compresses the variable's values.
bool initialize(const ItemGroup &group, const String &value) override
Initializes the variable.
void copyItemsValues(Int32ConstArrayView source, Int32ConstArrayView destination) override
Copies the values of entities numbered source into entities numbered destination.
IData * data() override
Data associated with the variable.
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 setIsSynchronized() override
Indicates that the variable is synchronized.
Real allocatedMemory() const override
Memory size (in Bytes) used by the variable.
void synchronize() override
Synchronizes the variable.
Parameters necessary for building a variable.
Arguments for VariableComparer methods.
Results of a comparison operation.
Information characterizing a variable.
Variable(const VariableBuildInfo &v, const VariableInfo &vi)
Creates a variable linked to the reference v.
void _setData(const Ref< IData > &data)
Positions the data.
bool isPartial() const override
Indicates if the variable is partial.
IVariableMng * variableMng() const override
Variable manager associated with the variable.
String name() const final
Variable name.
ItemGroup itemGroup() const final
Associated mesh group.
void syncReferences() override
Synchronizes references.
String fullName() const final
Full variable name (with family prefix).
eItemKind itemKind() const override
Kind of mesh entities on which the variable is based.
IItemFamily * itemFamily() const final
Associated entity family.
eDataType dataType() const override
Data type managed by the variable (Real, Integer, ...).
void fill(MutableMemoryView destination, ConstMemoryView source, const RunQueue *run_queue=nullptr)
Fills a memory region with a value.
@ ReduceMin
Minimum of values.
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool arcaneIsCheck()
True if running in check mode.
eDataInitialisationPolicy
Possible data initialization policy.
@ DIP_InitInitialWithNanResizeWithDefault
Initialization with NaN upon creation and default constructor thereafter.
@ DIP_InitWithNan
Initialization with NaN (Not a Number).
@ DIP_InitWithDefault
Initialization with the default constructor.
std::int64_t Int64
Signed integer type of 64 bits.
eVariableComparerComputeDifferenceMethod
Method used to calculate the difference between two values v1 and v2.
@ Relative
Uses (v1-v2) / v1.
@ LocalNormMax
Uses (v1-v2) / local_norm_max.
eDataInitialisationPolicy getGlobalDataInitialisationPolicy()
Gets the initialization policy for variables.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
@ SameOnAllReplica
Checks that the variable values are the same on all replicas.
@ Same
Compares with a reference.
@ Sync
Checks that the variable is synchronized.
eItemKind
Mesh entity type.
@ IK_Particle
Particle mesh entity.
@ IK_Unknown
Unknown or uninitialized mesh entity.
const char * itemKindName(eItemKind kind)
Entity kind name.
double Real
Type representing a real number.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.