//@HEADER //************************************************************************ // // Isorropia: Partitioning and Load Balancing Package // Copyright (2006) Sandia Corporation // //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive //license for use of this work by or on behalf of the U.S. Government. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // 3. Neither the name of the Corporation nor the names of the // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //************************************************************************ //@HEADER #include"Isorropia_EpetraMatcher.hpp" namespace Isorropia { #ifdef HAVE_EPETRA namespace Epetra { //#define ISORROPIA_MATCHING_STATS #ifdef ISORROPIA_MATCHING_STATS int minL,maxL; std::vector med; #endif Matcher::Matcher(const Epetra_CrsMatrix * matrixPtr,const Teuchos::ParameterList& paramlist) { //Matcher(Teuchos::RCP(matrixPtr,false), paramlist); int rc=0,i; A_=matrixPtr; rc=matrixPtr->ExtractCrsDataPointers(CRS_pointers_,CRS_indices_,CRS_vals_); if(rc!=0) std::cout<<"Input Processing Failed"<NumGlobalRows(); V_=matrixPtr->NumGlobalCols(); E_=matrixPtr->NumGlobalNonzeros(); choice_=1; std::string str(paramlist.get("Matching Algorithm")); if(str.compare("PHKDW")==0) choice_=1; else if(str.compare("PHK")==0) choice_=2; else if(str.compare("PDFS")==0) choice_=3; else if(str.compare("PPF")==0) choice_=4; #ifdef ISORROPIA_MATCHING_STATS std::cout<<"(U,V,E):"< matrixPtr,const Teuchos::ParameterList& paramlist) { //A_=matrixPtr.getRawPtr(); //Matcher(A_,paramlist); int rc=0,i; rc=matrixPtr->ExtractCrsDataPointers(CRS_pointers_,CRS_indices_,CRS_vals_); if(rc!=0) std::cout<<"Input Processing Failed"<NumGlobalRows(); V_=matrixPtr->NumGlobalCols(); E_=matrixPtr->NumGlobalNonzeros(); choice_=1; std::string str(paramlist.get("Matching Algorithm")); if(str.compare("PHKDW")==0) choice_=1; else if(str.compare("PHK")==0) choice_=2; else if(str.compare("PDFS")==0) choice_=3; else if(str.compare("PPF")==0) choice_=4; #ifdef ISORROPIA_MATCHING_STATS std::cout<<"(U,V,E):"<NumMyRows(); V_=graphPtr->NumMyCols(); E_=graphPtr->NumMyNonzeros(); int i,j,num,sum, *ind; CRS_pointers_=new int[U_+1]; CRS_indices_=new int[E_]; sum=0; for(i=0;iExtractMyRowView(i,num,ind); CRS_pointers_[i]=sum; for(j=0;j("Matching Algorithm")); if(str.compare("PHKDW")==0) choice_=1; else if(str.compare("PHK")==0) choice_=2; else if(str.compare("PDFS")==0) choice_=3; else if(str.compare("PPF")==0) choice_=4; #ifdef ISORROPIA_MATCHING_STATS std::cout<<"(U,V,E):"< graphPtr,const Teuchos::ParameterList& paramlist) { U_=graphPtr->NumMyRows(); V_=graphPtr->NumMyCols(); E_=graphPtr->NumMyNonzeros(); int i,j,num,sum, *ind; CRS_pointers_=new int[U_+1]; CRS_indices_=new int[E_]; sum=0; for(i=0;iExtractMyRowView(i,num,ind); CRS_pointers_[i]=sum; for(j=0;j("Matching Algorithm")); if(str.compare("PHKDW")==0) choice_=1; else if(str.compare("PHK")==0) choice_=2; else if(str.compare("PDFS")==0) choice_=3; else if(str.compare("PPF")==0) choice_=4; #ifdef ISORROPIA_MATCHING_STATS std::cout<<"(U,V,E):"< Matcher::applyRowPermutation() { //int nmatch = matched_; // suppress compiler warning const int *mrows = &mateU_[0]; //const int *mcols = &mateV_[0]; // suppress compiler warning // Create a new matrix with row permutation int max_entries = A_->MaxNumEntries(); // TODO: This row map is ok, but not optimal. Should we create a new row // map with mrows[i] ?? Teuchos::RCP perm_matrix = Teuchos::RCP (new Epetra_CrsMatrix(Copy, A_->RowMap(), max_entries)); int n = A_->NumGlobalRows(); double *values = new double[max_entries]; int *indices = new int[max_entries]; int num_entries; for (int i = 0; i < n ; i++) { // All in serial Comm so 0..n is fine A_->ExtractGlobalRowCopy(i, max_entries, num_entries, values, indices); perm_matrix->InsertGlobalValues(mrows[i], num_entries, values, indices); } perm_matrix->FillComplete(); delete[] values; delete[] indices; return (perm_matrix); } Teuchos::RCP Matcher::applyColumnPermutation() { //int nmatch = matched_; // suppress compiler warning //const int *mrows = &mateU_[0]; // suppress compiler warning const int *mcols = &mateV_[0]; // Create a new matrix with column permutation int max_entries = A_->MaxNumEntries(); Teuchos::RCP perm_matrix = Teuchos::RCP (new Epetra_CrsMatrix(Copy, A_->RowMap(), max_entries)); int n = A_->NumGlobalRows(); double *values = new double[max_entries]; int *indices = new int[max_entries]; int num_entries; for (int i = 0; i < n ; i++) { // All in serial Comm so 0..n is fine A_->ExtractGlobalRowCopy(i, max_entries, num_entries, values, indices); for (int j = 0; j < num_entries; j++) indices[j]=mcols[indices[j]]; perm_matrix->InsertGlobalValues(i, num_entries, values, indices); } perm_matrix->FillComplete(); delete[] values; delete[] indices; return (perm_matrix); } /* // We don't have any use cases for these two functions now. We might use the // getPermutedRowMap() // when creating the matrix for applyRowPermutation(). Not exposing them for // now. Epetra_Map* Matcher::getPermutedRowMap() { int *ptr=new int[U_]; complete_nonperfect_permutation(); for(int i=0;iRowMap()).GID(mateU_[i]); if(A_->Comm().NumProc()==1) { Epetra_Map* map=new Epetra_Map(-1,U_,ptr,0,A_->Comm()); //std::cout<<"Map created..!!"<ColMap()).GID(mateV_[i]); if(A_->Comm().NumProc()==1) { Epetra_Map* map=new Epetra_Map(-1,V_,ptr,0,A_->Comm()); return map; } else { std::cout<<"Original matrix is in distributed memory"< temp(U_,-1); #ifdef ISORROPIA_HAVE_OMP #pragma omp parallel for #endif for(i=0;i0) { for(i=0;i0) { for(i=0;i0) // means we found at least one augmenting path { k_star_=k+1; break; } else // otherwise construct the next layer k=k+2; if((Qend_-Qst_)==0) { finish_=true; break; } } delete [] startInd; delete [] Qsize; return k_star_; } int Matcher::recursive_path_finder(int k, int p) { //This function is used by HK and HKDW. This the DFS phase of the HK/HKDW //where we try to find the vertex disjoint augmenting path from the just //created layered graph. The input is the layer number, k and the vertex id, //p. This function goes down layer by layer (by using k) recursively to find //a vertex disjoint path starting from layer 0. int i,ind,res=0,lock=0; if(k>k_star_) return -1; for(i=CRS_pointers_[p];i0) // unlocked means this vertex is not part of other paths { parent_[ind]=p; if(mateV_[ind]==-1) // we found a vertex disjoint path return ind; else { res=recursive_path_finder(k+2,mateV_[ind]); if(res!=-1) return res; } } } } return -1; } int Matcher::dfs_path_finder(int u) { //This function is almost similar to the previous function which is to find //a vertex disjoint path. It is used for the algorithm DFS, PPF and in HKDW. The //difference is that this function operates on the original graph not on the //layerd subgraph. It also does the incorporates the lookahead mechanism and //scanning adjacency list alternately from backward and from forward in //alternate iterations for PPF and HKDW. int i,ind=-1,res=0,lock=0; if(choice_==1 || choice_==4) // for HKDW and PPF { for(i=lookahead_[u];i=0 && ind0) { parent_[ind]=u; lookahead_[u]=i+1; return ind; } } } if(icm_%2==1) // odd number iteration so scan the adj list forward dir { for(i=CRS_pointers_[u];i=0 && ind0) { parent_[ind]=u; res=dfs_path_finder(mateV_[ind]); if(res!=-1) return res; } } } else // even number iteration so scan from backward { for(i=CRS_pointers_[u+1]-1;i>=CRS_pointers_[u];i--) { ind=CRS_indices_[i]; assert(ind>=0 && ind0) { parent_[ind]=u; res=dfs_path_finder(mateV_[ind]); if(res!=-1) return res; } } } } else // for DFS.. no lookahead and diferent directional scanning { for(i=CRS_pointers_[u];i=0 && ind0) { parent_[ind]=u; if(mateV_[ind]==-1) return ind; else { res=dfs_path_finder(mateV_[ind]); if(res!=-1) return res; } } } } return -1; } int Matcher::find_set_del_M() { //This function starts the BFS phase for the HK and HKDW. It starts from the //layer 0 vertices and tries to find a vertex disjoint path from each of //these vertices. int i,j,count=0; delete_matched_v(); #ifdef ISORROPIA_HAVE_OMP #pragma omp parallel for private(j) #endif for(i=0;ilnt?maxL:lnt; minL=minLlnt?maxL:lnt; minL=minL med; #endif while(true) { icm_++; #ifdef ISORROPIA_HAVE_OMP #pragma omp parallel for #endif for(i=0;ilnt?maxL:lnt; minL=minL0) { mateU_[i]=ind; mateV_[ind]=i; #ifdef ISORROPIA_MATCHING_STATS #ifdef ISORROPIA_HAVE_OMP #pragma omp atomic count++; #endif #endif break; } } } return count; } int Matcher::match_dfs() { // Forking function for DFS based algorithm int totc=0; icm_=0; #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS) double start,end; start=omp_get_wtime(); #endif totc=dfs_augment(); #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS) end=omp_get_wtime(); #endif #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS) std::cout<<"Total time: "<<(end-start)<<" seconds"<<" matching="; #endif return totc; } int Matcher::match_hk() { // Forking function for HK based algorithm int totc=0,count=0; #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS) double start,end; #endif icm_=0; #if defined(ISORROPIA_HAVE_OMP) && defined(ISORROPIA_MATCHING_STATS) start=omp_get_wtime(); #endif while(true) { icm_++; //std::cout<<"bfs"<