14#include "arcane/materials/MeshMaterialVariable.h"
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/ScopedPtr.h"
18#include "arcane/utils/ITraceMng.h"
19#include "arcane/utils/NumericTypes.h"
20#include "arcane/utils/CheckedConvert.h"
22#include "arcane/materials/MaterialVariableBuildInfo.h"
23#include "arcane/materials/IMeshMaterial.h"
24#include "arcane/materials/MatItemEnumerator.h"
25#include "arcane/materials/MeshMaterialVariableSynchronizerList.h"
26#include "arcane/materials/ItemMaterialVariableBaseT.H"
27#include "arcane/materials/IMeshMaterialVariableSynchronizer.h"
28#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
30#include "arcane/core/IItemFamily.h"
31#include "arcane/core/IMesh.h"
32#include "arcane/core/IParallelMng.h"
33#include "arcane/core/ISubDomain.h"
34#include "arcane/core/IApplication.h"
35#include "arcane/core/IDataFactoryMng.h"
36#include "arcane/core/IParallelExchanger.h"
37#include "arcane/core/ISerializer.h"
38#include "arcane/core/ISerializeMessage.h"
39#include "arcane/core/internal/IVariableInternal.h"
40#include "arcane/core/materials/internal/IMeshComponentInternal.h"
41#include "arcane/core/VariableInfo.h"
42#include "arcane/core/VariableRefArray.h"
43#include "arcane/core/MeshVariable.h"
44#include "arcane/core/VariableArray.h"
45#include "arcane/core/ItemPrinter.h"
46#include "arcane/core/IVariableSynchronizer.h"
47#include "arcane/core/internal/IDataInternal.h"
48#include "arcane/core/Timer.h"
49#include "arcane/core/parallel/IStat.h"
50#include "arcane/core/datatype/DataTypeTraits.h"
51#include "arcane/core/datatype/DataStorageBuildInfo.h"
64template<
typename DataType>
void
65MaterialVariableScalarTraits<DataType>::
66saveData(IMeshComponent* component,IData* data,
67 Array<ContainerViewType>& cviews)
69 ConstArrayView<ArrayView<DataType>> views = cviews;
70 auto* true_data =
dynamic_cast<ValueDataType*
>(data);
72 ContainerType& values = true_data->_internal()->_internalDeprecatedValue();
74 MatVarIndex mvi = icell._varIndex();
75 values.add(views[mvi.arrayIndex()][mvi.valueIndex()]);
85template <
typename DataType>
void
86MaterialVariableScalarTraits<DataType>::
87copyTo(SmallSpan<const DataType> input, SmallSpan<const Int32> input_indexes,
88 SmallSpan<DataType> output, SmallSpan<const Int32> output_indexes,
89 const RunQueue& queue)
92 Integer nb_value = input_indexes.size();
101 output[output_indexes[i]] = input[input_indexes[i]];
108template<
typename DataType>
void
109MaterialVariableScalarTraits<DataType>::
110resizeAndFillWithDefault(ValueDataType* data,ContainerType& container,Integer dim1_size)
115 container.resize(dim1_size,DataType());
121template<
typename DataType>
void
122MaterialVariableScalarTraits<DataType>::
123resizeWithReserve(PrivatePartType* var, Integer dim1_size, Real reserve_ratio)
128 Int32 nb_add =
static_cast<Int32
>(dim1_size * reserve_ratio);
129 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add,
true));
135template<
typename DataType> ItemMaterialVariableScalar<DataType>::
136ItemMaterialVariableScalar(
const MaterialVariableBuildInfo& v,
137 PrivatePartType* global_var,
139: BaseClass(v,global_var,global_var_ref,mvs)
160template<
typename DataType>
void
161ItemMaterialVariableScalar<DataType>::
162fillFromArray(IMeshMaterial* mat,ConstArrayView<DataType> values)
167 MatCell mc = *imatcell;
168 MatVarIndex mvi = mc._varIndex();
169 setValue(mvi,values[index]);
188template<
typename DataType>
void
189ItemMaterialVariableScalar<DataType>::
190fillFromArray(IMeshMaterial* mat,ConstArrayView<DataType> values,
191 Int32ConstArrayView indexes)
193 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
194 Integer nb_index = indexes.size();
195 for( Integer i=0; i<nb_index; ++i ){
196 MatVarIndex mvi = mat_indexes[indexes[i]];
197 setValue(mvi,values[i]);
215template<
typename DataType>
void
216ItemMaterialVariableScalar<DataType>::
217fillToArray(IMeshMaterial* mat,ArrayView<DataType> values)
221 MatCell mc = *imatcell;
222 MatVarIndex mvi = mc._varIndex();
223 values[index] = this->operator[](mvi);
242template<
typename DataType>
void
243ItemMaterialVariableScalar<DataType>::
244fillToArray(IMeshMaterial* mat,ArrayView<DataType> values,Int32ConstArrayView indexes)
246 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
247 Integer nb_index = indexes.size();
248 for( Integer i=0; i<nb_index; ++i ){
249 MatVarIndex mvi = mat_indexes[indexes[i]];
250 values[i] = this->operator[](mvi);
259template<
typename DataType>
void
260ItemMaterialVariableScalar<DataType>::
261fillPartialValues(
const DataType& value)
265 Integer nb_var = m_vars.size();
266 for( Integer i=1; i<nb_var; ++i ){
268 m_vars[i]->fill(value);
275template<
typename DataType> Int32
276ItemMaterialVariableScalar<DataType>::
279 return (Int32)
sizeof(DataType);
285template<
typename DataType>
void
286ItemMaterialVariableScalar<DataType>::
287_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<std::byte> bytes)
const
292 ArrayView<DataType> values(value_size,
reinterpret_cast<DataType*
>(bytes.data()));
293 for( Integer z=0; z<value_size; ++z ){
294 values[z] = this->operator[](matvar_indexes[z]);
301template<
typename DataType>
void
302ItemMaterialVariableScalar<DataType>::
303copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteArrayView bytes)
const
305 auto* ptr =
reinterpret_cast<std::byte*
>(bytes.data());
306 return _copyToBufferLegacy(matvar_indexes,{ptr,bytes.size()});
312template<
typename DataType>
void
313ItemMaterialVariableScalar<DataType>::
314_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes,Span<const std::byte> bytes)
318 const Int32 value_size = CheckedConvert::toInt32(bytes.size() /
sizeof(DataType));
319 ConstArrayView<DataType> values(value_size,
reinterpret_cast<const DataType*
>(bytes.data()));
320 for( Integer z=0; z<value_size; ++z ){
321 setValue(matvar_indexes[z],values[z]);
328template<
typename DataType>
void
329ItemMaterialVariableScalar<DataType>::
330copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes,ByteConstArrayView bytes)
332 auto* ptr =
reinterpret_cast<const std::byte*
>(bytes.data());
333 return _copyFromBufferLegacy(matvar_indexes,{ptr,bytes.size()});
339template<
typename DataType>
void
340ItemMaterialVariableScalar<DataType>::
343 IParallelMng* pm = m_p->materialMng()->mesh()->parallelMng();
344 Timer timer(pm->timerMng(),
"MatTimer",Timer::TimerReal);
345 Int64 message_size = 0;
347 Timer::Sentry ts(&timer);
348 message_size = _synchronize2();
350 pm->stat()->add(
"MaterialSync",timer.lastActivationTime(),message_size);
356template<
typename DataType> Int64
357ItemMaterialVariableScalar<DataType>::
360 Int64 message_size = 0;
361 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
363 if (m_p->space()==MatVarSpace::Environment){
367 if (sync_version>=6){
368 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
371 message_size = mmvsl.totalMessageSize();
373 else if (sync_version==5 || sync_version==4 || sync_version==3){
376 else if (sync_version==2){
387template<
typename DataType>
void
388ItemMaterialVariableScalar<DataType>::
389synchronize(MeshMaterialVariableSynchronizerList& sync_list)
391 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
392 if (sync_version>=6){
402template<
typename DataType>
void
403ItemMaterialVariableScalar<DataType>::
423 IMeshMaterialMng* material_mng = m_p->materialMng();
424 IMesh* mesh = material_mng->mesh();
427 IItemFamily* family = mesh->cellFamily();
428 IParallelMng* pm = mesh->parallelMng();
429 if (!pm->isParallel())
431 ItemGroup all_items = family->allItems();
433 UniqueArray<DataType> saved_values;
436 ConstArrayView<DataType> var_values = m_global_variable->valueView();
437 saved_values.resize(var_values.size());
438 saved_values.copy(var_values);
442 IMeshEnvironment* env = *ienv;
444 IMeshMaterial* mat = *imat;
447 ArrayView<DataType> var_values = m_global_variable->valueView();
450 MatCell mc = *imatcell;
451 Cell c = mc.globalCell();
452 MatVarIndex mvi = mc._varIndex();
453 var_values[c.localId()] = this->operator[](mvi);
456 m_global_variable->synchronize();
458 ConstArrayView<DataType> var_values = m_global_variable->valueView();
461 MatCell mc = *imatcell;
462 Cell c = mc.globalCell();
463 MatVarIndex mvi = mc._varIndex();
464 setValue(mvi,var_values[c.localId()]);
472 ArrayView<DataType> var_values = m_global_variable->valueView();
475 EnvCell ec = *ienvcell;
476 Cell c = ec.globalCell();
477 MatVarIndex mvi = ec._varIndex();
478 var_values[c.localId()] = this->operator[](mvi);
481 m_global_variable->synchronize();
483 ConstArrayView<DataType> var_values = m_global_variable->valueView();
486 EnvCell ec = *ienvcell;
487 Cell c = ec.globalCell();
488 MatVarIndex mvi = ec._varIndex();
489 setValue(mvi,var_values[c.localId()]);
496 ArrayView<DataType> var_values = m_global_variable->valueView();
497 var_values.copy(saved_values);
499 m_global_variable->synchronize();
505template<
typename DataType>
void
506ItemMaterialVariableScalar<DataType>::
526 IMeshMaterialMng* material_mng = m_p->materialMng();
527 IMesh* mesh = material_mng->mesh();
530 IItemFamily* family = mesh->cellFamily();
531 IParallelMng* pm = mesh->parallelMng();
532 if (!pm->isParallel())
534 ItemGroup all_items = family->allItems();
535 ITraceMng* tm = pm->traceMng();
536 tm->info(4) <<
"MAT_SYNCHRONIZE_V2 name=" << this->name();
537 IDataFactoryMng* df = m_global_variable->dataFactoryMng();
539 DataStorageTypeInfo storage_type_info(VariableInfo::_internalGetStorageTypeInfo(m_global_variable->dataType(),2,0));
540 DataStorageBuildInfo storage_build_info(tm);
541 String storage_full_type = storage_type_info.fullName();
543 Ref<IData> xdata(df->createSimpleDataRef(storage_full_type,storage_build_info));
544 auto* data =
dynamic_cast< IArray2DataT<DataType>*
>(xdata.get());
548 ConstArrayView<MeshMaterialVariableIndexer*> indexers = material_mng->_internalApi()->variablesIndexer();
549 Integer nb_indexer = indexers.size();
550 data->_internal()->_internalDeprecatedValue().resize(family->maxLocalId(),nb_indexer+1);
551 Array2View<DataType> values(data->view());
554 for( MeshMaterialVariableIndexer* indexer : indexers ){
555 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
556 ConstArrayView<Int32> local_ids = indexer->localIds();
557 for( Integer j=0, n=matvar_indexes.size(); j<n; ++j ){
558 MatVarIndex mvi = matvar_indexes[j];
559 values[local_ids[j]][mvi.arrayIndex()] = this->operator[](mvi);
562 family->allItemsSynchronizer()->synchronizeData(data);
565 for( MeshMaterialVariableIndexer* indexer : indexers ){
566 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
567 ConstArrayView<Int32> local_ids = indexer->localIds();
568 for( Integer j=0, n=matvar_indexes.size(); j<n; ++j ){
569 MatVarIndex mvi = matvar_indexes[j];
570 setValue(mvi,values[local_ids[j]][mvi.arrayIndex()]);
574 m_global_variable->synchronize();
580template<
typename DataType>
void
581ItemMaterialVariableScalar<DataType>::
592 IMeshMaterialMng* material_mng = m_p->materialMng();
594 IMeshMaterialVariableSynchronizer* mmvs = material_mng->_internalApi()->allCellsMatEnvSynchronizer();
595 IVariableSynchronizer* var_syncer = mmvs->variableSynchronizer();
596 IParallelMng* pm = var_syncer->parallelMng();
598 if (!pm->isParallel())
601 mmvs->checkRecompute();
604 ITraceMng* tm = pm->traceMng();
605 tm->info(4) <<
"MAT_SYNCHRONIZE_V5 name=" << this->name();
610 Integer nb_rank = ranks.size();
611 std::vector< UniqueArray<DataType> > shared_values(nb_rank);
612 std::vector< UniqueArray<DataType> > ghost_values(nb_rank);
614 UniqueArray<Parallel::Request> requests;
618 for( Integer i=0; i<nb_rank; ++i ){
619 Int32 rank = ranks[i];
620 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
621 Integer total = ghost_matcells.size();
622 ghost_values[i].resize(total);
623 Integer total_byte = CheckedConvert::multiply(total,data_type_size);
624 ByteArrayView bytes(total_byte,(Byte*)(ghost_values[i].unguardedBasePointer()));
625 requests.add(pm->recv(bytes,rank,
false));
629 for( Integer i=0; i<nb_rank; ++i ){
630 Int32 rank = ranks[i];
631 ConstArrayView<MatVarIndex> shared_matcells(mmvs->sharedItems(i));
632 Integer total = shared_matcells.size();
633 shared_values[i].resize(total);
634 ArrayView<DataType> values(shared_values[i]);
635 for( Integer z=0; z<total; ++z ){
636 values[z] = this->operator[](shared_matcells[z]);
638 Integer total_byte = CheckedConvert::multiply(total,data_type_size);
639 ByteArrayView bytes(total_byte,(Byte*)(values.unguardedBasePointer()));
640 requests.add(pm->send(bytes,rank,
false));
643 pm->waitAllRequests(requests);
646 for( Integer i=0; i<nb_rank; ++i ){
647 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
648 Integer total = ghost_matcells.size();
649 ConstArrayView<DataType> values(ghost_values[i].constView());
650 for( Integer z=0; z<total; ++z ){
651 setValue(ghost_matcells[z],values[z]);
660template<
typename DataType>
void
661ItemMaterialVariableScalar<DataType>::
662dumpValues(std::ostream& ostr,AllEnvCellVectorView view)
664 ostr <<
"Dumping values for material variable name=" << this->name() <<
'\n';
666 AllEnvCell all_env_cell = *iallenvcell;
667 ostr <<
"Cell uid=" << ItemPrinter(all_env_cell.globalCell()) <<
" v=" << value(all_env_cell._varIndex()) <<
'\n';
668 for( CellComponentCellEnumerator ienvcell(all_env_cell); ienvcell.hasNext(); ++ienvcell ){
669 MatVarIndex evi = ienvcell._varIndex();
670 ostr <<
"env_value=" << value(evi) <<
", mvi=" << evi <<
'\n';
671 for( CellComponentCellEnumerator imatcell(*ienvcell); imatcell.hasNext(); ++imatcell ){
672 MatVarIndex mvi = imatcell._varIndex();
673 ostr <<
"mat_value=" << value(mvi) <<
", mvi=" << mvi <<
'\n';
682template<
typename DataType>
void
683ItemMaterialVariableScalar<DataType>::
684dumpValues(std::ostream& ostr)
686 ostr <<
"Dumping values for material variable name=" << this->name() <<
'\n';
687 IItemFamily* family = m_global_variable->itemFamily();
690 IMeshMaterialMng* material_mng = m_p->materialMng();
691 dumpValues(ostr,material_mng->view(family->allItems()));
697template<
typename DataType>
void
698ItemMaterialVariableScalar<DataType>::
699serialize(ISerializer* sbuf,Int32ConstArrayView ids)
701 IItemFamily* family = m_global_variable->itemFamily();
704 ITraceMng* tm = family->traceMng();
705 IMeshMaterialMng* mat_mng = m_p->materialMng();
706 const Integer nb_count = DataTypeTraitsT<DataType>::nbBasicType();
707 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
708 const eBasicDataType data_type = DataTypeTraitsT<BasicType>::basicDataType();
709 ItemVectorView ids_view(family->view(ids));
710 bool has_mat = this->space()!=MatVarSpace::Environment;
711 switch(sbuf->mode()){
712 case ISerializer::ModeReserve:
717 EnvCell envcell = *ienvcell;
720 nb_val += envcell.nbMaterial();
723 tm->info() <<
"RESERVE: nb_value=" << 1 <<
" size=" << (nb_val*nb_count);
724 sbuf->reserveInt64(1);
725 sbuf->reserveSpan(data_type,nb_val*nb_count);
728 case ISerializer::ModePut:
730 UniqueArray<DataType> values;
733 values.add(value(ienvcell._varIndex()));
736 values.add(value(imatcell._varIndex()));
741 Integer nb_value = values.size();
742 ConstArrayView<BasicType> basic_values(nb_value*nb_count,
reinterpret_cast<BasicType*
>(values.data()));
743 tm->info() <<
"PUT: nb_value=" << nb_value <<
" size=" << basic_values.size();
744 sbuf->putInt64(nb_value);
745 sbuf->putSpan(basic_values);
748 case ISerializer::ModeGet:
750 UniqueArray<BasicType> basic_values;
751 Int64 nb_value = sbuf->getInt64();
752 basic_values.resize(nb_value*nb_count);
753 sbuf->getSpan(basic_values);
754 Span<const DataType> data_values(
reinterpret_cast<DataType*
>(basic_values.data()),nb_value);
758 EnvCell envcell = *ienvcell;
759 setValue(ienvcell._varIndex(),data_values[index]);
763 setValue(imatcell._varIndex(),data_values[index]);
772 throw NotSupportedException(A_FUNCINFO,
"Invalid serialize");
782template<
typename ItemType,
typename DataType>
783MeshMaterialVariableScalar<ItemType,DataType>::
784MeshMaterialVariableScalar(
const MaterialVariableBuildInfo& v,PrivatePartType* global_var,
785 VariableRefType* global_var_ref,MatVarSpace mvs)
786: ItemMaterialVariableScalar<DataType>(v,global_var,global_var_ref,mvs)
787, m_true_global_variable_ref(global_var_ref)
794template<
typename ItemType,
typename DataType>
795MeshMaterialVariableScalar<ItemType,DataType>::
796~MeshMaterialVariableScalar()
806#define ARCANE_INSTANTIATE_MAT(type) \
807 template class ItemMaterialVariableBase< MaterialVariableScalarTraits<type> >;\
808 template class ItemMaterialVariableScalar<type>;\
809 template class MeshMaterialVariableScalar<Cell,type>;\
810 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableScalar<Cell,type>>
#define ARCANE_CHECK_ACCESSIBLE_POINTER(queue_or_runner_or_policy, ptr)
Macro qui vérifie en mode check si ptr est accessible pour une RunQueue ou un Runner.
#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_FATAL(...)
Macro envoyant une exception FatalErrorException.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
Boucle sur accélérateur avec arguments supplémentaires pour les réductions.
RunCommand makeCommand(const RunQueue &run_queue)
Créé une commande associée à la file run_queue.
Active toujours les traces dans les parties Arcane concernant les matériaux.
MatVarSpace
Espace de définition d'une variable matériau.
Integer arcaneCheckArraySize(unsigned long long size)
Vérifie que size peut être converti dans un 'Integer' pour servir de taille à un tableau....
ConstArrayView< Int32 > Int32ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 32 bits.
UniqueArray< Int32 > Int32UniqueArray
Tableau dynamique à une dimension d'entiers 32 bits.
ArrayView< Byte > ByteArrayView
Equivalent C d'un tableau à une dimension de caractères.
Int32 Integer
Type représentant un entier.
eBasicDataType
Type d'une donnée de base.