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
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();
75 values.add(views[mvi.arrayIndex()][mvi.valueIndex()]);
85template <
typename DataType>
void
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();
94 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output.data());
95 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input.data());
96 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input_indexes.data());
97 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output_indexes.data());
101 output[output_indexes[i]] = input[input_indexes[i]];
108template <
typename DataType>
void
115 container.resize(dim1_size, DataType());
121template <
typename DataType>
void
128 Int32 nb_add =
static_cast<Int32>(dim1_size * reserve_ratio);
129 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add,
true));
137 PrivatePartType* global_var,
139: BaseClass(v, global_var, global_var_ref, mvs)
161template <
typename DataType>
void
162ItemMaterialVariableScalar<DataType>::
163fillFromArray(IMeshMaterial* mat, ConstArrayView<DataType> values)
168 MatCell mc = *imatcell;
169 MatVarIndex mvi = mc._varIndex();
170 setValue(mvi, values[index]);
190template <
typename DataType>
void
191ItemMaterialVariableScalar<DataType>::
192fillFromArray(IMeshMaterial* mat, ConstArrayView<DataType> values,
193 Int32ConstArrayView indexes)
195 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
196 Integer nb_index = indexes.size();
197 for (Integer i = 0; i < nb_index; ++i) {
198 MatVarIndex mvi = mat_indexes[indexes[i]];
199 setValue(mvi, values[i]);
218template <
typename DataType>
void
219ItemMaterialVariableScalar<DataType>::
220fillToArray(IMeshMaterial* mat, ArrayView<DataType> values)
224 MatCell mc = *imatcell;
225 MatVarIndex mvi = mc._varIndex();
226 values[index] = this->operator[](mvi);
246template <
typename DataType>
void
247ItemMaterialVariableScalar<DataType>::
248fillToArray(IMeshMaterial* mat, ArrayView<DataType> values, Int32ConstArrayView indexes)
250 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
251 Integer nb_index = indexes.size();
252 for (Integer i = 0; i < nb_index; ++i) {
253 MatVarIndex mvi = mat_indexes[indexes[i]];
254 values[i] = this->operator[](mvi);
264template <
typename DataType>
void
265ItemMaterialVariableScalar<DataType>::
266fillPartialValues(
const DataType& value)
270 Integer nb_var = m_vars.size();
271 for (Integer i = 1; i < nb_var; ++i) {
273 m_vars[i]->fill(value);
280template <
typename DataType> Int32
281ItemMaterialVariableScalar<DataType>::
284 return (Int32)
sizeof(DataType);
290template <
typename DataType>
void
291ItemMaterialVariableScalar<DataType>::
292_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<std::byte> bytes)
const
297 ArrayView<DataType> values(value_size,
reinterpret_cast<DataType*
>(bytes.data()));
298 for (Integer z = 0; z < value_size; ++z) {
299 values[z] = this->operator[](matvar_indexes[z]);
306template <
typename DataType>
void
307ItemMaterialVariableScalar<DataType>::
308copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteArrayView bytes)
const
310 auto* ptr =
reinterpret_cast<std::byte*
>(bytes.data());
311 return _copyToBufferLegacy(matvar_indexes, { ptr, bytes.size() });
317template <
typename DataType>
void
318ItemMaterialVariableScalar<DataType>::
319_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<const std::byte> bytes)
323 const Int32 value_size = CheckedConvert::toInt32(bytes.size() /
sizeof(DataType));
324 ConstArrayView<DataType> values(value_size,
reinterpret_cast<const DataType*
>(bytes.data()));
325 for (Integer z = 0; z < value_size; ++z) {
326 setValue(matvar_indexes[z], values[z]);
333template <
typename DataType>
void
334ItemMaterialVariableScalar<DataType>::
335copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteConstArrayView bytes)
337 auto* ptr =
reinterpret_cast<const std::byte*
>(bytes.data());
338 return _copyFromBufferLegacy(matvar_indexes, { ptr, bytes.size() });
344template <
typename DataType>
void
345ItemMaterialVariableScalar<DataType>::
348 IParallelMng* pm = m_p->materialMng()->mesh()->parallelMng();
349 Timer timer(pm->timerMng(),
"MatTimer", Timer::TimerReal);
350 Int64 message_size = 0;
352 Timer::Sentry ts(&timer);
353 message_size = _synchronize2();
355 pm->stat()->add(
"MaterialSync", timer.lastActivationTime(), message_size);
361template <
typename DataType> Int64
362ItemMaterialVariableScalar<DataType>::
365 Int64 message_size = 0;
366 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
368 if (m_p->space() == MatVarSpace::Environment) {
369 if (sync_version < 6)
372 if (sync_version >= 6) {
373 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
376 message_size = mmvsl.totalMessageSize();
378 else if (sync_version == 5 || sync_version == 4 || sync_version == 3) {
381 else if (sync_version == 2) {
392template <
typename DataType>
void
393ItemMaterialVariableScalar<DataType>::
394synchronize(MeshMaterialVariableSynchronizerList& sync_list)
396 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
397 if (sync_version >= 6) {
407template <
typename DataType>
void
408ItemMaterialVariableScalar<DataType>::
428 IMeshMaterialMng* material_mng = m_p->materialMng();
429 IMesh* mesh = material_mng->mesh();
432 IItemFamily* family = mesh->cellFamily();
433 IParallelMng* pm = mesh->parallelMng();
434 if (!pm->isParallel())
436 ItemGroup all_items = family->allItems();
438 UniqueArray<DataType> saved_values;
441 ConstArrayView<DataType> var_values = m_global_variable->valueView();
442 saved_values.resize(var_values.size());
443 saved_values.copy(var_values);
447 IMeshEnvironment* env = *ienv;
449 IMeshMaterial* mat = *imat;
452 ArrayView<DataType> var_values = m_global_variable->valueView();
455 MatCell mc = *imatcell;
456 Cell c = mc.globalCell();
457 MatVarIndex mvi = mc._varIndex();
458 var_values[c.localId()] = this->operator[](mvi);
461 m_global_variable->synchronize();
463 ConstArrayView<DataType> var_values = m_global_variable->valueView();
466 MatCell mc = *imatcell;
467 Cell c = mc.globalCell();
468 MatVarIndex mvi = mc._varIndex();
469 setValue(mvi, var_values[c.localId()]);
476 ArrayView<DataType> var_values = m_global_variable->valueView();
479 EnvCell ec = *ienvcell;
480 Cell c = ec.globalCell();
481 MatVarIndex mvi = ec._varIndex();
482 var_values[c.localId()] = this->operator[](mvi);
485 m_global_variable->synchronize();
487 ConstArrayView<DataType> var_values = m_global_variable->valueView();
490 EnvCell ec = *ienvcell;
491 Cell c = ec.globalCell();
492 MatVarIndex mvi = ec._varIndex();
493 setValue(mvi, var_values[c.localId()]);
500 ArrayView<DataType> var_values = m_global_variable->valueView();
501 var_values.copy(saved_values);
503 m_global_variable->synchronize();
509template <
typename DataType>
void
510ItemMaterialVariableScalar<DataType>::
530 IMeshMaterialMng* material_mng = m_p->materialMng();
531 IMesh* mesh = material_mng->mesh();
534 IItemFamily* family = mesh->cellFamily();
535 IParallelMng* pm = mesh->parallelMng();
536 if (!pm->isParallel())
538 ItemGroup all_items = family->allItems();
539 ITraceMng* tm = pm->traceMng();
540 tm->info(4) <<
"MAT_SYNCHRONIZE_V2 name=" << this->name();
541 IDataFactoryMng* df = m_global_variable->dataFactoryMng();
543 DataStorageTypeInfo storage_type_info(VariableInfo::_internalGetStorageTypeInfo(m_global_variable->dataType(), 2, 0));
544 DataStorageBuildInfo storage_build_info(tm);
545 String storage_full_type = storage_type_info.fullName();
547 Ref<IData> xdata(df->createSimpleDataRef(storage_full_type, storage_build_info));
548 auto* data =
dynamic_cast<IArray2DataT<DataType>*
>(xdata.get());
552 ConstArrayView<MeshMaterialVariableIndexer*> indexers = material_mng->_internalApi()->variablesIndexer();
553 Integer nb_indexer = indexers.size();
554 data->_internal()->_internalDeprecatedValue().resize(family->maxLocalId(), nb_indexer + 1);
555 Array2View<DataType> values(data->view());
558 for (MeshMaterialVariableIndexer* indexer : indexers) {
559 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
560 ConstArrayView<Int32> local_ids = indexer->localIds();
561 for (Integer j = 0, n = matvar_indexes.size(); j < n; ++j) {
562 MatVarIndex mvi = matvar_indexes[j];
563 values[local_ids[j]][mvi.arrayIndex()] = this->operator[](mvi);
566 family->allItemsSynchronizer()->synchronizeData(data);
569 for (MeshMaterialVariableIndexer* indexer : indexers) {
570 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
571 ConstArrayView<Int32> local_ids = indexer->localIds();
572 for (Integer j = 0, n = matvar_indexes.size(); j < n; ++j) {
573 MatVarIndex mvi = matvar_indexes[j];
574 setValue(mvi, values[local_ids[j]][mvi.arrayIndex()]);
578 m_global_variable->synchronize();
584template <
typename DataType>
void
585ItemMaterialVariableScalar<DataType>::
595 IMeshMaterialMng* material_mng = m_p->materialMng();
597 IMeshMaterialVariableSynchronizer* mmvs = material_mng->_internalApi()->allCellsMatEnvSynchronizer();
598 IVariableSynchronizer* var_syncer = mmvs->variableSynchronizer();
599 IParallelMng* pm = var_syncer->parallelMng();
601 if (!pm->isParallel())
604 mmvs->checkRecompute();
607 ITraceMng* tm = pm->traceMng();
608 tm->info(4) <<
"MAT_SYNCHRONIZE_V5 name=" << this->name();
613 Integer nb_rank = ranks.size();
614 std::vector<UniqueArray<DataType>> shared_values(nb_rank);
615 std::vector<UniqueArray<DataType>> ghost_values(nb_rank);
617 UniqueArray<Parallel::Request> requests;
621 for (Integer i = 0; i < nb_rank; ++i) {
622 Int32 rank = ranks[i];
623 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
624 Integer total = ghost_matcells.size();
625 ghost_values[i].resize(total);
626 Integer total_byte = CheckedConvert::multiply(total, data_type_size);
627 ByteArrayView bytes(total_byte, (Byte*)(ghost_values[i].unguardedBasePointer()));
628 requests.add(pm->recv(bytes, rank,
false));
632 for (Integer i = 0; i < nb_rank; ++i) {
633 Int32 rank = ranks[i];
634 ConstArrayView<MatVarIndex> shared_matcells(mmvs->sharedItems(i));
635 Integer total = shared_matcells.size();
636 shared_values[i].resize(total);
637 ArrayView<DataType> values(shared_values[i]);
638 for (Integer z = 0; z < total; ++z) {
639 values[z] = this->operator[](shared_matcells[z]);
641 Integer total_byte = CheckedConvert::multiply(total, data_type_size);
642 ByteArrayView bytes(total_byte, (Byte*)(values.unguardedBasePointer()));
643 requests.add(pm->send(bytes, rank,
false));
646 pm->waitAllRequests(requests);
649 for (Integer i = 0; i < nb_rank; ++i) {
650 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
651 Integer total = ghost_matcells.size();
652 ConstArrayView<DataType> values(ghost_values[i].constView());
653 for (Integer z = 0; z < total; ++z) {
654 setValue(ghost_matcells[z], values[z]);
663template <
typename DataType>
void
664ItemMaterialVariableScalar<DataType>::
665dumpValues(std::ostream& ostr, AllEnvCellVectorView view)
667 ostr <<
"Dumping values for material variable name=" << this->name() <<
'\n';
669 AllEnvCell all_env_cell = *iallenvcell;
670 ostr <<
"Cell uid=" << ItemPrinter(all_env_cell.globalCell()) <<
" v=" << value(all_env_cell._varIndex()) <<
'\n';
671 for (CellComponentCellEnumerator ienvcell(all_env_cell); ienvcell.hasNext(); ++ienvcell) {
672 MatVarIndex evi = ienvcell._varIndex();
673 ostr <<
"env_value=" << value(evi) <<
", mvi=" << evi <<
'\n';
674 for (CellComponentCellEnumerator imatcell(*ienvcell); imatcell.hasNext(); ++imatcell) {
675 MatVarIndex mvi = imatcell._varIndex();
676 ostr <<
"mat_value=" << value(mvi) <<
", mvi=" << mvi <<
'\n';
685template <
typename DataType>
void
686ItemMaterialVariableScalar<DataType>::
687dumpValues(std::ostream& ostr)
689 ostr <<
"Dumping values for material variable name=" << this->name() <<
'\n';
690 IItemFamily* family = m_global_variable->itemFamily();
693 IMeshMaterialMng* material_mng = m_p->materialMng();
694 dumpValues(ostr, material_mng->view(family->allItems()));
700template <
typename DataType>
void
701ItemMaterialVariableScalar<DataType>::
702serialize(ISerializer* sbuf, Int32ConstArrayView ids)
704 IItemFamily* family = m_global_variable->itemFamily();
707 ITraceMng* tm = family->traceMng();
708 IMeshMaterialMng* mat_mng = m_p->materialMng();
709 const Integer nb_count = DataTypeTraitsT<DataType>::nbBasicType();
710 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
711 const eBasicDataType data_type = DataTypeTraitsT<BasicType>::basicDataType();
712 ItemVectorView ids_view(family->view(ids));
713 bool has_mat = this->space() != MatVarSpace::Environment;
714 switch (sbuf->mode()) {
715 case ISerializer::ModeReserve: {
719 EnvCell envcell = *ienvcell;
722 nb_val += envcell.nbMaterial();
725 tm->info() <<
"RESERVE: nb_value=" << 1 <<
" size=" << (nb_val * nb_count);
726 sbuf->reserveInt64(1);
727 sbuf->reserveSpan(data_type, nb_val * nb_count);
729 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);
747 case ISerializer::ModeGet: {
748 UniqueArray<BasicType> basic_values;
749 Int64 nb_value = sbuf->getInt64();
750 basic_values.resize(nb_value * nb_count);
751 sbuf->getSpan(basic_values);
752 Span<const DataType> data_values(
reinterpret_cast<DataType*
>(basic_values.data()), nb_value);
756 EnvCell envcell = *ienvcell;
757 setValue(ienvcell._varIndex(), data_values[index]);
761 setValue(imatcell._varIndex(), data_values[index]);
769 throw NotSupportedException(A_FUNCINFO,
"Invalid serialize");
779template <
typename ItemType,
typename DataType>
780MeshMaterialVariableScalar<ItemType, DataType>::
781MeshMaterialVariableScalar(
const MaterialVariableBuildInfo& v, PrivatePartType* global_var,
782 VariableRefType* global_var_ref, MatVarSpace mvs)
783: ItemMaterialVariableScalar<DataType>(v, global_var, global_var_ref, mvs)
784, m_true_global_variable_ref(global_var_ref)
791template <
typename ItemType,
typename DataType>
792MeshMaterialVariableScalar<ItemType, DataType>::
793~MeshMaterialVariableScalar()
803#define ARCANE_INSTANTIATE_MAT(type) \
804 template class ItemMaterialVariableBase<MaterialVariableScalarTraits<type>>; \
805 template class ItemMaterialVariableScalar<type>; \
806 template class MeshMaterialVariableScalar<Cell, type>; \
807 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableScalar<Cell, type>>
#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_FATAL(...)
Macro throwing a FatalErrorException.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
1D loop on accelerator with additional arguments.
Interface of a component (material or environment) of a mesh.
Scalar variable on a mesh material.
Represents an index on material and environment variables.
Characteristics for a scalar material variable.
RunCommand makeCommand(const RunQueue &run_queue)
Creates a command associated with the queue run_queue.
Always enables tracing in Arcane parts concerning materials.
MatVarSpace
Definition space for a material variable.
Integer arcaneCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as the size of an array....
ArrayView< Byte > ByteArrayView
C equivalent of a 1D array of characters.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
eBasicDataType
Type of a basic data item.
UniqueArray< Int32 > Int32UniqueArray
Dynamic 1D array of 32-bit integers.
double Real
Type representing a real number.
@ Cell
The mesh is AMR by cell.
std::int32_t Int32
Signed integer type of 32 bits.