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"
22#include "arcane/core/VariableDiff.h"
23#include "arcane/core/VariableBuildInfo.h"
24#include "arcane/core/VariableInfo.h"
25#include "arcane/core/IApplication.h"
26#include "arcane/core/IVariableMng.h"
27#include "arcane/core/IItemFamily.h"
28#include "arcane/core/IVariableSynchronizer.h"
29#include "arcane/core/IDataReader.h"
30#include "arcane/core/ItemGroup.h"
31#include "arcane/core/IDataFactoryMng.h"
32#include "arcane/core/IParallelMng.h"
33#include "arcane/core/IMesh.h"
34#include "arcane/core/VariableComparer.h"
36#include "arcane/core/datatype/DataTracer.h"
37#include "arcane/core/datatype/DataTypeTraits.h"
38#include "arcane/core/datatype/DataStorageBuildInfo.h"
40#include "arcane/core/VariableArray.h"
41#include "arcane/core/RawCopy.h"
43#include "arcane/core/internal/IDataInternal.h"
44#include "arcane/core/internal/IVariableMngInternal.h"
45#include "arcane/core/internal/IVariableInternal.h"
56template <
class DataType>
62 static constexpr bool IsNumeric = std::is_same_v<typename VarDataTypeTraits::IsNumeric, TrueType>;
63 using NormType =
typename VarDataTypeTraits::NormType;
71 const bool compare_ghost = compare_args.isCompareGhost();
73 return _checkAsArray(var, ref, current, compare_args);
87 bool compare_failed =
false;
94 NormType local_norm_max = {};
96 if constexpr (IsNumeric) {
98 if (is_use_local_norm) {
102 if (!item.
isOwn() && !compare_ghost)
105 if (group_index_table) {
106 index = (*group_index_table)[index];
110 if (index >= ref_size) {
114 DataType dref = ref[index];
115 NormType norm_max = VarDataTypeTraits::normeMax(dref);
116 if (norm_max > local_norm_max) {
117 local_norm_max = norm_max;
126 if (!item.
isOwn() && !compare_ghost)
129 if (group_index_table) {
130 index = (*group_index_table)[index];
134 DataType diff = DataType();
135 if (index >= ref_size) {
137 compare_failed =
true;
140 DataType dref = ref[index];
141 DataType dcurrent = current[index];
142 bool is_diff = _computeDifference(dref, dcurrent, diff, local_norm_max, diff_method);
144 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, item, NULL_ITEM_ID));
149 if (compare_failed) {
152 msg->
pinfo() <<
"Processor " << sid <<
" : "
153 <<
"comparison impossible because the number of the elements is different "
154 <<
" for the variable " << var_name <<
" ref_size=" << ref_size;
157 this->_sortAndDump(var, pm, compare_args);
171 if constexpr (std::is_same<TrueType, ReduceType>::value)
172 return _checkReplica2(replica_pm, var, var_value, compare_args);
174 ARCANE_UNUSED(replica_pm);
176 ARCANE_UNUSED(var_value);
177 ARCANE_UNUSED(compare_args);
191 bool compare_failed =
false;
198 NormType local_norm_max = {};
200 if constexpr (IsNumeric) {
202 if (is_use_local_norm) {
204 for (
Integer index = 0; index < current_size; ++index) {
205 if (index >= ref_size) {
209 DataType dref = ref[index];
210 typename VarDataTypeTraits::NormType norm_max = VarDataTypeTraits::normeMax(dref);
211 if (norm_max > local_norm_max) {
212 local_norm_max = norm_max;
219 for (
Integer index = 0; index < current_size; ++index) {
220 DataType diff = DataType();
221 if (index >= ref_size) {
223 compare_failed =
true;
226 DataType dref = ref[index];
227 DataType dcurrent = current[index];
228 if (_computeDifference(dref, dcurrent, diff, local_norm_max, diff_method)) {
229 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, index, NULL_ITEM_ID));
234 if (compare_failed) {
237 msg->
pinfo() <<
"Processor " << sid <<
" : "
238 <<
" comparison impossible because the number of elements is different"
239 <<
" for the variable " << var_name <<
" ref_size=" << ref_size;
242 this->_sortAndDump(var, pm, compare_args);
257 if (max_size != min_size) {
259 msg->
info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
260 <<
" because the number of elements is not the same on all the replica "
261 <<
" min=" << min_size <<
" max=" << max_size;
270 for (
Integer index = 0; index < size; ++index) {
271 DataType diff = DataType();
272 DataType min_val = min_values[index];
273 DataType max_val = max_values[index];
274 if (VarDataTypeTraits::verifDifferent(min_val, max_val, diff,
true)) {
275 this->m_diffs_info.add(DiffInfo(min_val, max_val, diff, index, NULL_ITEM_ID));
280 this->_sortAndDump(var, pm, compare_args);
284 bool _computeDifference(
const DataType& dref,
const DataType& dcurrent, DataType& diff,
285 const NormType& local_norm_max,
288 bool is_diff =
false;
289 switch (diff_method) {
291 is_diff = VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true);
294 is_diff = VarDataTypeTraits::verifDifferentNorm(dref, dcurrent, diff, local_norm_max,
true);
314 String storage_full_type =
info.storageTypeInfo().fullName();
315 Ref<IData> data = df->createSimpleDataRef(storage_full_type, storage_build_info);
316 m_value =
dynamic_cast<ValueDataType*
>(
data.get());
332template <
typename T> VariableArrayT<T>* VariableArrayT<T>::
333getReference(
const VariableBuildInfo& vb,
const VariableInfo& vi)
337 ThatClass* true_ptr =
nullptr;
341 true_ptr =
dynamic_cast<ThatClass*
>(var);
343 true_ptr =
new ThatClass(vb, vi);
344 vm->_internalApi()->addVariable(true_ptr);
346 ARCANE_CHECK_PTR(true_ptr);
358 auto* true_ptr =
dynamic_cast<ThatClass*
>(var);
360 ARCANE_FATAL(
"Can not build a reference from variable {0}", var->name());
368print(std::ostream& o)
const
372 o <<
"(dimension=" << size <<
") ";
392 itemGroup().synchronizer()->synchronize(
this);
409 itemGroup().synchronizer()->synchronize(
this, local_ids);
421 Real v2 = (
Real)(m_value->view().size());
438 return csa.checkReplica(var, values, compare_args);
442 VariableComparerResults
443 _checkIfSameOnAllReplicaHelper(IVariable* var, ConstArrayView<Byte> values,
444 const VariableComparerArgs& compare_args)
447 UniqueArray<Integer> int_values(size);
448 for (
Integer i = 0; i < size; ++i)
449 int_values[i] = values[i];
450 ArrayVariableDiff<Integer> csa;
451 return csa.checkReplica(var, int_values, compare_args);
461 switch (compare_args.compareMode()) {
472 reader->
read(
this, ref_data.
get());
482 ValueType& data_values = m_value->_internal()->_internalDeprecatedValue();
488 data_values.
copy(ref_array);
496 ARCANE_FATAL(
"Invalid value for compare mode '{0}'", (
int)compare_args.compareMode());
525 error() << String::format(
"Can not convert the string '{0}' to type '{1}'",
551 <<
") of the group `" << group.
name() <<
"' does not match "
562 ARCANE_ASSERT(source.
size() == destination.
size(),
563 (
"Impossible to copy: source and destination of different sizes !"));
566 for (
Integer i = 0; i < size; ++i)
567 value[destination[i]] = value[source[i]];
579 ARCANE_ASSERT((first_source.
size() == destination.
size()) && (second_source.
size() == destination.
size()),
580 (
"Impossible to copy: source and destination of different sizes !"));
583 for (
Integer i = 0; i < size; ++i) {
584 value[destination[i]] = (T)((value[first_source[i]] + value[second_source[i]]) / 2);
610 m_value->resize(new_size);
613 for (
Integer i = 0; i < new_size; ++i)
614 current_value.setAt(i, old_value.
at(new_to_old_ids[i]));
617 for (
Integer i = 0; i < new_size; ++i)
638 ARCANE_UNUSED(group);
647 Int32 new_size = resize_args.newSize();
648 Int32 nb_additional_element = resize_args.nbAdditionalCapacity();
649 bool use_no_init = resize_args.isUseNoInit();
651 auto* value_internal = m_value->_internal();
654 const bool is_collective_allocator = value_internal->_internalDeprecatedValue().allocator()->isCollective();
655 if (is_collective_allocator) {
656 value_internal->reserve(new_size + nb_additional_element);
658 else if (nb_additional_element != 0) {
659 Integer capacity = value_internal->capacity();
660 if (new_size > capacity)
661 value_internal->reserve(new_size + nb_additional_element);
667 Integer current_size = m_value->view().size();
671 value_internal->dispose();
674 value_internal->_internalDeprecatedValue().resizeNoInit(new_size);
676 value_internal->resize(new_size);
677 if (new_size > current_size) {
680 for (
Integer i = current_size; i < new_size; ++i)
686 if (use_nan || use_nan2) {
695 if (m_value->view().size() < value_internal->capacity()) {
696 value_internal->shrink();
706 Int64 capacity = value_internal->capacity();
717 _resize(VariableResizeArgs(n, nb_additional));
726 m_value->_internal()->shrink();
736 return m_value->_internal()->capacity();
742template <
typename DataType>
void VariableArrayT<DataType>::
743fill(
const DataType& value)
745 m_value->view().fill(value);
754 ARCANE_UNUSED(group);
761template <
typename DataType>
void
765 _checkSwapIsValid(&rhs);
768 m_value->swapValues(rhs.m_value);
771 rhs.syncReferences();
784 bool is_ok = (first_source.
size() == dsize) && (second_source.
size() == dsize);
786 ARCANE_FATAL(
"Unable to copy: source and destination of different sizes !");
790 for (
Integer i = 0; i < size; ++i)
791 value[destination[i]] = value[first_source[i]];
801 return m_value->_internal()->_internalDeprecatedValue();
807ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableArrayT);
808template 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.