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 (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 // CrsMatrix_MapColoring Test routine
00030 #include <Epetra_ConfigDefs.h>
00031 #include "EpetraExt_Version.h"
00032 
00033 #ifdef EPETRA_MPI
00034 #include "Epetra_MpiComm.h"
00035 #include <mpi.h>
00036 #endif
00037 
00038 #include "Epetra_SerialComm.h"
00039 #include "Epetra_Time.h"
00040 #include "Epetra_Map.h"
00041 #include "Epetra_CrsGraph.h"
00042 #include "Epetra_CrsMatrix.h"
00043 #include "Epetra_IntVector.h"
00044 #include "Epetra_MapColoring.h"
00045 #include "EpetraExt_MapColoring.h"
00046 #include "EpetraExt_MapColoringIndex.h"
00047 #include "EpetraExt_BlockMapIn.h"
00048 #include "EpetraExt_CrsMatrixIn.h"
00049 #include "../../test/epetra_test_err.h"
00050 
00051 #include <vector>
00052 
00053 //prototype
00054 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph *A, bool verbose);
00055 
00056 int main(int argc, char *argv[]) {
00057 
00058 
00059 #ifdef EPETRA_MPI
00060   // Initialize MPI
00061   MPI_Init( &argc, &argv );
00062 #endif
00063 
00064   bool verbose = false;
00065 
00066   if( argc < 2 || argc > 3 )
00067   {
00068     cout << "Usage: " << argv[0] << " [-v] base_name \n"
00069          << "\t{ Files: base_name_matrix contains matrix-market data and \n"
00070          << "\t  base_name_map contains the needed map.\n" << endl;
00071     exit(1);
00072   }
00073 
00074   int loc = 1;
00075   // Check if we should print results to standard out
00076   if(argv[loc][0]=='-' && argv[loc][1]=='v')
00077   { verbose = true; ++loc; }
00078 
00079 #ifdef EPETRA_MPI
00080   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00081 #else
00082   Epetra_SerialComm Comm;
00083 #endif
00084 
00085   int MyPID = Comm.MyPID();
00086 
00087   bool verbose1 = false;
00088   if(verbose) verbose1 = (MyPID==0);
00089 
00090   if(verbose1)
00091     cout << EpetraExt::EpetraExt_Version() << endl << endl;
00092 
00093   Comm.Barrier();
00094 
00095   string filename1(argv[loc++]);
00096   string filename2 = filename1 + "_map";
00097   filename1 += "_matrix";
00098 
00099   if(verbose1) cout << "Reading Epetra_BlockMap from file : " << filename2;
00100   Epetra_BlockMap* BlkMap(0);
00101   EpetraExt::MatrixMarketFileToBlockMap(filename2.c_str(), Comm, BlkMap);
00102   if(verbose1) cout << " Done." << endl;
00103 
00104   if(verbose1) cout << "Converting Epetra_BlockMap to Epetra_Map ... ";
00105   Epetra_Map* Map = new Epetra_Map( BlkMap->NumGlobalElements(),
00106                                     BlkMap->NumMyElements(),
00107                                     BlkMap->MyGlobalElements(),
00108                                     BlkMap->IndexBase(),
00109                                     BlkMap->Comm());
00110   if(verbose1) cout << " Done." << endl;
00111 
00112   if(verbose1) cout << "Reading Epetra_CrsMatrix from file : " << filename1;
00113   Epetra_CrsMatrix* A_Matrix(0);
00114   EpetraExt::MatrixMarketFileToCrsMatrix( filename1.c_str(), *Map, A_Matrix);
00115   if(verbose1) cout << " Done." << endl;
00116 
00117   A_Matrix->FillComplete();
00118 
00119   if(verbose) cout << Comm << endl << flush;
00120   Comm.Barrier();
00121 
00122   // Obtain the Epetra Compressed Row Sparse Graph
00123   const Epetra_CrsGraph & constA = A_Matrix->Graph();
00124   Epetra_CrsGraph & A = const_cast<Epetra_CrsGraph&>(constA);
00125 
00126   // Finish up graph construction
00127   assert(A.FillComplete() == 0);
00128 
00129   EpetraExt::CrsGraph_MapColoring
00130     Greedy0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00131                     0, false, verbose );
00132   Epetra_MapColoring & Greedy0ColorMap = Greedy0MapColoringTransform( A );
00133   printColoring(Greedy0ColorMap, &A,verbose);
00134 
00135   EpetraExt::CrsGraph_MapColoring
00136     Greedy1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00137                     1, false, verbose );
00138   Epetra_MapColoring & Greedy1ColorMap = Greedy1MapColoringTransform( A );
00139   printColoring(Greedy1ColorMap, &A,verbose);
00140 
00141   EpetraExt::CrsGraph_MapColoring
00142     Greedy2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00143                     2, false, verbose );
00144   Epetra_MapColoring & Greedy2ColorMap = Greedy2MapColoringTransform( A );
00145   printColoring(Greedy2ColorMap, &A,verbose);
00146 
00147   EpetraExt::CrsGraph_MapColoring
00148     Lubi0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00149                    0, false, verbose );
00150   Epetra_MapColoring & Lubi0ColorMap = Lubi0MapColoringTransform( A );
00151   printColoring(Lubi0ColorMap, &A,verbose);
00152 
00153   EpetraExt::CrsGraph_MapColoring
00154     Lubi1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00155                    1, false, verbose );
00156   Epetra_MapColoring & Lubi1ColorMap = Lubi1MapColoringTransform( A );
00157   printColoring(Lubi1ColorMap, &A,verbose);
00158 
00159   EpetraExt::CrsGraph_MapColoring
00160     Lubi2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00161                    2, false, verbose );
00162   Epetra_MapColoring & Lubi2ColorMap = Lubi2MapColoringTransform( A );
00163   printColoring(Lubi2ColorMap, &A,verbose);
00164 
00165 #ifdef EPETRA_MPI
00166   if( verbose ) cout << "Parallel Map Coloring 1!\n";
00167   EpetraExt::CrsGraph_MapColoring
00168     Parallel1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::PSEUDO_PARALLEL,
00169                        0, false, verbose );
00170   Epetra_MapColoring & Parallel1ColorMap = Parallel1MapColoringTransform( A );
00171   printColoring(Parallel1ColorMap, &A,verbose);
00172 
00173   if( verbose ) cout << "Parallel Map Coloring 2!\n";
00174   EpetraExt::CrsGraph_MapColoring
00175     Parallel2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN,
00176                        0, false, verbose );
00177   Epetra_MapColoring & Parallel2ColorMap = Parallel2MapColoringTransform( A );
00178   printColoring(Parallel2ColorMap, &A,verbose);
00179 #endif
00180 
00181 #ifdef EPETRA_MPI
00182   MPI_Finalize();
00183 #endif
00184 
00185   return 0;
00186 }
00187 
00188 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph * A, bool verbose) {
00189 
00190   int NumColors = ColorMap.NumColors();
00191   int * ListOfColors = ColorMap.ListOfColors();
00192 
00193   EpetraExt::CrsGraph_MapColoringIndex MapColoringIndexTransform( ColorMap );
00194   vector<Epetra_IntVector> & ColIndices = MapColoringIndexTransform( *A );
00195 
00196   if( verbose )
00197   {
00198 
00199     cout << endl;
00200     cout << "***************************************\n";
00201     cout << "Column Indexing by Color:\n";
00202     cout << "***************************************\n";
00203     cout << endl;
00204     for( int i = 0; i < NumColors; ++i )
00205     {
00206       cout << "COLOR: " << ListOfColors[i] << endl;
00207       cout << ColIndices[i];
00208     }
00209     cout << endl;
00210   }
00211 
00212   return;
00213 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines