Debian Codegraph @

src/atlas/parallel/HaloExchange.cc

raw
1 /*
2 * (C) Copyright 2013 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 /// @file HaloExchange.cc
12 /// @author Willem Deconinck
13 /// @date Nov 2013
14
15 #include <memory>
16 #include <numeric>
17 #include <sstream>
18 #include <stdexcept>
19
20 #include "pluto/pluto.h"
21
22 #include "atlas/array/Array.h"
23 #include "atlas/parallel/HaloExchange.h"
24 #include "atlas/parallel/mpi/Statistics.h"
25 #include "atlas/util/vector.h"
26
27 namespace atlas {
28 namespace parallel {
29
30 namespace {
31 struct IsGhostPoint {
32 IsGhostPoint(const int mypart, const int part[], const idx_t ridx[], const idx_t base, const int /*N*/) {
33 part_ = part;
34 ridx_ = ridx;
35 base_ = base;
36 mypart_ = mypart;
37 }
38
39 bool operator()(idx_t idx) {
40 if (part_[idx] != mypart_) {
41 return true;
42 }
43 if (ridx_[idx] != base_ + idx) {
44 return true;
45 }
46 return false;
47 }
48 int mypart_;
49 const int* part_;
50 const idx_t* ridx_;
51 idx_t base_;
52 };
53 } // namespace
54
55 HaloExchange::HaloExchange() :
56 HaloExchange("") {
57 }
58
59 HaloExchange::HaloExchange(const std::string& name):
60 name_(name),
61 is_setup_(false) {
62 }
63
64 HaloExchange::~HaloExchange() = default;
65
66 void HaloExchange::setup(const int part[], const idx_t remote_idx[], const int base, const idx_t size) {
67 setup(mpi::comm().name(), part, remote_idx, base, size);
68 }
69
70 void HaloExchange::setup(const int part[], const idx_t remote_idx[], const int base, idx_t parsize, idx_t halo_begin) {
71 setup(mpi::comm().name(), part, remote_idx, base, parsize, halo_begin);
72 }
73
74 void HaloExchange::setup(const std::string& mpi_comm, const int part[], const idx_t remote_idx[], const int base, const idx_t size) {
75 setup(mpi_comm, part, remote_idx, base, size, 0);
76 }
77
78 void HaloExchange::setup(const std::string& mpi_comm, const int part[], const idx_t remote_idx[], const int base, idx_t parsize, idx_t halo_begin) {
79 ATLAS_TRACE("HaloExchange::setup");
80 comm_ = &mpi::comm(mpi_comm);
81 myproc = comm().rank();
82 nproc = comm().size();
83
84 parsize_ = parsize;
85 sendcounts_.resize(nproc);
86 sendcounts_.assign(nproc, 0);
87 recvcounts_.resize(nproc);
88 recvcounts_.assign(nproc, 0);
89 senddispls_.resize(nproc);
90 senddispls_.assign(nproc, 0);
91 recvdispls_.resize(nproc);
92 recvdispls_.assign(nproc, 0);
93
94 /*
95 Find the amount of nodes this proc has to receive from each other proc
96 */
97
98 IsGhostPoint is_ghost(myproc, part, remote_idx, base, parsize_);
99 atlas::vector<idx_t> ghost_points(parsize_);
100 idx_t nghost = 0;
101
102 atlas_omp_parallel_for(int jj = halo_begin; jj < parsize_; ++jj) {
103 if (is_ghost(jj)) {
104 int p = part[jj];
105 atlas_omp_critical {
106 ++recvcounts_[p];
107 ghost_points[nghost] = jj;
108 nghost++;
109 }
110 }
111 }
112
113 recvcnt_ = std::accumulate(recvcounts_.begin(), recvcounts_.end(), 0);
114
115 /*
116 Find the amount of nodes this proc has to send to each other proc
117 */
118 ATLAS_TRACE_MPI(ALLTOALL) { comm().allToAll(recvcounts_, sendcounts_); }
119
120 sendcnt_ = std::accumulate(sendcounts_.begin(), sendcounts_.end(), 0);
121
122 recvdispls_[0] = 0;
123 senddispls_[0] = 0;
124 for (int jproc = 1; jproc < nproc; ++jproc) // start at 1
125 {
126 recvdispls_[jproc] = recvcounts_[jproc - 1] + recvdispls_[jproc - 1];
127 senddispls_[jproc] = sendcounts_[jproc - 1] + senddispls_[jproc - 1];
128 }
129 /*
130 Fill vector "send_requests" with remote index of nodes needed, but are on
131 other procs
132 We can also fill in the vector "recvmap_" which holds local indices of
133 requested nodes
134 */
135 std::vector<int> send_requests(recvcnt_);
136 std::vector<int> recv_requests(sendcnt_);
137 std::vector<int> cnt(nproc, 0);
138 recvmap_.resize(recvcnt_);
139 #ifdef __PGI
140 // No idea why PGI compiler (20.7) in Release build ( -fast -O3 ) decides to vectorize following loop
141 #pragma loop novector
142 #endif
143 for (idx_t jghost = 0; jghost < nghost; ++jghost) {
144 const idx_t jj = ghost_points[jghost];
145 const int p = part[jj];
146 const int req_idx = recvdispls_[p] + cnt[p];
147 send_requests[req_idx] = remote_idx[jj] - base;
148 recvmap_[req_idx] = jj;
149 cnt[p]++;
150 }
151
152 /*
153 Fill vector "recv_requests" with what is needed by other procs
154 */
155
156 ATLAS_TRACE_MPI(ALLTOALL) {
157 comm().allToAllv(send_requests.data(), recvcounts_.data(), recvdispls_.data(), recv_requests.data(),
158 sendcounts_.data(), senddispls_.data());
159 }
160
161 /*
162 What needs to be sent to other procs is asked by remote_idx, which is local
163 here
164 */
165 sendmap_.resize(sendcnt_);
166 for (int jj = 0; jj < sendcnt_; ++jj) {
167 sendmap_[jj] = recv_requests[jj];
168 }
169
170 is_setup_ = true;
171 backdoor.parsize = parsize_;
172 }
173
174 void HaloExchange::wait_for_send(std::vector<int>& send_counts_init, std::vector<eckit::mpi::Request>& send_req) const {
175 ATLAS_TRACE_MPI(WAIT, "mpi-wait send") {
176 for (int jproc = 0; jproc < nproc; ++jproc) {
177 if (send_counts_init[jproc] > 0) {
178 comm().wait(send_req[jproc]);
179 }
180 }
181 }
182 }
183
184 int HaloExchange::devices() const {
185 return pluto::devices();
186 }
187
188 bool HaloExchange::is_device_accessible(const void* ptr) const {
189 return pluto::is_device_accessible(ptr);
190 }
191
192 namespace {
193
194 template <typename Value>
195 void execute_halo_exchange(HaloExchange* This, Value field[], int var_strides[], int var_extents[], int var_rank) {
196 // WARNING: Only works if there is only one parallel dimension AND being
197 // slowest moving
198
199 array::ArrayShape shape{This->backdoor.parsize};
200 for (int j = 0; j < var_rank; ++j) {
201 shape.push_back(var_extents[j]);
202 }
203
204 array::ArrayStrides strides{var_extents[0] * var_strides[0]};
205 for (int j = 0; j < var_rank; ++j) {
206 strides.push_back(var_strides[j]);
207 }
208
209 std::unique_ptr<array::Array> arr(array::Array::wrap(field, array::ArraySpec{shape, strides}));
210
211 switch (arr->rank()) {
212 case 1: {
213 This->execute<Value, 1>(*arr);
214 break;
215 }
216 case 2: {
217 This->execute<Value, 2>(*arr);
218 break;
219 }
220 case 3: {
221 This->execute<Value, 3>(*arr);
222 break;
223 }
224 case 4: {
225 This->execute<Value, 4>(*arr);
226 break;
227 }
228 default:
229 throw_NotImplemented("Rank not supported in halo exchange", Here());
230 }
231 }
232
233 template <typename Value>
234 void execute_adjoint_halo_exchange(HaloExchange* This, Value field[], int var_strides[], int var_extents[],
235 int var_rank) {
236 // WARNING: Only works if there is only one parallel dimension AND being
237 // slowest moving
238
239 array::ArrayShape shape{This->backdoor.parsize};
240 for (int j = 0; j < var_rank; ++j) {
241 shape.push_back(var_extents[j]);
242 }
243
244 array::ArrayStrides strides{var_extents[0] * var_strides[0]};
245 for (int j = 0; j < var_rank; ++j) {
246 strides.push_back(var_strides[j]);
247 }
248
249 std::unique_ptr<array::Array> arr(array::Array::wrap(field, array::ArraySpec{shape, strides}));
250
251 switch (arr->rank()) {
252 case 1: {
253 This->execute_adjoint<Value, 1>(*arr);
254 break;
255 }
256 case 2: {
257 This->execute_adjoint<Value, 2>(*arr);
258 break;
259 }
260 case 3: {
261 This->execute_adjoint<Value, 3>(*arr);
262 break;
263 }
264 case 4: {
265 This->execute_adjoint<Value, 4>(*arr);
266 break;
267 }
268 default:
269 throw_NotImplemented("Rank not supported in halo exchange", Here());
270 }
271 }
272 } // namespace
273
274 extern "C" {
275
276 HaloExchange* atlas__HaloExchange__new() {
277 return new HaloExchange();
278 }
279
280 void atlas__HaloExchange__delete(HaloExchange* This) {
281 delete This;
282 }
283
284 void atlas__HaloExchange__setup(HaloExchange* This, int part[], idx_t remote_idx[], int base, int size) {
285 This->setup(part, remote_idx, base, size);
286 }
287
288
289 void atlas__HaloExchange__execute_adjoint_strided_int(HaloExchange* This, int field[], int var_strides[],
290 int var_extents[], int var_rank) {
291 execute_adjoint_halo_exchange(This, field, var_strides, var_extents, var_rank);
292 }
293
294 void atlas__HaloExchange__execute_adjoint_strided_long(HaloExchange* This, long field[], int var_strides[],
295 int var_extents[], int var_rank) {
296 execute_adjoint_halo_exchange(This, field, var_strides, var_extents, var_rank);
297 }
298
299 void atlas__HaloExchange__execute_adjoint_strided_float(HaloExchange* This, float field[], int var_strides[],
300 int var_extents[], int var_rank) {
301 execute_adjoint_halo_exchange(This, field, var_strides, var_extents, var_rank);
302 }
303
304 void atlas__HaloExchange__execute_adjoint_strided_double(HaloExchange* This, double field[], int var_strides[],
305 int var_extents[], int var_rank) {
306 execute_adjoint_halo_exchange(This, field, var_strides, var_extents, var_rank);
307 }
308
309 void atlas__HaloExchange__execute_strided_int(HaloExchange* This, int field[], int var_strides[], int var_extents[],
310 int var_rank) {
311 execute_halo_exchange(This, field, var_strides, var_extents, var_rank);
312 }
313
314 void atlas__HaloExchange__execute_strided_long(HaloExchange* This, long field[], int var_strides[], int var_extents[],
315 int var_rank) {
316 execute_halo_exchange(This, field, var_strides, var_extents, var_rank);
317 }
318
319 void atlas__HaloExchange__execute_strided_float(HaloExchange* This, float field[], int var_strides[], int var_extents[],
320 int var_rank) {
321 execute_halo_exchange(This, field, var_strides, var_extents, var_rank);
322 }
323
324 void atlas__HaloExchange__execute_strided_double(HaloExchange* This, double field[], int var_strides[],
325 int var_extents[], int var_rank) {
326 execute_halo_exchange(This, field, var_strides, var_extents, var_rank);
327 }
328 }
329
330 /////////////////////
331
332 } // namespace parallel
333 } // namespace atlas