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 int max_print = compare_args.maxPrint();
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) {
152 const String& var_name = var->
name();
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, max_print);
160 return VariableComparerResults(nb_diff);
163 VariableComparerResults
164 checkReplica(IVariable* var, ConstArrayView<DataType> var_value,
165 const VariableComparerArgs& compare_args)
167 IParallelMng* replica_pm = compare_args.replicaParallelMng();
171 using ReduceType =
typename VariableDataTypeTraitsT<DataType>::HasReduceMinMax;
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);
179 throw NotSupportedException(A_FUNCINFO);
184 VariableComparerResults
185 _checkAsArray(IVariable* var, ConstArrayView<DataType> ref, ConstArrayView<DataType> current,
186 const VariableComparerArgs& compare_args)
188 const int max_print = compare_args.maxPrint();
189 IParallelMng* pm = var->variableMng()->parallelMng();
190 ITraceMng* msg = pm->traceMng();
193 bool compare_failed =
false;
195 Integer current_size = current.size();
200 NormType local_norm_max = {};
202 if constexpr (IsNumeric) {
204 if (is_use_local_norm) {
206 for (
Integer index = 0; index < current_size; ++index) {
207 if (index >= ref_size) {
211 DataType dref = ref[index];
212 typename VarDataTypeTraits::NormType norm_max = VarDataTypeTraits::normeMax(dref);
213 if (norm_max > local_norm_max) {
214 local_norm_max = norm_max;
221 for(
Integer index=0; index<current_size; ++index ){
222 DataType diff = DataType();
223 if (index>=ref_size){
225 compare_failed =
true;
228 DataType dref = ref[index];
229 DataType dcurrent = current[index];
230 if (_computeDifference(dref, dcurrent, diff, local_norm_max, diff_method)) {
231 this->m_diffs_info.add(DiffInfo(dcurrent,dref,diff,index,NULL_ITEM_ID));
237 Int32 sid = pm->commRank();
238 const String& var_name = var->name();
239 msg->pinfo() <<
"Processor " << sid <<
" : "
240 <<
" comparaison impossible car nombre d'éléments différents"
241 <<
" pour la variable " << var_name <<
" ref_size=" << ref_size;
245 this->_sortAndDump(var,pm,max_print);
247 return VariableComparerResults(nb_diff);
250 VariableComparerResults
251 _checkReplica2(IParallelMng* pm, IVariable* var, ConstArrayView<DataType> var_values,
252 const VariableComparerArgs& compare_args)
254 const int max_print = compare_args.maxPrint();
255 ITraceMng* msg = pm->traceMng();
256 Integer size = var_values.size();
260 msg->info(5) <<
"CheckReplica2 rep_size=" << pm->commSize() <<
" rank=" << pm->commRank();
261 if (max_size!=min_size){
262 const String& var_name = var->name();
263 msg->info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
264 <<
" because the number of elements is not the same on all the replica "
265 <<
" min=" << min_size <<
" max="<< max_size;
266 return VariableComparerResults(max_size);
269 UniqueArray<DataType> min_values(var_values);
270 UniqueArray<DataType> max_values(var_values);
274 for(
Integer index=0; index<size; ++index ){
275 DataType diff = DataType();
276 DataType min_val = min_values[index];
277 DataType max_val = max_values[index];
278 if (VarDataTypeTraits::verifDifferent(min_val,max_val,diff,
true)){
279 this->m_diffs_info.add(DiffInfo(min_val,max_val,diff,index,NULL_ITEM_ID));
284 this->_sortAndDump(var,pm,max_print);
286 return VariableComparerResults(nb_diff);
288 bool _computeDifference(
const DataType& dref,
const DataType& dcurrent, DataType& diff,
289 const NormType& local_norm_max,
292 bool is_diff =
false;
293 switch (diff_method) {
295 is_diff = VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true);
298 is_diff = VarDataTypeTraits::verifDifferentNorm(dref, dcurrent, diff, local_norm_max,
true);
318 String storage_full_type =
info.storageTypeInfo().fullName();
319 Ref<IData> data = df->createSimpleDataRef(storage_full_type,storage_build_info);
320 m_value =
dynamic_cast<ValueDataType*
>(
data.get());
336template<
typename T> VariableArrayT<T>* VariableArrayT<T>::
337getReference(
const VariableBuildInfo& vb,
const VariableInfo& vi)
341 ThatClass* true_ptr =
nullptr;
345 true_ptr =
dynamic_cast<ThatClass*
>(var);
347 true_ptr =
new ThatClass(vb,vi);
348 vm->_internalApi()->addVariable(true_ptr);
350 ARCANE_CHECK_PTR(true_ptr);
362 auto* true_ptr =
dynamic_cast<ThatClass*
>(var);
364 ARCANE_FATAL(
"Can not build a reference from variable {0}",var->name());
372print(std::ostream& o)
const
376 o <<
"(dimension=" << size <<
") ";
396 itemGroup().synchronizer()->synchronize(
this);
413 itemGroup().synchronizer()->synchronize(
this, local_ids);
425 Real v2 = (
Real)(m_value->view().size());
441 return csa.checkReplica(var, values, compare_args);
445 VariableComparerResults
446 _checkIfSameOnAllReplicaHelper(IVariable* var, ConstArrayView<Byte> values,
447 const VariableComparerArgs& compare_args)
450 UniqueArray<Integer> int_values(size);
451 for(
Integer i=0; i<size; ++i )
452 int_values[i] = values[i];
453 ArrayVariableDiff<Integer> csa;
454 return csa.checkReplica(var, int_values, compare_args);
464 switch (compare_args.compareMode()) {
475 reader->
read(
this, ref_data.
get());
485 ValueType& data_values = m_value->_internal()->_internalDeprecatedValue();
491 data_values.
copy(ref_array);
499 ARCANE_FATAL(
"Invalid value for compare mode '{0}'", (
int)compare_args.compareMode());
527 error() << String::format(
"Can not convert the string '{0}' to type '{1}'",
553 <<
") of the group `" << group.
name() <<
"' does not match \n"
564 ARCANE_ASSERT(source.
size()==destination.
size(),
565 (
"Impossible to copy: source and destination of different sizes !"));
569 value[destination[i]] = value[source[i]];
581 ARCANE_ASSERT((first_source.
size()==destination.
size()) && (second_source.
size()==destination.
size()),
582 (
"Impossible to copy: source and destination of different sizes !"));
585 for(
Integer i=0; i<size; ++i ) {
586 value[destination[i]] = (T)((value[first_source[i]]+value[second_source[i]])/2);
612 m_value->resize(new_size);
615 for(
Integer i=0; i<new_size; ++i )
616 current_value.setAt(i,old_value.
at(new_to_old_ids[i]));
619 for(
Integer i=0; i<new_size; ++i )
640 ARCANE_UNUSED(group);
649 Int32 new_size = resize_args.newSize();
650 Int32 nb_additional_element = resize_args.nbAdditionalCapacity();
651 bool use_no_init = resize_args.isUseNoInit();
653 auto* value_internal = m_value->_internal();
655 if (nb_additional_element!=0){
656 Integer capacity = value_internal->capacity();
657 if (new_size>capacity)
658 value_internal->reserve(new_size+nb_additional_element);
664 Integer current_size = m_value->view().size();
668 value_internal->dispose();
671 value_internal->_internalDeprecatedValue().resizeNoInit(new_size);
673 value_internal->resize(new_size);
674 if (new_size>current_size){
677 for(
Integer i=current_size; i<new_size; ++i)
683 if (use_nan || use_nan2){
692 if (m_value->view().size() < value_internal->capacity()){
693 value_internal->shrink();
703 Int64 capacity = value_internal->capacity();
714 _resize(VariableResizeArgs(n,nb_additional));
723 m_value->_internal()->shrink();
733 return m_value->_internal()->capacity();
739template<
typename DataType>
void VariableArrayT<DataType>::
740fill(
const DataType& value)
742 m_value->view().fill(value);
751 ARCANE_UNUSED(group);
758template<
typename DataType>
void
762 _checkSwapIsValid(&rhs);
765 m_value->swapValues(rhs.m_value);
768 rhs.syncReferences();
781 bool is_ok = (first_source.
size()==dsize) && (second_source.
size()==dsize);
783 ARCANE_FATAL(
"Unable to copy: source and destination of different sizes !");
788 value[destination[i]] = value[first_source[i]];
798 return m_value->_internal()->_internalDeprecatedValue();
804ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableArrayT);
805template class VariableArrayT<String>;
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
static constexpr Integer simdAlignment()
Alignement pour les structures utilisant la vectorisation.
Exception lorsqu'un argument est invalide.
Vue modifiable d'un tableau d'un type T.
T & at(Int64 i)
Elément d'indice i. Vérifie toujours les débordements.
void copy(Span< const T > rhs)
Copie les valeurs de rhs dans l'instance.
Vue constante d'un tableau de type T.
constexpr Integer size() const noexcept
Nombre d'éléments du tableau.
Informations pour construire une instance de 'IData'.
Interface de lecture des données d'une variable.
virtual void read(IVariable *var, IData *data)=0
Lit les données data de la variable var.
Interface d'une famille d'entités.
virtual IVariableSynchronizer * allItemsSynchronizer()=0
Synchroniseur sur toutes les entités de la famille.
Interface du gestionnaire de parallélisme pour un sous-domaine.
virtual Int32 commRank() const =0
Rang de cette instance dans le communicateur.
Interface du gestionnaire de traces.
virtual TraceMessage pinfo()=0
Flot pour un message d'information parallèle.
virtual IVariable * checkVariable(const VariableInfo &infos)=0
Vérifie une variable.
virtual void synchronize(IVariable *var)=0
Synchronise la variable var en mode bloquant.
virtual eItemKind itemKind() const =0
Type des entités du maillage sur lequel repose la variable.
virtual bool isPartial() const =0
Indique si la variable est partielle.
virtual ItemGroup itemGroup() const =0
Groupe du maillage associé.
virtual String name() const =0
Nom de la variable.
Groupe d'entités de maillage.
const String & name() const
Nom du groupe.
SharedPtrT< GroupIndexTable > localIdToIndex() const
Table des local ids vers une position pour toutes les entités du groupe.
IItemFamily * itemFamily() const
Famille d'entité à laquelle appartient ce groupe (0 pour le group nul)
eItemKind itemKind() const
Genre du groupe. Il s'agit du genre de ses éléments.
bool null() const
true is le groupe est le groupe nul
IMesh * mesh() const
Maillage auquel appartient ce groupe (0 pour le group nul)
Classe de base d'un élément de maillage.
constexpr Int32 localId() const
Identifiant local de l'entité dans le sous-domaine du processeur.
bool isOwn() const
true si l'entité est appartient au sous-domaine
Référence à une instance.
InstanceType * get() const
Instance associée ou nullptr si aucune.
Chaîne de caractères unicode.
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flot pour un message de debug.
TraceMessage info() const
Flot pour un message d'information.
TraceMessage error() const
Flot pour un message d'erreur.
Vecteur 1D de données avec sémantique par valeur (style STL).
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
Copie les moyennes des valeurs des entités numéros first_source et second_source dans les entités num...
void shrinkMemory() override
Libère l'éventuelle mémoire supplémentaire allouée pour les données.
void compact(Int32ConstArrayView old_to_new_ids) override
Compacte les valeurs de la variable.
bool initialize(const ItemGroup &group, const String &value) override
Initialise la variable.
void copyItemsValues(Int32ConstArrayView source, Int32ConstArrayView destination) override
Copie les valeurs des entités numéros source dans les entités numéro destination.
IData * data() override
Données associées à la variable.
VariableComparerResults _compareVariable(const VariableComparerArgs &compare_args) final
Comparaison de valeurs entre variables.
void print(std::ostream &o) const override
Imprime les valeurs de la variable sur le flot o.
void setIsSynchronized() override
Indique que la variable est synchronisée.
Real allocatedMemory() const override
Taille mémoire (en Koctet) utilisée par la variable.
void synchronize() override
Synchronise la variable.
Paramètres nécessaires à la construction d'une variable.
Arguments des méthodes de VariableComparer.
Résultats d'une opération de comparaison.
Infos caractérisant une variable.
IMesh * mesh() const final
Maillage auquel est associé la variable.
Variable(const VariableBuildInfo &v, const VariableInfo &vi)
Créé une variable lié à la référence v.
void _setData(const Ref< IData > &data)
Positionne la donnée.
bool isPartial() const override
Indique si la variable est partielle.
IVariableMng * variableMng() const override
Gestionnaire de variable associé à la variable.
String name() const final
Nom de la variable.
ItemGroup itemGroup() const final
Groupe du maillage associé.
void syncReferences() override
Synchronise les références.
String fullName() const final
Nom complet de la variable (avec le préfixe de la famille)
eItemKind itemKind() const override
Type des entités du maillage sur lequel repose la variable.
IItemFamily * itemFamily() const final
Famille d'entité associée.
eDataType dataType() const override
Type de la donnée gérée par la variable (Real, Integer, ...)
@ ReduceMin
Minimum des valeurs.
@ ReduceMax
Maximum des valeurs.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
bool arcaneIsCheck()
Vrai si on est en mode vérification.
eDataInitialisationPolicy
Type de politique d'initialisation possible pour une donnée.
@ DIP_InitInitialWithNanResizeWithDefault
Initialisation avec des NaN pour à la création et le constructeur par défaut ensuite.
@ DIP_InitWithNan
Initialisation avec des NaN (Not a Number)
@ DIP_InitWithDefault
Initialisation avec le constructeur par défaut.
std::int64_t Int64
Type entier signé sur 64 bits.
eVariableComparerComputeDifferenceMethod
Méthode utilisée pour calculer la différence entre deux valeurs v1 et v2.
@ Relative
Utilise (v1-v2) / v1.
@ LocalNormMax
Utilise (v1-v2) / local_norm_max.
eDataInitialisationPolicy getGlobalDataInitialisationPolicy()
Récupère la politique d'initialisation des variables.
Int32 Integer
Type représentant un entier.
ConstArrayView< Int32 > Int32ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 32 bits.
@ SameReplica
Vérifie que les valeurs de la variable sont les même sur tous les replica.
@ Same
Compare avec une référence.
@ Sync
Vérifie que la variable est bien synchronisée.
eItemKind
Genre d'entité de maillage.
@ IK_Particle
Entité de maillage de genre particule.
@ IK_Unknown
Entité de maillage de genre inconnu ou non initialisé
const char * itemKindName(eItemKind kind)
Nom du genre d'entité.
double Real
Type représentant un réel.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.
std::int32_t Int32
Type entier signé sur 32 bits.