Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ComponentSimd.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/* ComponentSimd.h (C) 2000-2026 */
9/* */
10/* Support for vectorization for materials and media. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_MATERIALS_COMPONENTSIMD_H
13#define ARCANE_MATERIALS_COMPONENTSIMD_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17/*!
18 * \file ComponentSimd.h
19 *
20 * This file contains the different types to manage
21 * vectorization on components (materials and media).
22 */
23
26
27#include "arcane/materials/MatItem.h"
28#include "arcane/materials/MatItemEnumerator.h"
29#include "arcane/materials/ComponentPartItemVectorView.h"
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34#ifdef __INTEL_COMPILER
35#define A_ALIGNED_64 __attribute__((align_value(64)))
36#else
37#define A_ALIGNED_64
38#endif
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane::Materials
44{
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49/*!
50 * \brief SIMD indexer on a component.
51 */
52class ARCANE_MATERIALS_EXPORT ARCANE_ALIGNAS(64) SimdMatVarIndex
53{
54 public:
55
56 typedef SimdEnumeratorBase::SimdIndexType SimdIndexType;
57
58 public:
59
60 SimdMatVarIndex(Int32 array_index, SimdIndexType value_index)
61 : m_value_index(value_index)
62 , m_array_index(array_index)
63 {
64 }
65 SimdMatVarIndex() {}
66
67 public:
68
69 //! Returns the index of the value array in the list of variables.
70 Int32 arrayIndex() const { return m_array_index; }
71
72 //! Returns the index in the value array
73 const SimdIndexType& valueIndex() const { return m_value_index; }
74
75 private:
76
77 SimdIndexType m_value_index;
78 Int32 m_array_index;
79};
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84/*!
85 * \brief SIMD enumerator on a sub-part (pure or partial) of a
86 * subset of the cells of a component (material or medium)
87 */
88class ARCANE_MATERIALS_EXPORT ComponentPartSimdCellEnumerator
89: public SimdEnumeratorBase
90{
91 protected:
92
93 ComponentPartSimdCellEnumerator(IMeshComponent* component, Int32 component_part_index,
94 Int32ConstArrayView item_indexes)
95 : SimdEnumeratorBase(item_indexes)
96 , m_component_part_index(component_part_index)
97 , m_component(component)
98 {
99 }
100
101 public:
102
103 static ComponentPartSimdCellEnumerator create(ComponentPartItemVectorView v)
104 {
105 return ComponentPartSimdCellEnumerator(v.component(), v.componentPartIndex(), v.valueIndexes());
106 }
107
108 public:
109
110 SimdMatVarIndex _varIndex() const { return SimdMatVarIndex(m_component_part_index, *_currentSimdIndex()); }
111
112 operator SimdMatVarIndex() const
113 {
114 return _varIndex();
115 }
116
117 protected:
118
119 Integer m_component_part_index;
120 IMeshComponent* m_component;
121};
122
125{
126 return ComponentPartSimdCellEnumerator::create(v);
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
132#define ENUMERATE_SIMD_COMPONENTCELL(iname, env) \
133 A_ENUMERATE_COMPONENTCELL(ComponentPartSimdCell, iname, env)
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138template <typename Lambda> void
139simple_simd_env_loop(ComponentPartItemVectorView pure_items,
140 ComponentPartItemVectorView impure_items,
141 const Lambda& lambda)
142{
144 lambda(mvi);
145 }
146 ENUMERATE_SIMD_COMPONENTCELL(mvi, impure_items)
147 {
148 lambda(mvi);
149 }
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155class ARCANE_MATERIALS_EXPORT LoopFunctorEnvPartSimdCell
156{
157 public:
158
159 typedef const SimdMatVarIndex& IterType;
160
161 public:
162
163 LoopFunctorEnvPartSimdCell(ComponentPartItemVectorView pure_items,
164 ComponentPartItemVectorView impure_items)
165 : m_pure_items(pure_items)
166 , m_impure_items(impure_items)
167 {}
168
169 public:
170
171 static LoopFunctorEnvPartSimdCell create(const EnvCellVector& env);
172 static LoopFunctorEnvPartSimdCell create(IMeshEnvironment* env);
173
174 public:
175
176 template <typename Lambda>
177 void operator<<(Lambda&& lambda)
178 {
179 simple_simd_env_loop(m_pure_items, m_impure_items, lambda);
180 }
181
182 private:
183
184 ComponentPartItemVectorView m_pure_items;
185 ComponentPartItemVectorView m_impure_items;
186};
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190/*!
191 * \brief Macro to iterate over the entities of a component via a
192 * C++11 lambda function.
193 *
194 * The arguments are the same as for the ENUMERATE_COMPONENTITEM() macro.
195 *
196 * The code after the macro corresponds to the body of the C++11 lambda function.
197 * It must therefore be enclosed between two curly braces '{' '}' and end with a
198 * semicolon ';'. For example:
199 *
200 \code
201 * ENUMERATE_COMPONENTITEM_LAMBDA(){
202 * };
203 \endcode
204 *
205 * \note Even if the code is similar to that of a loop, it is a
206 * C++11 lambda function and therefore it is not possible to use keywords like
207 * 'break' or 'continue'. If you want to stop an iteration
208 * you must use the keyword 'return'.
209 */
210#define ENUMERATE_COMPONENTITEM_LAMBDA(iter_type, iter, container) \
211 Arcane::Materials::LoopFunctor##iter_type ::create((container)) << [=](Arcane::Materials::LoopFunctor##iter_type ::IterType iter)
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216/*!
217 * \brief Base class for variable views.
218 */
219class MatVariableViewBase
220{
221 public:
222
223 MatVariableViewBase(IMeshMaterialVariable* var)
224 : m_variable(var)
225 {
226 }
227
228 public:
229
230 IMeshMaterialVariable* variable() const { return m_variable; }
231
232 private:
233
234 IMeshMaterialVariable* m_variable;
235};
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240/*!
241 * \brief Read view on a scalar mesh variable.
242 */
243template <typename ItemType, typename DataType>
244class MatItemVariableScalarInViewT
245: public MatVariableViewBase
246{
247 private:
248
249 typedef MatVarIndex ItemIndexType;
250 typedef A_ALIGNED_64 DataType* DataTypeAlignedPtr;
251
252 public:
253
254 MatItemVariableScalarInViewT(IMeshMaterialVariable* var, ArrayView<DataType>* v)
255 : MatVariableViewBase(var)
256 , m_value(v)
257 , m_value0(v[0].unguardedBasePointer())
258 {}
259
260 //! Vector access operator with indirection.
261 typename SimdTypeTraits<DataType>::SimdType
262 operator[](const SimdMatVarIndex& mvi) const
263 {
264 typedef typename SimdTypeTraits<DataType>::SimdType SimdType;
265 return SimdType(m_value[mvi.arrayIndex()].data(), mvi.valueIndex());
266 }
267
268 //! Access operator for the \a item entity
269 DataType operator[](ItemIndexType mvi) const
270 {
271 return this->m_value[mvi.arrayIndex()][mvi.valueIndex()];
272 }
273
274 //! Access operator for the \a item entity
275 DataType operator[](ComponentItemLocalId lid) const
276 {
277 return this->m_value[lid.localId().arrayIndex()][lid.localId().valueIndex()];
278 }
279
280 //! Access operator for the \a item entity
281 DataType operator[](PureMatVarIndex pmvi) const
282 {
283 return this->m_value0[pmvi.valueIndex()];
284 }
285
286 //! Access operator for the \a item entity
287 DataType value(ItemIndexType mvi) const
288 {
289 return this->m_value[mvi.arrayIndex()][mvi.valueIndex()];
290 }
291
292 DataType value0(PureMatVarIndex idx) const
293 {
294 return this->m_value0[idx.valueIndex()];
295 }
296
297 //! Partial value of the variable for the \a mc iterator
299 {
300 return this->operator[](mc._varIndex());
301 }
302
303 //! Partial value of the variable for the \a mc iterator
304 DataType operator[](EnvCellEnumerator mc) const
305 {
306 return this->operator[](mc._varIndex());
307 }
308
309 private:
310
311 ArrayView<DataType>* m_value;
312 DataTypeAlignedPtr m_value0;
313};
314
315/*---------------------------------------------------------------------------*/
316/*---------------------------------------------------------------------------*/
317
318/*!
319 * \brief Read view on a scalar mesh variable.
320 */
321template <typename ItemType, typename DataType>
322class MatItemVariableScalarOutViewT
323: public MatVariableViewBase
324{
325 private:
326
327 typedef MatVarIndex ItemIndexType;
328 typedef A_ALIGNED_64 DataType* DataTypeAlignedPtr;
329
330 public:
331
332 MatItemVariableScalarOutViewT(IMeshMaterialVariable* var, ArrayView<DataType>* v)
333 : MatVariableViewBase(var)
334 , m_value(v)
335 , m_value0(v[0].unguardedBasePointer())
336 {}
337
338 //! Vector access operator with indirection.
339 SimdSetter<DataType> operator[](const SimdMatVarIndex& mvi) const
340 {
341 return SimdSetter<DataType>(m_value[mvi.arrayIndex()].data(), mvi.valueIndex());
342 }
343
344 //! Access operator for the \a item entity
345 DataType& operator[](ItemIndexType mvi) const
346 {
347 return this->m_value[mvi.arrayIndex()][mvi.valueIndex()];
348 }
349
350 //! Access operator for the \a item entity
351 DataType& operator[](ComponentItemLocalId lid) const
352 {
353 return this->m_value[lid.localId().arrayIndex()][lid.localId().valueIndex()];
354 }
355
356 DataType& operator[](PureMatVarIndex pmvi) const
357 {
358 return this->m_value0[pmvi.valueIndex()];
359 }
360
361 //! Access operator for the \a item entity
362 DataType& value(ItemIndexType mvi) const
363 {
364 return this->m_value[mvi.arrayIndex()][mvi.valueIndex()];
365 }
366
367 DataType& value0(PureMatVarIndex idx) const
368 {
369 return this->m_value0[idx.valueIndex()];
370 }
371
372 //! Partial value of the variable for the \a mc iterator
374 {
375 return this->operator[](mc._varIndex());
376 }
377
378 //! Partial value of the variable for the \a mc iterator
379 DataType& operator[](EnvCellEnumerator mc) const
380 {
381 return this->operator[](mc._varIndex());
382 }
383
384 private:
385
386 ArrayView<DataType>* m_value;
387 DataTypeAlignedPtr m_value0;
388};
389
390/*---------------------------------------------------------------------------*/
391/*---------------------------------------------------------------------------*/
392
393/*!
394 * \brief Read view.
395 */
396template <typename DataType>
402
403/*---------------------------------------------------------------------------*/
404/*---------------------------------------------------------------------------*/
405
406/*!
407 * \brief Write view
408 */
409template <typename DataType>
415
416/*---------------------------------------------------------------------------*/
417/*---------------------------------------------------------------------------*/
418
419} // namespace Arcane::Materials
420
421/*---------------------------------------------------------------------------*/
422/*---------------------------------------------------------------------------*/
423
424#endif
#define ARCANE_ALIGNAS(value)
Macro to guarantee the alignment of a class to value bytes.
Declarations of Arcane's general types.
Modifiable view of an array of type T.
Enumerator over the constituents of a cell.
Int32ConstArrayView valueIndexes() const
List of valueIndex() of the part.
SIMD enumerator on a sub-part (pure or partial) of a subset of the cells of a component (material or ...
constexpr __host__ __device__ MatVarIndex localId() const
Generic index to access variable values.
Enumerator over the cells of an environment.
Vector over the entities of an environment.
Interface of a component (material or environment) of a mesh.
Interface of a material variable on a mesh.
Read view on a scalar mesh variable.
SimdTypeTraits< DataType >::SimdType operator[](const SimdMatVarIndex &mvi) const
Vector access operator with indirection.
DataType operator[](PureMatVarIndex pmvi) const
Access operator for the item entity.
DataType operator[](ItemIndexType mvi) const
Access operator for the item entity.
DataType operator[](CellComponentCellEnumerator mc) const
Partial value of the variable for the mc iterator.
DataType operator[](ComponentItemLocalId lid) const
Access operator for the item entity.
DataType operator[](EnvCellEnumerator mc) const
Partial value of the variable for the mc iterator.
DataType value(ItemIndexType mvi) const
Access operator for the item entity.
Read view on a scalar mesh variable.
SimdSetter< DataType > operator[](const SimdMatVarIndex &mvi) const
Vector access operator with indirection.
DataType & value(ItemIndexType mvi) const
Access operator for the item entity.
DataType & operator[](ItemIndexType mvi) const
Access operator for the item entity.
DataType & operator[](ComponentItemLocalId lid) const
Access operator for the item entity.
DataType & operator[](CellComponentCellEnumerator mc) const
Partial value of the variable for the mc iterator.
DataType & operator[](EnvCellEnumerator mc) const
Partial value of the variable for the mc iterator.
Represents an index on material and environment variables.
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.
IMeshMaterialVariable * materialVariable() const
Associated material variable.
Index of a pure material item in a variable.
#define ENUMERATE_COMPONENTITEM(ClassName_, iname, container,...)
Generic macro to iterate over entities of a material or an environment.
Always enables tracing in Arcane parts concerning materials.
ConstituentItemIndexedSelectionEnumerator< ComponentCellVectorView > arcaneImplCreateConstituentEnumerator(ComponentCell, ComponentCellVectorSelectionView container)
Enumerator over a constituent selection.
MatItemVariableScalarOutViewT< Cell, DataType > viewOut(CellMaterialVariableScalarRef< DataType > &var)
Write view.
MatItemVariableScalarInViewT< Cell, DataType > viewIn(const CellMaterialVariableScalarRef< DataType > &var)
Read view.
class ARCANE_MATERIALS_EXPORT(64) SimdMatVarIndex
SIMD indexer on a component.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
std::int32_t Int32
Signed integer type of 32 bits.