14#include "arcane/core/Array2Variable.h"
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/CheckedConvert.h"
22#include "arcane/utils/ArrayShape.h"
23#include "arcane/utils/ITraceMng.h"
25#include "arcane/datatype/DataTypeTraits.h"
26#include "arcane/datatype/DataStorageBuildInfo.h"
28#include "arcane/core/VariableDiff.h"
29#include "arcane/core/VariableBuildInfo.h"
30#include "arcane/core/VariableInfo.h"
31#include "arcane/core/IApplication.h"
32#include "arcane/core/IVariableMng.h"
33#include "arcane/core/IItemFamily.h"
34#include "arcane/core/IVariableSynchronizer.h"
35#include "arcane/core/IDataReader.h"
36#include "arcane/core/ItemGroup.h"
37#include "arcane/core/IParallelMng.h"
38#include "arcane/core/IDataFactoryMng.h"
39#include "arcane/core/IMesh.h"
40#include "arcane/core/VariableComparer.h"
42#include "arcane/core/internal/IDataInternal.h"
43#include "arcane/core/internal/IVariableMngInternal.h"
44#include "arcane/core/internal/IVariableInternal.h"
55template <
class DataType>
68 const bool compare_ghost = compare_args.isCompareGhost();
82 Integer current_total_nb_element = current.totalNbElement();
90 if (current_total_nb_element == 0)
92 Integer ref_size1 = ref.dim1Size();
93 Integer current_size1 = current.dim1Size();
94 Integer ref_size2 = ref.dim2Size();
95 Integer current_size2 = current.dim2Size();
96 if (ref_size2 != current_size2) {
97 msg->
pinfo() <<
"Processor: " << sid <<
" VDIFF: Variable '" << var->
name()
98 <<
" bad dim2 size: ref=" << ref_size2 <<
" current=" << current_size2;
102 if (!item.
isOwn() && !compare_ghost)
105 if (group_index_table)
106 index = (*group_index_table)[index];
108 if (index >= ref_size1 || index >= current_size1) {
110 msg->
pinfo() <<
"Processor: " << sid <<
" VDIFF: Variable '" << var->
name()
111 <<
"wrong number of elements : impossible comparison";
116 for (
Integer z = 0; z < ref_size2; ++z) {
117 DataType diff = DataType();
118 DataType dref = lref[z];
119 DataType dcurrent = lcurrent[z];
120 if (VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true)) {
121 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, item, z));
127 this->_sortAndDump(var, pm, compare_args);
141 if constexpr (std::is_same<TrueType, ReduceType>::value)
142 return _checkReplica2(replica_pm, var, var_values, compare_args);
144 ARCANE_UNUSED(replica_pm);
146 ARCANE_UNUSED(var_values);
147 ARCANE_UNUSED(compare_args);
165 Integer total_nb_element = var_values.totalNbElement();
166 Integer ref_size1 = var_values.dim1Size();
167 Integer ref_size2 = var_values.dim2Size();
173 max_dims[0] = min_dims[0] = ref_size1;
174 max_dims[1] = min_dims[1] = ref_size2;
178 msg->
info(4) <<
"Array2Variable::CheckReplica2 rep_size=" << pm->
commSize() <<
" rank=" << pm->
commRank();
179 if (max_dims[0] != min_dims[0] || max_dims[1] != min_dims[1]) {
181 msg->
info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
182 <<
" because the number of elements is not the same on all the replica "
183 <<
" min=" << min_dims[0] <<
"," << min_dims[1]
184 <<
" max=" << max_dims[0] <<
"," << max_dims[1];
187 if (total_nb_element == 0)
198 if (group_index_table)
199 index = (*group_index_table)[index];
201 if (index >= ref_size1) {
203 msg->
pinfo() <<
"Processor: " << msg->
traceId() <<
" VDIFF: Variable '" << var->
name()
204 <<
"wrong number of elements : impossible comparison";
209 for (
Integer z = 0; z < ref_size2; ++z) {
210 DataType diff = DataType();
211 DataType dref = lref[z];
212 DataType dcurrent = lcurrent[z];
213 if (VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true)) {
214 this->m_diffs_info.add(DiffInfo(dcurrent, dref, diff, item, z));
221 this->_sortAndDump(var, pm, compare_args);
240 String storage_full_type =
info.storageTypeInfo().fullName();
241 Ref<IData> data = df->createSimpleDataRef(storage_full_type, storage_build_info);
242 m_data =
dynamic_cast<ValueDataType*
>(
data.get());
254 ThatClass* true_ptr =
nullptr;
258 true_ptr =
dynamic_cast<ThatClass*
>(var);
260 true_ptr =
new ThatClass(vb, vi);
263 ARCANE_CHECK_PTR(true_ptr);
275 ThatClass* true_ptr =
dynamic_cast<ThatClass*
>(var);
277 ARCANE_FATAL(
"Cannot build a reference from variable {0}", var->name());
291 m_data->_internal()->resizeOnlyDim1(s);
307 m_data->_internal()->
resize(dim1_size, dim2_size);
322 Int32 dim1_size = valueView().dim1Size();
323 Int32 dim2_size = CheckedConvert::toInt32(shape.totalNbElement());
325 m_data->_internal()->resize(dim1_size, dim2_size);
326 m_data->setShape(shape);
337 m_data->_internal()->shrink();
345print(std::ostream&)
const
363 itemGroup().synchronizer()->synchronize(
this);
380 itemGroup().synchronizer()->synchronize(
this, local_ids);
391 Real v1 =
static_cast<Real>(
sizeof(T));
392 Real v2 =
static_cast<Real>(m_data->view().totalNbElement());
409 return csa.checkReplica(var, values, compare_args);
413 VariableComparerResults
414 _checkIfSameOnAllReplicaHelper(IVariable* var, ConstArray2View<Byte> values,
415 const VariableComparerArgs& compare_args)
417 Integer dim1_size = values.dim1Size();
418 Integer dim2_size = values.dim2Size();
419 UniqueArray2<Integer> int_values(dim1_size, dim2_size);
420 for (
Integer i = 0; i < dim1_size; ++i)
421 for (
Integer j = 0; j < dim2_size; ++j)
422 int_values[i][j] = values[i][j];
423 Array2VariableDiff<Integer> csa;
424 return csa.checkReplica(var, int_values, compare_args);
434 switch (compare_args.compareMode()) {
443 reader->
read(
this, ref_data.
get());
454 Integer dim1_size = valueView().dim1Size();
458 ValueType& data_values = m_data->_internal()->_internalDeprecatedValue();
465 data_values.
copy(ref_array);
473 ARCANE_FATAL(
"Invalid value for compare mode '{0}'", (
int)compare_args.compareMode());
482 Int32 new_size = resize_args.newSize();
483 Int32 nb_additional_element = resize_args.nbAdditionalCapacity();
484 bool use_no_init = resize_args.isUseNoInit();
492 ValueType& data_values = m_data->_internal()->_internalDeprecatedValue();
495 Integer dim2_size = data_values.dim2Size();
497 const bool is_collective_allocator = data_values.allocator()->isCollective();
498 if (is_collective_allocator) {
499 data_values.reserve(new_size + nb_additional_element * dim2_size);
501 else if (nb_additional_element != 0) {
502 Integer capacity = data_values.capacity();
503 if (new_size > capacity)
504 data_values.reserve(new_size + nb_additional_element * dim2_size);
510 Integer current_size = data_values.dim1Size();
518 data_values.resizeNoInit(new_size, dim2_size);
520 data_values.resize(new_size, dim2_size);
522 if (new_size > current_size) {
525 if (use_nan || use_nan2) {
526 for (
Integer i = current_size; i < new_size; ++i)
533 if (container_ref.totalNbElement() < container_ref.capacity())
534 container_ref.shrink();
549 ARCANE_ASSERT(source.
size() == destination.
size(),
550 (
"Unable to copy: source and destination have different sizes !"));
552 const Integer dim2_size = valueView().dim2Size();
556 for (
Integer i = 0; i < nb_copy; ++i) {
557 for (
Integer j = 0; j < dim2_size; ++j)
558 value[destination[i]][j] = value[source[i]][j];
571 ARCANE_ASSERT((first_source.
size() == destination.
size()) && (second_source.
size() == destination.
size()),
572 (
"Unable to copy: source and destination have different sizes !"));
574 const Integer dim2_size = valueView().dim2Size();
578 for (
Integer i = 0; i < nb_copy; ++i) {
579 for (
Integer j = 0; j < dim2_size; ++j)
580 value[destination[i]][j] = (T)((value[first_source[i]][j] + value[second_source[i]][j]) / 2);
596 ValueType& current_value = m_data->_internal()->_internalDeprecatedValue();
597 Integer current_size = current_value.dim1Size();
598 if (current_size == 0)
601 Integer dim2_size = current_value.dim2Size();
609 current_value.
resize(new_size, dim2_size);
615 for (
Integer i = 0; i < new_size; ++i) {
616 Integer nto = new_to_old_ids[i];
619 for (
Integer j = 0; j < dim2_size; ++j)
620 v.setAt(j, ov.at(j));
624 for (
Integer i = 0; i < new_size; ++i) {
625 Integer nto = new_to_old_ids[i];
626 for (
Integer j = 0; j < dim2_size; ++j)
627 current_value[i][j] = old_value[nto][j];
653template <
typename DataType>
void
657 _checkSwapIsValid(&rhs);
660 m_data->swapValues(rhs.m_data);
663 rhs.syncReferences();
669template <
typename DataType>
auto
670Array2VariableT<DataType>::
673 return m_data->_internal()->_internalDeprecatedValue();
679template <
typename DataType>
void
686 const Int32 nb_rank = shape_with_item.nbDimension();
688 auto array_view = m_data->view();
689 Int32 dim0_size = array_view.dim1Size();
691 shape_with_item.setDimension(0, dim0_size);
692 Int32 nb_orig_shape = shape.nbDimension();
693 for (
Int32 i = 0; i < nb_orig_shape; ++i) {
694 shape_with_item.setDimension(i + 1, shape.dimension(i));
699 for (
Int32 i = (nb_orig_shape + 1); i < nb_rank; ++i) {
700 shape_with_item.setDimension(i, 1);
707ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(
Array2VariableT);
#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.
Exception when an argument is invalid.
VariableComparerResults _compareVariable(const VariableComparerArgs &compare_args) final
Comparison of values between variables.
void compact(Int32ConstArrayView old_to_new_ids) override
Compresses the variable's values.
void setIsSynchronized() override
Indicates that the variable is synchronized.
Array2VariableT(const VariableBuildInfo &v, const VariableInfo &vi)
Constructs a variable based on the reference v.
IData * data() override
Data associated with 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...
void copyItemsValues(Int32ConstArrayView source, Int32ConstArrayView destination) override
Copies the values of entities numbered source into entities numbered destination.
void synchronize() override
Synchronizes the variable.
Real allocatedMemory() const override
Memory size (in Bytes) used by the variable.
void print(std::ostream &o) const override
Prints the variable's values to the stream o.
void shrinkMemory() override
Frees any additional memory allocated for the data.
Mutable view for a 2D array.
void copy(Span2< const DataType > rhs)
Resizes the instance based on the dimensions of rhs and copies the values of rhs into it.
ArrayView< DataType > viewAsArray()
View of the array as a 1D array.
void resize(Int64 new_size)
Resizes only the first dimension, leaving the second dimension unchanged.
ConstArrayView< DataType > at(Int64 i) const
Element at index i. Always checks for bounds.
Modifiable view of an array of type T.
View for a constant 2D array.
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 const String & traceId() const =0
Manager identifier.
virtual TraceMessage pinfo()=0
Stream for a parallel information message.
virtual TraceMessage info()=0
Stream for an information message.
Variable manager interface.
virtual IVariable * checkVariable(const VariableInfo &infos)=0
Checks a variable.
virtual IVariableMngInternal * _internalApi()=0
Internal Arcane API.
virtual void synchronize(IVariable *var)=0
Synchronizes the variable var in blocking mode.
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.
SharedPtrT< GroupIndexTable > localIdToIndex() const
Table of local ids to a position for all entities 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).
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.
2D data vector with value semantics (STL style).
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.
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.
void resize(Integer n) override
Sets the number of elements for an array variable.
@ 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.
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.
@ IK_Particle
Particle mesh entity.
@ IK_Unknown
Unknown or uninitialized mesh entity.
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.