Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ItemFunctor.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/* ItemFunctor.h (C) 2000-2024 */
9/* */
10/* Functor on entities. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_ITEMFUNCTOR_H
13#define ARCANE_ITEMFUNCTOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/RangeFunctor.h"
18#include "arcane/utils/Functor.h"
19
20#include "arcane/core/Item.h"
21#include "arcane/core/ItemVectorView.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*!
33 * \brief Base class for functors on a list of entities.
34 *
35 * This class allows splitting an iteration over an ItemVector by
36 * ensuring that iterations occur on a multiple of \a m_block_size.
37 * For now, this value is always 8, and thus iterations over
38 * entities are done in blocks of 8 values. This allows guaranteeing for
39 * vectorization that the sub-views of \a m_items will be correctly aligned.
40 */
41class ARCANE_CORE_EXPORT AbstractItemRangeFunctor
42: public IRangeFunctor
43{
44 public:
45
46 static const Integer DEFAULT_GRAIN_SIZE = 400;
47
48 AbstractItemRangeFunctor(ItemVectorView items_view, Int32 grain_size);
49
50 public:
51
52 //! Number of blocks.
53 Int32 nbBlock() const { return m_nb_block; }
54
55 //! Desired size of an iteration interval.
56 Int32 blockGrainSize() const { return m_block_grain_size; }
57
58 protected:
59
60 ItemVectorView m_items;
61 Int32 m_block_size = 0;
62 Int32 m_nb_block = 0;
63 Int32 m_block_grain_size = 0;
64
65 protected:
66
67 ItemVectorView _view(Int32 begin_block, Int32 nb_block, Int32* true_begin = nullptr) const;
68
69 private:
70};
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75/*!
76 * \brief Functor for iterating over a list of entities.
77 */
78template <typename InstanceType, typename ItemType>
79class ItemRangeFunctorT
80: public AbstractItemRangeFunctor
81{
82 private:
83
84 typedef void (InstanceType::*FunctionType)(ItemVectorViewT<ItemType>);
85
86 public:
87
88 ItemRangeFunctorT(ItemVectorView items_view, InstanceType* instance,
89 FunctionType function, Integer grain_size = DEFAULT_GRAIN_SIZE)
90 : AbstractItemRangeFunctor(items_view, grain_size)
91 , m_instance(instance)
92 , m_function(function)
93 {
94 }
95
96 private:
97
98 InstanceType* m_instance;
99 FunctionType m_function;
100
101 public:
102
103 virtual void executeFunctor(Int32 begin, Int32 size)
104 {
105 //cout << "** BLOCKED RANGE! range=" << range.begin() << " end=" << range.end() << " size=" << range.size() << "\n";
106 //CellVectorView sub_view = m_cells.subView(range.begin(),range.size());
107 ItemVectorViewT<ItemType> sub_view(this->_view(begin, size));
108 //cout << "** SUB_VIEW v=" << sub_view.size();
109 (m_instance->*m_function)(sub_view);
110 }
111};
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116/*!
117 * \brief Functor on an iteration interval instantiated via a lambda function.
118 *
119 * This class is used with the C++1x lambda function mechanism.
120 */
121template <typename LambdaType>
122class LambdaItemRangeFunctorT
123: public AbstractItemRangeFunctor
124{
125 public:
126
127 LambdaItemRangeFunctorT(ItemVectorView items_view, const LambdaType& lambda_function,
128 Int32 grain_size = DEFAULT_GRAIN_SIZE)
129 : AbstractItemRangeFunctor(items_view, grain_size)
130 , m_lambda_function(lambda_function)
131 {
132 }
133
134 public:
135
136 void executeFunctor(Int32 begin, Int32 size) override
137 {
138 Int32 true_begin = 0;
139 ItemVectorView sub_view(this->_view(begin, size, &true_begin));
140 // The lambda can have two prototypes:
141 // - it takes only an ItemVectorView as argument (historical version)
142 // - it takes an ItemVectorView and the starting index of the vector. This
143 // allows knowing the iteration index
144 if constexpr (std::is_invocable_v<LambdaType, ItemVectorView>)
145 m_lambda_function(sub_view);
146 else
147 m_lambda_function(sub_view, true_begin);
148 }
149
150 private:
151
152 const LambdaType& m_lambda_function;
153};
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158/*!
159 * \brief Functor for calculating elements of a group.
160 */
161class ItemGroupComputeFunctor
162: public IFunctor
163{
164 public:
165
166 ItemGroupComputeFunctor() = default;
167
168 public:
169
170 void setGroup(ItemGroupImpl* group) { m_group = group; }
171
172 protected:
173
174 ItemGroupImpl* m_group = nullptr;
175};
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180} // namespace Arcane
181
182/*---------------------------------------------------------------------------*/
183/*---------------------------------------------------------------------------*/
184
185#endif
Int32 nbBlock() const
Number of blocks.
Definition ItemFunctor.h:53
Int32 blockGrainSize() const
Desired size of an iteration interval.
Definition ItemFunctor.h:56
Interface of a functor on an iteration interval.
virtual void executeFunctor(Int32 begin, Int32 size)
Executes the associated method.
View on a typed array of entities.
View on a vector of entities.
void executeFunctor(Int32 begin, Int32 size) override
Executes the associated method.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
std::int32_t Int32
Signed integer type of 32 bits.