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>::
170 setValue(mvi, values[index]);
190template <
typename DataType>
void
191ItemMaterialVariableScalar<DataType>::
197 for (
Integer i = 0; i < nb_index; ++i) {
199 setValue(mvi, values[i]);
218template <
typename DataType>
void
219ItemMaterialVariableScalar<DataType>::
226 values[index] = this->operator[](mvi);
246template <
typename DataType>
void
247ItemMaterialVariableScalar<DataType>::
252 for (
Integer i = 0; i < nb_index; ++i) {
254 values[i] = this->operator[](mvi);
264template <
typename DataType>
void
265ItemMaterialVariableScalar<DataType>::
266fillPartialValues(
const DataType& value)
271 for (
Integer i = 1; i < nb_var; ++i) {
280template <
typename DataType>
Int32
284 return (
Int32)
sizeof(DataType);
290template <
typename DataType>
void
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();
350 Int64 message_size = 0;
353 message_size = _synchronize2();
361template <
typename DataType>
Int64
365 Int64 message_size = 0;
366 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
369 if (sync_version < 6)
372 if (sync_version >= 6) {
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>::
667 ostr <<
"Dumping values for material variable name=" << this->
name() <<
'\n';
673 ostr <<
"env_value=" << value(evi) <<
", mvi=" << evi <<
'\n';
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();
700template <
typename DataType>
void
701ItemMaterialVariableScalar<DataType>::
704 IItemFamily* family = m_global_variable->itemFamily();
714 switch (sbuf->
mode()) {
715 case ISerializer::ModeReserve: {
722 nb_val += envcell.nbMaterial();
725 tm->
info() <<
"RESERVE: nb_value=" << 1 <<
" size=" << (nb_val * nb_count);
733 values.
add(value(ienvcell._varIndex()));
736 values.
add(value(imatcell._varIndex()));
743 tm->
info() <<
"PUT: nb_value=" << nb_value <<
" size=" << basic_values.
size();
750 basic_values.
resize(nb_value * nb_count);
757 setValue(ienvcell._varIndex(), data_values[index]);
761 setValue(imatcell._varIndex(), data_values[index]);
779template <
typename ItemType,
typename DataType>
784, m_true_global_variable_ref(global_var_ref)
791template <
typename ItemType,
typename DataType>
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>>
809ARCANE_INSTANTIATE_MAT(
Byte);
810ARCANE_INSTANTIATE_MAT(
Int8);
811ARCANE_INSTANTIATE_MAT(Int16);
812ARCANE_INSTANTIATE_MAT(Int32);
813ARCANE_INSTANTIATE_MAT(Int64);
814ARCANE_INSTANTIATE_MAT(BFloat16);
815ARCANE_INSTANTIATE_MAT(
Float16);
816ARCANE_INSTANTIATE_MAT(
Float32);
817ARCANE_INSTANTIATE_MAT(Real);
818ARCANE_INSTANTIATE_MAT(Real2);
819ARCANE_INSTANTIATE_MAT(Real3);
820ARCANE_INSTANTIATE_MAT(Real2x2);
821ARCANE_INSTANTIATE_MAT(Real3x3);
#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.
Integer size() const
Number of elements in the vector.
Modifiable view of an array of type T.
void resize(Int64 s)
Changes the number of elements in the array to s.
void add(ConstReferenceType val)
Adds element val to the end of the array.
const T * data() const
Access to the root of the array without any protection.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of an entity family.
virtual ItemGroup allItems() const =0
Group of all entities.
virtual ITraceMng * traceMng() const =0
Associated trace manager.
virtual ItemVectorView view(Int32ConstArrayView local_ids)=0
View on the entities.
Interface of the parallelism manager for a subdomain.
virtual IStat * stat()=0
Statistics manager.
virtual ITimerMng * timerMng() const =0
Timer manager.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
@ ModePut
The serializer expects reserve().
@ ModeGet
The serializer expects get().
virtual Int64 getInt64()=0
Retrieve a size.
virtual void putSpan(Span< const Real > values)
Add the array values.
virtual eMode mode() const =0
Current operating mode.
virtual void getSpan(Span< Real > values)
Retrieve the array values.
virtual void reserveSpan(eBasicDataType dt, Int64 n)=0
Reserves memory for n values of dt.
virtual void putInt64(Int64 value)=0
Add the integer value.
virtual TraceMessage info()=0
Stream for an information message.
Utility class for printing information about an entity.
View on a vector of entities.
View over a list of cells with environment information.
Arcane cell with material and environment information.
Enumerator over the constituents of a cell.
Cell globalCell() const
Global cell.
__host__ __device__ MatVarIndex _varIndex() const
Arcane cell of an environment.
virtual MeshMaterialVariableIndexer * variableIndexer() const =0
Indexer to access partial variables.
Interface of a component (material or environment) of a mesh.
virtual IMeshComponentInternal * _internalApi()=0
Internal API.
Interface for the material and environment manager of a mesh.
virtual AllEnvCellVectorView view(const CellGroup &cells)=0
View of environment cells corresponding to the group cells.
Interface of a mesh material.
Scalar variable on a mesh material.
ARCANE_MATERIALS_EXPORT void dumpValues(std::ostream &ostr) override
Dumps the variable values to the stream ostr.
UniqueArray< PrivatePartType * > m_vars
Variables for different materials.
Represents a material in a multi-material cell.
Represents an index on material and environment variables.
Characteristics for a scalar material variable.
Scalar variable on a mesh material.
Synchronizing a list of material variables.
String name() const override
Name of the variable.
MatVarSpace space() const override
Variable definition space (material+environment or environment only).
Exception when an operation is not supported.
virtual void add(const String &name, double elapsed_time, Int64 msg_size)=0
Adds a statistic.
View of an array of elements of type T.
View of an array of elements of type T.
Sentinel for the timer. The sentinel associated with a timer allows it to be triggered upon its const...
@ TimerReal
Timer using real time.
Real lastActivationTime() const
Returns the time (in seconds) spent during the last activation of the timer.
1D data vector with value semantics (STL style).
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.
@ Environment
Variable having values only on environments.
std::int8_t Int8
Signed integer type of 8 bits.
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.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
@ Float16
Float16 data type.
UniqueArray< Int32 > Int32UniqueArray
Dynamic 1D array of 32-bit integers.
double Real
Type representing a real number.
unsigned char Byte
Type of a byte.
float Float32
IEEE-753 single-precision floating-point type.
@ DT_Int64
64-bit integer data type
@ Cell
The mesh is AMR by cell.
std::int32_t Int32
Signed integer type of 32 bits.