Debian Codegraph @

src/atlas/parallel/Collect.cc

raw
1 /*
2 * (C) Copyright 2025- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "Collect.h"
12
13 #include <algorithm>
14 #include <cmath>
15 #include <numeric>
16 #include <stdexcept>
17
18 #include "pluto/pluto.h"
19
20 namespace atlas::parallel {
21
22 int Collect::devices() const {
23 return pluto::devices();
24 }
25
26 void Collect::setup(const idx_t recv_size, const int recv_part[], const idx_t recv_remote_idx[], const int recv_idx_base) {
27 setup(mpi::comm().name(), recv_size, recv_part, recv_remote_idx, recv_idx_base );
28 }
29
30 void Collect::setup(const std::string& mpi_comm,
31 const idx_t recv_size, const int recv_part[], const idx_t recv_remote_idx[], const int recv_idx_base) {
32
33 ATLAS_TRACE("Collect::setup()");
34 comm_ = &mpi::comm(mpi_comm);
35 mpi_size_ = comm().size();
36 mpi_rank_ = comm().rank();
37 int nproc = mpi_size_;
38
39 recvcnt_ = recv_size;
40 sendcounts_.resize(nproc);
41 sendcounts_.assign(nproc, 0);
42 recvcounts_.resize(nproc);
43 recvcounts_.assign(nproc, 0);
44 senddispls_.resize(nproc);
45 senddispls_.assign(nproc, 0);
46 recvdispls_.resize(nproc);
47 recvdispls_.assign(nproc, 0);
48
49 atlas_omp_parallel {
50 std::vector<int> recvcounts(nproc, 0);
51 atlas_omp_for(std::size_t jrecv = 0; jrecv < recvcnt_; ++jrecv) {
52 int p = recv_part[jrecv];
53 ++recvcounts[p];
54 }
55 atlas_omp_critical {
56 for (int p=0; p < nproc; ++p) {
57 recvcounts_[p] += recvcounts[p];
58 }
59 }
60 }
61
62 /*
63 Find the amount of nodes this rank has to send to each other ranks
64 */
65 ATLAS_TRACE_MPI(ALLTOALL) { comm().allToAll(recvcounts_, sendcounts_); }
66
67 sendcnt_ = std::accumulate(sendcounts_.begin(), sendcounts_.end(), 0);
68
69 recvdispls_[0] = 0;
70 senddispls_[0] = 0;
71 for (int jproc = 1; jproc < nproc; ++jproc) // start at 1
72 {
73 recvdispls_[jproc] = recvcounts_[jproc - 1] + recvdispls_[jproc - 1];
74 senddispls_[jproc] = sendcounts_[jproc - 1] + senddispls_[jproc - 1];
75 }
76
77 /*
78 Fill vector "send_requests" with remote index of nodes needed, but are on
79 other procs
80 We can also fill in the vector "recvmap_" which holds local indices of
81 requested nodes
82 */
83 std::vector<int> send_requests(recvcnt_);
84 std::vector<int> recv_requests(sendcnt_);
85 std::vector<int> cnt(nproc, 0);
86 recvmap_.resize(recvcnt_);
87 #ifdef __PGI
88 // No idea why PGI compiler (20.7) in Release build ( -fast -O3 ) decides to vectorize following loop
89 #pragma loop novector
90 #endif
91 Log::trace() << __LINE__ << std::endl;
92
93 for (int jrecv = 0; jrecv < recvcnt_; ++jrecv) {
94 const int p = recv_part[jrecv];
95 const int req_idx = recvdispls_[p] + cnt[p];
96 send_requests[req_idx] = recv_remote_idx[jrecv] - recv_idx_base;
97 recvmap_[req_idx] = jrecv;
98 cnt[p]++;
99 }
100
101 Log::trace() << __LINE__ << std::endl;
102
103 /*
104 Fill vector "recv_requests" with what is needed by other procs
105 */
106 ATLAS_TRACE_MPI(ALLTOALL) {
107 ATLAS_ASSERT(send_requests.size() == recvcnt_);
108 ATLAS_ASSERT(std::accumulate(recvcounts_.begin(),recvcounts_.end(),0) == recvcnt_);
109 ATLAS_ASSERT(recv_requests.size() == sendcnt_);
110 ATLAS_ASSERT(std::accumulate(sendcounts_.begin(),sendcounts_.end(),0) == sendcnt_);
111
112 comm().allToAllv(send_requests.data(), recvcounts_.data(), recvdispls_.data(), recv_requests.data(),
113 sendcounts_.data(), senddispls_.data());
114 }
115 Log::trace() << __LINE__ << std::endl;
116
117
118 /*
119 What needs to be sent to other procs is asked by remote_idx, which is local
120 here
121 */
122 sendmap_.resize(sendcnt_);
123 for (int jj = 0; jj < sendcnt_; ++jj) {
124 sendmap_[jj] = recv_requests[jj];
125 }
126 Log::trace() << __LINE__ << std::endl;
127
128 is_setup_ = true;
129 }
130
131 void Collect::counts_displs_setup(const idx_t var_size, std::vector<int>& send_counts_init,
132 std::vector<int>& recv_counts_init, std::vector<int>& send_counts,
133 std::vector<int>& recv_counts, std::vector<int>& send_displs,
134 std::vector<int>& recv_displs) const {
135 for (size_t jproc = 0; jproc < static_cast<size_t>(mpi_size_); ++jproc) {
136 send_counts_init[jproc] = sendcounts_[jproc];
137 recv_counts_init[jproc] = recvcounts_[jproc];
138 send_counts[jproc] = sendcounts_[jproc] * var_size;
139 recv_counts[jproc] = recvcounts_[jproc] * var_size;
140 send_displs[jproc] = senddispls_[jproc] * var_size;
141 recv_displs[jproc] = recvdispls_[jproc] * var_size;
142 }
143 }
144
145 void Collect::wait_for_send(std::vector<int>& send_counts_init, std::vector<eckit::mpi::Request>& send_req) const {
146 ATLAS_TRACE_MPI(WAIT, "mpi-wait send") {
147 for (size_t jproc = 0; jproc < static_cast<size_t>(mpi_size_); ++jproc) {
148 if (send_counts_init[jproc] > 0) {
149 comm().wait(send_req[jproc]);
150 }
151 }
152 }
153 }
154
155 } // namespace atlas::parallel