COMBINATORIAL_BLAS 1.6
BPMaximalMatching.h
Go to the documentation of this file.
1#ifndef BP_MAXIMAL_MATCHING_H
2#define BP_MAXIMAL_MATCHING_H
3
4#include "CombBLAS/CombBLAS.h"
5#include <iostream>
6#include <functional>
7#include <algorithm>
8#include <vector>
9#include <limits>
10#include "Utility.h"
11#include "MatchingDefs.h"
12
13#define NO_INIT 0
14#define GREEDY 1
15#define KARP_SIPSER 2
16#define DMD 3
17MTRand GlobalMT(123); // for reproducible result
18
19namespace combblas {
20
21// This is not tested with CSC yet
22// TODO: test with CSC and Setting SPA (similar to Weighted Greedy)
23template <typename Par_DCSC_Bool, typename IT>
24void MaximalMatching(Par_DCSC_Bool & A, Par_DCSC_Bool & AT, FullyDistVec<IT, IT>& mateRow2Col,
25 FullyDistVec<IT, IT>& mateCol2Row, FullyDistVec<IT, IT>& degColRecv, int type, bool rand=true)
26{
27
28 typedef VertexTypeML < IT, IT> VertexType;
29 int nprocs, myrank;
30 MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
31 MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
32 int nthreads = 1;
33#ifdef _OPENMP
34#pragma omp parallel
35 {
36 nthreads = omp_get_num_threads();
37 }
38#endif
39
40 FullyDistVec<IT, IT> degCol = degColRecv;
41
42 //unmatched row and column vertices
43 FullyDistSpVec<IT, IT> unmatchedRow(mateRow2Col, [](IT mate){return mate==-1;});
44 FullyDistSpVec<IT, IT> degColSG(A.getcommgrid(), A.getncol());
45 //FullyDistVec<IT, IT> degCol(A.getcommgrid());
46 //A.Reduce(degCol, Column, plus<IT>(), static_cast<IT>(0)); // Reduce is not multithreaded
47
48
49 FullyDistSpVec<IT, VertexType> unmatchedCol(A.getcommgrid(), A.getncol());
50 // every veretx is unmatched. keep non-isolated vertices
51 unmatchedCol = EWiseApply<VertexType>(unmatchedCol, degCol,
52 [](VertexType vtx, IT deg){return VertexType();},
53 [](VertexType vtx, IT deg){return deg>0;},
54 true, VertexType());
55
56
57 FullyDistSpVec<IT, VertexType> fringeRow(A.getcommgrid(), A.getnrow());
58 FullyDistSpVec<IT, IT> fringeRow2(A.getcommgrid(), A.getnrow());
59 FullyDistSpVec<IT, VertexType> deg1Col(A.getcommgrid(), A.getncol());
60
61
62 IT curUnmatchedCol = unmatchedCol.getnnz();
63 IT curUnmatchedRow = unmatchedRow.getnnz();
64 IT newlyMatched = 1; // ensure the first pass of the while loop
65 int iteration = 0;
66 double tStart = MPI_Wtime();
67 std::vector<std::vector<double> > timing;
68
69#ifdef DETAIL_STATS
70 if(myrank == 0)
71 {
72 cout << "=======================================================\n";
73 cout << "@@@@@@ Number of processes: " << nprocs << endl;
74 cout << "=======================================================\n";
75 cout << "It | UMRow | UMCol | newlyMatched | Time "<< endl;
76 cout << "=======================================================\n";
77 }
78#endif
79 MPI_Barrier(MPI_COMM_WORLD);
80
81
82 while(curUnmatchedCol !=0 && curUnmatchedRow!=0 && newlyMatched != 0 )
83 {
84 unmatchedCol.ApplyInd([](VertexType vtx, IT idx){return VertexType(idx,idx);});
85 if(type==DMD)
86 {
87 unmatchedCol = EWiseApply<VertexType>(unmatchedCol, degCol,
88 [](VertexType vtx, IT deg){return VertexType(vtx.parent,deg);},
89 [](VertexType vtx, IT deg){return true;},
90 false, VertexType());
91 }
92 else if(rand)
93 {
94 unmatchedCol.Apply([](VertexType vtx){return VertexType(vtx.parent, static_cast<IT>((GlobalMT.rand() * 9999999)+1));});
95 }
96
97 // ======================== step1: One step of BFS =========================
98 std::vector<double> times;
99 double t1 = MPI_Wtime();
100 if(type==GREEDY)
101 {
102 SpMV<Select2ndMinSR<bool, VertexType>>(A, unmatchedCol, fringeRow, false);
103 }
104 else if(type==DMD)
105 {
106 SpMV<Select2ndMinSR<bool, VertexType>>(A, unmatchedCol, fringeRow, false);
107 }
108 else //(type==KARP_SIPSER)
109 {
110 deg1Col = EWiseApply<VertexType>(unmatchedCol, degCol,
111 [](VertexType vtx, IT deg){return vtx;},
112 [](VertexType vtx, IT deg){return deg==1;},
113 false, VertexType());
114
115 if(deg1Col.getnnz()>9)
116 SpMV<Select2ndMinSR<bool, VertexType>>(A, deg1Col, fringeRow, false);
117 else
118 SpMV<Select2ndMinSR<bool, VertexType>>(A, unmatchedCol, fringeRow, false);
119 }
120 // Remove matched row vertices
121 fringeRow = EWiseApply<VertexType>(fringeRow, mateRow2Col,
122 [](VertexType vtx, IT mate){return vtx;},
123 [](VertexType vtx, IT mate){return mate==-1;},
124 false, VertexType());
125
126 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
127 // ===========================================================================
128
129
130 // ======================== step2: Update matching =========================
131
132 fringeRow2 = EWiseApply<IT>(fringeRow, mateRow2Col,
133 [](VertexType vtx, IT mate){return vtx.parent;},
134 [](VertexType vtx, IT mate){return true;},
135 false, VertexType());
136
137 FullyDistSpVec<IT, IT> newMatchedCols = fringeRow2.Invert(A.getncol());
138 FullyDistSpVec<IT, IT> newMatchedRows = newMatchedCols.Invert(A.getnrow());
139 mateCol2Row.Set(newMatchedCols);
140 mateRow2Col.Set(newMatchedRows);
141 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
142 // ===========================================================================
143
144
145 // =============== step3: Update degree of unmatched columns =================
146 unmatchedRow.Select(mateRow2Col, [](IT mate){return mate==-1;});
147 unmatchedCol.Select(mateCol2Row, [](IT mate){return mate==-1;});
148
149 if(type!=GREEDY)
150 {
151 // update degree
152 newMatchedRows.Apply([](IT val){return 1;}); // needed if the matrix is Boolean since the SR::multiply isn't called
153 SpMV< SelectPlusSR<bool, IT>>(AT, newMatchedRows, degColSG, false); // degree of column vertices to matched rows
154 // subtract degree of column vertices
155 degCol.EWiseApply(degColSG,
156 [](IT old_deg, IT new_deg){return old_deg-new_deg;},
157 [](IT old_deg, IT new_deg){return true;},
158 false, static_cast<IT>(0));
159 // remove isolated vertices
160 unmatchedCol = EWiseApply<VertexType>(unmatchedCol, degCol,
161 [](VertexType vtx, IT deg){return vtx;},
162 [](VertexType vtx, IT deg){return deg>0;},
163 false, VertexType());
164 }
165 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
166 // ===========================================================================
167
168
169 ++iteration;
170 newlyMatched = newMatchedCols.getnnz();
171 times.push_back(std::accumulate(times.begin(), times.end(), 0.0));
172 timing.push_back(times);
173#ifdef DETAIL_STATS
174 if(myrank == 0)
175 {
176 printf("%3d %10lld %10lld %10lld %18lf\n", iteration , curUnmatchedRow, curUnmatchedCol, newlyMatched, times.back());
177 }
178#endif
179 curUnmatchedCol = unmatchedCol.getnnz();
180 curUnmatchedRow = unmatchedRow.getnnz();
181 MPI_Barrier(MPI_COMM_WORLD);
182
183 }
184
185 IT cardinality = mateRow2Col.Count([](IT mate){return mate!=-1;});
186 std::vector<double> totalTimes(timing[0].size(),0);
187 for(int i=0; i<timing.size(); i++)
188 {
189 for(int j=0; j<timing[i].size(); j++)
190 {
191 totalTimes[j] += timing[i][j];
192 }
193 }
194
195
196 if(myrank == 0)
197 {
198#ifdef DETAIL_STATS
199 cout << "==========================================================\n";
200 cout << "\n================individual timings =======================\n";
201 cout << " SpMV Update-Match Update-UMC Total "<< endl;
202 cout << "==========================================================\n";
203 for(int i=0; i<timing.size(); i++)
204 {
205 for(int j=0; j<timing[i].size(); j++)
206 {
207 printf("%12.5lf ", timing[i][j]);
208 }
209 cout << endl;
210 }
211
212 cout << "-------------------------------------------------------\n";
213 for(int i=0; i<totalTimes.size(); i++)
214 printf("%12.5lf ", totalTimes[i]);
215 cout << endl;
216#endif
217 std::cout << "****** maximal matching runtime ********\n";
218 std::cout << "nprocesses nthreads ncores algorithm Unmatched-Rows Cardinality Total Time***\n";
219 std::cout << nprocs << " " << nthreads << " " << nprocs * nthreads << " ";
220 if(type == DMD) std::cout << "DMD";
221 else if(type == GREEDY) std::cout << "Greedy";
222 else if(type == KARP_SIPSER) std::cout << "Karp-Sipser";
223 if(rand && (type == KARP_SIPSER || type == GREEDY) ) std::cout << "-rand";
224 std::cout << " ";
225 printf("%lld %lld %lf\n", curUnmatchedRow, cardinality, totalTimes.back());
226 std::cout << "-------------------------------------------------------\n\n";
227 }
228 //isMatching(mateCol2Row, mateRow2Col);
229}
230
231
232
233// Special version of the greedy algorithm (works for both CSC (multithreaded) and DCSC)
234// That uses WeightMaxSR semiring
235// TODO: check if this is 1/2 approx of the weighted matching (probably no)
236// TODO: should we remove degCol?
237// TODO: can be merged with the generalized MaximalMatching
238template <typename Par_MAT_Double, typename IT>
239void WeightedGreedy(Par_MAT_Double & A, FullyDistVec<IT, IT>& mateRow2Col,
240 FullyDistVec<IT, IT>& mateCol2Row, FullyDistVec<IT, IT>& degCol)
241{
242
243 typedef VertexTypeML < IT, double> VertexType;
244 int nthreads=1;
245#ifdef THREADED
246#pragma omp parallel
247 {
248 nthreads = omp_get_num_threads();
249 }
250#endif
251 PreAllocatedSPA<VertexType> SPA(A.seq(), nthreads*4);
252 int nprocs, myrank;
253 MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
254 MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
255
256
257 //unmatched row and column vertices
258 FullyDistSpVec<IT, IT> unmatchedRow(mateRow2Col, [](IT mate){return mate==-1;});
259
260
261
262 FullyDistSpVec<IT, VertexType> unmatchedCol(A.getcommgrid(), A.getncol());
263 // every veretx is unmatched. keep non-isolated vertices
264 unmatchedCol = EWiseApply<VertexType>(unmatchedCol, degCol,
265 [](VertexType vtx, IT deg){return VertexType();},
266 [](VertexType vtx, IT deg){return deg>0;},
267 true, VertexType());
268
269
270 FullyDistSpVec<IT, VertexType> fringeRow(A.getcommgrid(), A.getnrow());
271 FullyDistSpVec<IT, IT> fringeRow2(A.getcommgrid(), A.getnrow());
272 FullyDistSpVec<IT, VertexType> fringeRow3(A.getcommgrid(), A.getnrow());
273
274 IT curUnmatchedCol = unmatchedCol.getnnz();
275 IT curUnmatchedRow = unmatchedRow.getnnz();
276 IT newlyMatched = 1; // ensure the first pass of the while loop
277 int iteration = 0;
278
279 std::vector<std::vector<double> > timing;
280
281#ifdef DETAIL_STATS
282 if(myrank == 0)
283 {
284 cout << "=======================================================\n";
285 cout << "@@@@@@ Number of processes: " << nprocs << endl;
286 cout << "=======================================================\n";
287 cout << "It | UMRow | UMCol | newlyMatched | Time "<< endl;
288 cout << "=======================================================\n";
289 }
290#endif
291 MPI_Barrier(MPI_COMM_WORLD);
292
293
294 while(curUnmatchedCol !=0 && curUnmatchedRow!=0 && newlyMatched != 0 )
295 {
296 // anything is fine in the second argument
297 unmatchedCol.ApplyInd([](VertexType vtx, IT idx){return VertexType(idx,idx);});
298
299
300 // ======================== step1: One step of BFS =========================
301 std::vector<double> times;
302 double t1 = MPI_Wtime();
303
304 SpMV<WeightMaxMLSR<double, VertexType>>(A, unmatchedCol, fringeRow, false, SPA);
305
306 // Remove matched row vertices
307 fringeRow = EWiseApply<VertexType>(fringeRow, mateRow2Col,
308 [](VertexType vtx, IT mate){return vtx;},
309 [](VertexType vtx, IT mate){return mate==-1;},
310 false, VertexType());
311
312 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
313 // ===========================================================================
314
315
316 // ======================== step2: Update matching =========================
317
318 fringeRow2 = EWiseApply<IT>(fringeRow, mateRow2Col,
319 [](VertexType vtx, IT mate){return vtx.parent;},
320 [](VertexType vtx, IT mate){return true;},
321 false, VertexType());
322
323 FullyDistSpVec<IT, IT> newMatchedCols = fringeRow2.Invert(A.getncol());
324 FullyDistSpVec<IT, IT> newMatchedRows = newMatchedCols.Invert(A.getnrow());
325 mateCol2Row.Set(newMatchedCols);
326 mateRow2Col.Set(newMatchedRows);
327 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
328 // ===========================================================================
329
330
331 // =============== step3: Update unmatched columns and rows =================
332
333 unmatchedRow.Select(mateRow2Col, [](IT mate){return mate==-1;});
334 unmatchedCol.Select(mateCol2Row, [](IT mate){return mate==-1;});
335 if(myrank == 0){times.push_back(MPI_Wtime()-t1); t1 = MPI_Wtime();}
336 // ===========================================================================
337
338
339 ++iteration;
340 newlyMatched = newMatchedCols.getnnz();
341 times.push_back(std::accumulate(times.begin(), times.end(), 0.0));
342 timing.push_back(times);
343#ifdef DETAIL_STATS
344 if(myrank == 0)
345 {
346 printf("%3d %10lld %10lld %10lld %18lf\n", iteration , curUnmatchedRow, curUnmatchedCol, newlyMatched, times.back());
347 }
348#endif
349 curUnmatchedCol = unmatchedCol.getnnz();
350 curUnmatchedRow = unmatchedRow.getnnz();
351 MPI_Barrier(MPI_COMM_WORLD);
352
353 }
354
355 IT cardinality = mateRow2Col.Count([](IT mate){return mate!=-1;});
356 std::vector<double> totalTimes(timing[0].size(),0);
357 for(int i=0; i<timing.size(); i++)
358 {
359 for(int j=0; j<timing[i].size(); j++)
360 {
361 totalTimes[j] += timing[i][j];
362 }
363 }
364
365
366 if(myrank == 0)
367 {
368#ifdef DETAIL_STATS
369 cout << "==========================================================\n";
370 cout << "\n================individual timings =======================\n";
371 cout << " SpMV Update-Match Update-UMC Total "<< endl;
372 cout << "==========================================================\n";
373 for(int i=0; i<timing.size(); i++)
374 {
375 for(int j=0; j<timing[i].size(); j++)
376 {
377 printf("%12.5lf ", timing[i][j]);
378 }
379 cout << endl;
380 }
381
382 cout << "-------------------------------------------------------\n";
383 for(int i=0; i<totalTimes.size(); i++)
384 printf("%12.5lf ", totalTimes[i]);
385 cout << endl;
386#endif
387#ifdef TIMING
388 std::cout << "****** maximal matching runtime ********\n";
389 std::cout << "Unmatched-Rows Cardinality Total Time***\n";
390 printf("%lld %lld %lf\n", curUnmatchedRow, cardinality, totalTimes.back());
391 std::cout << "-------------------------------------------------------\n\n";
392#endif
393 }
394 //isMatching(mateCol2Row, mateRow2Col);
395}
396
397
398
399
400template <class Par_DCSC_Bool, class IT, class NT>
401bool isMaximalmatching(Par_DCSC_Bool & A, FullyDistVec<IT,NT> & mateRow2Col, FullyDistVec<IT,NT> & mateCol2Row)
402{
403 typedef VertexTypeML < IT, IT> VertexType;
404 int myrank;
405 MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
406 FullyDistSpVec<IT, IT> fringeRow(A.getcommgrid(), A.getnrow());
407 FullyDistSpVec<IT, IT> fringeCol(A.getcommgrid(), A.getncol());
408 FullyDistSpVec<IT, IT> unmatchedRow(mateRow2Col, [](IT mate){return mate==-1;});
409 FullyDistSpVec<IT, IT> unmatchedCol(mateCol2Row, [](IT mate){return mate==-1;});
410 unmatchedRow.setNumToInd();
411 unmatchedCol.setNumToInd();
412
413
414 SpMV<Select2ndMinSR<bool, VertexType>>(A, unmatchedCol, fringeRow, false);
415 fringeRow = EWiseMult(fringeRow, mateRow2Col, true, (IT) -1);
416 if(fringeRow.getnnz() != 0)
417 {
418 if(myrank == 0)
419 std::cout << "Not maximal matching!!\n";
420 return false;
421 }
422
423 Par_DCSC_Bool tA = A;
424 tA.Transpose();
425 SpMV<Select2ndMinSR<bool, VertexType>>(tA, unmatchedRow, fringeCol, false);
426 fringeCol = EWiseMult(fringeCol, mateCol2Row, true, (IT) -1);
427 if(fringeCol.getnnz() != 0)
428 {
429 if(myrank == 0)
430 std::cout << "Not maximal matching**!!\n";
431 return false;
432 }
433 return true;
434}
435
436}
437
438#endif
439
MTRand GlobalMT(123)
int64_t IT
double rand()
int size
Definition: common.h:20
int nprocs
Definition: comms.cpp:55
#define GREEDY
#define KARP_SIPSER
#define DMD
Definition: CCGrid.h:4
void MaximalMatching(Par_DCSC_Bool &A, Par_DCSC_Bool &AT, FullyDistVec< IT, IT > &mateRow2Col, FullyDistVec< IT, IT > &mateCol2Row, FullyDistVec< IT, IT > &degColRecv, int type, bool rand=true)
bool isMaximalmatching(Par_DCSC_Bool &A, FullyDistVec< IT, NT > &mateRow2Col, FullyDistVec< IT, NT > &mateCol2Row)
FullyDistSpVec< IT, VT > SpMV(const SpParMat< IT, bool, UDER > &A, const FullyDistSpVec< IT, VT > &x, OptBuf< int32_t, VT > &optbuf)
Definition: BFSFriends.h:328
void WeightedGreedy(Par_MAT_Double &A, FullyDistVec< IT, IT > &mateRow2Col, FullyDistVec< IT, IT > &mateCol2Row, FullyDistVec< IT, IT > &degCol)
Dcsc< IU, typename promote_trait< NU1, NU2 >::T_promote > EWiseMult(const Dcsc< IU, NU1 > &A, const Dcsc< IU, NU2 > *B, bool exclude)
Definition: Friends.h:834
double A