Debian Codegraph @

src/spatial.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009, 2022 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "music/spatial.hh" // Must be included first on BG/Ls
20
21 #include "music/debug.hh"
22
23 #if MUSIC_USE_MPI
24
25 #include <sstream>
26
27 #include "music/error.hh"
28 #include "music/communication.hh"
29 #include "music/connector.hh" // used only for debugging
30
31 namespace MUSIC {
32
33 NegotiationIterator::BufferTraversal::BufferTraversal
34 (std::vector<NegotiationIntervals>& buffers_)
35 : buffers (buffers_), buffer (0), interval (0)
36 {
37 findInterval ();
38 }
39
40
41 void
42 NegotiationIterator::BufferTraversal::findInterval ()
43 {
44 while (buffer < buffers.size () && interval == buffers[buffer].size ())
45 {
46 // Check next buffer
47 ++buffer;
48 interval = 0;
49 }
50 }
51
52
53 bool
54 NegotiationIterator::BufferTraversal::end ()
55 {
56 return buffer == buffers.size ();
57 }
58
59
60 void
61 NegotiationIterator::BufferTraversal::operator++ ()
62 {
63 ++interval;
64 findInterval ();
65 }
66
67
68 SpatialNegotiationData*
69 NegotiationIterator::BufferTraversal::dereference ()
70 {
71 return &buffers[buffer][interval];
72 }
73
74
75 void
76 NegotiationIterator::init (Implementation* impl)
77 {
78 implementation_ = impl;
79 end_ = false;
80 ++*this;
81 }
82
83
84 NegotiationIterator::~NegotiationIterator ()
85 {
86 delete implementation_;
87 }
88
89
90 NegotiationIterator::NegotiationIterator (const NegotiationIterator& i)
91 : implementation_ (i.implementation_->copy ()),
92 current_ (i.current_),
93 end_ (i.end_)
94 {
95 }
96
97
98 const NegotiationIterator&
99 NegotiationIterator::operator= (const NegotiationIterator& i)
100 {
101 delete implementation_;
102 implementation_ = i.implementation_->copy ();
103 current_ = i.current_;
104 end_ = i.end_;
105 return *this;
106 }
107
108
109 NegotiationIterator&
110 NegotiationIterator::operator++ ()
111 {
112 if (implementation_->end ())
113 end_ = true;
114 else
115 {
116 current_ = *implementation_->dereference ();
117 ++*implementation_;
118 while (!implementation_->end ()
119 && implementation_->dereference ()->begin () == current_.end ()
120 && implementation_->dereference ()->local () == current_.local ()
121 && implementation_->dereference ()->rank () == current_.rank ())
122 {
123 // join intervals
124 current_.setEnd (implementation_->dereference ()->end ());
125 ++*implementation_;
126 }
127 }
128 return *this;
129 }
130
131
132 SpatialNegotiationData*
133 NegotiationIterator::operator-> ()
134 {
135 return &current_;
136 }
137
138
139 SpatialNegotiator::SpatialNegotiator (IndexMap* ind, Index::Type type_, MPI_Comm c)
140 : comm(c), indices (ind), type (type_)
141 {
142
143 nProcesses = mpi_get_comm_size (comm);
144 localRank = mpi_get_rank (comm);
145 negotiateWidth ();
146
147 }
148
149
150
151 void
152 SpatialNegotiator::negotiateWidth ()
153 {
154
155 // First determine local least upper bound and width
156 int u = 0;
157 int w = 0;
158 for (IndexMap::iterator i = indices->begin ();
159 i != indices->end ();
160 ++i)
161 {
162 if (i->end () > u)
163 u = i->end ();
164 w += i->end () - i->begin ();
165
166 }
167
168 // Now take maximum over all processes
169 std::vector<int> m (nProcesses);
170 MPI_Allgather (&u, 1, MPI_INT, &m[0], 1, MPI_INT, comm);
171 for (unsigned int i = 0; i < nProcesses; ++i)
172 if (m[i] > u)
173 u = m[i];
174 width = u;
175 MPI_Allgather (&w, 1, MPI_INT, &m[0], 1, MPI_INT, comm);
176 for (unsigned int i = 0; i < nProcesses; ++i)
177 if (m[i] > w)
178 w = m[i];
179
180 maxLocalWidth_ = w;
181
182
183 }
184
185
186 void
187 SpatialOutputNegotiator::negotiateWidth ()
188 {
189 if (localRank == 0)
190 {
191 // Receiver might need to know sender width
192 MPI_Send (&width, 1, MPI_INT, 0, WIDTH_MSG, intercomm);
193 int remoteWidth;
194 MPI_Recv (&remoteWidth, 1, MPI_INT, 0, WIDTH_MSG, intercomm, MPI_STATUS_IGNORE);
195 if (remoteWidth != width)
196 {
197 std::ostringstream msg;
198 msg << "sender and receiver width mismatch ("
199 << width << " != " << remoteWidth << ")";
200 error (msg.str ());
201 }
202 }
203 }
204
205
206 void
207 SpatialInputNegotiator::negotiateWidth ()
208 {
209 if (localRank == 0)
210 {
211 int remoteWidth;
212 MPI_Recv (&remoteWidth, 1, MPI_INT, 0, WIDTH_MSG, intercomm, MPI_STATUS_IGNORE);
213 // NOTE: For now, the handling of Index::WILDCARD_MAX is a bit
214 // incomplete since, if there is any index interval on the
215 // receiver side with index larger than the sender side width,
216 // we will still choose sender side width as receiver side
217 // width.
218 /* remedius
219 * temporal fix is width = remoteWidth; shouldn't be like this!!!
220 */
221 if (width == Index::WILDCARD_MAX || width < remoteWidth )
222 width = remoteWidth;
223 MPI_Send (&width, 1, MPI_INT, 0, WIDTH_MSG, intercomm);
224 }
225 // Broadcast result (if we used a wildcard)
226 MPI_Bcast (&width, 1, MPI_INT, 0, comm);
227 if (maxLocalWidth_ == Index::WILDCARD_MAX)
228 maxLocalWidth_ = width;
229 }
230
231
232 NegotiationIterator
233 SpatialNegotiator::canonicalDistribution (int width, int nProcesses)
234 {
235 class Wrapper : public NegotiationIterator::Implementation {
236 int i;
237 int r;
238 int w;
239 int nPerProcess;
240 SpatialNegotiationData data;
241 public:
242 Wrapper (int width, int nProcesses)
243 : i (0), r (0), w (width)
244 {
245 nPerProcess = w / nProcesses;
246 if (w % nProcesses > 0)
247 ++nPerProcess;
248 }
249 bool end () { return i >= w; }
250 void operator++ () { ++r; i += nPerProcess; }
251 SpatialNegotiationData* dereference ()
252 {
253 int high = std::min (i + nPerProcess, w);
254 data = SpatialNegotiationData (IndexInterval (i, high, 0), r, 0);
255 return &data;
256 }
257 Implementation* copy ()
258 {
259 return new Wrapper (*this);
260 }
261 };
262
263 return NegotiationIterator (new Wrapper (width, nProcesses));
264 }
265
266
267 NegotiationIterator
268 SpatialNegotiator::wrapIntervals (IndexMap::iterator beg,
269 IndexMap::iterator end,
270 Index::Type type,
271 int rank)
272 {
273 class Wrapper : public NegotiationIterator::Implementation {
274 protected:
275 SpatialNegotiationData data;
276 IndexMap::iterator i;
277 private:
278 IndexMap::iterator end_;
279 protected:
280 int rank_;
281 int cur_displ_;
282 public:
283 Wrapper (IndexMap::iterator beg,
284 IndexMap::iterator end,
285 int rank)
286 : i (beg), end_ (end), rank_ (rank)
287 {
288 cur_displ_ = 0;
289 }
290 bool end () { return i == end_; }
291 void operator++ () {
292 cur_displ_ +=(i->end() - i->begin());
293 ++i;
294 if(end())
295 cur_displ_ = 0;}
296 };
297
298 class GlobalWrapper : public Wrapper {
299 public:
300 GlobalWrapper (IndexMap::iterator beg,
301 IndexMap::iterator end,
302 int rank)
303 : Wrapper (beg, end, rank)
304 {
305 }
306 SpatialNegotiationData* dereference ()
307 {
308 data = SpatialNegotiationData (*i, rank_, cur_displ_);
309 data.setLocal (0);
310 return &data;
311 }
312 Implementation* copy ()
313 {
314 return new GlobalWrapper (*this);
315 }
316 };
317
318 class LocalWrapper : public Wrapper {
319 public:
320 LocalWrapper (IndexMap::iterator beg,
321 IndexMap::iterator end,
322 int rank)
323 : Wrapper (beg, end, rank)
324 {
325 }
326 SpatialNegotiationData* dereference ()
327 {
328 data = SpatialNegotiationData (*i, rank_, cur_displ_);
329 return &data;
330 }
331 Implementation* copy ()
332 {
333 return new LocalWrapper (*this);
334 }
335 };
336
337 Wrapper* w;
338 if (type == Index::GLOBAL)
339 w = new GlobalWrapper (beg, end, rank);
340 else
341 w = new LocalWrapper (beg, end, rank);
342 return NegotiationIterator (w);
343 }
344
345
346 // Compute intersection intervals between source and dest. Store
347 // the resulting intervals with rank from source in buffer
348 // belonging to rank in dest.
349 void
350 SpatialNegotiator::intersectToBuffers
351 (std::vector<NegotiationIntervals>& source,
352 std::vector<NegotiationIntervals>& dest,
353 std::vector<NegotiationIntervals>& buffers)
354 {
355 // Cleanup old buffer content
356 for (std::vector<NegotiationIntervals>::iterator i = buffers.begin ();
357 i != buffers.end ();
358 ++i)
359 i->clear ();
360 for (std::vector<NegotiationIntervals>::iterator d = dest.begin ();
361 d != dest.end ();
362 ++d)
363 for (std::vector<NegotiationIntervals>::iterator s = source.begin ();
364 s != source.end ();
365 ++s)
366 intersectToBuffers2 (*s, *d, buffers);
367 }
368
369
370 void
371 SpatialNegotiator::intersectToBuffers
372 (NegotiationIterator source,
373 NegotiationIterator dest,
374 std::vector<NegotiationIntervals>& buffers)
375 {
376 // Cleanup old buffer content
377 for (std::vector<NegotiationIntervals>::iterator i = buffers.begin ();
378 i != buffers.end ();
379 ++i)
380 i->clear ();
381 intersectToBuffers2 (source, dest, buffers);
382 }
383
384
385 void
386 SpatialNegotiator::intersectToBuffers2
387 (NegotiationIterator source,
388 NegotiationIterator dest,
389 std::vector<NegotiationIntervals>& buffers)
390 {
391 while (!source.end () && !dest.end ())
392 {
393 MUSIC_LOGX("(" << source->begin () << ", "
394 << source->end () << ", "
395 << source->local () << ", "
396 << source->rank () << ") and ("
397 << dest->begin () << ", "
398 << dest->end () << ", "
399 << dest->local () << ", "
400 << dest->rank () << ")");
401 if (source->begin () < dest->begin ())
402 if (dest->begin () < source->end ())
403 if (dest->end () < source->end ())
404 {
405 // NOTE: put into helper function to get overview
406 SpatialNegotiationData d (dest->begin (),
407 dest->end (),
408 dest->local () - source->local (),
409 source->rank (),
410 source->displ()+(dest->begin()-source->begin())
411 );
412 buffers[dest->rank ()].push_back (d);
413 ++dest;
414 }
415 else
416 {
417 SpatialNegotiationData d (dest->begin (),
418 source->end (),
419 dest->local () - source->local (),
420 source->rank (),
421 source->displ()+(dest->begin()-source->begin())
422 );
423 buffers[dest->rank ()].push_back (d);
424 ++source;
425 }
426 else
427 ++source;
428 else
429 if (source->begin () < dest->end ())
430 if (source->end () < dest->end ())
431 {
432 SpatialNegotiationData d (source->begin (),
433 source->end (),
434 dest->local () - source->local (),
435 source->rank (),
436 source->displ()
437 );
438 buffers[dest->rank ()].push_back (d);
439 ++source;
440 }
441 else
442 {
443 SpatialNegotiationData d (source->begin (),
444 dest->end (),
445 dest->local () - source->local (),
446 source->rank (),
447 source->displ()
448 );
449 buffers[dest->rank ()].push_back (d);
450 ++dest;
451 }
452 else
453 ++dest;
454 }
455 }
456
457
458 void
459 SpatialNegotiator::send (MPI_Comm& comm,
460 int destRank,
461 NegotiationIntervals& intervals)
462 {
463 SpatialNegotiationData* data = &intervals[0];
464 int nIntervals = intervals.size ();
465 // first send size
466 MPI_Send (&nIntervals, 1, MPI_INT, destRank, SPATIAL_NEGOTIATION_MSG, comm);
467 MPI_Send (data,
468 sizeof (SpatialNegotiationData) / sizeof (int) * nIntervals,
469 MPI_INT,
470 destRank,
471 SPATIAL_NEGOTIATION_MSG,
472 comm);
473 }
474
475
476 void
477 SpatialNegotiator::receive (MPI_Comm& comm,
478 int sourceRank,
479 NegotiationIntervals& intervals)
480 {
481 int nIntervals;
482 MPI_Recv (&nIntervals, 1, MPI_INT, sourceRank, SPATIAL_NEGOTIATION_MSG, comm,
483 MPI_STATUS_IGNORE);
484 intervals.resize (nIntervals);
485 MPI_Recv (&intervals[0],
486 sizeof (SpatialNegotiationData) / sizeof (int)
487 * nIntervals,
488 MPI_INT,
489 sourceRank,
490 SPATIAL_NEGOTIATION_MSG,
491 comm,
492 MPI_STATUS_IGNORE);
493 }
494
495
496 void
497 SpatialNegotiator::allToAll (std::vector<NegotiationIntervals>& out,
498 std::vector<NegotiationIntervals>& in)
499 {
500 if (out.size () != nProcesses || in.size () != nProcesses)
501 error ("internal error in SpatialNegotiator::allToAll ()");
502 in[localRank] = out[localRank];
503 for (unsigned int i = 0; i < localRank; ++i)
504 receive (comm, i, in[i]);
505 for (unsigned int i = localRank + 1; i < nProcesses; ++i)
506 send (comm, i, out[i]);
507 for (unsigned int i = 0; i < localRank; ++i)
508 send (comm, i, out[i]);
509 for (unsigned int i = localRank + 1; i < nProcesses; ++i)
510 receive (comm, i, in[i]);
511 }
512 NegotiationIterator
513 SpatialNegotiator::negotiateSimple()
514 {
515 return wrapIntervals (indices->begin (),
516 indices->end (),
517 type,
518 localRank);
519 }
520
521 SpatialOutputNegotiator::SpatialOutputNegotiator (IndexMap* indices,
522 Index::Type type, MPI_Comm c,
523 MPI_Comm ic)
524 : SpatialNegotiator (indices, type, c)
525 {
526 intercomm = ic;
527 negotiateWidth ();
528 }
529
530
531 NegotiationIterator
532 SpatialOutputNegotiator::negotiate (int remoteNProc,
533 Connector* connector)
534 {
535 #ifdef MUSIC_DEBUG
536 connector_ = connector;
537 #endif
538 local.resize (nProcesses);
539 results.resize (nProcesses);
540 remote.resize (remoteNProc);
541 NegotiationIterator mappedDist = wrapIntervals (indices->begin (),
542 indices->end (),
543 type,
544 localRank);
545 NegotiationIterator canonicalDist
546 = canonicalDistribution (width, nProcesses);
547
548 // NOTE: Find a better name for variable `results'
549 intersectToBuffers (mappedDist, canonicalDist, results);
550
551 // Send to virtual connector
552 allToAll (results, local);
553
554 // Receive from remote connector
555 for (int i = 0; i < remoteNProc; ++i)
556 receive (intercomm, i, remote[i]);
557
558 results.resize (remoteNProc);
559 intersectToBuffers (local, remote, results);
560
561 // Send to remote connector
562 for (int i = 0; i < remoteNProc; ++i)
563 send (intercomm, i, results[i]);
564
565 results.resize (nProcesses);
566 intersectToBuffers (remote, local, results);
567
568 // Send back to real connector
569 allToAll (results, local);
570
571 return NegotiationIterator (local);
572 }
573
574 SpatialInputNegotiator::SpatialInputNegotiator (IndexMap* indices,
575 Index::Type type, MPI_Comm c,
576 MPI_Comm ic)
577 : SpatialNegotiator (indices, type, c)
578 {
579 intercomm = ic;
580 negotiateWidth ();
581 }
582
583
584 NegotiationIterator
585 SpatialInputNegotiator::negotiate ( int remoteNProc,
586 Connector* connector)
587 {
588 #ifdef MUSIC_DEBUG
589 connector_ = connector;
590 #endif
591 remote.resize (remoteNProc);
592 NegotiationIterator mappedDist = wrapIntervals (indices->begin (),
593 indices->end (),
594 type,
595 localRank);
596 NegotiationIterator canonicalDist
597 = canonicalDistribution (width, remoteNProc);
598 intersectToBuffers (mappedDist, canonicalDist, remote);
599 for (int i = 0; i < remoteNProc; ++i)
600 send (intercomm, i, remote[i]);
601 for (int i = 0; i < remoteNProc; ++i)
602 receive (intercomm, i, remote[i]);
603
604 return NegotiationIterator (remote);
605 }
606
607 }
608 #endif