Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
VariableView.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/* VariableView.h (C) 2000-2023 */
9/* */
10/* Classes managing views on variables. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_VARIABLEVIEW_H
13#define ARCANE_VARIABLEVIEW_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
19#include "arcane/core/DataView.h"
20#include "arcane/core/ItemLocalId.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25/*!
26 * \file VariableView.h
27 *
28 * This file contains the type declarations to manage
29 * views on the mesh variables.
30 *
31 * The types and methods in this file are obsolete. The new version
32 * of views with accelerator support is in the file
33 * 'accelerator/Views.h'.
34 */
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39// TODO: Implement ReadWrite views for SIMD accessors
40
41namespace Arcane
42{
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
46
47// For compatibility with existing code
48template <typename DataType>
49using ViewSetter ARCANE_DEPRECATED_REASON("Use 'DataViewSetter' type instead") = DataViewSetter<DataType>;
50template <typename DataType>
51using ViewGetterSetter ARCANE_DEPRECATED_REASON("Use 'DataViewGetterSetter' type instead") = DataViewGetterSetter<DataType>;
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56/*!
57 * \brief Base class for views on variables.
58 */
59class VariableViewBase
60{
61 public:
62
63 // For now, it does not use \a var
64 // but it should not be deleted
65 VariableViewBase(IVariable*) {}
66};
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71/*!
72 * \brief Class to access a 1D array of a read/write view.
73 */
74template <typename DataType>
76{
77 public:
78
79 using ValueType = DataType;
80 using DataTypeReturnReference = Span<DataType>;
81};
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85
86/*!
87 * \brief Class to access a 1D array of a read/write view.
88 */
89template <typename DataType>
90class View1DSetter
91{
92 public:
93
94 using ValueType = DataType;
95 using DataTypeReturnReference = View1DSetter<DataType>;
96 View1DSetter(Span<DataType> data)
97 : m_data(data)
98 {}
99 DataViewSetter<DataType> operator[](Int64 index) const
100 {
101 return DataViewSetter<DataType>(m_data.ptrAt(index));
102 }
103
104 private:
105
106 Span<DataType> m_data;
107};
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112/*!
113 * \brief Write view on a scalar mesh variable.
114 */
115template <typename ItemType, typename Accessor>
116class ItemVariableScalarOutViewT
117: public VariableViewBase
118{
119 public:
120
121 using DataType = typename Accessor::ValueType;
122 using DataTypeReturnReference = DataType&;
123 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
124
125 public:
126
127 ItemVariableScalarOutViewT(IVariable* var, Span<DataType> v)
128 : VariableViewBase(var)
129 , m_values(v.data())
130 , m_size(v.size())
131 {}
132
133 //! Vector access operator with indirection.
135 {
136 return SimdSetter<DataType>(m_values, simd_item.simdLocalIds());
137 }
138
139 //! Vector access operator without indirection.
141 {
142 return SimdDirectSetter<DataType>(m_values + simd_item.baseLocalId());
143 }
144
145 //! Access operator for the \a item entity
146 Accessor operator[](ItemIndexType i) const
147 {
148 ARCANE_CHECK_AT(i.localId(), m_size);
149 return Accessor(this->m_values + i.localId());
150 }
151
152 //! Access operator for the \a item entity
153 Accessor value(ItemIndexType i) const
154 {
155 ARCANE_CHECK_AT(i.localId(), m_size);
156 return Accessor(this->m_values + i.localId());
157 }
158
159 //! Sets the value for the \a item entity at \a v
160 void setValue(ItemIndexType i, const DataType& v) const
161 {
162 ARCANE_CHECK_AT(i.localId(), m_size);
163 this->m_values[i.localId()] = v;
164 }
165
166 private:
167
168 DataType* m_values;
169 Int64 m_size;
170};
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175/*!
176 * \brief Read view on a scalar mesh variable.
177 */
178template <typename ItemType, typename DataType>
179class ItemVariableScalarInViewT
180: public VariableViewBase
181{
182 private:
183
184 typedef typename ItemTraitsT<ItemType>::LocalIdType ItemIndexType;
185
186 public:
187
188 ItemVariableScalarInViewT(IVariable* var, Span<const DataType> v)
189 : VariableViewBase(var)
190 , m_values(v)
191 {}
192
193 //! Vector access operator with indirection.
194 typename SimdTypeTraits<DataType>::SimdType
196 {
197 typedef typename SimdTypeTraits<DataType>::SimdType SimdType;
198 return SimdType(m_values.data(), simd_item.simdLocalIds());
199 }
200
201 //! Vector access operator with indirection.
202 typename SimdTypeTraits<DataType>::SimdType
204 {
205 typedef typename SimdTypeTraits<DataType>::SimdType SimdType;
206 return SimdType(m_values.data() + simd_item.baseLocalId());
207 }
208
209 //! Access operator for the \a item entity
210 const DataType& operator[](ItemIndexType i) const
211 {
212 return this->m_values[i.localId()];
213 }
214
215 //! Access operator for the \a item entity
216 const DataType& value(ItemIndexType i) const
217 {
218 return this->m_values[i.localId()];
219 }
220
221 private:
222
223 Span<const DataType> m_values;
224};
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
229/*!
230 * \brief Read view on an array mesh variable.
231 */
232template <typename ItemType, typename DataType>
233class ItemVariableArrayInViewT
234: public VariableViewBase
235{
236 private:
237
238 typedef typename ItemTraitsT<ItemType>::LocalIdType ItemIndexType;
239
240 public:
241
242 ItemVariableArrayInViewT(IVariable* var, Span2<const DataType> v)
243 : VariableViewBase(var)
244 , m_values(v)
245 {}
246
247 //! Access operator for the \a item entity
248 Span<const DataType> operator[](ItemIndexType i) const
249 {
250 return this->m_values[i.localId()];
251 }
252
253 //! Access operator for the \a item entity
254 Span<const DataType> value(ItemIndexType i) const
255 {
256 return this->m_values[i.localId()];
257 }
258
259 private:
260
261 Span2<const DataType> m_values;
262};
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267/*!
268 * \brief Write view on an array mesh variable.
269 */
270template <typename ItemType, typename Accessor>
271class ItemVariableArrayOutViewT
272: public VariableViewBase
273{
274 private:
275
276 using DataType = typename Accessor::ValueType;
277 using DataTypeReturnType = typename Accessor::DataTypeReturnReference;
278 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
279
280 public:
281
282 ItemVariableArrayOutViewT(IVariable* var, Span2<DataType> v)
283 : VariableViewBase(var)
284 , m_values(v)
285 {}
286
287 //! Access operator for the \a item entity
288 DataTypeReturnType operator[](ItemIndexType i) const
289 {
290 return DataTypeReturnType(this->m_values[i.localId()]);
291 }
292
293 //! Access operator for the \a item entity
294 Span<DataType> value(ItemIndexType i) const
295 {
296 return DataTypeReturnType(this->m_values[i.localId()]);
297 }
298
299 private:
300
301 Span2<DataType> m_values;
302};
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307/*!
308 * \brief Write view on a mesh scalar variable of type 'RealN'.
309
310 This class specializes modifiable views for the reals 'Real2', 'Real3',
311 'Real2x2' and 'Real3x3'. The specialization ensures that only the entire vector
312 of these real numbers can be modified. For example:
313
314 \code
315 VariableNodeReal3View force_view = ...;
316 Node node = ...;
317
318 // OK:
319 force_view[node] = Real3(x,y,z);
320
321 // Forbidden:
322 // force_view[node].x = ...;
323
324 \endcode
325*/
326template <typename ItemType, typename Accessor>
328: public VariableViewBase
329{
330 public:
331
332 using DataType = typename Accessor::ValueType;
333 using DataTypeReturnReference = DataType&;
334 using ItemIndexType = typename ItemTraitsT<ItemType>::LocalIdType;
335
336 public:
337
338 //! Constructs the view
340 : VariableViewBase(var)
341 , m_values(v.data())
342 , m_size(v.size())
343 {}
344
345 //! Vector access operator with indirection.
347 {
348 return SimdSetter<DataType>(m_values, simd_item.simdLocalIds());
349 }
350
351 //! Vector access operator without indirection.
353 {
354 return SimdDirectSetter<DataType>(m_values + simd_item.baseLocalId());
355 }
356
357 //! Access operator for the \a item entity
358 Accessor operator[](ItemIndexType item) const
359 {
360 ARCANE_CHECK_AT(item.localId(), m_size);
361 return Accessor(this->m_values + item.localId());
362 }
363
364 //! Access operator for the \a item entity
365 Accessor value(ItemIndexType item) const
366 {
367 ARCANE_CHECK_AT(item.localId(), m_size);
368 return Accessor(this->m_values + item.localId());
369 }
370
371 //! Positions the value for the \a item entity at \a v
372 void setValue(ItemIndexType item, const DataType& v) const
373 {
374 ARCANE_CHECK_AT(item.localId(), m_size);
375 this->m_values[item.localId()] = v;
376 }
377
378 private:
379
380 DataType* m_values;
381 Int64 m_size;
382};
383
384/*---------------------------------------------------------------------------*/
385/*---------------------------------------------------------------------------*/
386
387/*!
388 * \brief Write view.
389 */
390template <typename ItemType, typename DataType> auto
396
397/*!
398 * \brief Write view.
399 */
400template <typename ItemType> auto
406
407/*!
408 * \brief Write view.
409 */
410template <typename ItemType> auto
416
417/*!
418 * \brief Write view.
419 */
420template <typename ItemType, typename DataType> auto
426
427/*---------------------------------------------------------------------------*/
428/*---------------------------------------------------------------------------*/
429
430/*!
431 * \brief Read/write view.
432 */
433template <typename ItemType, typename DataType> auto
439
440/*!
441 * \brief Read/write view.
442 */
443template <typename ItemType> auto
449
450/*!
451 * \brief Read/write view.
452 */
453template <typename ItemType> auto
459
460/*!
461 * \brief Read/write view.
462 */
463template <typename ItemType, typename DataType> auto
469
470/*---------------------------------------------------------------------------*/
471/*---------------------------------------------------------------------------*/
472
473/*!
474 * \brief Read view.
475 */
476template <typename ItemType, typename DataType> auto
481
482/*!
483 * \brief Read view.
484 */
485template <typename ItemType, typename DataType> auto
490
491/*---------------------------------------------------------------------------*/
492/*---------------------------------------------------------------------------*/
493
494typedef ItemVariableScalarInViewT<Node, Byte> VariableNodeByteInView;
495typedef ItemVariableScalarInViewT<Edge, Byte> VariableEdgeByteInView;
496typedef ItemVariableScalarInViewT<Face, Byte> VariableFaceByteInView;
497typedef ItemVariableScalarInViewT<Cell, Byte> VariableCellByteInView;
498typedef ItemVariableScalarInViewT<Particle, Byte> VariableParticleByteInView;
499
500typedef ItemVariableScalarInViewT<Node, Int16> VariableNodeInt16InView;
501typedef ItemVariableScalarInViewT<Edge, Int16> VariableEdgeInt16InView;
502typedef ItemVariableScalarInViewT<Face, Int16> VariableFaceInt16InView;
503typedef ItemVariableScalarInViewT<Cell, Int16> VariableCellInt16InView;
504typedef ItemVariableScalarInViewT<Particle, Int16> VariableParticleInt16InView;
505
506typedef ItemVariableScalarInViewT<Node, Int32> VariableNodeInt32InView;
507typedef ItemVariableScalarInViewT<Edge, Int32> VariableEdgeInt32InView;
508typedef ItemVariableScalarInViewT<Face, Int32> VariableFaceInt32InView;
509typedef ItemVariableScalarInViewT<Cell, Int32> VariableCellInt32InView;
510typedef ItemVariableScalarInViewT<Particle, Int32> VariableParticleInt32InView;
511
512typedef ItemVariableScalarInViewT<Node, Int64> VariableNodeInt64InView;
513typedef ItemVariableScalarInViewT<Edge, Int64> VariableEdgeInt64InView;
514typedef ItemVariableScalarInViewT<Face, Int64> VariableFaceInt64InView;
515typedef ItemVariableScalarInViewT<Cell, Int64> VariableCellInt64InView;
516typedef ItemVariableScalarInViewT<Particle, Int64> VariableParticleInt64InView;
517
518typedef ItemVariableScalarInViewT<Node, Real> VariableNodeRealInView;
519typedef ItemVariableScalarInViewT<Edge, Real> VariableEdgeRealInView;
520typedef ItemVariableScalarInViewT<Face, Real> VariableFaceRealInView;
521typedef ItemVariableScalarInViewT<Cell, Real> VariableCellRealInView;
522typedef ItemVariableScalarInViewT<Particle, Real> VariableParticleRealInView;
523
524typedef ItemVariableScalarInViewT<Node, Real2> VariableNodeReal2InView;
525typedef ItemVariableScalarInViewT<Edge, Real2> VariableEdgeReal2InView;
526typedef ItemVariableScalarInViewT<Face, Real2> VariableFaceReal2InView;
527typedef ItemVariableScalarInViewT<Cell, Real2> VariableCellReal2InView;
528typedef ItemVariableScalarInViewT<Particle, Real2> VariableParticleReal2InView;
529
530typedef ItemVariableScalarInViewT<Node, Real3> VariableNodeReal3InView;
531typedef ItemVariableScalarInViewT<Edge, Real3> VariableEdgeReal3InView;
532typedef ItemVariableScalarInViewT<Face, Real3> VariableFaceReal3InView;
533typedef ItemVariableScalarInViewT<Cell, Real3> VariableCellReal3InView;
534typedef ItemVariableScalarInViewT<Particle, Real3> VariableParticleReal3InView;
535
536/*---------------------------------------------------------------------------*/
537/*---------------------------------------------------------------------------*/
538
539typedef ItemVariableScalarOutViewT<Node, DataViewSetter<Byte>> VariableNodeByteOutView;
540typedef ItemVariableScalarOutViewT<Edge, DataViewSetter<Byte>> VariableEdgeByteOutView;
541typedef ItemVariableScalarOutViewT<Face, DataViewSetter<Byte>> VariableFaceByteOutView;
542typedef ItemVariableScalarOutViewT<Cell, DataViewSetter<Byte>> VariableCellByteOutView;
543typedef ItemVariableScalarOutViewT<Particle, DataViewSetter<Byte>> VariableParticleByteOutView;
544
545typedef ItemVariableScalarOutViewT<Node, DataViewSetter<Int16>> VariableNodeInt16OutView;
546typedef ItemVariableScalarOutViewT<Edge, DataViewSetter<Int16>> VariableEdgeInt16OutView;
547typedef ItemVariableScalarOutViewT<Face, DataViewSetter<Int16>> VariableFaceInt16OutView;
548typedef ItemVariableScalarOutViewT<Cell, DataViewSetter<Int16>> VariableCellInt16OutView;
549typedef ItemVariableScalarOutViewT<Particle, DataViewSetter<Int16>> VariableParticleInt16OutView;
550
551typedef ItemVariableScalarOutViewT<Node, DataViewSetter<Int32>> VariableNodeInt32OutView;
552typedef ItemVariableScalarOutViewT<Edge, DataViewSetter<Int32>> VariableEdgeInt32OutView;
553typedef ItemVariableScalarOutViewT<Face, DataViewSetter<Int32>> VariableFaceInt32OutView;
554typedef ItemVariableScalarOutViewT<Cell, DataViewSetter<Int32>> VariableCellInt32OutView;
555typedef ItemVariableScalarOutViewT<Particle, DataViewSetter<Int32>> VariableParticleInt32OutView;
556
557typedef ItemVariableScalarOutViewT<Node, DataViewSetter<Int64>> VariableNodeInt64OutView;
558typedef ItemVariableScalarOutViewT<Edge, DataViewSetter<Int64>> VariableEdgeInt64OutView;
559typedef ItemVariableScalarOutViewT<Face, DataViewSetter<Int64>> VariableFaceInt64OutView;
560typedef ItemVariableScalarOutViewT<Cell, DataViewSetter<Int64>> VariableCellInt64OutView;
561typedef ItemVariableScalarOutViewT<Particle, DataViewSetter<Int64>> VariableParticleInt64OutView;
562
563typedef ItemVariableScalarOutViewT<Node, DataViewSetter<Real>> VariableNodeRealOutView;
564typedef ItemVariableScalarOutViewT<Edge, DataViewSetter<Real>> VariableEdgeRealOutView;
565typedef ItemVariableScalarOutViewT<Face, DataViewSetter<Real>> VariableFaceRealOutView;
566typedef ItemVariableScalarOutViewT<Cell, DataViewSetter<Real>> VariableCellRealOutView;
567typedef ItemVariableScalarOutViewT<Particle, DataViewSetter<Real>> VariableParticleRealOutView;
568
569typedef ItemVariableRealNScalarOutViewT<Node, DataViewSetter<Real2>> VariableNodeReal2OutView;
570typedef ItemVariableRealNScalarOutViewT<Edge, DataViewSetter<Real2>> VariableEdgeReal2OutView;
571typedef ItemVariableRealNScalarOutViewT<Face, DataViewSetter<Real2>> VariableFaceReal2OutView;
572typedef ItemVariableRealNScalarOutViewT<Cell, DataViewSetter<Real2>> VariableCellReal2OutView;
573typedef ItemVariableRealNScalarOutViewT<Particle, DataViewSetter<Real2>> VariableParticleReal2OutView;
574
575typedef ItemVariableRealNScalarOutViewT<Node, DataViewSetter<Real3>> VariableNodeReal3OutView;
576typedef ItemVariableRealNScalarOutViewT<Edge, DataViewSetter<Real3>> VariableEdgeReal3OutView;
577typedef ItemVariableRealNScalarOutViewT<Face, DataViewSetter<Real3>> VariableFaceReal3OutView;
578typedef ItemVariableRealNScalarOutViewT<Cell, DataViewSetter<Real3>> VariableCellReal3OutView;
579typedef ItemVariableRealNScalarOutViewT<Particle, DataViewSetter<Real3>> VariableParticleReal3OutView;
580
581/*---------------------------------------------------------------------------*/
582/*---------------------------------------------------------------------------*/
583
584} // End namespace Arcane
585
586/*---------------------------------------------------------------------------*/
587/*---------------------------------------------------------------------------*/
588
589#endif
Declarations of types on entities.
Class for accessing an element of a read/write view.
Class for accessing an element of a write view.
Interface of a variable.
Definition IVariable.h:40
Read view on an array mesh variable.
Span< const DataType > value(ItemIndexType i) const
Access operator for the item entity.
Span< const DataType > operator[](ItemIndexType i) const
Access operator for the item entity.
Write view on an array mesh variable.
Span< DataType > value(ItemIndexType i) const
Access operator for the item entity.
DataTypeReturnType operator[](ItemIndexType i) const
Access operator for the item entity.
Write view on a mesh scalar variable of type 'RealN'.
ItemVariableRealNScalarOutViewT(IVariable *var, Span< DataType > v)
Constructs the view.
void setValue(ItemIndexType item, const DataType &v) const
Positions the value for the item entity at v.
SimdSetter< DataType > operator[](SimdItemIndexT< ItemType > simd_item) const
Vector access operator with indirection.
Accessor operator[](ItemIndexType item) const
Access operator for the item entity.
SimdDirectSetter< DataType > operator[](SimdItemDirectIndexT< ItemType > simd_item) const
Vector access operator without indirection.
Accessor value(ItemIndexType item) const
Access operator for the item entity.
Read view on a scalar mesh variable.
SimdTypeTraits< DataType >::SimdType operator[](SimdItemIndexT< ItemType > simd_item) const
Vector access operator with indirection.
const DataType & value(ItemIndexType i) const
Access operator for the item entity.
const DataType & operator[](ItemIndexType i) const
Access operator for the item entity.
SimdTypeTraits< DataType >::SimdType operator[](SimdItemDirectIndexT< ItemType > simd_item) const
Vector access operator with indirection.
Write view on a scalar mesh variable.
void setValue(ItemIndexType i, const DataType &v) const
Sets the value for the item entity at v.
SimdDirectSetter< DataType > operator[](SimdItemDirectIndexT< ItemType > simd_item) const
Vector access operator without indirection.
Accessor operator[](ItemIndexType i) const
Access operator for the item entity.
Accessor value(ItemIndexType i) const
Access operator for the item entity.
SimdSetter< DataType > operator[](SimdItemIndexT< ItemType > simd_item) const
Vector access operator with indirection.
Array variable on a mesh entity type.
Scalar variable on a mesh entity type.
Object allowing positioning of values in a SIMD vector.
Definition SimdItem.h:400
Vector index without indirection for an entity type.
Definition SimdItem.h:214
Vector index with indirection for an entity type. TODO: store the indices in a vector register to be ...
Definition SimdItem.h:182
const SimdIndexType &ARCANE_RESTRICT simdLocalIds() const
List of local IDs of the instance entities.
Definition SimdItem.h:199
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:324
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
IVariable * variable() const
Associated variable.
Class to access a 1D array of a read/write view.
Class to access a 1D array of a read/write view.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
auto viewOut(MeshVariableScalarRefT< ItemType, DataType > &var)
Write view.
std::int64_t Int64
Signed integer type of 64 bits.
auto viewIn(const MeshVariableScalarRefT< ItemType, DataType > &var)
Read view.
auto viewInOut(MeshVariableScalarRefT< ItemType, DataType > &var)
Read/write view.