Alien  1.3.0
Developer 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
103 void init();
104
105 bool isPrepared() const { return m_state == Prepared; }
106
107 void setVerboseMode(bool verbose);
108
112 template <typename T>
113 void prepare(T&& t);
114
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;
132
145 ScalarIndexSet buildScalarIndexSet(const String& name,
146 ConstArrayView<Integer> localIds,
147 const IAbstractFamily& family,
148 Integer kind, eKeepAlive alive = DontClone);
149
158 ScalarIndexSet buildScalarIndexSet(const String& name, const IAbstractFamily& family,
159 Integer kind, eKeepAlive alive = DontClone);
160
172 VectorIndexSet buildVectorIndexSet(const String& name,
173 ConstArrayView<Integer> localIds,
174 const IAbstractFamily& family,
175 const UniqueArray<Integer>& kind,
176 eKeepAlive alive = DontClone);
187 VectorIndexSet buildVectorIndexSet(const String& name,
188 const IAbstractFamily& family,
189 const UniqueArray<Integer>& kind,
190 eKeepAlive alive = DontClone);
192
200 void removeIndex(const ScalarIndexSet& entry, ConstArrayView<Integer> localIds);
201
203 UniqueArray<Integer> getIndexes(const ScalarIndexSet& entry) const;
204
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
216 IMessagePassingMng* parallelMng() const { return m_parallel_mng; }
217
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;
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
268 ScalarIndexSetVector m_entries;
269
271 std::map<const IAbstractFamily*, std::shared_ptr<IAbstractFamily>> m_abstract_families;
272
274 std::map<Integer, UniqueArray<Integer>> m_entry_all_items;
275
277 std::map<Integer, UniqueArray<Integer>> m_entry_all_indices;
278
280 std::map<Integer, ConstArrayView<Integer>> m_entry_own_items;
281
283 std::map<Integer, ConstArrayView<Integer>> m_entry_own_indices;
284
286 std::map<Integer, const IAbstractFamily*> m_entry_families;
287
288 protected:
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
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.
std::map< Integer, UniqueArray< Integer > > m_entry_all_items
Local ids, sorted by owned then ghosts. By entry.
IMessagePassingMng * parallelMng() const
Parallel Manager used for the index computation.
std::map< Integer, ConstArrayView< Integer > > m_entry_own_indices
Unique ids, only for owned, by entry.
std::map< const IAbstractFamily *, std::shared_ptr< IAbstractFamily > > m_abstract_families
Abstract families and associated clones (if handled).
std::map< Integer, ConstArrayView< Integer > > m_entry_own_items
Local ids, only for owned, by entry.
std::map< Integer, UniqueArray< Integer > > m_entry_all_indices
Unique ids, sorted by owned then ghosts. By entry.
Integer m_local_owner
current owner.
ScalarIndexSetVector m_entries
Table des Entry connues localement.
std::map< Integer, const IAbstractFamily * > m_entry_families
Family, by entry.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17