EpetraExt Package Browser (Single Doxygen Collection) Development
example/MapColoring/cxx_main.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 
00042 // CrsMatrix_MapColoring Test routine
00043 #include <Epetra_ConfigDefs.h>
00044 #include "EpetraExt_Version.h"
00045 
00046 #ifdef EPETRA_MPI
00047 #include "Epetra_MpiComm.h"
00048 #include <mpi.h>
00049 #endif
00050 
00051 #include "Epetra_SerialComm.h"
00052 #include "Epetra_Time.h"
00053 #include "Epetra_Map.h"
00054 #include "Epetra_CrsGraph.h"
00055 #include "Epetra_CrsMatrix.h"
00056 #include "Epetra_IntVector.h"
00057 #include "Epetra_MapColoring.h"
00058 #include "EpetraExt_MapColoring.h"
00059 #include "EpetraExt_MapColoringIndex.h"
00060 #include "EpetraExt_BlockMapIn.h"
00061 #include "EpetraExt_CrsMatrixIn.h"
00062 #include "../../test/epetra_test_err.h"
00063 
00064 #include <vector>
00065 
00066 //prototype
00067 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph *A, bool verbose);
00068 
00069 int main(int argc, char *argv[]) {
00070 
00071 
00072 #ifdef EPETRA_MPI
00073   // Initialize MPI
00074   MPI_Init( &argc, &argv );
00075 #endif
00076 
00077   bool verbose = false;
00078 
00079   if( argc < 2 || argc > 3 )
00080   {
00081     cout << "Usage: " << argv[0] << " [-v] base_name \n"
00082          << "\t{ Files: base_name_matrix contains matrix-market data and \n"
00083          << "\t  base_name_map contains the needed map.\n" << endl;
00084     exit(1);
00085   }
00086 
00087   int loc = 1;
00088   // Check if we should print results to standard out
00089   if(argv[loc][0]=='-' && argv[loc][1]=='v')
00090   { verbose = true; ++loc; }
00091 
00092 #ifdef EPETRA_MPI
00093   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00094 #else
00095   Epetra_SerialComm Comm;
00096 #endif
00097 
00098   int MyPID = Comm.MyPID();
00099 
00100   bool verbose1 = false;
00101   if(verbose) verbose1 = (MyPID==0);
00102 
00103   if(verbose1)
00104     cout << EpetraExt::EpetraExt_Version() << endl << endl;
00105 
00106   Comm.Barrier();
00107 
00108   string filename1(argv[loc++]);
00109   string filename2 = filename1 + "_map";
00110   filename1 += "_matrix";
00111 
00112   if(verbose1) cout << "Reading Epetra_BlockMap from file : " << filename2;
00113   Epetra_BlockMap* BlkMap(0);
00114   EpetraExt::MatrixMarketFileToBlockMap(filename2.c_str(), Comm, BlkMap);
00115   if(verbose1) cout << " Done." << endl;
00116 
00117   if(verbose1) cout << "Converting Epetra_BlockMap to Epetra_Map ... ";
00118   Epetra_Map* Map = new Epetra_Map( BlkMap->NumGlobalElements(),
00119                                     BlkMap->NumMyElements(),
00120                                     BlkMap->MyGlobalElements(),
00121                                     BlkMap->IndexBase(),
00122                                     BlkMap->Comm());
00123   if(verbose1) cout << " Done." << endl;
00124 
00125   if(verbose1) cout << "Reading Epetra_CrsMatrix from file : " << filename1;
00126   Epetra_CrsMatrix* A_Matrix(0);
00127   EpetraExt::MatrixMarketFileToCrsMatrix( filename1.c_str(), *Map, A_Matrix);
00128   if(verbose1) cout << " Done." << endl;
00129 
00130   A_Matrix->FillComplete();
00131 
00132   if(verbose) cout << Comm << endl << flush;
00133   Comm.Barrier();
00134 
00135   // Obtain the Epetra Compressed Row Sparse Graph
00136   const Epetra_CrsGraph & constA = A_Matrix->Graph();
00137   Epetra_CrsGraph & A = const_cast<Epetra_CrsGraph&>(constA);
00138 
00139   // Finish up graph construction
00140   assert(A.FillComplete() == 0);
00141 
00142   EpetraExt::CrsGraph_MapColoring
00143     Greedy0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00144                     0, false, verbose );
00145   Epetra_MapColoring & Greedy0ColorMap = Greedy0MapColoringTransform( A );
00146   printColoring(Greedy0ColorMap, &A,verbose);
00147 
00148   EpetraExt::CrsGraph_MapColoring
00149     Greedy1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00150                     1, false, verbose );
00151   Epetra_MapColoring & Greedy1ColorMap = Greedy1MapColoringTransform( A );
00152   printColoring(Greedy1ColorMap, &A,verbose);
00153 
00154   EpetraExt::CrsGraph_MapColoring
00155     Greedy2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00156                     2, false, verbose );
00157   Epetra_MapColoring & Greedy2ColorMap = Greedy2MapColoringTransform( A );
00158   printColoring(Greedy2ColorMap, &A,verbose);
00159 
00160   EpetraExt::CrsGraph_MapColoring
00161     Lubi0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00162                    0, false, verbose );
00163   Epetra_MapColoring & Lubi0ColorMap = Lubi0MapColoringTransform( A );
00164   printColoring(Lubi0ColorMap, &A,verbose);
00165 
00166   EpetraExt::CrsGraph_MapColoring
00167     Lubi1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00168                    1, false, verbose );
00169   Epetra_MapColoring & Lubi1ColorMap = Lubi1MapColoringTransform( A );
00170   printColoring(Lubi1ColorMap, &A,verbose);
00171 
00172   EpetraExt::CrsGraph_MapColoring
00173     Lubi2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00174                    2, false, verbose );
00175   Epetra_MapColoring & Lubi2ColorMap = Lubi2MapColoringTransform( A );
00176   printColoring(Lubi2ColorMap, &A,verbose);
00177 
00178 #ifdef EPETRA_MPI
00179   if( verbose ) cout << "Parallel Map Coloring 1!\n";
00180   EpetraExt::CrsGraph_MapColoring
00181     Parallel1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::PSEUDO_PARALLEL,
00182                        0, false, verbose );
00183   Epetra_MapColoring & Parallel1ColorMap = Parallel1MapColoringTransform( A );
00184   printColoring(Parallel1ColorMap, &A,verbose);
00185 
00186   if( verbose ) cout << "Parallel Map Coloring 2!\n";
00187   EpetraExt::CrsGraph_MapColoring
00188     Parallel2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN,
00189                        0, false, verbose );
00190   Epetra_MapColoring & Parallel2ColorMap = Parallel2MapColoringTransform( A );
00191   printColoring(Parallel2ColorMap, &A,verbose);
00192 #endif
00193 
00194 #ifdef EPETRA_MPI
00195   MPI_Finalize();
00196 #endif
00197 
00198   return 0;
00199 }
00200 
00201 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph * A, bool verbose) {
00202 
00203   int NumColors = ColorMap.NumColors();
00204   int * ListOfColors = ColorMap.ListOfColors();
00205 
00206   EpetraExt::CrsGraph_MapColoringIndex MapColoringIndexTransform( ColorMap );
00207   vector<Epetra_IntVector> & ColIndices = MapColoringIndexTransform( *A );
00208 
00209   if( verbose )
00210   {
00211 
00212     cout << endl;
00213     cout << "***************************************\n";
00214     cout << "Column Indexing by Color:\n";
00215     cout << "***************************************\n";
00216     cout << endl;
00217     for( int i = 0; i < NumColors; ++i )
00218     {
00219       cout << "COLOR: " << ListOfColors[i] << endl;
00220       cout << ColIndices[i];
00221     }
00222     cout << endl;
00223   }
00224 
00225   return;
00226 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines