Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
DefaultAbstractFamily.cc
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#include "DefaultAbstractFamily.h"
20
21#include <algorithm>
22
23#include <alien/utils/Precomp.h>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Alien
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34DefaultAbstractFamily::DefaultAbstractFamily(const DefaultAbstractFamily& family)
35: m_parallel_mng(family.m_parallel_mng)
36, m_unique_ids(family.m_unique_ids)
37, m_owners(family.m_owners)
38{}
39
40/*---------------------------------------------------------------------------*/
41
42DefaultAbstractFamily::DefaultAbstractFamily(ConstArrayView<Int64> uniqueIds,
43 ConstArrayView<Integer> owners,
44 IMessagePassingMng* parallel_mng)
45: m_parallel_mng(parallel_mng)
46{
47 copy(m_unique_ids, uniqueIds);
48 copy(m_owners, owners);
49}
50
51/*---------------------------------------------------------------------------*/
52
53DefaultAbstractFamily::DefaultAbstractFamily(ConstArrayView<Int64> uniqueIds,
54 IMessagePassingMng* parallel_mng)
55: m_parallel_mng(parallel_mng)
56{
57 const Integer commSize = m_parallel_mng->commSize();
58 const Integer commRank = m_parallel_mng->commRank();
59 const Integer localSize = uniqueIds.size();
60 UniqueArray<Integer> sizes(commSize);
61 Arccore::MessagePassing::mpAllGather(
62 m_parallel_mng, ConstArrayView<Integer>(1, &localSize), sizes);
63 UniqueArray<Integer> starts(commSize + 1);
64 starts[0] = 0;
65 for (Integer i = 0; i < commSize; ++i)
66 starts[i + 1] = starts[i] + sizes[i];
67
68 UniqueArray<Int64> allUniqueIds;
69 Arccore::MessagePassing::mpAllGatherVariable(m_parallel_mng, uniqueIds, allUniqueIds);
70 m_unique_ids.reserve(allUniqueIds.size());
71 m_owners.reserve(allUniqueIds.size());
72
73 // remise en tête des uids locaux associé à commRank
74 addRange(m_unique_ids, subConstView(allUniqueIds, starts[commRank], sizes[commRank]));
75 addRange(m_owners, commRank, sizes[commRank]);
76 for (Integer iRank = 0; iRank < commSize; ++iRank) {
77 if (iRank != commRank) {
78 addRange(m_unique_ids, subConstView(allUniqueIds, starts[iRank], sizes[iRank]));
79 addRange(m_owners, iRank, sizes[iRank]);
80 }
81 }
82
83#ifndef NDEBUG
84 ALIEN_ASSERT((m_unique_ids.size() == allUniqueIds.size()), ("Inconsistant sizes"));
85 ALIEN_ASSERT((m_owners.size() == allUniqueIds.size()), ("Inconsistant sizes"));
86 for (Integer i = 0; i < localSize; ++i) {
87 ALIEN_ASSERT((m_unique_ids[i] == uniqueIds[i]), ("Bad local numbering"));
88 ALIEN_ASSERT((m_owners[i] == commRank), ("Bad local owner"));
89 }
90
91 // Check duplicated uids
92 std::sort(allUniqueIds.begin(), allUniqueIds.end());
93 for (Integer i = 1; i < allUniqueIds.size(); ++i)
94 ALIEN_ASSERT((allUniqueIds[i - 1] != allUniqueIds[i]), ("Duplicated uid"));
95#endif /* NDEBUG */
96}
97
98/*---------------------------------------------------------------------------*/
99
100void DefaultAbstractFamily::uniqueIdToLocalId(ArrayView<Int32> localIds,
101 ConstArrayView<Int64> uniqueIds) const
102{
103 for (Integer i = 0; i < uniqueIds.size(); ++i) {
104 Integer localId = -1;
105 for (Integer j = 0; j < m_unique_ids.size(); ++j)
106 if (uniqueIds[i] == m_unique_ids[j]) {
107 localId = j;
108 break;
109 }
110 if (localId == -1)
111 throw Alien::FatalErrorException(A_FUNCINFO, "UniqueId not found");
112 localIds[i] = localId;
113 }
114}
115
116/*---------------------------------------------------------------------------*/
117
119DefaultAbstractFamily::item(Int32 localId) const
120{
121 return IAbstractFamily::Item(m_unique_ids[localId], m_owners[localId]);
122}
123
124/*---------------------------------------------------------------------------*/
125
127DefaultAbstractFamily::owners(ConstArrayView<Int32> localIds) const
128{
129 const Integer size = localIds.size();
130 SharedArray<Integer> result(size);
131 for (Integer i = 0; i < size; ++i) {
132 result[i] = m_owners[localIds[i]];
133 }
134 return SafeConstArrayView<Integer>(result);
135}
136
137/*---------------------------------------------------------------------------*/
138
140DefaultAbstractFamily::uids(ConstArrayView<Int32> localIds) const
141{
142 const Integer size = localIds.size();
143 SharedArray<Int64> result(size);
144 for (Integer i = 0; i < size; ++i) {
145 result[i] = m_unique_ids[localIds[i]];
146 }
147 return SafeConstArrayView<Int64>(result);
148}
149
150/*---------------------------------------------------------------------------*/
151
154{
155 SharedArray<Int32> local_ids(m_unique_ids.size());
156 for (Integer i = 0; i < m_unique_ids.size(); ++i)
157 local_ids[i] = i;
158 return SafeConstArrayView<Int32>(local_ids);
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164} // namespace Alien
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
IAbstractFamily::Item item(Int32 localId) const override
Give back an Item from its local id.
SafeConstArrayView< Int32 > allLocalIds() const override
Local ids of this family members.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17