Arcane  4.2.3.0
User documentation
Loading...
Searching...
No Matches
MDVariableViews.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/* MDVariableViews.h (C) 2000-2026 */
9/* */
10/* multi-dimensional variable view management for accelerators. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_ACCELERATOR_MDVARIABLEVIEWS_H
13#define ARCANE_ACCELERATOR_MDVARIABLEVIEWS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/ArrayLayout.h"
18
20
21#include "arccore/common/NumMatrixDataView.h"
22#include "arccore/common/NumVectorDataView.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane::Accelerator
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32/*!
33 * \brief Base class for views of multi-dimensional variable over a mesh item.
34 */
35template <typename ItemType_, typename Accessor_, typename Extents_>
36class MeshMDVariableViewBase
37{
38 using Accessor = Accessor_;
39 using AddedFirstExtentsType = typename Extents_::template AddedFirstExtentsType<DynExtent>;
40
41 public:
42
43 using ItemType = ItemType_;
44 using DataType = Accessor::ValueType;
45 using AccessorReturnType = Accessor::AccessorReturnType;
46 using Extents = Extents_;
47 using ItemLocalIdType = ItemType::LocalIdType;
48
49 protected:
50
52
53 protected:
54
55 explicit constexpr ARCCORE_HOST_DEVICE MeshMDVariableViewBase(const MDSpanType& v)
56 : m_mdspan(v)
57 {
58 }
59
60 public:
61
62 constexpr ARCCORE_HOST_DEVICE AccessorReturnType operator()(ItemLocalIdType id) const
63 requires(Extents::rank() == 0)
64 {
65 return Accessor::build(m_mdspan.ptrAt(id.localId()));
66 }
67 constexpr ARCCORE_HOST_DEVICE AccessorReturnType operator()(ItemLocalIdType id, Int32 i1) const
68 requires(Extents::rank() == 1)
69 {
70 return Accessor::build(m_mdspan.ptrAt(id.localId(), i1));
71 }
72
73 constexpr ARCCORE_HOST_DEVICE AccessorReturnType operator()(ItemLocalIdType id, Int32 i1, Int32 i2) const
74 requires(Extents::rank() == 2)
75 {
76 return Accessor::build(m_mdspan.ptrAt(id.localId(), i1, i2));
77 }
78
79 constexpr ARCCORE_HOST_DEVICE AccessorReturnType operator()(ItemLocalIdType id, Int32 i, Int32 j, Int32 k) const
80 requires(Extents::rank() == 3)
81 {
82 return Accessor::build(m_mdspan.ptrAt(id.localId(), i, j, k));
83 }
84
85 private:
86
87 MDSpanType m_mdspan;
88};
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92/*!
93 * \brief Read-only view of multi-dimensional variable over a mesh item.
94 */
95template <typename ItemType_, typename DataType_, typename Extents>
96class MeshMDVariableInView
97: protected MeshMDVariableViewBase<ItemType_, DataViewGetter<DataType_>, Extents>
98{
99 using BaseClass = MeshMDVariableViewBase<ItemType_, DataViewGetter<DataType_>, Extents>;
100
101 public:
102
103 using ItemType = ItemType_;
104 using DataType = DataType_;
105 using ExtentsType = Extents;
106 using ItemLocalIdType = ItemType::LocalIdType;
108
109 using BaseClass::operator();
110
111 private:
112
113 using MDSpanType = VariableRefType::MDSpanType;
114
115 public:
116
117 MeshMDVariableInView(const ViewBuildInfo& view_bi, const VariableRefType& var_ref)
118 : BaseClass(var_ref.m_mdspan)
119 {
120 IVariable* var = var_ref.underlyingVariable().variable();
121 VariableViewBase vb(view_bi, var);
122 }
123};
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127/*!
128 * \brief Read-write view of multi-dimensional variable over a mesh item.
129 */
130template <typename ItemType_, typename DataType_, typename Extents>
131class MeshMDVariableInOutView
132: protected MeshMDVariableViewBase<ItemType_, DataViewGetterSetter<DataType_>, Extents>
133{
134 using BaseClass = MeshMDVariableViewBase<ItemType_, DataViewGetterSetter<DataType_>, Extents>;
135
136 public:
137
138 using ItemType = ItemType_;
139 using DataType = DataType_;
140 using ExtentsType = Extents;
141 using ItemLocalIdType = ItemType::LocalIdType;
143
144 using BaseClass::operator();
145
146 private:
147
148 using MDSpanType = VariableRefType::MDSpanType;
149
150 public:
151
152 MeshMDVariableInOutView(const ViewBuildInfo& view_bi, VariableRefType& var_ref)
153 : BaseClass(var_ref.m_mdspan)
154 {
155 IVariable* var = var_ref.underlyingVariable().variable();
156 VariableViewBase vb(view_bi, var);
157 }
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165/*!
166 * \brief Base class for views of multi-dimensional vector variable over a mesh item.
167 *
168 * \a MatrixAccessor_ has to be NumVectorxDataViewGetter or NumVectorDataViewGetterSetter
169 */
170template <typename ItemType_, typename VectorAccessor_, typename Extents>
171class MeshVectorMDVariableViewBase
172{
173 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
174
175 public:
176
177 using ItemType = ItemType_;
178 using VectorAccessor = VectorAccessor_;
179 using VectorElementAccessor = VectorAccessor_::VectorElemenAccessor;
180 using NumVectorType = VectorAccessor::NumVectorType;
181 using VectorAccessorReturnType = VectorAccessor::AccessorReturnType;
182 using VectorElementAccessorReturnType = VectorElementAccessor::AccessorReturnType;
183 using ItemLocalIdType = ItemType::LocalIdType;
184
185 private:
186
188
189 public:
190
191 MeshVectorMDVariableViewBase(const MDSpanType& vector_mdspan)
192 : m_vector_mdspan(vector_mdspan)
193 {
194 }
195
196 public:
197
198 //! \name Operations for variable of dimension MDDim0
199 ///@{
200
201 //! Accessor of the vector for item \a id
202 constexpr ARCCORE_HOST_DEVICE VectorAccessorReturnType operator()(ItemLocalIdType id) const
203 requires(Extents::rank() == 0)
204 {
205 return VectorAccessor::build(m_vector_mdspan.ptrAt(id.localId()));
206 }
207
208 //! accessor for the element (i) of the vector for item \a id
209 constexpr ARCCORE_HOST_DEVICE VectorElementAccessorReturnType operator()(ItemLocalIdType id, Int32 i) const
210 requires(Extents::rank() == 0)
211 {
212 return VectorElementAccessor::build(&m_vector_mdspan(id.localId())(i));
213 }
214 ///@}
215
216 //! \name Operations for variable of dimension MDDim1
217 //! Accessor of the vector of index \a index for item \a id
218 constexpr ARCCORE_HOST_DEVICE VectorAccessorReturnType operator()(ItemLocalIdType id, Int32 index) const
219 requires(Extents::rank() == 1)
220 {
221 return VectorAccessor::build(m_vector_mdspan.ptrAt(id.localId(), index));
222 }
223
224 //! Accessor for the element (i) of the vector for item \a id and index \a index
225 constexpr ARCCORE_HOST_DEVICE VectorElementAccessorReturnType operator()(ItemLocalIdType id, Int32 index, Int32 i) const
226 requires(Extents::rank() == 1)
227 {
228 return VectorElementAccessor::build(&m_vector_mdspan(id.localId(), index)(i));
229 }
230 ///@}
231
232 private:
233
234 MDSpanType m_vector_mdspan;
235};
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239/*!
240 * \brief Read-only view of multi-dimensional vector variable over a mesh item.
241 */
242template <typename ItemType_, typename DataType_, int Size_, typename Extents>
243class MeshVectorMDVariableInView
244: public MeshVectorMDVariableViewBase<ItemType_, NumVectorDataViewGetter<DataType_, Size_>, Extents>
245{
246 using BaseClass = MeshVectorMDVariableViewBase<ItemType_, NumVectorDataViewGetter<DataType_, Size_>, Extents>;
247
248 public:
249
250 static const int Size = Size_;
251 using ItemType = ItemType_;
252 using DataType = DataType_;
253 using ItemLocalIdType = ItemType::LocalIdType;
254 using ConstReferenceType = NumVectorDataViewGetter<DataType, Size>;
256
257 private:
258
259 using MDSpanType = VariableRefType::MDSpanType;
260
261 public:
262
263 MeshVectorMDVariableInView(const ViewBuildInfo& view_bi, const VariableRefType& var_ref)
264 : BaseClass(var_ref.m_vector_mdspan)
265 {
266 IVariable* var = var_ref.underlyingVariable().variable();
267 VariableViewBase vvb(view_bi, var);
268 }
269};
270
271/*---------------------------------------------------------------------------*/
272/*---------------------------------------------------------------------------*/
273/*!
274 * \brief Read-write view of multi-dimensional vector variable over a mesh item.
275 */
276template <typename ItemType_, typename DataType_, int Size_, typename Extents>
277class MeshVectorMDVariableInOutView
278: public MeshVectorMDVariableViewBase<ItemType_, NumVectorDataViewGetterSetter<DataType_, Size_>, Extents>
279{
280 using BaseClass = MeshVectorMDVariableViewBase<ItemType_, NumVectorDataViewGetterSetter<DataType_, Size_>, Extents>;
281
282 public:
283
284 static const int Size = Size_;
285 using ItemType = ItemType_;
286 using DataType = DataType_;
287 using ItemLocalIdType = ItemType::LocalIdType;
288 using ConstReferenceType = NumVectorDataViewGetter<DataType, Size>;
290
291 private:
292
293 using MDSpanType = VariableRefType::MDSpanType;
294
295 public:
296
297 MeshVectorMDVariableInOutView(const ViewBuildInfo& view_bi, const VariableRefType& var_ref)
298 : BaseClass(var_ref.m_vector_mdspan)
299 {
300 IVariable* var = var_ref.underlyingVariable().variable();
301 VariableViewBase vvb(view_bi, var);
302 }
303};
304
305/*---------------------------------------------------------------------------*/
306/*---------------------------------------------------------------------------*/
307
308/*---------------------------------------------------------------------------*/
309/*---------------------------------------------------------------------------*/
310/*!
311 * \brief Base class for views of multi-dimensional matrix variable over a mesh item.
312 *
313 * \a MatrixAccessor_ has to be NumMatrixDataViewGetter or NumMatrixDataViewGetterSetter
314 */
315template <typename ItemType_, typename MatrixAccessor_, typename Extents>
316class MeshMatrixMDVariableViewBase
317{
318 using AddedFirstExtentsType = typename Extents::template AddedFirstExtentsType<DynExtent>;
319
320 public:
321
322 using ItemType = ItemType_;
323 using MatrixAccessor = MatrixAccessor_;
324 using MatrixElementAccessor = MatrixAccessor_::MatrixElemenAccessor;
325 using NumMatrixType = MatrixAccessor::NumMatrixType;
326 using MatrixAccessorReturnType = MatrixAccessor::AccessorReturnType;
327 using MatrixElementAccessorReturnType = MatrixElementAccessor::AccessorReturnType;
328 using ItemLocalIdType = ItemType::LocalIdType;
329
330 private:
331
333
334 public:
335
336 MeshMatrixMDVariableViewBase(const MDSpanType& matrix_mdspan)
337 : m_matrix_mdspan(matrix_mdspan)
338 {
339 }
340
341 public:
342
343 //! \name Operations for variable of dimension MDDim0
344 ///@{
345
346 //! Accessor of the matrix for item \a id
347 constexpr ARCCORE_HOST_DEVICE MatrixAccessorReturnType operator()(ItemLocalIdType id) const
348 requires(Extents::rank() == 0)
349 {
350 return MatrixAccessor::build(m_matrix_mdspan.ptrAt(id.localId()));
351 }
352
353 //! accessor for the element (i,j) of the matrix for item \a id
354 constexpr ARCCORE_HOST_DEVICE MatrixElementAccessorReturnType operator()(ItemLocalIdType id, Int32 i, Int32 j) const
355 requires(Extents::rank() == 0)
356 {
357 return MatrixElementAccessor::build(&m_matrix_mdspan(id.localId())(i, j));
358 }
359 ///@}
360
361 //! \name Operations for variable of dimension MDDim1
362 //! Accessor of the matrix of index \a index for item \a id
363 constexpr ARCCORE_HOST_DEVICE MatrixAccessorReturnType operator()(ItemLocalIdType id, Int32 index) const
364 requires(Extents::rank() == 1)
365 {
366 return MatrixAccessor::build(m_matrix_mdspan.ptrAt(id.localId(), index));
367 }
368
369 //! Accessor for the element (i,j) of the matrix for item \a id and index \a index
370 constexpr ARCCORE_HOST_DEVICE MatrixElementAccessorReturnType operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j) const
371 requires(Extents::rank() == 1)
372 {
373 return MatrixElementAccessor::build(&m_matrix_mdspan(id.localId(), index)(i, j));
374 }
375 ///@}
376
377 private:
378
379 MDSpanType m_matrix_mdspan;
380};
381
382/*---------------------------------------------------------------------------*/
383/*---------------------------------------------------------------------------*/
384/*!
385 * \brief Read-only view of multi-dimensional matrix variable over a mesh item.
386 */
387template <typename ItemType_, typename DataType_, int Row, int Column, typename Extents>
388class MeshMatrixMDVariableInView
389: public MeshMatrixMDVariableViewBase<ItemType_, NumMatrixDataViewGetter<DataType_, Row, Column>, Extents>
390{
391 using BaseClass = MeshMatrixMDVariableViewBase<ItemType_, NumMatrixDataViewGetter<DataType_, Row, Column>, Extents>;
392
393 public:
394
395 using ItemType = ItemType_;
396 using DataType = DataType_;
397 using ItemLocalIdType = ItemType::LocalIdType;
398 using ConstReferenceType = NumMatrixDataViewGetter<DataType, Row, Column>;
400
401 private:
402
403 using MDSpanType = VariableRefType::MDSpanType;
404
405 public:
406
407 MeshMatrixMDVariableInView(const ViewBuildInfo& view_bi, const VariableRefType& var_ref)
408 : BaseClass(var_ref.m_matrix_mdspan)
409 {
410 IVariable* var = var_ref.underlyingVariable().variable();
411 VariableViewBase vvb(view_bi, var);
412 }
413};
414
415/*---------------------------------------------------------------------------*/
416/*---------------------------------------------------------------------------*/
417/*!
418 * \brief Read-write view of multi-dimensional matrix variable over a mesh item.
419 */
420template <typename ItemType_, typename DataType_, int Row, int Column, typename Extents>
421class MeshMatrixMDVariableInOutView
422: public MeshMatrixMDVariableViewBase<ItemType_, NumMatrixDataViewGetterSetter<DataType_, Row, Column>, Extents>
423{
424 using BaseClass = MeshMatrixMDVariableViewBase<ItemType_, NumMatrixDataViewGetterSetter<DataType_, Row, Column>, Extents>;
425
426 public:
427
428 using ItemType = ItemType_;
429 using DataType = DataType_;
430 using ItemLocalIdType = ItemType::LocalIdType;
431 using ConstReferenceType = NumMatrixDataViewGetter<DataType, Row, Column>;
433
434 private:
435
436 using MDSpanType = VariableRefType::MDSpanType;
437
438 public:
439
440 MeshMatrixMDVariableInOutView(const ViewBuildInfo& view_bi, const VariableRefType& var_ref)
441 : BaseClass(var_ref.m_matrix_mdspan)
442 {
443 IVariable* var = var_ref.underlyingVariable().variable();
444 VariableViewBase vvb(view_bi, var);
445 }
446};
447
448/*---------------------------------------------------------------------------*/
449/*---------------------------------------------------------------------------*/
450/*!
451 * \brief Read-only view of vector multi-dimensional mesh variable
452 */
453template <typename ItemType, typename DataType, int Size, typename Extents>
458
459/*---------------------------------------------------------------------------*/
460/*---------------------------------------------------------------------------*/
461/*!
462 * \brief Read-only view of matrix multi-dimensional mesh variable
463 */
464template <typename ItemType, typename DataType, int Row, int Column, typename Extents>
469
470/*---------------------------------------------------------------------------*/
471/*---------------------------------------------------------------------------*/
472/*!
473 * \brief Read-only view of multi-dimensional mesh variable
474 */
475template <typename ItemType, typename DataType, typename Extents>
480
481/*---------------------------------------------------------------------------*/
482/*---------------------------------------------------------------------------*/
483/*!
484 * \brief Read-write view of vector multi-dimensional mesh variable
485 */
486template <typename ItemType, typename DataType, int Size, typename Extents>
491
492/*---------------------------------------------------------------------------*/
493/*---------------------------------------------------------------------------*/
494/*!
495 * \brief Read-write view of matrix multi-dimensional mesh variable
496 */
497template <typename ItemType, typename DataType, int Row, int Column, typename Extents>
502
503/*---------------------------------------------------------------------------*/
504/*---------------------------------------------------------------------------*/
505/*!
506 * \brief Read-write view of matrix multi-dimensional mesh variable
507 */
508template <typename ItemType, typename DataType, typename Extents>
513
514/*---------------------------------------------------------------------------*/
515/*---------------------------------------------------------------------------*/
516
517} // End namespace Arcane::Accelerator
518
519/*---------------------------------------------------------------------------*/
520/*---------------------------------------------------------------------------*/
521
522#endif
Read-write view of multi-dimensional variable over a mesh item.
Read-only view of multi-dimensional variable over a mesh item.
Read-write view of multi-dimensional matrix variable over a mesh item.
Read-only view of multi-dimensional matrix variable over a mesh item.
constexpr __host__ __device__ MatrixElementAccessorReturnType operator()(ItemLocalIdType id, Int32 i, Int32 j) const
accessor for the element (i,j) of the matrix for item id
constexpr __host__ __device__ MatrixElementAccessorReturnType operator()(ItemLocalIdType id, Int32 index, Int32 i, Int32 j) const
Accessor for the element (i,j) of the matrix for item id and index index.
constexpr __host__ __device__ MatrixAccessorReturnType operator()(ItemLocalIdType id) const
Accessor of the matrix for item id.
Read-write view of multi-dimensional vector variable over a mesh item.
Read-only view of multi-dimensional vector variable over a mesh item.
constexpr __host__ __device__ VectorAccessorReturnType operator()(ItemLocalIdType id) const
Accessor of the vector for item id.
constexpr __host__ __device__ VectorElementAccessorReturnType operator()(ItemLocalIdType id, Int32 i) const
accessor for the element (i) of the vector for item id
constexpr __host__ __device__ VectorElementAccessorReturnType operator()(ItemLocalIdType id, Int32 index, Int32 i) const
Accessor for the element (i) of the vector for item id and index index.
Base class for variable views.
Interface of a variable.
Definition IVariable.h:40
Base class for multidimensional views.
constexpr DataType * ptrAt(ExtentIndexType i, ExtentIndexType j, ExtentIndexType k, ExtentIndexType l) const
Pointer to the value for element i,j,k,l.
const UnderlyingVariableType & underlyingVariable() const
Associated underlying variable.
Class managing a multi-dimensional variable on a mesh entity.
Class managing a multi-dimensional NumMatrix type variable on a mesh entity.
Class managing a multi-dimensional NumVector type variable on a mesh entity.
Read-only view for a NumMatrix<DataType_,Row,Column>.
Read-only view for a NumVector<DataType_, Size_>.
IVariable * variable() const
Associated variable.
Namespace for accelerator usage.
auto viewInOut(const ViewBuildInfo &vbi, CellMaterialVariableScalarRef< DataType > &var)
Read/write view for scalar material variables.
auto viewIn(const ViewBuildInfo &vbi, const CellMaterialVariableScalarRef< DataType > &var)
Read view for scalar material variables.
std::int32_t Int32
Signed integer type of 32 bits.