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 = var->_internalApi()->replicaParallelMng();
172 using ReduceType =
typename VariableDataTypeTraitsT<DataType>::HasReduceMinMax;
173 if constexpr(std::is_same<TrueType,ReduceType>::value)
174 return _checkReplica2(replica_pm, var, var_value, compare_args);
176 ARCANE_UNUSED(replica_pm);
178 ARCANE_UNUSED(var_value);
179 ARCANE_UNUSED(compare_args);
180 throw NotSupportedException(A_FUNCINFO);
185 VariableComparerResults
186 _checkAsArray(IVariable* var, ConstArrayView<DataType> ref, ConstArrayView<DataType> current,
187 const VariableComparerArgs& compare_args)
189 const int max_print = compare_args.maxPrint();
190 IParallelMng* pm = var->variableMng()->parallelMng();
191 ITraceMng* msg = pm->traceMng();
194 bool compare_failed =
false;
196 Integer current_size = current.size();
201 NormType local_norm_max = {};
203 if constexpr (IsNumeric) {
205 if (is_use_local_norm) {
207 for (
Integer index = 0; index < current_size; ++index) {
208 if (index >= ref_size) {
212 DataType dref = ref[index];
213 typename VarDataTypeTraits::NormType norm_max = VarDataTypeTraits::normeMax(dref);
214 if (norm_max > local_norm_max) {
215 local_norm_max = norm_max;
222 for(
Integer index=0; index<current_size; ++index ){
223 DataType diff = DataType();
224 if (index>=ref_size){
226 compare_failed =
true;
229 DataType dref = ref[index];
230 DataType dcurrent = current[index];
231 if (_computeDifference(dref, dcurrent, diff, local_norm_max, diff_method)) {
232 this->m_diffs_info.add(DiffInfo(dcurrent,dref,diff,index,NULL_ITEM_ID));
238 Int32 sid = pm->commRank();
239 const String& var_name = var->name();
240 msg->pinfo() <<
"Processor " << sid <<
" : "
241 <<
" comparaison impossible car nombre d'éléments différents"
242 <<
" pour la variable " << var_name <<
" ref_size=" << ref_size;
246 this->_sortAndDump(var,pm,max_print);
248 return VariableComparerResults(nb_diff);
251 VariableComparerResults
252 _checkReplica2(IParallelMng* pm, IVariable* var, ConstArrayView<DataType> var_values,
253 const VariableComparerArgs& compare_args)
255 const int max_print = compare_args.maxPrint();
256 ITraceMng* msg = pm->traceMng();
257 Integer size = var_values.size();
261 msg->info(5) <<
"CheckReplica2 rep_size=" << pm->commSize() <<
" rank=" << pm->commRank();
262 if (max_size!=min_size){
263 const String& var_name = var->name();
264 msg->info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
265 <<
" because the number of elements is not the same on all the replica "
266 <<
" min=" << min_size <<
" max="<< max_size;
267 return VariableComparerResults(max_size);
270 UniqueArray<DataType> min_values(var_values);
271 UniqueArray<DataType> max_values(var_values);
275 for(
Integer index=0; index<size; ++index ){
276 DataType diff = DataType();
277 DataType min_val = min_values[index];
278 DataType max_val = max_values[index];
279 if (VarDataTypeTraits::verifDifferent(min_val,max_val,diff,
true)){
280 this->m_diffs_info.add(DiffInfo(min_val,max_val,diff,index,NULL_ITEM_ID));
285 this->_sortAndDump(var,pm,max_print);
287 return VariableComparerResults(nb_diff);
289 bool _computeDifference(
const DataType& dref,
const DataType& dcurrent, DataType& diff,
290 const NormType& local_norm_max,
293 bool is_diff =
false;
294 switch (diff_method) {
296 is_diff = VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true);
299 is_diff = VarDataTypeTraits::verifDifferentNorm(dref, dcurrent, diff, local_norm_max,
true);
319 String storage_full_type =
info.storageTypeInfo().fullName();
320 Ref<IData> data = df->createSimpleDataRef(storage_full_type,storage_build_info);
321 m_value =
dynamic_cast<ValueDataType*
>(
data.get());
337template<
typename T> VariableArrayT<T>* VariableArrayT<T>::
338getReference(
const VariableBuildInfo& vb,
const VariableInfo& vi)
342 ThatClass* true_ptr =
nullptr;
346 true_ptr =
dynamic_cast<ThatClass*
>(var);
348 true_ptr =
new ThatClass(vb,vi);
349 vm->_internalApi()->addVariable(true_ptr);
351 ARCANE_CHECK_PTR(true_ptr);
363 auto* true_ptr =
dynamic_cast<ThatClass*
>(var);
365 ARCANE_FATAL(
"Can not build a reference from variable {0}",var->name());
373print(std::ostream& o)
const
377 o <<
"(dimension=" << size <<
") ";
397 itemGroup().synchronizer()->synchronize(
this);
414 itemGroup().synchronizer()->synchronize(
this, local_ids);
426 Real v2 = (
Real)(m_value->view().size());
442 return csa.checkReplica(var, values, compare_args);
446 VariableComparerResults
447 _checkIfSameOnAllReplicaHelper(IVariable* var, ConstArrayView<Byte> values,
448 const VariableComparerArgs& compare_args)
451 UniqueArray<Integer> int_values(size);
452 for(
Integer i=0; i<size; ++i )
453 int_values[i] = values[i];
454 ArrayVariableDiff<Integer> csa;
455 return csa.checkReplica(var, int_values, compare_args);
465 switch (compare_args.compareMode()) {
476 reader->
read(
this, ref_data.
get());
486 ValueType& data_values = m_value->_internal()->_internalDeprecatedValue();
492 data_values.
copy(ref_array);
500 ARCANE_FATAL(
"Invalid value for compare mode '{0}'", (
int)compare_args.compareMode());
528 error() << String::format(
"Can not convert the string '{0}' to type '{1}'",
554 <<
") of the group `" << group.
name() <<
"' does not match \n"
565 ARCANE_ASSERT(source.size()==destination.
size(),
566 (
"Impossible to copy: source and destination of different sizes !"));
568 const Integer size = source.size();
570 value[destination[i]] = value[source[i]];
582 ARCANE_ASSERT((first_source.
size()==destination.
size()) && (second_source.
size()==destination.
size()),
583 (
"Impossible to copy: source and destination of different sizes !"));
586 for(
Integer i=0; i<size; ++i ) {
587 value[destination[i]] = (T)((value[first_source[i]]+value[second_source[i]])/2);
613 m_value->resize(new_size);
616 for(
Integer i=0; i<new_size; ++i )
617 current_value.setAt(i,old_value.
at(new_to_old_ids[i]));
620 for(
Integer i=0; i<new_size; ++i )
641 ARCANE_UNUSED(group);
650 Int32 new_size = resize_args.newSize();
651 Int32 nb_additional_element = resize_args.nbAdditionalCapacity();
652 bool use_no_init = resize_args.isUseNoInit();
654 auto* value_internal = m_value->_internal();
656 if (nb_additional_element!=0){
657 Integer capacity = value_internal->capacity();
658 if (new_size>capacity)
659 value_internal->reserve(new_size+nb_additional_element);
665 Integer current_size = m_value->view().size();
669 value_internal->dispose();
672 value_internal->_internalDeprecatedValue().resizeNoInit(new_size);
674 value_internal->resize(new_size);
675 if (new_size>current_size){
678 for(
Integer i=current_size; i<new_size; ++i)
684 if (use_nan || use_nan2){
693 if (m_value->view().size() < value_internal->capacity()){
694 value_internal->shrink();
704 Int64 capacity = value_internal->capacity();
724 m_value->_internal()->shrink();
734 return m_value->_internal()->capacity();
740template<
typename DataType>
void VariableArrayT<DataType>::
741fill(
const DataType& value)
743 m_value->view().fill(value);
752 ARCANE_UNUSED(group);
759template<
typename DataType>
void
763 _checkSwapIsValid(&rhs);
766 m_value->swapValues(rhs.m_value);
769 rhs.syncReferences();
782 bool is_ok = (first_source.
size()==dsize) && (second_source.
size()==dsize);
784 ARCANE_FATAL(
"Unable to copy: source and destination of different sizes !");
789 value[destination[i]] = value[first_source[i]];
799 return m_value->_internal()->_internalDeprecatedValue();
805ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableArrayT);
806template 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'.
Classe de base d'une table de hachage entre les items d'un groupe et leurs positions dans la table.
Interface du gestionnaire de fabrique d'une donnée.
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.
Interface d'une variable.
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
Exception lorsqu'une opération n'est pas supportée.
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).
Implémentation d'une variable sur un tableau 1D.
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.
Classe template d'informations sur un type d'une variable.
Classe de base pour les comparaisons de valeurs entre deux variables.
Infos caractérisant une variable.
Arguments pour redimensionner 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.
@ SameOnAllReplica
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.