Arcane  4.1.15.0
User documentation
Loading...
Searching...
No Matches
MaterialVariableViews.h
Go to the documentation of this file.
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/* MaterialVariableViews.h (C) 2000-2026 */
9/* */
10/* Management of views on material variables for accelerators. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_ACCELERATOR_MATERIALVARIABLEVIEWS_H
13#define ARCANE_ACCELERATOR_MATERIALVARIABLEVIEWS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/materials/IMeshMaterialVariable.h"
20
21#include "arcane/accelerator/AcceleratorGlobal.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27using namespace Arcane;
28using namespace Arcane::Materials;
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace Arcane::Accelerator
34{
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38/*!
39 * \brief Base class for views on material variables.
40 */
41class ARCANE_ACCELERATOR_EXPORT MatVariableViewBase
42{
43 public:
44
45 // Currently does not use parameters yet
46 MatVariableViewBase(const ViewBuildInfo&, IMeshMaterialVariable*);
47
48 // Currently does not use parameters yet
49 MatVariableViewBase() = default;
50};
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54/*!
55 * \brief Read view on a scalar constituent variable.
56 */
57template <typename ItemType, typename DataType>
58class MatItemVariableScalarInViewT
59: public MatVariableViewBase
60{
61 // TODO: Should SIMD handling be added like in ItemVariableScalarInViewT?
62
63 private:
64
65 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
66
67 public:
68
69 MatItemVariableScalarInViewT(const ViewBuildInfo& vbi, IMeshMaterialVariable* var, ArrayView<DataType>* v)
70 : MatVariableViewBase(vbi, var)
71 , m_value(v)
72 {}
73 MatItemVariableScalarInViewT() = default;
74
75 //! Access operator for entity \a item
76 ARCCORE_HOST_DEVICE const DataType& operator[](ComponentItemLocalId lid) const
77 {
78 return this->m_value[lid.localId().arrayIndex()][lid.localId().valueIndex()];
79 }
80
81 //! Access operator for entity \a item
82 ARCCORE_HOST_DEVICE const DataType& operator[](PureMatVarIndex pmvi) const
83 {
84 return this->m_value[0][pmvi.valueIndex()];
85 }
86
87 //! Override to access the global value from the cell id
88 ARCCORE_HOST_DEVICE const DataType& operator[](ItemIndexType item) const
89 {
90 return this->m_value[0][item.localId()];
91 }
92
93 //! Access operator for entity \a item
94 ARCCORE_HOST_DEVICE const DataType& value(ComponentItemLocalId mvi) const
95 {
96 return this->m_value[mvi.localId().arrayIndex()][mvi.localId().valueIndex()];
97 }
98
99 ARCCORE_HOST_DEVICE const DataType& value0(PureMatVarIndex idx) const
100 {
101 return this->m_value[0][idx.valueIndex()];
102 }
103
104 private:
105
106 ArrayView<DataType>* m_value = nullptr;
107};
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111/*!
112 * \brief Write view on a scalar constituent variable.
113 */
114template <typename ItemType, typename Accessor>
115class MatItemVariableScalarOutViewT
116: public MatVariableViewBase
117{
118 private:
119
120 using DataType = typename Accessor::ValueType;
121 using DataTypeReturnType = DataType&;
122 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
123
124 // TODO: Should ARCANE_CHECK_AT(mvi.arrayIndex(), m_value.size()); be added?
125 // The check on the other dimension will still be missing.
126 // TODO: Should SIMD type handling be added like in ItemVariableScalarOutViewT?
127
128 public:
129
130 MatItemVariableScalarOutViewT(const ViewBuildInfo& vbi, IMeshMaterialVariable* var, ArrayView<DataType>* v)
131 : MatVariableViewBase(vbi, var)
132 , m_value(v)
133 {}
134 MatItemVariableScalarOutViewT() = default;
135
136 //! Access operator for entity \a item
137 ARCCORE_HOST_DEVICE Accessor operator[](ComponentItemLocalId lid) const
138 {
139 return Accessor(this->m_value[lid.localId().arrayIndex()].data() + lid.localId().valueIndex());
140 }
141
142 ARCCORE_HOST_DEVICE Accessor operator[](PureMatVarIndex pmvi) const
143 {
144 return Accessor(this->m_value[0][pmvi.valueIndex()]);
145 }
146
147 //! Override to access the global value from the cell id
148 ARCCORE_HOST_DEVICE Accessor operator[](ItemIndexType item) const
149 {
150 ARCANE_CHECK_AT(item.localId(), this->m_value[0].size());
151 return Accessor(this->m_value[0].data() + item.localId());
152 }
153
154 //! Access operator for entity \a item
155 ARCCORE_HOST_DEVICE Accessor value(ComponentItemLocalId lid) const
156 {
157 return Accessor(this->m_value[lid.localId().arrayIndex()].data() + lid.localId().valueIndex());
158 }
159
160 //! Positions the value for entity \a item at \a v
161 ARCCORE_HOST_DEVICE void setValue(ComponentItemLocalId lid, const DataType& v) const
162 {
163 this->m_value[lid.localId().arrayIndex()][lid.localId().valueIndex()] = v;
164 }
165
166 ARCCORE_HOST_DEVICE Accessor value0(PureMatVarIndex idx) const
167 {
168 return Accessor(this->m_value[0][idx.valueIndex()]);
169 }
170
171 private:
172
173 ArrayView<DataType>* m_value = nullptr;
174};
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178/*!
179 * \brief Read-only view on a 1D Array constituent variable.
180 */
181template <typename ItemType, typename DataType>
182class MatItemVariableArrayInViewT
183: public MatVariableViewBase
184{
185 private:
186
187 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
188
189 public:
190
192
193 public:
194
195 MatItemVariableArrayInViewT(const ViewBuildInfo& vbi, IMeshMaterialVariable* var,
196 const ContainerViewType& v)
197 : MatVariableViewBase(vbi, var)
198 , m_value(v)
199 {}
200 MatItemVariableArrayInViewT() = default;
201
202 //! Access operator for entity \a item
203 ARCCORE_HOST_DEVICE SmallSpan<const DataType> operator[](ComponentItemLocalId lid) const
204 {
205 return m_value[lid];
206 }
207
208 //! Access operator for entity \a item
210 {
211 return m_value[pmvi];
212 }
213
214 //! Override to access the global value from the cell id
215 ARCCORE_HOST_DEVICE SmallSpan<const DataType> operator[](ItemIndexType item) const
216 {
217 return m_value[item];
218 }
219
220 //! Access operator for entity \a item
221 ARCCORE_HOST_DEVICE SmallSpan<const DataType> value(ComponentItemLocalId mvi) const
222 {
223 return m_value[mvi];
224 }
225
226 //! Override to access the global value from the cell id
227 ARCCORE_HOST_DEVICE SmallSpan<const DataType> value(ItemIndexType item) const
228 {
229 return m_value[item];
230 }
231
232 private:
233
234 ContainerViewType m_value;
235};
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239/*!
240 * \brief Write view on a 1D Array constituent variable.
241 */
242template <typename ItemType, typename Accessor>
243class MatItemVariableArrayOutViewT
244: public MatVariableViewBase
245{
246 private:
247
248 using DataType = typename Accessor::ValueType;
249 using DataTypeReturnType = DataType&;
250 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
251 using ContainerViewType = MeshMaterialVariableArrayContainerView<DataType>;
252
253 public:
254
255 MatItemVariableArrayOutViewT(const ViewBuildInfo& vbi, IMeshMaterialVariable* var, const ContainerViewType& v)
256 : MatVariableViewBase(vbi, var)
257 , m_value(v)
258 {}
259 MatItemVariableArrayOutViewT() = default;
260
261 //! Access operator for entity \a item
262 ARCCORE_HOST_DEVICE Accessor operator[](ComponentItemLocalId lid) const
263 {
264 return Accessor(m_value[lid]);
265 }
266
267 ARCCORE_HOST_DEVICE Accessor operator[](PureMatVarIndex pmvi) const
268 {
269 return Accessor(m_value[pmvi]);
270 }
271
272 //! Override to access the global value from the cell id
273 ARCCORE_HOST_DEVICE Accessor operator[](ItemIndexType item) const
274 {
275 return Accessor(m_value[item]);
276 }
277
278 //! Access operator for entity \a item
279 ARCCORE_HOST_DEVICE Accessor value(ComponentItemLocalId lid) const
280 {
281 return Accessor(m_value[lid]);
282 }
283
284 private:
285
286 ContainerViewType m_value;
287};
288
289/*---------------------------------------------------------------------------*/
290/*---------------------------------------------------------------------------*/
291/*!
292 * \brief Write view for scalar material variables.
293 */
294template <typename DataType> auto
296{
297 using Accessor = DataViewSetter<DataType>;
298 return MatItemVariableScalarOutViewT<Cell, Accessor>(vbi, var.materialVariable(), var._internalValue());
299}
300
301/*---------------------------------------------------------------------------*/
302/*---------------------------------------------------------------------------*/
303/*!
304 * \brief Read/write view for 1D Array constituent variables
305 */
306template <typename DataType> auto
308{
309 using Accessor = View1DSetter<DataType>;
311 return ViewType(vbi, var.materialVariable(), var.containerView());
312}
313
314/*---------------------------------------------------------------------------*/
315/*---------------------------------------------------------------------------*/
316/*!
317 * \brief Write view for scalar environment variables
318 */
319template <typename DataType> auto
321{
322 using Accessor = DataViewSetter<DataType>;
323 return MatItemVariableScalarOutViewT<Cell, Accessor>(vbi, var.materialVariable(), var._internalValue());
324}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328/*!
329 * \brief Read/write view for 1D Array constituent variables
330 */
331template <typename DataType> auto
333{
334 using Accessor = View1DSetter<DataType>;
336 return ViewType(vbi, var.materialVariable(), var.containerView());
337}
338
339/*---------------------------------------------------------------------------*/
340/*---------------------------------------------------------------------------*/
341
342/*---------------------------------------------------------------------------*/
343/*---------------------------------------------------------------------------*/
344/*!
345 * \brief Read/write view for scalar material variables
346 */
347template <typename DataType> auto
349{
350 using Accessor = DataViewGetterSetter<DataType>;
351 return MatItemVariableScalarOutViewT<Cell, Accessor>(vbi, var.materialVariable(), var._internalValue());
352}
353
354/*---------------------------------------------------------------------------*/
355/*---------------------------------------------------------------------------*/
356/*!
357 * \brief Read/write view for 1D Array constituent variables
358 */
359template <typename DataType> auto
361{
362 using Accessor = View1DGetterSetter<DataType>;
364 return ViewType(vbi, var.materialVariable(), var.containerView());
365}
366
367/*---------------------------------------------------------------------------*/
368/*---------------------------------------------------------------------------*/
369/*!
370 * \brief Read/write view for scalar material variables
371 */
372template <typename DataType> auto
374{
375 using Accessor = DataViewGetterSetter<DataType>;
376 return MatItemVariableScalarOutViewT<Cell, Accessor>(vbi, var.materialVariable(), var._internalValue());
377}
378
379/*---------------------------------------------------------------------------*/
380/*---------------------------------------------------------------------------*/
381/*!
382 * \brief Read/write view for 1D Array environment variables
383 */
384template <typename DataType> auto
386{
387 using Accessor = View1DGetterSetter<DataType>;
389 return ViewType(vbi, var.materialVariable(), var.containerView());
390}
391
392/*---------------------------------------------------------------------------*/
393/*---------------------------------------------------------------------------*/
394
395/*---------------------------------------------------------------------------*/
396/*---------------------------------------------------------------------------*/
397/*!
398 * \brief Read view for scalar material variables
399 */
400template <typename DataType> auto
402{
403 return MatItemVariableScalarInViewT<Cell, DataType>(vbi, var.materialVariable(), var._internalValue());
404}
405
406/*---------------------------------------------------------------------------*/
407/*---------------------------------------------------------------------------*/
408/*!
409 * \brief Read view for 1D array material variables
410 */
411template <typename DataType> auto
413{
414 return MatItemVariableArrayInViewT<Cell, DataType>(vbi, var.materialVariable(), var.constContainerView());
415}
416
417/*---------------------------------------------------------------------------*/
418/*---------------------------------------------------------------------------*/
419/*!
420 * \brief Read view for scalar material variables
421 */
422template <typename DataType> auto
424{
425 return MatItemVariableScalarInViewT<Cell, DataType>(vbi, var.materialVariable(), var._internalValue());
426}
427
428/*---------------------------------------------------------------------------*/
429/*---------------------------------------------------------------------------*/
430/*!
431 * \brief Read view for 1D array material variables
432 */
433template <typename DataType> auto
435{
436 return MatItemVariableArrayInViewT<Cell, DataType>(vbi, var.materialVariable(), var.constContainerView());
437}
438
439/*---------------------------------------------------------------------------*/
440/*---------------------------------------------------------------------------*/
441
442} // namespace Arcane::Accelerator
443
444/*---------------------------------------------------------------------------*/
445/*---------------------------------------------------------------------------*/
446
447#endif
Read-only view on a 1D Array constituent variable.
__host__ __device__ SmallSpan< const DataType > value(ComponentItemLocalId mvi) const
Access operator for entity item.
__host__ __device__ SmallSpan< const DataType > value(ItemIndexType item) const
Override to access the global value from the cell id.
__host__ __device__ SmallSpan< const DataType > operator[](ItemIndexType item) const
Override to access the global value from the cell id.
__host__ __device__ SmallSpan< const DataType > operator[](ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ SmallSpan< const DataType > operator[](PureMatVarIndex pmvi) const
Access operator for entity item.
Write view on a 1D Array constituent variable.
__host__ __device__ Accessor operator[](ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ Accessor value(ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ Accessor operator[](ItemIndexType item) const
Override to access the global value from the cell id.
Read view on a scalar constituent variable.
__host__ __device__ const DataType & operator[](ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ const DataType & value(ComponentItemLocalId mvi) const
Access operator for entity item.
__host__ __device__ const DataType & operator[](PureMatVarIndex pmvi) const
Access operator for entity item.
__host__ __device__ const DataType & operator[](ItemIndexType item) const
Override to access the global value from the cell id.
Write view on a scalar constituent variable.
__host__ __device__ Accessor operator[](ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ Accessor value(ComponentItemLocalId lid) const
Access operator for entity item.
__host__ __device__ void setValue(ComponentItemLocalId lid, const DataType &v) const
Positions the value for entity item at v.
__host__ __device__ Accessor operator[](ItemIndexType item) const
Override to access the global value from the cell id.
Class to access a 1D array of a read/write view.
Class to access a 1D array of a read/write view.
Modifiable view of an array of type T.
Array variable on the cells of a mesh material. For now, this class is only instantiated for cells.
Array variable over the cells of a material in the mesh. For now, this class is only instantiated for...
constexpr __host__ __device__ MatVarIndex localId() const
Generic index to access variable values.
Interface of a material variable on a mesh.
constexpr __host__ __device__ Int32 arrayIndex() const
Returns the index of the value array in the list of variables.
constexpr __host__ __device__ Int32 valueIndex() const
Returns the index in the value array.
View of the container for 1D array constituent variables.
IMeshMaterialVariable * materialVariable() const
Associated material variable.
Index of a pure material item in a variable.
View of an array of elements of type T.
Definition Span.h:805
Namespace for accelerator usage.
auto viewInOut(const ViewBuildInfo &vbi, CellMaterialVariableScalarRef< DataType > &var)
Read/write view for scalar material variables.
auto viewOut(const ViewBuildInfo &vbi, CellMaterialVariableScalarRef< DataType > &var)
Write view for scalar material variables.
auto viewIn(const ViewBuildInfo &vbi, const CellMaterialVariableScalarRef< DataType > &var)
Read view for scalar material variables.
Always enables tracing in Arcane parts concerning materials.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --