Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MeshMaterialVariableArray.inst.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MeshMaterialVariableArray.inst.h (C) 2000-2025 */
9/* */
10/* Variable array on a mesh material. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/NotImplementedException.h"
15#include "arcane/utils/NumericTypes.h"
16#include "arcane/utils/ITraceMng.h"
17#include "arcane/utils/CheckedConvert.h"
18
19#include "arcane/materials/MeshMaterialVariable.h"
20#include "arcane/materials/MaterialVariableBuildInfo.h"
21#include "arcane/materials/MeshMaterialVariableSynchronizerList.h"
22#include "arcane/materials/MatItemEnumerator.h"
23#include "arcane/materials/ComponentItemVectorView.h"
24#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
25
26#include "arcane/core/Array2Variable.h"
27#include "arcane/core/VariableRefArray2.h"
28#include "arcane/core/MeshVariable.h"
29#include "arcane/core/ISerializer.h"
30#include "arcane/core/internal/IVariableInternal.h"
31
32#include "arcane/materials/ItemMaterialVariableBaseT.H"
33
34#include "arcane/datatype/DataTypeTraits.h"
35#include "arcane/datatype/DataStorageBuildInfo.h"
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40namespace Arcane::Materials
41{
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46template <typename DataType> void
48copyTo(SmallSpan2<const DataType> input, SmallSpan<const Int32> input_indexes,
49 SmallSpan2<DataType> output, SmallSpan<const Int32> output_indexes,
50 const RunQueue& queue)
51{
52 // TODO: check if indexes and dim2Size() are identical
53 Integer nb_value = input_indexes.size();
54 Integer dim2_size = input.dim2Size();
55
56 auto command = makeCommand(queue);
57 command << RUNCOMMAND_LOOP1(iter, nb_value)
58 {
59 auto [i] = iter();
60 auto xo = output[output_indexes[i]];
61 auto xi = input[input_indexes[i]];
62 for (Integer j = 0; j < dim2_size; ++j)
63 xo[j] = xi[j];
64 };
65}
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
70template <typename DataType> void
72resizeAndFillWithDefault(ValueDataType* data, ContainerType& container, Integer dim1_size)
73{
74 ContainerType& values = data->_internal()->_internalDeprecatedValue();
75 Integer dim2_size = values.dim2Size();
76
77 //TODO: create an Array2 version that specifies a value to give
78 // for initialization during a resize() (like for Array::resize()).
79 container.resize(dim1_size, dim2_size);
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85template <typename DataType> void
87resizeWithReserve(PrivatePartType* var, Integer dim1_size, Real reserve_ratio)
88{
89 // To avoid reallocating every time the number of material cells increases,
90 // allocate a little more than necessary.
91 // By default, we allocate 5% more.
92 Int32 nb_add = static_cast<Int32>(dim1_size * reserve_ratio);
93 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add, true));
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99template <typename DataType> void
101saveData(IMeshComponent* component, IData* data,
102 Array<ContainerViewType>& cviews)
103{
104 // For 2D arrays, we resize \a values directly
105 // by adding the number of elements for our
106 // component.
107 ConstArrayView<Array2View<DataType>> views = cviews;
108 if (views.empty())
109 return;
110 auto* true_data = dynamic_cast<ValueDataType*>(data);
111 ARCANE_CHECK_POINTER(true_data);
112 ContainerType& values = true_data->_internal()->_internalDeprecatedValue();
113 ComponentItemVectorView component_view = component->view();
114
115 Integer dim2_size = views[0].dim2Size();
116 Integer current_dim1_size = values.dim1Size();
117 Integer added_dim1_size = component_view.nbItem();
118 values.resizeNoInit(current_dim1_size + added_dim1_size, dim2_size);
119 ConstArrayView<MatVarIndex> mvi_indexes = component_view._matvarIndexes();
120
121 for (Integer i = 0, n = mvi_indexes.size(); i < n; ++i) {
122 MatVarIndex mvi = mvi_indexes[i];
123 values[i + current_dim1_size].copy(views[mvi.arrayIndex()][mvi.valueIndex()]);
124 }
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130template <typename DataType> ItemMaterialVariableArray<DataType>::
131ItemMaterialVariableArray(const MaterialVariableBuildInfo& v, PrivatePartType* global_var,
132 VariableRef* global_var_ref, MatVarSpace mvs)
133: BaseClass(v, global_var, global_var_ref, mvs)
134{
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140template <typename DataType> void
141ItemMaterialVariableArray<DataType>::
142dumpValues(std::ostream& ostr)
143{
144 ARCANE_UNUSED(ostr);
145 throw NotImplementedException(A_FUNCINFO);
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151template <typename DataType> void
152ItemMaterialVariableArray<DataType>::
153dumpValues(std::ostream& ostr, AllEnvCellVectorView view)
154{
155 ARCANE_UNUSED(ostr);
156 ARCANE_UNUSED(view);
157 throw NotImplementedException(A_FUNCINFO);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163template <typename DataType> void
164ItemMaterialVariableArray<DataType>::
165serialize(ISerializer* sbuf, Int32ConstArrayView ids)
166{
167 // TODO: try to merge this method with the scalar variant.
168 IItemFamily* family = m_global_variable->itemFamily();
169 if (!family)
170 return;
171 IMeshMaterialMng* mat_mng = m_p->materialMng();
173 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
175 ItemVectorView ids_view(family->view(ids));
176 Int32 dim2_size = m_global_variable->valueView().dim2Size();
177 bool has_mat = this->space() != MatVarSpace::Environment;
178 switch (sbuf->mode()) {
179 case ISerializer::ModeReserve: {
180 Int64 nb_val = 0;
181 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
182 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
183 ++nb_val; // 1 value for the environment
184 EnvCell envcell = *ienvcell;
185 if (has_mat)
186 nb_val += envcell.nbMaterial(); // 1 value per environment material.
187 }
188 }
189 sbuf->reserve(m_global_variable->fullName());
190 sbuf->reserveInt64(1); // For the number of values.
191 Int64 nb_basic_value = nb_val * nb_count * dim2_size;
192 sbuf->reserveSpan(data_type, nb_basic_value); // For the number of values.
193 } break;
196 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
197 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
198 values.addRange(value(ienvcell._varIndex()));
199 if (has_mat) {
200 ENUMERATE_CELL_MATCELL (imatcell, (*ienvcell)) {
201 values.addRange(value(imatcell._varIndex()));
202 }
203 }
204 }
205 }
206 Int64 nb_value = values.largeSize();
207 Span<const BasicType> basic_values(reinterpret_cast<BasicType*>(values.data()), nb_value * nb_count);
208 sbuf->put(m_global_variable->fullName());
209 sbuf->putInt64(nb_value);
210 sbuf->putSpan(basic_values);
211 } break;
213 UniqueArray<BasicType> basic_values;
214 String ref_name;
215 sbuf->get(ref_name);
216 if (m_global_variable->fullName() != ref_name)
217 ARCANE_FATAL("Bad serialization expected={0} found={1}", m_global_variable->fullName(), ref_name);
218 Int32 nb_value = CheckedConvert::toInt32(sbuf->getInt64());
219 Int32 nb_basic_value = nb_value * nb_count;
220 basic_values.resize(nb_basic_value);
221 sbuf->getSpan(basic_values);
222 if (dim2_size != 0) {
223 SmallSpan2<DataType> data_values(reinterpret_cast<DataType*>(basic_values.data()), nb_value, dim2_size);
224 Int32 index = 0;
225 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
226 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
227 EnvCell envcell = *ienvcell;
228 setValue(ienvcell._varIndex(), data_values[index]);
229 ++index;
230 if (has_mat) {
231 ENUMERATE_CELL_MATCELL (imatcell, envcell) {
232 setValue(imatcell._varIndex(), data_values[index]);
233 ++index;
234 }
235 }
236 }
237 }
238 }
239 } break;
240 default:
241 ARCANE_THROW(NotSupportedException, "Invalid serialize");
242 }
243}
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248template <typename DataType> void
249ItemMaterialVariableArray<DataType>::
250synchronize()
251{
252 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
253 mmvsl.add(this);
254 mmvsl.apply();
255}
256
257/*---------------------------------------------------------------------------*/
258/*---------------------------------------------------------------------------*/
259
260template <typename DataType> void
263{
264 sync_list.add(this);
265}
266
267/*---------------------------------------------------------------------------*/
268/*---------------------------------------------------------------------------*/
269
270template <typename DataType> void
272resize(Integer dim2_size)
273{
274 for (PrivatePartType* pvar : m_vars) {
275 if (pvar) {
276 Integer dim1_size = pvar->valueView().dim1Size();
277 pvar->directResize(dim1_size, dim2_size);
278 }
279 }
280
281 //m_data->value().resize(dim1_size,dim2_size);
282 /*info() << "RESIZE(2) AFTER " << fullName()
283 << " total=" << m_data->value().totalNbElement()
284 << " dim1_size=" << m_data->value().dim1Size()
285 << " dim2_size=" << m_data->value().dim2Size()
286 << " addr=" << m_data->value().view().unguardedBasePointer();*/
287 this->syncReferences();
288}
289
290/*---------------------------------------------------------------------------*/
291/*---------------------------------------------------------------------------*/
292
293template <typename DataType> Int32
294ItemMaterialVariableArray<DataType>::
295dataTypeSize() const
296{
297 Int32 dim2_size = m_vars[0]->valueView().dim2Size();
298 return (Int32)sizeof(DataType) * dim2_size;
299}
300
301/*---------------------------------------------------------------------------*/
302/*---------------------------------------------------------------------------*/
303
304template <typename DataType> void
305ItemMaterialVariableArray<DataType>::
306_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<std::byte> bytes) const
307{
308 const Integer one_data_size = dataTypeSize();
309 Integer dim2_size = m_vars[0]->valueView().dim2Size();
310
311 // TODO: Check that the size is a multiple of 'one_data_size' and that
312 // the alignment is correct.
313 const Int32 value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
314 Array2View<DataType> values(reinterpret_cast<DataType*>(bytes.data()), value_size, dim2_size);
315 for (Integer z = 0; z < value_size; ++z) {
316 values[z].copy(value(matvar_indexes[z]));
317 }
318}
319
320/*---------------------------------------------------------------------------*/
321/*---------------------------------------------------------------------------*/
322
323template <typename DataType> void
324ItemMaterialVariableArray<DataType>::
325copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteArrayView bytes) const
326{
327 auto* ptr = reinterpret_cast<std::byte*>(bytes.data());
328 return _copyToBufferLegacy(matvar_indexes, { ptr, bytes.size() });
329}
330
331/*---------------------------------------------------------------------------*/
332/*---------------------------------------------------------------------------*/
333
334template <typename DataType> void
335ItemMaterialVariableArray<DataType>::
336_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<const std::byte> bytes)
337{
338 const Integer one_data_size = dataTypeSize();
339 Integer dim2_size = m_vars[0]->valueView().dim2Size();
340
341 // TODO: Check that the size is a multiple of 'one_data_size' and that
342 // the alignment is correct.
343 const Integer value_size = CheckedConvert::toInt32(bytes.size() / one_data_size);
344 ConstArray2View<DataType> values(reinterpret_cast<const DataType*>(bytes.data()), value_size, dim2_size);
345 for (Integer z = 0; z < value_size; ++z) {
346 setValue(matvar_indexes[z], values[z]);
347 }
348}
349
350/*---------------------------------------------------------------------------*/
351/*---------------------------------------------------------------------------*/
352
353template <typename DataType> void
354ItemMaterialVariableArray<DataType>::
355copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteConstArrayView bytes)
356{
357 auto* ptr = reinterpret_cast<const std::byte*>(bytes.data());
358 return _copyFromBufferLegacy(matvar_indexes, { ptr, bytes.size() });
359}
360
361/*---------------------------------------------------------------------------*/
362/*---------------------------------------------------------------------------*/
363
364/*---------------------------------------------------------------------------*/
365/*---------------------------------------------------------------------------*/
366
367template <typename ItemType, typename DataType>
368MeshMaterialVariableArray<ItemType, DataType>::
369MeshMaterialVariableArray(const MaterialVariableBuildInfo& v, PrivatePartType* global_var,
370 VariableRefType* global_var_ref, MatVarSpace mvs)
371: ItemMaterialVariableArray<DataType>(v, global_var, global_var_ref, mvs)
372, m_true_global_variable_ref(global_var_ref) // Will be destroyed by the base class
373{
374}
375
376/*---------------------------------------------------------------------------*/
377/*---------------------------------------------------------------------------*/
378
379#define ARCANE_INSTANTIATE_MAT(type) \
380 template class ItemMaterialVariableBase<MaterialVariableArrayTraits<type>>; \
381 template class ItemMaterialVariableArray<type>; \
382 template class MeshMaterialVariableArray<Cell, type>; \
383 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableArray<Cell, type>>
384
385/*---------------------------------------------------------------------------*/
386/*---------------------------------------------------------------------------*/
387
388} // End namespace Arcane::Materials
389
390/*---------------------------------------------------------------------------*/
391/*---------------------------------------------------------------------------*/
#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_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
1D loop on accelerator with additional arguments.
Int64 largeSize() const
Number of elements in the vector (in 64 bits).
void addRange(ConstReferenceType val, Int64 n)
Adds n elements of value val to the end of the array.
void resize(Int64 s)
Changes the number of elements in the array to s.
const T * data() const
Access to the root of the array without any protection.
Interface of an entity family.
Definition IItemFamily.h:83
virtual ItemVectorView view(Int32ConstArrayView local_ids)=0
View on the entities.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual Int64 getInt64()=0
Retrieve a size.
virtual void put(Span< const Real > values)=0
Add the array values.
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 get(ArrayView< Real > values)=0
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.
View on a vector of entities.
View over a vector of entities of a component.
Integer nbItem() const
Number of entities in the view.
Arcane cell of an environment.
Interface of a component (material or environment) of a mesh.
Interface for the material and environment manager of a mesh.
Represents an index on material and environment variables.
Characteristics for an array material variable.
void add(MeshMaterialVariable *var)
Adds the variable var to the list of variables to synchronize.
MatVarSpace space() const override
Variable definition space (material+environment or environment only).
View for a 2D array whose size is an 'Int32'.
Definition Span2.h:249
View of an array of elements of type T.
Definition Span.h:635
1D data vector with value semantics (STL style).
#define ENUMERATE_CELL_MATCELL(iname, env_cell)
Macro to iterate over all MatCell cells of a cell.
#define ENUMERATE_CELL_ENVCELL(iname, all_env_cell)
Macro to iterate over all EnvCell cells of a cell.
#define ENUMERATE_ALLENVCELL(iname,...)
Macro to iterate over all AllEnvCell cells of a group.
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.
ARCANE_DATATYPE_EXPORT Integer dataTypeSize(eDataType type)
Size of data type type (which must be different from DT_String).
Definition DataTypes.cc:111
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.
Definition UtilsTypes.h:482
eBasicDataType
Type of a basic data item.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.