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) {
151 const String& var_name = var->
name();
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);
159 return VariableComparerResults(nb_diff);
162 VariableComparerResults
163 checkReplica(IVariable* var, ConstArrayView<DataType> var_value,
164 const VariableComparerArgs& compare_args)
166 IParallelMng* replica_pm = var->_internalApi()->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 IParallelMng* pm = var->variableMng()->parallelMng();
189 ITraceMng* msg = pm->traceMng();
192 bool compare_failed =
false;
194 Integer current_size = current.size();
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));
236 Int32 sid = pm->commRank();
237 const String& var_name = var->name();
238 msg->pinfo() <<
"Processor " << sid <<
" : "
239 <<
" comparaison impossible car nombre d'éléments différents"
240 <<
" pour la variable " << var_name <<
" ref_size=" << ref_size;
244 this->_sortAndDump(var, pm, compare_args);
246 return VariableComparerResults(nb_diff);
249 VariableComparerResults
250 _checkReplica2(IParallelMng* pm, IVariable* var, ConstArrayView<DataType> var_values,
251 const VariableComparerArgs& compare_args)
253 ITraceMng* msg = pm->traceMng();
254 Integer size = var_values.size();
258 msg->info(5) <<
"CheckReplica2 rep_size=" << pm->commSize() <<
" rank=" << pm->commRank();
259 if (max_size!=min_size){
260 const String& var_name = var->name();
261 msg->info() <<
"Can not compare values on replica for variable '" << var_name <<
"'"
262 <<
" because the number of elements is not the same on all the replica "
263 <<
" min=" << min_size <<
" max="<< max_size;
264 return VariableComparerResults(max_size);
267 UniqueArray<DataType> min_values(var_values);
268 UniqueArray<DataType> max_values(var_values);
272 for(
Integer index=0; index<size; ++index ){
273 DataType diff = DataType();
274 DataType min_val = min_values[index];
275 DataType max_val = max_values[index];
276 if (VarDataTypeTraits::verifDifferent(min_val,max_val,diff,
true)){
277 this->m_diffs_info.add(DiffInfo(min_val,max_val,diff,index,NULL_ITEM_ID));
282 this->_sortAndDump(var, pm, compare_args);
284 return VariableComparerResults(nb_diff);
286 bool _computeDifference(
const DataType& dref,
const DataType& dcurrent, DataType& diff,
287 const NormType& local_norm_max,
290 bool is_diff =
false;
291 switch (diff_method) {
293 is_diff = VarDataTypeTraits::verifDifferent(dref, dcurrent, diff,
true);
296 is_diff = VarDataTypeTraits::verifDifferentNorm(dref, dcurrent, diff, local_norm_max,
true);
316 String storage_full_type =
info.storageTypeInfo().fullName();
317 Ref<IData> data = df->createSimpleDataRef(storage_full_type,storage_build_info);
318 m_value =
dynamic_cast<ValueDataType*
>(
data.get());
334template<
typename T> VariableArrayT<T>* VariableArrayT<T>::
335getReference(
const VariableBuildInfo& vb,
const VariableInfo& vi)
339 ThatClass* true_ptr =
nullptr;
343 true_ptr =
dynamic_cast<ThatClass*
>(var);
345 true_ptr =
new ThatClass(vb,vi);
346 vm->_internalApi()->addVariable(true_ptr);
348 ARCANE_CHECK_PTR(true_ptr);
360 auto* true_ptr =
dynamic_cast<ThatClass*
>(var);
362 ARCANE_FATAL(
"Can not build a reference from variable {0}",var->name());
370print(std::ostream& o)
const
374 o <<
"(dimension=" << size <<
") ";
394 itemGroup().synchronizer()->synchronize(
this);
411 itemGroup().synchronizer()->synchronize(
this, local_ids);
423 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());
525 error() << String::format(
"Can not convert the string '{0}' to type '{1}'",
551 <<
") of the group `" << group.
name() <<
"' does not match \n"
562 ARCANE_ASSERT(source.
size()==destination.
size(),
563 (
"Impossible to copy: source and destination of different sizes !"));
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();
653 if (nb_additional_element!=0){
654 Integer capacity = value_internal->capacity();
655 if (new_size>capacity)
656 value_internal->reserve(new_size+nb_additional_element);
662 Integer current_size = m_value->view().size();
666 value_internal->dispose();
669 value_internal->_internalDeprecatedValue().resizeNoInit(new_size);
671 value_internal->resize(new_size);
672 if (new_size>current_size){
675 for(
Integer i=current_size; i<new_size; ++i)
681 if (use_nan || use_nan2){
690 if (m_value->view().size() < value_internal->capacity()){
691 value_internal->shrink();
701 Int64 capacity = value_internal->capacity();
712 _resize(VariableResizeArgs(n,nb_additional));
721 m_value->_internal()->shrink();
731 return m_value->_internal()->capacity();
737template<
typename DataType>
void VariableArrayT<DataType>::
738fill(
const DataType& value)
740 m_value->view().fill(value);
749 ARCANE_UNUSED(group);
756template<
typename DataType>
void
760 _checkSwapIsValid(&rhs);
763 m_value->swapValues(rhs.m_value);
766 rhs.syncReferences();
779 bool is_ok = (first_source.
size()==dsize) && (second_source.
size()==dsize);
781 ARCANE_FATAL(
"Unable to copy: source and destination of different sizes !");
786 value[destination[i]] = value[first_source[i]];
796 return m_value->_internal()->_internalDeprecatedValue();
802ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(VariableArrayT);
803template 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.
@ 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.