Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
RedistributorCommPlan.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 "RedistributorCommPlan.h"
20
21#include <arccore/message_passing/Messages.h>
22
23#include <alien/utils/Precomp.h>
24
25namespace Alien
26{
27
28using namespace Arccore;
29using namespace Arccore::MessagePassing;
30
31RedistributorCommPlan::RedistributorCommPlan(
32int globalSize, IMessagePassingMng* super_pm, IMessagePassingMng* target_pm)
33: m_super_pm(super_pm)
34, m_tgt_pm(target_pm)
35, m_proc_num(super_pm->commSize() + 1, -1) // Why + 1 ?
36, m_tgt_dist(super_pm->commSize() + 1, -1)
37{
38 m_tgt_distribution.reset(new VectorDistribution(globalSize, m_tgt_pm));
39 Int32 tgt_rank = -1;
40 if (m_tgt_pm) {
41 tgt_rank = m_tgt_pm->commRank();
42 m_proc_num.resize(m_tgt_pm->commSize());
43 }
44
45 // TODO avoid communication if m_pm_dst is m_pm_super
46 UniqueArray<Int32> reverse_proc_num(m_super_pm->commSize());
47 Arccore::MessagePassing::mpAllGather(
48 m_super_pm, ConstArrayView<Int32>(1, &tgt_rank), reverse_proc_num);
49
50 for (Int32 i = 0; i < (Int32)reverse_proc_num.size(); ++i) {
51 Int32 rank = reverse_proc_num[i];
52 if (rank < 0)
53 continue;
54 m_proc_num[rank] = i;
55 }
56 this->_buildTgtDist();
57}
58
59RedistributorCommPlan::~RedistributorCommPlan() {}
60
61std::shared_ptr<IMessagePassingMng>
62RedistributorCommPlan::tgtParallelMng() const
63{
64 return m_tgt_distribution->sharedParallelMng();
65}
66
67IMessagePassingMng*
68RedistributorCommPlan::superParallelMng() const
69{
70 return m_super_pm;
71}
72
73const VectorDistribution&
74RedistributorCommPlan::distribution() const
75{
76 return *m_tgt_distribution;
77}
78
79ConstArrayView<Int32>
80RedistributorCommPlan::tgtDist() const
81{
82 return m_tgt_dist.view();
83}
84
85Int32 RedistributorCommPlan::procNum(Int32 proc) const
86{
87 return m_proc_num[proc];
88}
89
90void RedistributorCommPlan::_buildTgtDist()
91{
92 Int32 super_comm_size = m_super_pm->commSize();
93 Int32 super_rank = 0;
94 // Not all procs know the target communicator.
95 // Thus we need to communicate the distribution to these processors.
96 if (m_tgt_pm) {
97 Int32 dst_comm_size = m_tgt_pm->commSize();
98 for (Int32 p = 0; p < dst_comm_size; p++) {
99 m_tgt_dist[m_proc_num[p]] = m_tgt_distribution->offset(p);
100 }
101 m_tgt_dist[super_comm_size] = m_tgt_distribution->globalSize();
102
103 // Fill other processes.
104 if (m_tgt_dist[0] < 0)
105 m_tgt_dist[0] = 0;
106 for (Int32 p = super_comm_size; p > 0; --p) {
107 if (m_tgt_dist[p] < 0) {
108 m_tgt_dist[p] = m_tgt_dist[p + 1];
109 }
110 }
111 super_rank = m_super_pm->commRank();
112 }
113
114 // This communication can be avoided if we store a root in the ctor.
115 Int32 root = Arccore::MessagePassing::mpAllReduce(
116 m_super_pm, Arccore::MessagePassing::ReduceMax, super_rank);
117
118 // Broadcast from root to all processes.
119 Arccore::MessagePassing::mpBroadcast(m_super_pm, m_tgt_dist, root);
120}
121
122} // namespace Alien
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17