-
Notifications
You must be signed in to change notification settings - Fork 2
/
maxematch_npp.hpp
700 lines (582 loc) · 23.8 KB
/
maxematch_npp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* Maximal edge matching using graph communicator and send/recv */
#pragma once
#ifndef MAXEMATCHNPP_HPP
#define MAXEMATCHNPP_HPP
#include "graph.hpp"
#include <numeric>
#include <cstring>
#include <cassert>
#define MATE_REQUEST_TAG 101 // mate[x] = y, if mate[y] = x, then match (x/y in different processes)
#define MATE_REJECT_TAG 102 // reject vertex mate, requires to update mate
#define MATE_INVALID_TAG 103 // invalidate edge
class MaxEdgeMatchNPP
{
public:
MaxEdgeMatchNPP(Graph* g):
g_(g), D_(0), M_(0),
sources_(0), targets_(0),
sendbuf_(0), recvbuf_(0),
nghosts_in_target_(0), ghost_per_node_(0),
nghosts_target_indices_(0), sendbuf_ctr_(0),
sendreq_ctr_(0), indegree_(-1), outdegree_(-1)
{
const GraphElem lnv = g_->get_lnv();
comm_ = g_->get_comm();
MPI_Comm_size(comm_, &size_);
MPI_Comm_rank(comm_, &rank_);
// create communication graph,
// update in|out degrees
create_graph_topo();
for (int i = 0; i < outdegree_; i++)
{
nghosts_in_target_.insert({targets_[i], 0});
nghosts_target_indices_.insert({targets_[i], 0});
sendbuf_ctr_.insert({targets_[i], 0});
}
ghost_per_node_.resize(lnv, 0);
mate_.resize(lnv);
std::fill(mate_.begin(), mate_.end(), -1);
// populate counter that tracks number
// of ghosts not owned by me
GraphElem tot_ghosts = 0;
for (GraphElem i = 0; i < lnv; i++)
{
GraphElem e0, e1;
g_->edge_range(i, e0, e1);
for (GraphElem e = e0; e < e1; e++)
{
Edge const& edge = g_->get_edge(e);
// all edge is active in the beginning,
// so no need to check edge.active_
const int target = g_->get_owner(edge.tail_);
if (target != rank_)
{
nghosts_in_target_[target] += 1;
ghost_per_node_[i] += 1; // per vertex cross edges
}
}
tot_ghosts += ghost_per_node_[i];
}
// initialize input buffer
// sends a pair of vertices with tag,
// can send at most 2 messages along a
// cross edge
sendbuf_ = new GraphElem[tot_ghosts*2*2];
sendreq_ = new MPI_Request[tot_ghosts*2];
// prefix sum for calculating
// indices of outgoing buffers
GraphElem disp = 0;
for (int t = 0; t < outdegree_; t++)
{
nghosts_target_indices_[targets_[t]] = disp;
disp += (nghosts_in_target_[targets_[t]]*2*2);
}
}
~MaxEdgeMatchNPP() {}
void clear()
{
M_.clear();
mate_.clear();
sources_.clear();
targets_.clear();
nghosts_in_target_.clear();
ghost_per_node_.clear();
nghosts_target_indices_.clear();
sendbuf_ctr_.clear();
recvbuf_.clear();
delete []sendbuf_;
delete []sendreq_;
MPI_Comm_free(&g_comm_);
}
// sources
// indegree -- number of processes for which
// the calling process is the destination
// destinations
// outdegree -- number of processes for which calling
// process is source
void create_graph_topo()
{
const GraphElem lnv = g_->get_lnv();
for (GraphElem v = 0; v < lnv; v++)
{
GraphElem e0, e1;
g_->edge_range(v, e0, e1);
for (GraphElem e = e0; e < e1; e++)
{
Edge const& edge = g_->get_edge(e);
const int owner = g_->get_owner(edge.tail_);
if (owner != rank_)
{
// graph topology assumes directed
// graph, so edges stored twice
if (std::find(targets_.begin(), targets_.end(), owner)
== targets_.end()
&& std::find(sources_.begin(), sources_.end(), owner)
== sources_.end())
{
targets_.push_back(owner);
sources_.push_back(owner);
}
}
}
}
MPI_Dist_graph_create_adjacent(comm_, sources_.size(), sources_.data(),
MPI_UNWEIGHTED, targets_.size(), targets_.data(), MPI_UNWEIGHTED,
MPI_INFO_NULL, 0 /*reorder ranks?*/, &g_comm_);
// indegree/outdegree
// No need another MPI function call (just size of sources/targets
// would do), but just checking...
int weighted;
MPI_Dist_graph_neighbors_count(g_comm_, &indegree_, &outdegree_, &weighted);
// to get sources/targets, use MPI_Dist_graph_neighbors
assert(indegree_ == sources_.size());
assert(outdegree_ == targets_.size());
}
/* Validation */
// if mate[mate[v]] == v then
// we're good
void check_results()
{
// gather M_ and mate_
const int lnv = g_->get_lnv();
unsigned int m_size = M_.size(), m_global_size = 0;
// i,j
m_size *= 2;
GraphElem* M_buf = new GraphElem[m_size];
GraphElem* M_global = nullptr;
GraphElem* mate_global = nullptr;
// communication params from M_ and mate_
int* rcounts = nullptr;
int* rdispls = nullptr;
int* m_rcounts = nullptr;
int* m_rdispls = nullptr;
// communication params for M
if (rank_ == 0)
{
rcounts = new int[size_];
rdispls = new int[size_];
m_rcounts = new int[size_];
m_rdispls = new int[size_];
}
// put M_ into a contiguous buffer
for (int i = 0, j = 0; i < m_size; i+=2, j++)
{
M_buf[i] = M_[j].ij_[0];
M_buf[i+1] = M_[j].ij_[1];
}
MPI_Gather(&m_size, 1, MPI_INT, rcounts, 1, MPI_INT, 0, comm_);
MPI_Gather(&lnv, 1, MPI_INT, m_rcounts, 1, MPI_INT, 0, comm_);
MPI_Reduce(&m_size, &m_global_size, 1, MPI_INT, MPI_SUM, 0, comm_);
// communication params (at root)
if (rank_ == 0)
{
const GraphElem nv = g_->get_nv();
mate_global = new GraphElem[nv];
M_global = new GraphElem[m_global_size];
unsigned int index = 0, m_index = 0;
for (int p = 0; p < size_; p++)
{
rdispls[p] = index;
index += rcounts[p];
m_rdispls[p] = m_index;
m_index += m_rcounts[p];
}
}
MPI_Barrier(comm_);
// M_
MPI_Gatherv(M_buf, m_size, MPI_GRAPH_TYPE, M_global, rcounts, rdispls,
MPI_GRAPH_TYPE, 0, comm_);
// mate
MPI_Gatherv(mate_.data(), lnv, MPI_LONG, mate_global, m_rcounts, m_rdispls,
MPI_GRAPH_TYPE, 0, comm_);
MPI_Barrier(comm_);
// data gathered, now validate
if (rank_ == 0)
{
bool success = true;
for (int i = 0; i < m_global_size; i+=2)
{
if ((mate_global[mate_global[M_global[i]]] != M_global[i])
|| (mate_global[mate_global[M_global[i+1]]] != M_global[i+1]))
{
std::cout << "Validation FAILED." << std::endl;
std::cout << "mate_[mate_[" << M_global[i] << "]] != " << M_global[i] << " OR "
<< "mate_[mate_[" << M_global[i+1] << "]] != " << M_global[i+1] << std::endl;
success = false;
break;
}
}
if (success)
std::cout << "Validation SUCCESS." << std::endl;
}
// clear buffers
delete []M_global;
delete []mate_global;
delete []M_buf;
delete []rcounts;
delete []rdispls;
delete []m_rcounts;
delete []m_rdispls;
}
// print the contents of M_
void print_M() const
{
// gather M_
unsigned int m_size = M_.size(), m_global_size = 0;
// i,j
m_size *= 2;
GraphElem* M_buf = new GraphElem[m_size];
GraphElem* M_global = nullptr;
int* rcounts = nullptr;
int* rdispls = nullptr;
// communication params
if (rank_ == 0)
{
rcounts = new int[size_];
rdispls = new int[size_];
}
// put M_ into a contiguous buffer
for (int i = 0, j = 0; i < m_size; i+=2, j++)
{
M_buf[i] = M_[j].ij_[0];
M_buf[i+1] = M_[j].ij_[1];
}
MPI_Gather(&m_size, 1, MPI_INT, rcounts, 1, MPI_INT, 0, comm_);
MPI_Reduce(&m_size, &m_global_size, 1, MPI_INT, MPI_SUM, 0, comm_);
// communication params (at root)
if (rank_ == 0)
{
M_global = new GraphElem[m_global_size];
unsigned int index = 0;
for (int p = 0; p < size_; p++)
{
rdispls[p] = index;
index += rcounts[p];
}
}
MPI_Gatherv(M_buf, m_size, MPI_GRAPH_TYPE, M_global, rcounts, rdispls,
MPI_GRAPH_TYPE, 0, comm_);
MPI_Barrier(comm_);
// print mates
if (rank_ == 0)
{
std::cout << "Matched vertices: " << std::endl;
for (int i = 0; i < m_global_size; i+=2)
std::cout << M_global[i] << " ---- " << M_global[i+1] << std::endl;
}
// clear buffers
delete []M_global;
delete []M_buf;
delete []rcounts;
delete []rdispls;
}
// TODO FIXME not expecting a, b to
// be large, if large then following
// absolute tolerance test will fail:
// http://realtimecollisiondetection.net/blog/?p=89
bool is_same(double a, double b)
{ return std::abs(a - b) <= std::numeric_limits<double>::epsilon(); }
// expecting v to be local index
// require global_to_local
// before passing
// local computation
void compute_mate(GraphElem v, Edge& max_edge)
{
GraphElem e0, e1;
g_->edge_range(v, e0, e1);
for (GraphElem e = e0; e < e1; e++)
{
EdgeActive& edge = g_->get_active_edge(e);
if (edge.active_)
{
if (edge.edge_.weight_ > max_edge.weight_)
max_edge = edge.edge_;
// break tie using vertex index
if (is_same(edge.edge_.weight_, max_edge.weight_))
if (edge.edge_.tail_ > max_edge.tail_)
max_edge = edge.edge_;
}
}
}
// maximal edge matching
std::vector<EdgeTuple> const& operator()()
{
maxematch_npp();
return M_;
}
// search v in M_ (local list
// of matched vertices)
bool is_matched(GraphElem v)
{
auto found = std::find_if(M_.begin(), M_.end(),
[&](EdgeTuple const& et)
{ return ((et.ij_[0] == v) || (et.ij_[1] == v)); });
if (found == std::end(M_))
return false;
return true;
}
// x is owned by me, y may be a ghost
// deactivate edge x -- y and decrement
inline void deactivate_edge(GraphElem x, GraphElem y)
{
GraphElem e0, e1;
const GraphElem lx = g_->global_to_local(x);
const int y_owner = g_->get_owner(y);
g_->edge_range(lx, e0, e1);
for (GraphElem e = e0; e < e1; e++)
{
EdgeActive& edge = g_->get_active_edge(e);
if (edge.edge_.tail_ == y && edge.active_)
{
edge.active_ = false;
if (y_owner != rank_)
ghost_per_node_[lx] -= 1;
break;
}
}
}
// x is owned by me
// compute y = mate[x], if mate[y] = x, match
// else if y = -1, invalidate all edges adj(x)
void find_mate(GraphElem x)
{
const GraphElem lx = g_->global_to_local(x);
Edge x_max_edge;
compute_mate(lx, x_max_edge);
const GraphElem y = mate_[lx] = x_max_edge.tail_;
// initiate matching request
if (y != -1)
{
// check if y can be matched
const int y_owner = g_->get_owner(y);
if (y_owner == rank_)
{
if (mate_[g_->global_to_local(y)] == x)
{
D_.push_back(x);
D_.push_back(y);
M_.emplace_back(x, y, x_max_edge.weight_);
// mark x-y inactive, because its matched
deactivate_edge(x, y);
deactivate_edge(y, x);
}
}
else // send REQUEST
{
deactivate_edge(x, y);
// FIXME TODO maintain a separate function
// for Isend like base P2P version, aka TaggedIsend
const GraphElem index = nghosts_target_indices_[y_owner]
+ sendbuf_ctr_[y_owner];
sendbuf_[index] = y;
sendbuf_[index + 1] = x;
// FIXME if ranks are reordered, then
// this may fail
MPI_Isend(&sendbuf_[index], 2, MPI_GRAPH_TYPE,
y_owner, MATE_REQUEST_TAG, g_comm_,
&sendreq_[sendreq_ctr_]);
MPI_Request_free(&sendreq_[sendreq_ctr_]);
sendbuf_ctr_[y_owner] += 2;
sendreq_ctr_++;
}
}
else // mate[x] = -1, deactivate all x - adj(x) edges
{
GraphElem e0, e1;
g_->edge_range(lx, e0, e1);
for (GraphElem e = e0; e < e1; e++)
{
EdgeActive& edge = g_->get_active_edge(e);
// deactivate only if edge is active
if (edge.active_)
{
edge.active_ = false;
const GraphElem z = edge.edge_.tail_;
const int z_owner = g_->get_owner(z);
if (z_owner == rank_)
deactivate_edge(z, x); // z - x
else // send INVALID (z - x)
{
ghost_per_node_[lx] -= 1;
const GraphElem index = nghosts_target_indices_[z_owner] + sendbuf_ctr_[z_owner];
sendbuf_[index] = z;
sendbuf_[index + 1] = x;
MPI_Isend(&sendbuf_[index], 2, MPI_GRAPH_TYPE,
z_owner, MATE_INVALID_TAG, g_comm_,
&sendreq_[sendreq_ctr_]);
MPI_Request_free(&sendreq_[sendreq_ctr_]);
sendbuf_ctr_[z_owner] += 2;
sendreq_ctr_++;
}
}
}
}
}
// process matched vertices
// in Part #2
void process_neighbors(GraphElem v)
{
GraphElem e0, e1;
const GraphElem lv = g_->global_to_local(v);
g_->edge_range(lv, e0, e1);
// find unmatched vertices
// in v's neighborhood
for (GraphElem e = e0; e < e1; e++)
{
EdgeActive& edge = g_->get_active_edge(e);
if (edge.active_)
{
const GraphElem x = edge.edge_.tail_;
if (mate_[lv] != x)
{
// invalidate v - x, because v
// is already matched, and not
// with x
edge.active_ = false;
const int x_owner = g_->get_owner(x);
// find another mate for x, as v
// is already matched
if (x_owner == rank_)
{
// invalidate x - v
deactivate_edge(x, v);
// find new candidate
if (mate_[g_->global_to_local(x)] == v)
find_mate(x);
}
else // send REJECT to invalidate x-v and recompute mate[x]
{
ghost_per_node_[lv] -= 1;
const GraphElem index = nghosts_target_indices_[x_owner] + sendbuf_ctr_[x_owner];
sendbuf_[index] = x;
sendbuf_[index + 1] = v;
MPI_Isend(&sendbuf_[index], 2, MPI_GRAPH_TYPE,
x_owner, MATE_REJECT_TAG, g_comm_,
&sendreq_[sendreq_ctr_]);
MPI_Request_free(&sendreq_[sendreq_ctr_]);
sendbuf_ctr_[x_owner] += 2;
sendreq_ctr_++;
}
}
}
}
}
// remote operations, needs ncomm before progressing
// ----------------------------------------------------
void process_messages()
{
GraphElem g_l[2];
MPI_Status status;
int flag = -1;
MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, g_comm_,
&flag, &status);
// post recv
if (flag)
{
MPI_Recv(g_l, 2, MPI_GRAPH_TYPE, status.MPI_SOURCE,
status.MPI_TAG, g_comm_, &status);
}
else
return;
// REQUEST: may result in a match
if (status.MPI_TAG == MATE_REQUEST_TAG)
{
bool matched = false;
// check if y is already matched
if (!is_matched(g_l[0]))
{
// deactivate edge
deactivate_edge(g_l[0], g_l[1]);
if (mate_[g_->global_to_local(g_l[0])] == g_l[1])
{
M_.emplace_back(g_l[0], g_l[1], 0.0);
D_.push_back(g_l[0]);
D_.push_back(g_l[1]);
matched = true;
}
}
// send REJECT if matching not possible
if (!matched)
{
// deactivate edge
deactivate_edge(g_l[0], g_l[1]);
const int x_owner = g_->get_owner(g_l[1]);
const GraphElem index = nghosts_target_indices_[x_owner] + sendbuf_ctr_[x_owner];
sendbuf_[index] = g_l[1];
sendbuf_[index + 1] = g_l[0];
MPI_Isend(&sendbuf_[index], 2, MPI_GRAPH_TYPE,
x_owner, MATE_REJECT_TAG, g_comm_,
&sendreq_[sendreq_ctr_]);
MPI_Request_free(&sendreq_[sendreq_ctr_]);
sendbuf_ctr_[x_owner] += 2;
sendreq_ctr_++;
}
}
else if (status.MPI_TAG == MATE_REJECT_TAG)
{
deactivate_edge(g_l[0], g_l[1]);
// recalculate mate[x]
if (mate_[g_->global_to_local(g_l[0])] == g_l[1])
find_mate(g_l[0]);
}
else // INVALID: deactivate x -- v
deactivate_edge(g_l[0], g_l[1]);
}
// maximal weight matching main
void maxematch_npp()
{
const GraphElem lnv = g_->get_lnv();
/* Phase #1: Part #1 -- Process locally owned vertices */
for (GraphElem i = 0; i < lnv; i++)
find_mate(g_->local_to_global(i));
/* Phase #1: Part #2 -- Handle remotely owned vertices */
while(1)
{
process_messages();
do_matching();
// exit criteria
// check if all cross edges have been processed
GraphElem count = std::accumulate(ghost_per_node_.begin(),
ghost_per_node_.end(), 0);
//std::cout << "[" << rank_ << "] count: " << count << std::endl;
if (count == 0)
break;
} // end of while(D_)
MPI_Waitall(sendreq_ctr_, sendreq_, MPI_STATUSES_IGNORE);
}
// locally process matched vertices
// ignore ghost vertices
void do_matching()
{
while (!D_.empty())
{
GraphElem v = D_.back();
D_.pop_back();
const int v_owner = g_->get_owner(v);
if (v_owner == rank_) // check neighbors of v
process_neighbors(v);
}
}
private:
Graph* g_;
std::vector<GraphElem> D_, mate_;
std::vector<EdgeTuple> M_;
// count of ghost vertices not owned by me
// and counters
std::vector<GraphElem> ghost_per_node_;
std::unordered_map<int, GraphElem>
nghosts_in_target_, // ghost vertices in target rank
nghosts_target_indices_, // indices of data
sendbuf_ctr_;
// intermediate communication buffers
GraphElem* sendbuf_;
MPI_Request* sendreq_;
GraphElem sendreq_ctr_;
std::vector<GraphElem> recvbuf_;
std::vector<int> sources_, targets_;
int indegree_, outdegree_;
int rank_, size_;
MPI_Comm comm_;
MPI_Comm g_comm_; // neighborhood comm
};
#endif