Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraMatcher.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) Sandia Corporation
6 //
7 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 //license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 //************************************************************************
38 //@HEADER
39
40
41 #include"Isorropia_EpetraMatcher.hpp"
42
43 namespace Isorropia {
44 #ifdef HAVE_EPETRA
45 namespace Epetra {
46
47 //#define ISORROPIA_MATCHING_STATS
48
49 #ifdef ISORROPIA_MATCHING_STATS
50 int minL,maxL;
51 std::vector<int> med;
52 #endif
53
54 Matcher::Matcher(const Epetra_CrsMatrix * matrixPtr,const Teuchos::ParameterList& paramlist)
55 {
56 //Matcher(Teuchos::RCP<const Epetra_CrsMatrix>(matrixPtr,false), paramlist);
57
58 int rc=0,i;
59 A_=matrixPtr;
60 rc=matrixPtr->ExtractCrsDataPointers(CRS_pointers_,CRS_indices_,CRS_vals_);
61
62 if(rc!=0)
63 std::cout<<"Input Processing Failed"<<std::endl;
64
65 U_=matrixPtr->NumGlobalRows();
66 V_=matrixPtr->NumGlobalCols();
67 E_=matrixPtr->NumGlobalNonzeros();
68
69 choice_=1;
70 std::string str(paramlist.get<std::string>("Matching Algorithm"));
71 if(str.compare("PHKDW")==0)
72 choice_=1;
73 else
74 if(str.compare("PHK")==0)
75 choice_=2;
76 else
77 if(str.compare("PDFS")==0)
78 choice_=3;
79 else
80 if(str.compare("PPF")==0)
81 choice_=4;
82
83 #ifdef ISORROPIA_MATCHING_STATS
84 std::cout<<"(U,V,E):"<<U_<<","<<V_<<","<<E_<<std::endl;
85 #endif
86
87 finish_=false;
88 avgDegU_=E_/U_+1;
89 mateU_=new int[U_];
90 mateV_=new int[V_];
91
92 if(choice_==1 || choice_==2)
93 {
94 LU_=new int[U_];
95 LV_=new int[V_];
96 Queue_=new int[U_];
97 }
98 if(choice_==1 || choice_==4)
99 lookahead_=new int[U_];
100
101 if(choice_==3 || choice_==4)
102 unmatchedU_=new int[U_];
103
104 for(i=0;i<U_;i++)
105 {
106 mateU_[i]=-1;
107 if(choice_==1 || choice_==4)
108 lookahead_[i]=CRS_pointers_[i];
109 if(choice_==1 || choice_==2)
110 {
111 LU_[i]=-1;
112 Queue_[i]=-1;
113 }
114 if(choice_==3||choice_==4)
115 unmatchedU_[i]=i;
116 }
117
118 #ifdef ISORROPIA_HAVE_OMP
119 scannedV_=new omp_lock_t[V_];
120 #endif
121 parent_=new int[V_];
122
123 for(i=0;i<V_;i++)
124 {
125 #ifdef ISORROPIA_HAVE_OMP
126 omp_init_lock(&scannedV_[i]);
127 #endif
128 mateV_[i]=-1;
129 parent_[i]=-1;
130 if(choice_==1 || choice_==2)
131 LV_[i]=-1;
132 }
133
134 #ifdef ISORROPIA_HAVE_OMP
135 #pragma omp parallel
136 numThread_=omp_get_num_threads();
137 #endif
138 //std::cout<<"finished:"<<std::endl;
139 }
140
141 Matcher::Matcher(Teuchos::RCP<const Epetra_CrsMatrix> matrixPtr,const Teuchos::ParameterList& paramlist)
142 {
143
144 //A_=matrixPtr.getRawPtr();
145 //Matcher(A_,paramlist);
146
147 int rc=0,i;
148 rc=matrixPtr->ExtractCrsDataPointers(CRS_pointers_,CRS_indices_,CRS_vals_);
149 if(rc!=0)
150 std::cout<<"Input Processing Failed"<<std::endl;
151
152 U_=matrixPtr->NumGlobalRows();
153 V_=matrixPtr->NumGlobalCols();
154 E_=matrixPtr->NumGlobalNonzeros();
155
156 choice_=1;
157 std::string str(paramlist.get<std::string>("Matching Algorithm"));
158 if(str.compare("PHKDW")==0)
159 choice_=1;
160 else
161 if(str.compare("PHK")==0)
162 choice_=2;
163 else
164 if(str.compare("PDFS")==0)
165 choice_=3;
166 else
167 if(str.compare("PPF")==0)
168 choice_=4;
169
170 #ifdef ISORROPIA_MATCHING_STATS
171 std::cout<<"(U,V,E):"<<U_<<","<<V_<<","<<E_<<std::endl;
172 #endif
173
174 finish_=false;
175 avgDegU_=E_/U_+1;
176 mateU_=new int[U_];
177 mateV_=new int[V_];
178
179 if(choice_==1 || choice_==2)
180 {
181 LU_=new int[U_];
182 LV_=new int[V_];
183 Queue_=new int[U_];
184 }
185 if(choice_==1 || choice_==4)
186 lookahead_=new int[U_];
187
188 if(choice_==3 || choice_==4)
189 unmatchedU_=new int[U_];
190
191 for(i=0;i<U_;i++)
192 {
193 mateU_[i]=-1;
194 if(choice_==1 || choice_==4)
195 lookahead_[i]=CRS_pointers_[i];
196 if(choice_==1 || choice_==2)
197 {
198 LU_[i]=-1;
199 Queue_[i]=-1;
200 }
201 if(choice_==3||choice_==4)
202 unmatchedU_[i]=i;
203 }
204
205 #ifdef ISORROPIA_HAVE_OMP
206 scannedV_=new omp_lock_t[V_];
207 #endif
208 parent_=new int[V_];
209
210 for(i=0;i<V_;i++)
211 {
212 #ifdef ISORROPIA_HAVE_OMP
213 omp_init_lock(&scannedV_[i]);
214 #endif
215 mateV_[i]=-1;
216 parent_[i]=-1;
217 if(choice_==1 || choice_==2)
218 LV_[i]=-1;
219 }
220
221 #ifdef ISORROPIA_HAVE_OMP
222 #pragma omp parallel
223 numThread_=omp_get_num_threads();
224 #endif
225
226 std::cout<<"finished:"<<std::endl;
227 }
228
229 Matcher::Matcher(const Epetra_CrsGraph * graphPtr,const Teuchos::ParameterList& paramlist)
230 {
231 U_=graphPtr->NumMyRows();
232 V_=graphPtr->NumMyCols();
233 E_=graphPtr->NumMyNonzeros();
234 int i,j,num,sum, *ind;
235 CRS_pointers_=new int[U_+1];
236 CRS_indices_=new int[E_];
237 sum=0;
238 for(i=0;i<U_;i++)
239 {
240 graphPtr->ExtractMyRowView(i,num,ind);
241 CRS_pointers_[i]=sum;
242 for(j=0;j<num;j++)
243 CRS_indices_[sum+j]=ind[j];
244 sum=sum+num;
245 }
246 CRS_pointers_[U_]=sum;
247
248 choice_=1;
249 std::string str(paramlist.get<std::string>("Matching Algorithm"));
250 if(str.compare("PHKDW")==0)
251 choice_=1;
252 else
253 if(str.compare("PHK")==0)
254 choice_=2;
255 else
256 if(str.compare("PDFS")==0)
257 choice_=3;
258 else
259 if(str.compare("PPF")==0)
260 choice_=4;
261
262 #ifdef ISORROPIA_MATCHING_STATS
263 std::cout<<"(U,V,E):"<<U_<<","<<V_<<","<<E_<<std::endl;
264 #endif
265
266 finish_=false;
267 avgDegU_=E_/U_+1;
268 mateU_=new int[U_];
269 mateV_=new int[V_];
270
271 if(choice_==1 || choice_==2)
272 {
273 LU_=new int[U_];
274 LV_=new int[V_];
275 Queue_=new int[U_];
276 }
277 if(choice_==1 || choice_==4)
278 lookahead_=new int[U_];
279
280 if(choice_==3 || choice_==4)
281 unmatchedU_=new int[U_];
282
283 for(i=0;i<U_;i++)
284 {
285 mateU_[i]=-1;
286 if(choice_==1 || choice_==4)
287 lookahead_[i]=CRS_pointers_[i];
288 if(choice_==1 || choice_==2)
289 {
290 LU_[i]=-1;
291 Queue_[i]=-1;
292 }
293 if(choice_==3||choice_==4)
294 unmatchedU_[i]=i;
295 }
296
297 #ifdef ISORROPIA_HAVE_OMP
298 scannedV_=new omp_lock_t[V_];
299 #endif
300 parent_=new int[V_];
301
302 for(i=0;i<V_;i++)
303 {
304 #ifdef ISORROPIA_HAVE_OMP
305 omp_init_lock(&scannedV_[i]);
306 #endif
307 mateV_[i]=-1;
308 parent_[i]=-1;
309 if(choice_==1 || choice_==2)
310 LV_[i]=-1;
311 }
312
313 #ifdef ISORROPIA_HAVE_OMP
314 #pragma omp parallel
315 numThread_=omp_get_num_threads();
316 #endif
317 }
318
319 Matcher::Matcher(Teuchos::RCP<const Epetra_CrsGraph> graphPtr,const Teuchos::ParameterList& paramlist)
320 {
321 U_=graphPtr->NumMyRows();
322 V_=graphPtr->NumMyCols();
323 E_=graphPtr->NumMyNonzeros();
324 int i,j,num,sum, *ind;
325 CRS_pointers_=new int[U_+1];
326 CRS_indices_=new int[E_];
327 sum=0;
328 for(i=0;i<U_;i++)
329 {
330 graphPtr->ExtractMyRowView(i,num,ind);
331 CRS_pointers_[i]=sum;
332 for(j=0;j<num;j++)
333 CRS_indices_[sum+j]=ind[j];
334 sum=sum+num;
335 }
336 CRS_pointers_[U_]=sum;
337
338 choice_=1;
339 std::string str(paramlist.get<std::string>("Matching Algorithm"));
340 if(str.compare("PHKDW")==0)
341 choice_=1;
342 else
343 if(str.compare("PHK")==0)
344 choice_=2;
345 else
346 if(str.compare("PDFS")==0)
347 choice_=3;
348 else
349 if(str.compare("PPF")==0)
350 choice_=4;
351
352 #ifdef ISORROPIA_MATCHING_STATS
353 std::cout<<"(U,V,E):"<<U_<<","<<V_<<","<<E_<<std::endl;
354 #endif
355
356 finish_=false;
357 avgDegU_=E_/U_+1;
358 mateU_=new int[U_];
359 mateV_=new int[V_];
360
361 if(choice_==1 || choice_==2)
362 {
363 LU_=new int[U_];
364 LV_=new int[V_];
365 Queue_=new int[U_];
366 }
367 if(choice_==1 || choice_==4)
368 lookahead_=new int[U_];
369
370 if(choice_==3 || choice_==4)
371 unmatchedU_=new int[U_];
372
373 for(i=0;i<U_;i++)
374 {
375 mateU_[i]=-1;
376 if(choice_==1 || choice_==4)
377 lookahead_[i]=CRS_pointers_[i];
378 if(choice_==1 || choice_==2)
379 {
380 LU_[i]=-1;
381 Queue_[i]=-1;
382 }
383 if(choice_==3||choice_==4)
384 unmatchedU_[i]=i;
385 }
386
387 #ifdef ISORROPIA_HAVE_OMP
388 scannedV_=new omp_lock_t[V_];
389 #endif
390 parent_=new int[V_];
391
392 for(i=0;i<V_;i++)
393 {
394 #ifdef ISORROPIA_HAVE_OMP
395 omp_init_lock(&scannedV_[i]);
396 #endif
397 mateV_[i]=-1;
398 parent_[i]=-1;
399 if(choice_==1 || choice_==2)
400 LV_[i]=-1;
401 }
402
403 #ifdef ISORROPIA_HAVE_OMP
404 #pragma omp parallel
405 numThread_=omp_get_num_threads();
406 #endif
407 }
408
409
410 Matcher::~Matcher()
411 {
412 delete [] mateU_;
413 delete [] mateV_;
414
415 if(choice_==1 || choice_==2)
416 {
417 delete [] LU_;
418 delete [] LV_;
419 delete [] Queue_;
420 }
421 if(choice_==1 ||choice_==4)
422 delete [] lookahead_;
423 if(choice_==3 ||choice_==4)
424 delete [] unmatchedU_;
425
426 if (CRS_indices_) { delete [] CRS_indices_; CRS_indices_ = NULL; }
427 if (CRS_pointers_) { delete [] CRS_pointers_; CRS_pointers_ = NULL; }
428 if (parent_) { delete [] parent_; parent_ = NULL; }
429
430 #ifdef ISORROPIA_HAVE_OMP
431 for(int i=0;i<V_;i++)
432 omp_destroy_lock(&scannedV_[i]);
433 #endif
434
435 }
436
437 void Matcher::getMatchedColumnsForRowsCopy(int len, int& size, int* array) const
438 {
439 const int *ptr=&mateU_[0];
440 size=MIN(matched_,len);
441 memcpy (array, ptr, size * sizeof(int));
442 }
443
444 void Matcher::getMatchedRowsForColumnsCopy(int len, int& size, int* array) const
445 {
446 const int *ptr=&mateV_[0];
447 size=MIN(matched_,len);
448 memcpy (array, ptr, size * sizeof(int));
449 }
450
451 int Matcher::getNumberOfMatchedVertices()
452 {
453 return matched_;
454 }
455
456 Teuchos::RCP<Epetra_CrsMatrix> Matcher::applyRowPermutation()
457 {
458 //int nmatch = matched_; // suppress compiler warning
459 const int *mrows = &mateU_[0];
460 //const int *mcols = &mateV_[0]; // suppress compiler warning
461
462 // Create a new matrix with row permutation
463 int max_entries = A_->MaxNumEntries();
464 // TODO: This row map is ok, but not optimal. Should we create a new row
465 // map with mrows[i] ??
466 Teuchos::RCP<Epetra_CrsMatrix> perm_matrix = Teuchos::RCP<Epetra_CrsMatrix>
467 (new Epetra_CrsMatrix(Copy, A_->RowMap(), max_entries));
468 int n = A_->NumGlobalRows();
469
470 double *values = new double[max_entries];
471 int *indices = new int[max_entries];
472 int num_entries;
473 for (int i = 0; i < n ; i++)
474 {
475 // All in serial Comm so 0..n is fine
476 A_->ExtractGlobalRowCopy(i, max_entries, num_entries, values,
477 indices);
478 perm_matrix->InsertGlobalValues(mrows[i], num_entries,
479 values, indices);
480 }
481 perm_matrix->FillComplete();
482
483 delete[] values;
484 delete[] indices;
485 return (perm_matrix);
486 }
487
488 Teuchos::RCP<Epetra_CrsMatrix> Matcher::applyColumnPermutation()
489 {
490 //int nmatch = matched_; // suppress compiler warning
491 //const int *mrows = &mateU_[0]; // suppress compiler warning
492 const int *mcols = &mateV_[0];
493
494 // Create a new matrix with column permutation
495 int max_entries = A_->MaxNumEntries();
496 Teuchos::RCP<Epetra_CrsMatrix> perm_matrix = Teuchos::RCP<Epetra_CrsMatrix>
497 (new Epetra_CrsMatrix(Copy, A_->RowMap(), max_entries));
498 int n = A_->NumGlobalRows();
499
500 double *values = new double[max_entries];
501 int *indices = new int[max_entries];
502 int num_entries;
503 for (int i = 0; i < n ; i++)
504 {
505 // All in serial Comm so 0..n is fine
506 A_->ExtractGlobalRowCopy(i, max_entries, num_entries, values,
507 indices);
508 for (int j = 0; j < num_entries; j++) indices[j]=mcols[indices[j]];
509 perm_matrix->InsertGlobalValues(i, num_entries, values, indices);
510 }
511 perm_matrix->FillComplete();
512 delete[] values;
513 delete[] indices;
514 return (perm_matrix);
515 }
516
517 /* // We don't have any use cases for these two functions now. We might use the
518 // getPermutedRowMap()
519 // when creating the matrix for applyRowPermutation(). Not exposing them for
520 // now.
521 Epetra_Map* Matcher::getPermutedRowMap()
522 {
523 int *ptr=new int[U_];
524 complete_nonperfect_permutation();
525 for(int i=0;i<U_;i++)
526 ptr[i]=(A_->RowMap()).GID(mateU_[i]);
527
528 if(A_->Comm().NumProc()==1)
529 {
530 Epetra_Map* map=new Epetra_Map(-1,U_,ptr,0,A_->Comm());
531 //std::cout<<"Map created..!!"<<std::endl;
532 return map;
533 }
534 else
535 {
536 std::cout<<"Original matrix is in distributed memory"<<std::endl;
537 return NULL;
538 }
539 }
540 Epetra_Map* Matcher::getPermutedColumnMap()
541 {
542 int *ptr=new int[V_];
543 complete_nonperfect_permutation();
544 for(int i=0;i<V_;i++)
545 ptr[i]=(A_->ColMap()).GID(mateV_[i]);
546
547 if(A_->Comm().NumProc()==1)
548 {
549 Epetra_Map* map=new Epetra_Map(-1,V_,ptr,0,A_->Comm());
550 return map;
551 }
552 else
553 {
554 std::cout<<"Original matrix is in distributed memory"<<std::endl;
555 return NULL;
556 }
557 }*/
558
559 void Matcher::complete_nonperfect_permutation()
560 {
561 int i,j,rowfill,colfill,flag;
562
563 std::vector<int> temp(U_,-1);
564
565 #ifdef ISORROPIA_HAVE_OMP
566 #pragma omp parallel for
567 #endif
568 for(i=0;i<U_;i++)
569 temp[i]=mateU_[i];
570
571 rowfill=U_-matched_;
572
573 j=0;
574 while(rowfill>0)
575 {
576 for(i=0;i<U_;i++)
577 {
578 if(mateU_[i]==-1)
579 {
580 flag=0;
581 for(;j<V_;j++)
582 {
583 if(mateV_[j]==-1)
584 {
585 mateU_[i]=j;
586 j++;
587 rowfill--;
588 flag=1;
589 break;
590 }
591 }
592 if(flag==0)
593 {
594 mateU_[i]=j;
595 j++;
596 rowfill--;
597 }
598 }
599 }
600 }
601
602 colfill=V_-matched_;
603
604 j=0;
605 while(colfill>0)
606 {
607 for(i=0;i<V_;i++)
608 {
609 if(mateV_[i]==-1)
610 {
611 flag=0;
612 for(;j<U_;j++)
613 {
614 if(temp[j]==-1)
615 {
616 mateV_[i]=j;
617 j++;
618 colfill--;
619 flag=1;
620 break;
621 }
622 }
623 if(flag==0)
624 {
625 mateV_[i]=j;
626 j++;
627 colfill--;
628 }
629 }
630 }
631 }
632 }
633
634 void Matcher::delete_matched_v()
635 {
636 // This function only applicable to HK and HKDW.
637 // It removes the matched vertices in the last layer of the layered graph.
638
639 #ifdef ISORROPIA_HAVE_OMP
640 int i,j;
641 #pragma omp parallel for private(j)
642 for(i=Qst_;i<Qend_;i++)
643 {
644 j=Queue_[i];
645 if(LV_[j]==k_star_ && mateV_[j]!=-1) // k_star is the last layer and
646 omp_test_lock(&scannedV_[j]); // mateV[j]!=-1 means matched..!
647 }
648 #endif
649 }
650
651 int Matcher::augment_matching(int tv)
652 {
653 //This function increases the matching size by one with the help of a
654 //augmenting path. The input is an integer which is the id of the last
655 //columns vertex of the augmenting path. We trace the whole path by
656 //backtraking using parent array. while tracing back we flip the unmatched
657 //edges to matched edges.
658 int u,v,t,lnt=1;
659 v=tv;
660 while(true)
661 {
662 u=parent_[v];
663 t=mateU_[u];
664 mateV_[v]=u;
665 mateU_[u]=v;
666 if(t==-1)
667 break;
668 lnt+=2;
669 v=t;
670 }
671
672 return lnt;
673 }
674
675 int Matcher::construct_layered_graph()
676 {
677 //This function is used by the HK and HKDW. This is the BFS phase of HK/HKDW
678 //where we implicitely build the layered sub graph out of the original
679 //graph.
680
681 int k,i,j,t,tst,tend,fflag,s,tid,mem,pqind;
682 Qst_=Qend_=tst=tend=k=fflag=0;
683 #ifdef ISORROPIA_MATCHING_STATS
684 maxL=0;
685 minL=U_+V_+1;
686 #endif
687 int* Qsize=new int[numThread_]; // holds current localQ size for each thread
688 int** localQ=new int*[numThread_]; // localQ for each thread
689 int* startInd=new int[numThread_]; // the tail of the localQ for each thread
690
691
692 #ifdef ISORROPIA_HAVE_OMP
693 #pragma omp parallel for
694 #endif
695 for(i=0;i<V_;i++)
696 {
697 #ifdef ISORROPIA_HAVE_OMP
698 omp_unset_lock(&scannedV_[i]);
699 #endif
700 LV_[i]=-1;
701 }
702
703 for(i=0;i<U_;i++) // Collecting all remaining unmatched row vertices
704 {
705 if(mateU_[i]==-1)
706 {
707 LU_[i]=0;
708 Queue_[Qend_++]=i;
709 }
710 else
711 LU_[i]=-1;
712 }
713
714 if((Qend_-Qst_)==0)
715 {
716 finish_=true;
717 return 0;
718 }
719 else
720 BFSInd_=Qend_; // BFSInd keeps record of the vertices in Layer 0.
721
722 while(true)
723 {
724 //mem=MIN(((avgDegU_*(Qend_-Qst_))/numThread_),V_);
725 mem=V_;
726 for(i=0;i<numThread_;i++)
727 {
728 startInd[i]=0;
729 Qsize[i]=mem;
730 localQ[i]=new int[mem];
731 }
732
733 #ifdef ISORROPIA_HAVE_OMP
734 #pragma omp parallel for private(i,t,j,pqind,tid)
735 #endif
736 for(s=Qst_;s<Qend_;s++) // start of layer construction
737 {
738 #ifdef ISORROPIA_HAVE_OMP
739 tid=omp_get_thread_num();
740 #else
741 // FIXME (mfh 07 Feb 2013) When I found this code, tid was
742 // not initialized if ISORROPIA_HAVE_OMP was not defined.
743 // I'm not sure what its value should be, but it's
744 // reasonable to set it to zero (one thread, whose ID (tid
745 // == "thread ID") is zero).
746 tid = 0;
747 #endif
748 pqind=startInd[tid];
749
750 i=Queue_[s]; // starting with a unmatched row vertex
751 for(t=CRS_pointers_[i];t<CRS_pointers_[i+1];t++) //scanning adj list
752 {
753 j=CRS_indices_[t];
754 if(mateU_[i]!=j) // making sure that it is not already paired
755 {
756 if(mateV_[j]==-1) // if unmatched then aug path found
757 {
758 fflag=1;
759 LV_[j]=k+1;
760 }
761 else // else adding it to the layer graph
762 {
763 //making sure that it has not already in the prev layer
764 if(fflag==0 && (LV_[j]==-1 || LV_[j]==k+1))
765 {
766 if(LV_[j]==-1)
767 {
768 if(Qsize[tid]==pqind)
769 {
770 //localQ for this thread is overloaded so
771 //add some new space.
772 int newsize=pqind+avgDegU_;
773 Qsize[tid]=newsize;
774 int * temp=new int[newsize];
775 for(i=0;i<pqind;i++)
776 temp[i]=localQ[tid][i];
777 delete [] localQ[tid];
778 localQ[tid]=temp;
779
780 }
781
782 localQ[tid][pqind++]=mateV_[j];
783 }
784 LU_[mateV_[j]]=k+2;
785 LV_[j]=k+1;
786 }
787 }
788 }
789 }
790 #ifdef ISORROPIA_HAVE_OMP
791 #pragma omp flush
792 #endif
793 startInd[tid]=pqind;
794 }
795
796 // A layer has been constructed now collect all the vertices from localQ
797 // which is local to each thread to the global Queue.
798 Qst_=Qend_;
799 tst=Qst_;
800 for(int ii=0;ii<numThread_;ii++)
801 for(int jj=0;jj<startInd[ii];jj++)
802 Queue_[tst++]=localQ[ii][jj];
803 Qend_=tst;
804
805 for(i=0;i<numThread_;i++)
806 delete [] localQ[i];
807
808 if(fflag>0) // means we found at least one augmenting path
809 {
810 k_star_=k+1;
811 break;
812 }
813 else // otherwise construct the next layer
814 k=k+2;
815
816 if((Qend_-Qst_)==0)
817 {
818 finish_=true;
819 break;
820 }
821 }
822
823 delete [] startInd;
824 delete [] Qsize;
825 return k_star_;
826 }
827
828
829 int Matcher::recursive_path_finder(int k, int p)
830 {
831 //This function is used by HK and HKDW. This the DFS phase of the HK/HKDW
832 //where we try to find the vertex disjoint augmenting path from the just
833 //created layered graph. The input is the layer number, k and the vertex id,
834 //p. This function goes down layer by layer (by using k) recursively to find
835 //a vertex disjoint path starting from layer 0.
836 int i,ind,res=0,lock=0;
837
838 if(k>k_star_)
839 return -1;
840
841 for(i=CRS_pointers_[p];i<CRS_pointers_[p+1];i++)
842 {
843 ind=CRS_indices_[i];
844 if(LV_[ind]==k+1) //making sure that the vertex is in the next layer
845 {
846 #ifdef ISORROPIA_HAVE_OMP
847 lock=omp_test_lock(&scannedV_[ind]);
848 #endif
849 if(lock>0) // unlocked means this vertex is not part of other paths
850 {
851 parent_[ind]=p;
852 if(mateV_[ind]==-1) // we found a vertex disjoint path
853 return ind;
854 else
855 {
856 res=recursive_path_finder(k+2,mateV_[ind]);
857 if(res!=-1)
858 return res;
859 }
860 }
861 }
862 }
863
864 return -1;
865 }
866
867 int Matcher::dfs_path_finder(int u)
868 {
869 //This function is almost similar to the previous function which is to find
870 //a vertex disjoint path. It is used for the algorithm DFS, PPF and in HKDW. The
871 //difference is that this function operates on the original graph not on the
872 //layerd subgraph. It also does the incorporates the lookahead mechanism and
873 //scanning adjacency list alternately from backward and from forward in
874 //alternate iterations for PPF and HKDW.
875
876 int i,ind=-1,res=0,lock=0;
877
878 if(choice_==1 || choice_==4) // for HKDW and PPF
879 {
880 for(i=lookahead_[u];i<CRS_pointers_[u+1];i++) // the lookahead scheme
881 {
882 ind=CRS_indices_[i];
883 assert(ind>=0 && ind<V_);
884 if(mateV_[ind]==-1)
885 {
886 #ifdef ISORROPIA_HAVE_OMP
887 lock=omp_test_lock(&scannedV_[ind]);
888 #endif
889 if(lock>0)
890 {
891 parent_[ind]=u;
892 lookahead_[u]=i+1;
893 return ind;
894 }
895 }
896 }
897
898
899 if(icm_%2==1) // odd number iteration so scan the adj list forward dir
900 {
901 for(i=CRS_pointers_[u];i<CRS_pointers_[u+1];i++)
902 {
903 ind=CRS_indices_[i];
904 assert(ind>=0 && ind<V_);
905 #ifdef ISORROPIA_HAVE_OMP
906 lock=omp_test_lock(&scannedV_[ind]);
907 #endif
908 if(lock>0)
909 {
910 parent_[ind]=u;
911 res=dfs_path_finder(mateV_[ind]);
912 if(res!=-1)
913 return res;
914 }
915 }
916 }
917 else // even number iteration so scan from backward
918 {
919 for(i=CRS_pointers_[u+1]-1;i>=CRS_pointers_[u];i--)
920 {
921 ind=CRS_indices_[i];
922 assert(ind>=0 && ind<V_);
923 #ifdef ISORROPIA_HAVE_OMP
924 lock=omp_test_lock(&scannedV_[ind]);
925 #endif
926 if(lock>0)
927 {
928 parent_[ind]=u;
929 res=dfs_path_finder(mateV_[ind]);
930 if(res!=-1)
931 return res;
932 }
933 }
934 }
935 }
936 else // for DFS.. no lookahead and diferent directional scanning
937 {
938 for(i=CRS_pointers_[u];i<CRS_pointers_[u+1];i++)
939 {
940 ind=CRS_indices_[i];
941 assert (ind>=0 && ind<V_);
942 #ifdef ISORROPIA_HAVE_OMP
943 lock=omp_test_lock(&scannedV_[ind]);
944 #endif
945 if(lock>0)
946 {
947 parent_[ind]=u;
948 if(mateV_[ind]==-1)
949 return ind;
950 else
951 {
952 res=dfs_path_finder(mateV_[ind]);
953 if(res!=-1)
954 return res;
955 }
956 }
957 }
958 }
959 return -1;
960 }
961
962 int Matcher::find_set_del_M()
963 {
964 //This function starts the BFS phase for the HK and HKDW. It starts from the
965 //layer 0 vertices and tries to find a vertex disjoint path from each of
966 //these vertices.
967
968 int i,j,count=0;
969 delete_matched_v();
970
971 #ifdef ISORROPIA_HAVE_OMP
972 #pragma omp parallel for private(j)
973 #endif
974 for(i=0;i<BFSInd_;i++)
975 {
976
977 j=recursive_path_finder(0,Queue_[i]);
978 if(j!=-1)
979 {
980 int lnt=augment_matching(j);
981 #ifdef ISORROPIA_MATCHING_STATS
982 #ifdef ISORROPIA_HAVE_OMP
983 #pragma omp atomic
984 #endif
985 count++;
986 #ifdef ISORROPIA_HAVE_OMP
987 #pragma omp critical
988 #endif
989 {
990 maxL=maxL>lnt?maxL:lnt;
991 minL=minL<lnt?minL:lnt;
992 med.push_back(lnt);
993 }
994 #else
995 (void) lnt; // suppress compiler warning
996 #endif // ISORROPIA_MATCHING_STATS
997 }
998
999 }
1000 return count;
1001 }
1002
1003 int Matcher::DW_phase()
1004 {
1005 //This is the additional Duff and Wiberg phase for the HKDW. This function
1006 //does nothing but first unset the locks and then runs PPF from the
1007 //remaining unmatched row vertices after the BFS phase of HK.
1008
1009 int i,count=0;
1010
1011 #ifdef ISORROPIA_HAVE_OMP
1012 #pragma omp parallel for
1013 #endif
1014 for(i=0;i<V_;i++)
1015 {
1016 #ifdef ISORROPIA_HAVE_OMP
1017 omp_unset_lock(&scannedV_[i]); // unsetting the locks
1018 #endif
1019 }
1020
1021 #ifdef ISORROPIA_HAVE_OMP
1022 #pragma omp parallel for
1023 #endif
1024 for(i=0;i<BFSInd_;i++) // calling the PPF
1025 {
1026 int u=Queue_[i];
1027 if(mateU_[u]==-1)
1028 {
1029 int ind=dfs_path_finder(u);
1030 if(ind!=-1)
1031 {
1032 int lnt=augment_matching(ind);
1033 #ifdef ISORROPIA_MATCHING_STATS
1034 #ifdef ISORROPIA_HAVE_OMP
1035 #pragma omp atomic
1036 #endif
1037 count++;
1038
1039 #ifdef ISORROPIA_HAVE_OMP
1040 #pragma omp critical
1041 #endif
1042 {
1043 maxL=maxL>lnt?maxL:lnt;
1044 minL=minL<lnt?minL:lnt;
1045 med.push_back(lnt);
1046 }
1047 #else
1048 (void) lnt; // suppress compiler warning
1049 #endif // ISORROPIA_MATCHING_STATS
1050 }
1051 }
1052 }
1053 return count;
1054 }
1055
1056 int Matcher::dfs_augment()
1057 {
1058
1059 //This function is the starter function for PPF and DFS. It unsets the locks
1060 //the call dfs_path_finder and then call the augment_matching.
1061
1062 int i,flag=0,flag1=0,count=0,totc=0,index=U_,cur=0;
1063 icm_=0;
1064 #ifdef ISORROPIA_MATCHING_STATS
1065 std::vector<int> med;
1066 #endif
1067 while(true)
1068 {
1069 icm_++;
1070
1071 #ifdef ISORROPIA_HAVE_OMP
1072 #pragma omp parallel for
1073 #endif
1074 for(i=0;i<V_;i++)
1075 {
1076 #ifdef ISORROPIA_HAVE_OMP
1077 omp_unset_lock(&scannedV_[i]);
1078 #endif
1079 }
1080
1081 cur=0;
1082 for(i=0;i<U_;i++)
1083 if(mateU_[i]==-1)
1084 unmatchedU_[cur++]=i;
1085 index=cur;
1086 flag=flag1=count=0;
1087 #ifdef ISORROPIA_MATCHING_STATS
1088 maxL=0;
1089 minL=U_+V_+1;
1090 #endif
1091
1092 #ifdef ISORROPIA_HAVE_OMP
1093 #pragma omp parallel for
1094 #endif
1095 for(i=0;i<index;i++)
1096 {
1097
1098 #ifdef ISORROPIA_HAVE_OMP
1099 #pragma omp flush
1100 #endif
1101 flag=1;
1102 int u=unmatchedU_[i];
1103 int ind=dfs_path_finder(u);
1104 if(ind!=-1)
1105 {
1106 #ifdef ISORROPIA_HAVE_OMP
1107 #pragma omp flush
1108 #endif
1109 flag1=1;
1110 int lnt=augment_matching(ind);
1111 #ifdef ISORROPIA_MATCHING_STATS
1112 #ifdef ISORROPIA_HAVE_OMP
1113 #pragma omp atomic
1114 #endif
1115 count++;
1116 #ifdef ISORROPIA_HAVE_OMP
1117 #pragma omp critical
1118 #endif
1119 {
1120 maxL=maxL>lnt?maxL:lnt;
1121 minL=minL<lnt?minL:lnt;
1122 med.push_back(lnt);
1123 }
1124 #else
1125 (void) lnt; // suppress compiler warning
1126 #endif // ISORROPIA_MATCHING_STATS
1127 }
1128 }
1129
1130 if(flag==0 || flag1==0)
1131 break;
1132 else
1133 {
1134 #ifdef ISORROPIA_MATCHING_STATS
1135 sort(med.begin(),med.end());
1136 totc+=count;
1137 //std::cout<<"["<<icm_<<"] unmatched="<<index<<" matched="<<count<<" size="<<totc<<" minL= "<<minL<<" maxL="<<maxL<<" medL="<<med[count/2]<<std::endl;
1138 std::cout<<icm_<<","<<index<<","<<count<<","<<(index*1.0)/count<<","<<minL<<","<<med[count/2]<<","<<maxL<<std::endl;
1139 med.clear();
1140 #endif
1141
1142 cur=0;
1143 for(i=0;i<index;i++)
1144 {
1145 if(mateU_[unmatchedU_[i]]==-1)
1146 unmatchedU_[cur++]=unmatchedU_[i];
1147 }
1148 index=cur;
1149
1150 }
1151 }
1152
1153 return totc;
1154 }
1155
1156 int Matcher::SGM()
1157 {
1158 int i,j,lock,ind,count=0;
1159 #ifdef ISORROPIA_HAVE_OMP
1160 #pragma omp parallel for private(j,ind,lock)
1161 #endif
1162 for(i=0;i<U_;i++)
1163 {
1164 for(j=CRS_pointers_[i];j<CRS_pointers_[i+1];j++)
1165 {
1166 ind=CRS_indices_[j];
1167 #ifdef ISORROPIA_HAVE_OMP
1168 lock=omp_test_lock(&scannedV_[ind]);
1169 #else
1170 // mfh 07 Feb 2013: lock wasn't getting initialized if
1171 // ISORROPIA_HAVE_OMP was not defined. omp_test_lock()
1172 // returns nonzero if the thread successfully acquired the
1173 // lock. If there's only one thread, that thread will
1174 // always be successful, so the default value of lock
1175 // should be nonzero.
1176 lock = 1;
1177 #endif
1178 if(lock>0)
1179 {
1180
1181 mateU_[i]=ind;
1182 mateV_[ind]=i;
1183 #ifdef ISORROPIA_MATCHING_STATS
1184 #ifdef ISORROPIA_HAVE_OMP
1185 #pragma omp atomic
1186 count++;
1187 #endif
1188 #endif
1189 break;
1190 }
1191 }
1192 }
1193 return count;
1194 }
1195 int Matcher::match_dfs()
1196 {
1197 // Forking function for DFS based algorithm
1198 int totc=0;
1199 icm_=0;
1200
1201 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1202 double start,end;
1203 start=omp_get_wtime();
1204 #endif
1205
1206 totc=dfs_augment();
1207
1208 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1209 end=omp_get_wtime();
1210 #endif
1211
1212 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1213 std::cout<<"Total time: "<<(end-start)<<" seconds"<<" matching=";
1214 #endif
1215 return totc;
1216 }
1217
1218 int Matcher::match_hk()
1219 {
1220 // Forking function for HK based algorithm
1221 int totc=0,count=0;
1222
1223 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1224 double start,end;
1225 #endif
1226
1227 icm_=0;
1228
1229 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1230 start=omp_get_wtime();
1231 #endif
1232
1233 while(true)
1234 {
1235 icm_++;
1236 //std::cout<<"bfs"<<std::endl;
1237 construct_layered_graph();
1238 if(finish_)
1239 break;
1240 //std::cout<<"dfs"<<std::endl;
1241 count=find_set_del_M();
1242 if(choice_==1)
1243 {
1244 //std::cout<<"dw"<<std::endl;
1245 count+=DW_phase();
1246 }
1247 #ifdef ISORROPIA_MATCHING_STATS
1248 totc+=count;
1249 sort(med.begin(),med.end());
1250 //std::cout<<"["<<icm_<<"] unmatched="<<BFSInd_<<" matched="<<count<<" size="<<totc<<" minL= "<<minL<<" maxL="<<maxL<<" medL="<<med[count/2]<<std::endl;
1251 std::cout<<icm_<<","<<BFSInd_<<","<<count<<","<<(BFSInd_*1.0)/count<<","<<minL<<","<<med[count/2]<<","<<maxL<<std::endl;
1252 med.clear();
1253 #endif
1254 }
1255
1256 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1257 end=omp_get_wtime();
1258 #endif
1259
1260 #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS)
1261 std::cout<<"Total time: "<<(end-start)<<" seconds"<<" matching=";
1262 #endif
1263 return totc;
1264 }
1265
1266 int Matcher::match()
1267 {
1268 // User interface function for the matching..
1269
1270 //std::cout<<"mathc"<<std::endl;
1271 int totc=0;
1272 totc=SGM();
1273 switch(choice_)
1274 {
1275 case 1:totc+=match_hk();
1276 break;
1277 case 2:totc+=match_hk();
1278 break;
1279 case 3:totc+=match_dfs();
1280 break;
1281 case 4:totc+=match_dfs();
1282 break;
1283 default:totc+=match_hk();
1284 }
1285 #ifdef ISORROPIA_MATCHING_STATS
1286 std::cout<<totc<<std::endl;
1287 #endif
1288 matched_ = totc;
1289 /*std::cout<<"MateU: ";
1290 for(int i=0; i<5;i++)
1291 std::cout<<mateU_[i]<<",";
1292 std::cout<<std::endl;
1293
1294 for(int i=0;i<5;i++)
1295 mateV_[mateU_[i]]=i;*/
1296 //std::cout<<"MateV: ";
1297
1298 //for(int i=0; i<5;i++)
1299 //std::cout<<mateV_[i]<<",";
1300 //std::cout<<std::endl;
1301 return 0;
1302 }
1303 }// Epetra namespace
1304 #endif
1305 }// Isorropia namespace
1306