Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SimpleCSRVectorRedistributor.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 "SimpleCSRVectorRedistributor.h"
20
21#include <alien/kernels/redistributor/RedistributorCommPlan.h>
22#include <alien/utils/Precomp.h>
23
24namespace Alien
25{
26
27using namespace Arccore;
28
29SimpleCSRVectorRedistributor::SimpleCSRVectorRedistributor(const RedistributorCommPlan* commPlan, const VectorDistribution& src_dist)
30: m_comm_plan(commPlan)
31, m_pm_super(m_comm_plan->superParallelMng())
32, m_pm_dst(m_comm_plan->tgtParallelMng().get())
33{
34 this->_computeCommPlan(src_dist);
35}
36
37SimpleCSRVectorRedistributor::~SimpleCSRVectorRedistributor() {}
38
39void SimpleCSRVectorRedistributor::_computeCommPlan(const VectorDistribution& src_dist)
40{
41 Int32 super_comm_size = m_pm_super->commSize();
42
43 // This array will contain the target distribution, relative to the super communication
44 // manager.
45 ConstArrayView<Int32> tgt_dist = m_comm_plan->tgtDist();
46
47 const Integer localSize = src_dist.localSize();
48 const Integer offset = src_dist.offset();
49
50 // Prepare communication buffer
51 UniqueArray<Int32> snd_rows(localSize, 0);
52
53 for (Integer i = 0; i < localSize; ++i)
54 snd_rows[i] = i + offset;
55
56 Int32 p = 0;
57 // I prefer to pass p and rowid in an explicit way !
58 auto is_mine = [&](Int32 p, Int32 rowid) {
59 return (tgt_dist[p] <= rowid) && (rowid < tgt_dist[p + 1]);
60 };
61
62 m_snd_offset.resize(super_comm_size + 1);
63 m_snd_offset.fill(-1);
64 m_snd_offset[0] = 0;
65 for (Integer i = 0; i < localSize; i++) {
66 Int32 row_id = snd_rows[i];
67 while ((!is_mine(p, row_id)) && p < super_comm_size) {
68 p++;
69 m_snd_offset[p] = i;
70 }
71 }
72 for (int p2 = p; p2 < super_comm_size; p2++)
73 m_snd_offset[p2 + 1] = localSize;
74
75 UniqueArray<Int32> snd_count(super_comm_size);
76 Alien::RedistributionTools::computeCounts(m_snd_offset.constView(), snd_count.view());
77 m_rcv_offset.resize(super_comm_size + 1);
78 UniqueArray<Int32> rcv_count(super_comm_size);
79
80 Arccore::MessagePassing::mpAllToAll(m_pm_super, snd_count, rcv_count, 1);
81
82 m_rcv_offset[0] = 0;
83 for (p = 0; p < super_comm_size; ++p) {
84 m_rcv_offset[p + 1] = m_rcv_offset[p] + rcv_count[p];
85 }
86}
87
88void SimpleCSRVectorRedistributor::distribute(
89const SimpleCSRVector<Real>& src, SimpleCSRVector<Real>& tgt)
90{
91 // Copy values to a send buffer, in case src and dst are the same matrix.
92 UniqueArray<Real> snd_values = src.fullValues(); // TODO: avoid this copy
93 UniqueArray<Real> rcv_values(this->rcvSize());
94
95 Alien::RedistributionTools::exchange(m_pm_super, snd_values.constView(),
96 m_snd_offset.constView(), rcv_values.view(), m_rcv_offset.constView());
97
98 ArrayView<Real> values = tgt.fullValues();
99 for (Integer i = 0; i < values.size(); ++i)
100 values[i] = rcv_values[i];
101}
102
103void SimpleCSRVectorRedistributor::distributeBack(
104const SimpleCSRVector<Real>& src, SimpleCSRVector<Real>& tgt)
105{
106 // Copy values to a send buffer, in case src and dst are the same matrix.
107 UniqueArray<Real> snd_values = src.fullValues(); // TODO: avoid this copy
108 UniqueArray<Real> rcv_values(this->rcvBackSize());
109
110 Alien::RedistributionTools::exchange(m_pm_super, snd_values.constView(),
111 m_rcv_offset.constView(), rcv_values.view(), m_snd_offset.constView());
112
113 ArrayView<Real> values = tgt.fullValues();
114 for (Integer i = 0; i < values.size(); ++i)
115 values[i] = rcv_values[i];
116}
117
118} // namespace Alien
Computes a vector distribution.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17