14#include "arcane/materials/MeshMaterialVariable.h"
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/TraceInfo.h"
18#include "arcane/utils/ITraceMng.h"
19#include "arcane/utils/Real2.h"
20#include "arcane/utils/Real3.h"
21#include "arcane/utils/Real2x2.h"
22#include "arcane/utils/Real3x3.h"
23#include "arcane/utils/Mutex.h"
25#include "arcane/utils/PlatformUtils.h"
27#include "arcane/core/materials/IMeshMaterial.h"
28#include "arcane/core/materials/ComponentItemVectorView.h"
29#include "arcane/core/Variable.h"
30#include "arcane/core/VariableDependInfo.h"
31#include "arcane/core/MeshVariable.h"
32#include "arcane/core/IItemFamily.h"
33#include "arcane/core/IMesh.h"
34#include "arcane/core/IObserver.h"
35#include "arcane/core/IParallelMng.h"
37#include "arcane/core/materials/internal/IMeshComponentInternal.h"
38#include "arcane/core/materials/internal/IMeshMaterialMngInternal.h"
40#include "arcane/materials/MaterialVariableBuildInfo.h"
41#include "arcane/materials/MatItemEnumerator.h"
42#include "arcane/materials/MeshMaterialVariableRef.h"
43#include "arcane/materials/MeshMaterialVariableDependInfo.h"
44#include "arcane/materials/IMeshMaterialVariableComputeFunction.h"
45#include "arcane/materials/IMeshMaterialVariableSynchronizer.h"
46#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
47#include "arcane/materials/internal/MeshMaterialVariableIndexer.h"
49#include "arcane/accelerator/RunCommandLoop.h"
60MeshMaterialVariablePrivate::
64, m_material_mng(v.materialMng())
69 if (!platform::getEnvironmentVariable(
"ARCANE_NO_RECURSIVE_DEPEND").null())
70 m_has_recursive_depend =
false;
76MeshMaterialVariablePrivate::
77~MeshMaterialVariablePrivate()
79 if (m_global_variable_changed_observer)
80 std::cerr <<
"WARNING: MeshMaterialVariablePrivate: in destructor: observer is not destroyed\n";
86Int32 MeshMaterialVariablePrivate::
89 return m_variable->dataTypeSize();
95void MeshMaterialVariablePrivate::
96copyToBuffer(SmallSpan<const MatVarIndex> matvar_indexes,
97 Span<std::byte> bytes, RunQueue* queue)
const
99 m_variable->_copyToBuffer(matvar_indexes, bytes, queue);
105void MeshMaterialVariablePrivate::
106copyFromBuffer(SmallSpan<const MatVarIndex> matvar_indexes,
107 Span<const std::byte> bytes, RunQueue* queue)
109 m_variable->_copyFromBuffer(matvar_indexes, bytes, queue);
115Ref<IData> MeshMaterialVariablePrivate::
116internalCreateSaveDataRef(Integer nb_value)
118 return m_variable->_internalCreateSaveDataRef(nb_value);
124void MeshMaterialVariablePrivate::
125saveData(IMeshComponent* component, IData* data)
127 m_variable->_saveData(component, data);
133void MeshMaterialVariablePrivate::
134restoreData(IMeshComponent* component, IData* data, Integer data_index,
135 Int32ConstArrayView ids,
bool allow_null_id)
137 m_variable->_restoreData(component, data, data_index, ids, allow_null_id);
143void MeshMaterialVariablePrivate::
144copyBetweenPartialAndGlobal(
const CopyBetweenPartialAndGlobalArgs& args)
146 m_variable->_copyBetweenPartialAndGlobal(args);
152void MeshMaterialVariablePrivate::
153initializeNewItemsWithZero(InitializeWithZeroArgs& args)
155 m_variable->_initializeNewItemsWithZero(args);
161void MeshMaterialVariablePrivate::
162syncReferences(
bool check_resize)
164 m_variable->_syncReferences(check_resize);
170void MeshMaterialVariablePrivate::
171resizeForIndexer(ResizeVariableIndexerArgs& args)
173 m_variable->_resizeForIndexer(args);
182MeshMaterialVariable::
183MeshMaterialVariable(
const MaterialVariableBuildInfo& v, MatVarSpace mvs)
184: m_p(new MeshMaterialVariablePrivate(v, mvs, this))
192MeshMaterialVariable::
193~MeshMaterialVariable()
201void MeshMaterialVariable::
209 Mutex::ScopedLock sl(m_p->materialMng()->variableLock());
210 ++m_p->m_nb_reference;
216void MeshMaterialVariable::
221 ref->setNextReference(m_p->m_first_reference);
222 if (m_p->m_first_reference) {
226 _list->setPreviousReference(ref);
229 ref->setPreviousReference(
nullptr);
231 m_p->m_first_reference = ref;
237void MeshMaterialVariable::
247 if (m_p->m_first_reference == tmp)
248 m_p->m_first_reference = m_p->m_first_reference->nextReference();
252 ref->setNextReference(
nullptr);
253 ref->setPreviousReference(
nullptr);
255 Int32 nb_ref = --m_p->m_nb_reference;
271 delete m_p->m_global_variable_changed_observer;
272 m_p->m_global_variable_changed_observer =
nullptr;
273 m_p->materialMng()->_internalApi()->removeVariable(
this);
282firstReference()
const
284 return m_p->m_first_reference;
303 return m_p->m_refs[index]->variable();
309void MeshMaterialVariable::
310setKeepOnChange(
bool v)
312 m_p->m_keep_on_change = v;
318bool MeshMaterialVariable::
321 return m_p->m_keep_on_change;
327void MeshMaterialVariable::
330 if (m_p->hasRecursiveDepend()) {
341 bool need_update =
false;
342 Int64 modified_time = m_p->m_modified_times[mat_id];
345 if (mt > modified_time) {
353 if (mt > modified_time) {
374void MeshMaterialVariable::
385 return m_p->m_modified_times[id];
388void MeshMaterialVariable::
394void MeshMaterialVariable::
400void MeshMaterialVariable::
407void MeshMaterialVariable::
413void MeshMaterialVariable::
419void MeshMaterialVariable::
426void MeshMaterialVariable::
429 m_p->m_compute_function = v;
435 return m_p->m_compute_function.get();
438void MeshMaterialVariable::
454 return m_p->materialMng()->traceMng();
477 static_assert(
sizeof(
MatVarIndex) == 2 *
sizeof(
Int32),
"Bad size for MatVarIndex");
478 auto* ptr =
reinterpret_cast<const Int32*
>(indexes.
data());
479 return { ptr, indexes.
size() * 2 };
485void MeshMaterialVariable::
486_copyToBuffer(SmallSpan<const MatVarIndex> matvar_indexes,
487 Span<std::byte> bytes, RunQueue* queue)
const
489 const Integer one_data_size = dataTypeSize();
490 SmallSpan<const Int32> indexes(_toInt32Indexes(matvar_indexes));
491 const Int32 nb_item = matvar_indexes.size();
492 MutableMemoryView destination_buffer(makeMutableMemoryView(bytes.data(), one_data_size, nb_item));
493 ConstMultiMemoryView source_view(m_views_as_bytes.view(), one_data_size);
494 MemoryUtils::copyWithIndexedSource(destination_buffer, source_view, indexes, queue);
500void MeshMaterialVariable::
501_copyFromBuffer(SmallSpan<const MatVarIndex> matvar_indexes,
502 Span<const std::byte> bytes, RunQueue* queue)
505 SmallSpan<const Int32> indexes(_toInt32Indexes(matvar_indexes));
506 const Int32 nb_item = matvar_indexes.size();
507 MutableMultiMemoryView destination_view(m_views_as_bytes.view(), one_data_size);
509 MemoryUtils::copyWithIndexedDestination(destination_view, source_buffer, indexes, queue);
515void MeshMaterialVariable::
516_genericCopyTo(Span<const std::byte> input,
517 SmallSpan<const Int32> input_indexes,
518 Span<std::byte> output,
519 SmallSpan<const Int32> output_indexes,
520 const RunQueue& queue, Int32 data_type_size)
523 Integer nb_value = input_indexes.size();
526 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output.data());
527 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input.data());
528 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input_indexes.data());
529 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output_indexes.data());
530 const Int32 dim2_size = data_type_size;
536 Int32 output_base = output_indexes[i] * dim2_size;
537 Int32 input_base = input_indexes[i] * dim2_size;
538 for (Int32 j = 0; j < dim2_size; ++j)
539 output[output_base + j] = input[input_base + j];
546IMeshMaterialVariableInternal* MeshMaterialVariable::
549 return m_p->_internalApi();
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
File containing declarations concerning the message passing model.
Memory and allocator management functions.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
1D loop on accelerator with additional arguments.
Base class for 1D data vectors.
void add(ConstReferenceType val)
Adds element val to the end of the array.
virtual Int64 modifiedTime()=0
Time when the variable was updated.
static Int64 incrementModifiedTime()
Increments the modification counter and returns its value before modification.
virtual void update()=0
Recalculates the variable if necessary.
virtual Int32 id() const =0
Component identifier.
virtual IMeshComponentInternal * _internalApi()=0
Internal API.
Interface of the functor class for recalculating a variable.
virtual void execute(IMeshMaterial *mat)=0
Executes the calculation function.
Interface of a material variable on a mesh.
virtual Int64 modifiedTime(IMeshMaterial *mat)=0
Time when the variable was updated.
Interface of a mesh material.
Represents an index on material and environment variables.
Information about a variable dependency.
IMeshMaterialVariable * variable() const
Variable.
Base class for material variable references.
MeshMaterialVariableRef * nextReference()
Next reference (or null) on variable().
MeshMaterialVariableRef * previousReference()
Previous reference (or null) on variable().
String name() const override
Name of the variable.
View of an array of elements of type T.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Unicode character string.
Information about a variable dependency.
IVariable * variable() const
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.
MemoryAllocationOptions getAllocatorForMostlyReadOnlyData()
Default allocator for mostly read-only data.
ARCANE_DATATYPE_EXPORT Integer dataTypeSize(eDataType type)
Size of data type type (which must be different from DT_String).
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstMemoryView makeConstMemoryView(const void *ptr, Int32 datatype_size, Int64 nb_element)
Creates a read-only memory view.
std::int32_t Int32
Signed integer type of 32 bits.
Memory management utility functions.