Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
IndexManager.h
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#pragma once
20
21#include <algorithm>
22#include <map>
23#include <vector>
24
25#include <alien/index_manager/IAbstractFamily.h>
26#include <alien/index_manager/ScalarIndexSet.h>
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Alien
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37/* TODO: Optimize by vectorizing access for internal structures (getIndex) */
38class ALIEN_EXPORT IndexManager
39{
40 public:
41 typedef UniqueArray<ScalarIndexSet> VectorIndexSet;
42
43 typedef std::vector<ScalarIndexSet*> ScalarIndexSetVector;
44
45 enum eKeepAlive
46 {
47 Clone,
48 DontClone
49 };
50
51 public:
52 class Iterator
53 {
54 public:
55 explicit Iterator(const ScalarIndexSetVector::iterator& it)
56 : m_iterator(it)
57 {}
58 void operator++() { m_iterator++; }
59 bool operator!=(const Iterator& it) { return m_iterator != it.m_iterator; }
60 ScalarIndexSet& operator*() { return **m_iterator; }
61
62 private:
63 ScalarIndexSetVector::iterator m_iterator;
64 };
65
66 class ConstIterator
67 {
68 public:
69 explicit ConstIterator(const ScalarIndexSetVector::const_iterator& it)
70 : m_iterator(it)
71 {}
72 void operator++() { m_iterator++; }
73 bool operator!=(const ConstIterator& it) { return m_iterator != it.m_iterator; }
74 const ScalarIndexSet& operator*() { return **m_iterator; }
75
76 private:
77 ScalarIndexSetVector::const_iterator m_iterator;
78 };
79
80 public:
82 {
83 Integer m_entry_uid;
84 Integer m_entry_kind;
85 Int64 m_item_uid;
86 Integer m_item_localid;
87 Integer m_item_index;
88 Integer m_item_owner;
89
90 bool operator==(const InternalEntryIndex& m) const
91 {
92 return m_entry_uid == m.m_entry_uid && m.m_item_localid == m_item_localid;
93 }
94 };
95
96 public:
97 explicit IndexManager(
98 Alien::IMessagePassingMng* parallelMng, Alien::ITraceMng* traceMng = nullptr);
99
100 virtual ~IndexManager();
101
102 //! Initialisation
103 void init();
104
105 bool isPrepared() const { return m_state == Prepared; }
106
107 void setVerboseMode(bool verbose);
108
109 /*! Prepare
110 * Compute all indices, using the specified sorting algorithm.
111 */
112 template <typename T>
113 void prepare(T&& t);
114
115 /*! Prepare
116 * Compute all indices, using default algorithm.
117 */
118 void prepare();
119
120 /* @defgroup stats Index characteristics.
121 * Only valid after call to prepare.
122 * @{
123 */
124 void stats(Integer& globalSize, Integer& minLocalIndex, Integer& localSize) const;
125
126 Integer globalSize() const;
127
128 Integer minLocalIndex() const;
129
130 Integer localSize() const;
131 /*! }@ */
132
133 /*! @defgroup new Define new entries.
134 * @{
135 */
136 /*! New scalar entry, defined on a set of abstract items.
137 *
138 * @param name
139 * @param localIds
140 * @param family
141 * @param kind
142 * @param alive
143 * @return
144 */
145 ScalarIndexSet buildScalarIndexSet(const String& name,
146 ConstArrayView<Integer> localIds,
147 const IAbstractFamily& family,
148 Integer kind, eKeepAlive alive = DontClone);
149
150 /*! New scalar entry, on elements of a family.
151 *
152 * @param name
153 * @param family
154 * @param kind
155 * @param alive
156 * @return
157 */
158 ScalarIndexSet buildScalarIndexSet(const String& name, const IAbstractFamily& family,
159 Integer kind, eKeepAlive alive = DontClone);
160
161 /*! New vector entry, on a set of abstract items.
162 *
163 * @param name
164 * @param localIds
165 * @param family
166 * @param kind
167 * @param alive
168 * @return
169 *
170 * Current implementation handles multi-scalar entries as vector.
171 */
172 VectorIndexSet buildVectorIndexSet(const String& name,
173 ConstArrayView<Integer> localIds,
174 const IAbstractFamily& family,
175 const UniqueArray<Integer>& kind,
176 eKeepAlive alive = DontClone);
177 /*! New vector entry, on elements of a family.
178 *
179 * @param name
180 * @param family
181 * @param kind
182 * @param alive
183 * @return
184 *
185 * Current implementation handles multi-scalar entries as vector.
186 */
187 VectorIndexSet buildVectorIndexSet(const String& name,
188 const IAbstractFamily& family,
189 const UniqueArray<Integer>& kind,
190 eKeepAlive alive = DontClone);
191 /*! }@ */
192
193 /*! Remove a entities from the index.
194 *
195 * @param entry
196 * @param localIds
197 *
198 * Must be called before prepare.
199 */
200 void removeIndex(const ScalarIndexSet& entry, ConstArrayView<Integer> localIds);
201
202 //! Give a translation table, indexed by items.
203 UniqueArray<Integer> getIndexes(const ScalarIndexSet& entry) const;
204
205 //! Give a vector translation table, indexed by items then by entries.
206 UniqueArray2<Integer> getIndexes(const VectorIndexSet& entries) const;
207
208 ConstArrayView<Integer> getOwnIndexes(const ScalarIndexSet& entry) const;
209 ConstArrayView<Integer> getOwnLocalIds(const ScalarIndexSet& entry) const;
210 ConstArrayView<Integer> getAllIndexes(const ScalarIndexSet& entry) const;
211 ConstArrayView<Integer> getAllLocalIds(const ScalarIndexSet& entry) const;
212
213 const IAbstractFamily& getFamily(const ScalarIndexSet& entry) const;
214
215 //! Parallel Manager used for the index computation.
216 IMessagePassingMng* parallelMng() const { return m_parallel_mng; }
217
218 //! define null index : default = -1, if true null_index = max_index+1
219 void setMaxNullIndexOpt(bool flag);
220
221 Integer nullIndex() const;
222
223 Iterator begin() { return Iterator(m_entries.begin()); }
224 Iterator end() { return Iterator(m_entries.end()); }
225 ConstIterator begin() const { return ConstIterator(m_entries.begin()); }
226 ConstIterator end() const { return ConstIterator(m_entries.end()); }
227
228 private:
229 ScalarIndexSet buildEntry(
230 const String& name, const IAbstractFamily* itemFamily, Integer kind);
231
232 void defineIndex(const ScalarIndexSet& entry, ConstArrayView<Integer> localIds);
233
234 typedef std::vector<InternalEntryIndex> EntryIndexMap;
235
236 void begin_prepare(EntryIndexMap& entry_index);
237 void begin_parallel_prepare(EntryIndexMap& entry_index);
238 void end_parallel_prepare(EntryIndexMap& entry_index);
239 void sequential_prepare(EntryIndexMap& entry_index);
240 void end_prepare(EntryIndexMap& entryIndex);
241
242 const IAbstractFamily* addNewAbstractFamily(
243 const IAbstractFamily* family, eKeepAlive alive);
244
245 private:
246 Alien::IMessagePassingMng* m_parallel_mng = nullptr;
247 Alien::ITraceMng* m_trace_mng = nullptr;
248 Integer m_local_owner; //!< current owner.
249
250 enum State
251 {
252 Undef,
253 Initialized,
254 Prepared
255 } m_state;
256
257 bool m_verbose;
258
259 Integer m_local_entry_count;
260 Integer m_global_entry_count;
261 Integer m_global_entry_offset;
262 Integer m_local_removed_entry_count;
263 Integer m_global_removed_entry_count;
264
265 bool m_max_null_index_opt;
266
267 //! Table des Entry connues localement
268 ScalarIndexSetVector m_entries;
269
270 //! Abstract families and associated clones (if handled)
271 std::map<const IAbstractFamily*, std::shared_ptr<IAbstractFamily>> m_abstract_families;
272
273 //! Local ids, sorted by owned then ghosts. By entry.
274 std::map<Integer, UniqueArray<Integer>> m_entry_all_items;
275
276 //! Unique ids, sorted by owned then ghosts. By entry.
277 std::map<Integer, UniqueArray<Integer>> m_entry_all_indices;
278
279 //! Local ids, only for owned, by entry.
280 std::map<Integer, ConstArrayView<Integer>> m_entry_own_items;
281
282 //! Unique ids, only for owned, by entry.
283 std::map<Integer, ConstArrayView<Integer>> m_entry_own_indices;
284
285 //! Family, by entry.
286 std::map<Integer, const IAbstractFamily*> m_entry_families;
287
288 protected:
289 //! \internal Structure interne temporaire dans prepare(), defineEntry() et
290 //! removeIndex()
291 struct EntryLocalId;
292 std::map<Integer, std::shared_ptr<EntryLocalId>> m_entry_local_ids;
293
294 struct ParallelRequests;
295 std::shared_ptr<ParallelRequests> parallel;
296
297 //! \internal Structure interne de communication dans prepare()
298 struct EntrySendRequest;
299 struct EntryRecvRequest;
300};
301
302/*---------------------------------------------------------------------------*/
303/*---------------------------------------------------------------------------*/
304
305template <typename T>
307{
308 EntryIndexMap entry_index;
309
310 begin_prepare(entry_index);
311
312 if (m_parallel_mng->commSize() > 1) {
313 begin_parallel_prepare(entry_index);
314
315 std::sort(entry_index.begin(), entry_index.end(), t);
316
317 end_parallel_prepare(entry_index);
318 }
319 else {
320 std::sort(entry_index.begin(), entry_index.end(), t);
321
322 sequential_prepare(entry_index);
323 }
324
325 end_prepare(entry_index);
326}
327
328/*---------------------------------------------------------------------------*/
329/*---------------------------------------------------------------------------*/
330} // namespace Alien
331
332/*---------------------------------------------------------------------------*/
333/*---------------------------------------------------------------------------*/
Interface for abstract families of items.
void init()
Initialisation.
IMessagePassingMng * parallelMng() const
Parallel Manager used for the index computation.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17