EpetraExt Package Browser (Single Doxygen Collection) Development
test/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_IntVector.h"
00043 #include "Epetra_MapColoring.h"
00044 #include "EpetraExt_MapColoring.h"
00045 #include "EpetraExt_MapColoringIndex.h"
00046 #include "../epetra_test_err.h"
00047 
00048 #include <vector>
00049 
00050 // prototype
00051 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph *A, bool verbose);
00052 
00053 int main(int argc, char *argv[]) {
00054 
00055 
00056 #ifdef EPETRA_MPI
00057 
00058   // Initialize MPI
00059 
00060   MPI_Init( &argc, &argv );
00061   //int size, rank; // Number of MPI processes, My process ID
00062 
00063   //MPI_Comm_size(MPI_COMM_WORLD, &size);
00064   //MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00065 
00066 #else
00067 
00068   //int size = 1; // Serial case (not using MPI)
00069   //int rank = 0;
00070 
00071 #endif
00072 
00073   bool verbose = false;
00074 
00075   int nx = 5;
00076   int ny = 5;
00077 
00078   if( argc > 1 )
00079   {
00080     if( argc > 4 )
00081     {
00082       cout << "Usage: " << argv[0] << " [-v [nx [ny]]]" << endl;
00083       exit(1);
00084     }
00085 
00086     int loc = 1;
00087     // Check if we should print results to standard out
00088     if(argv[loc][0]=='-' && argv[loc][1]=='v')
00089     { verbose = true; ++loc; }
00090 
00091     if (loc < argc) nx = atoi( argv[loc++] );
00092     if( loc < argc) ny = atoi( argv[loc] );
00093   }
00094 
00095 #ifdef EPETRA_MPI
00096   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00097 #else
00098   Epetra_SerialComm Comm;
00099 #endif
00100 
00101   int MyPID = Comm.MyPID();
00102   int NumProc = Comm.NumProc();
00103 
00104   bool verbose1 = false;
00105   if(verbose) verbose1 = (MyPID==0);
00106 
00107   if(verbose1)
00108     cout << EpetraExt::EpetraExt_Version() << endl << endl;
00109 
00110   Comm.Barrier();
00111 
00112   if(verbose) cout << Comm << endl << flush;
00113   Comm.Barrier();
00114 
00115   int NumGlobalElements = nx * ny;
00116   if( NumGlobalElements < NumProc )
00117   {
00118     cout << "NumGlobalElements = " << NumGlobalElements <<
00119             " cannot be < number of processors = " << NumProc;
00120     exit(1);
00121   } 
00122   
00123   int IndexBase = 0;
00124   Epetra_Map Map( NumGlobalElements, IndexBase, Comm );
00125 
00126   // Extract the global indices of the elements local to this processor
00127   int NumMyElements = Map.NumMyElements();
00128   std::vector<int> MyGlobalElements( NumMyElements );
00129   Map.MyGlobalElements( &MyGlobalElements[0] );
00130   if( verbose ) cout << Map;
00131 
00132   // Create the number of non-zeros for a tridiagonal (1D problem) or banded
00133   // (2D problem) matrix
00134   std::vector<int> NumNz( NumMyElements, 5 );
00135   int global_i;
00136   int global_j;
00137   for (int i = 0; i < NumMyElements; ++i)
00138   {
00139     global_j = MyGlobalElements[i] / nx;
00140     global_i = MyGlobalElements[i] - global_j * nx;
00141     if (global_i == 0)    NumNz[i] -= 1;  // By having separate statements,
00142     if (global_i == nx-1) NumNz[i] -= 1;  // this works for 2D as well as 1D
00143     if (global_j == 0)    NumNz[i] -= 1;  // systems (i.e. nx x 1 or 1 x ny)
00144     if (global_j == ny-1) NumNz[i] -= 1;  // or even a 1 x 1 system
00145   }
00146   if(verbose)
00147   { 
00148     cout << endl << "NumNz: ";
00149     for (int i = 0; i < NumMyElements; i++) cout << NumNz[i] << " ";
00150     cout << endl;
00151   } // end if
00152   
00153   // Create the Epetra Compressed Row Sparse Graph
00154   Epetra_CrsGraph A( Copy, Map, &NumNz[0] );
00155   
00156   std::vector<int> Indices(5);
00157   int NumEntries;
00158   
00159   for (int i = 0; i < NumMyElements; ++i )
00160   {
00161     global_j = MyGlobalElements[i] / nx;
00162     global_i = MyGlobalElements[i] - global_j * nx;
00163     NumEntries = 0;
00164     // (i,j-1) entry
00165     if (global_j > 0 && ny > 1)
00166       Indices[NumEntries++] = global_i   + (global_j-1)*nx;
00167     // (i-1,j) entry
00168     if (global_i > 0)
00169       Indices[NumEntries++] = global_i-1 +  global_j   *nx;
00170     // (i,j) entry
00171     Indices[NumEntries++] = MyGlobalElements[i];
00172     // (i+1,j) entry
00173     if (global_i < nx-1)
00174       Indices[NumEntries++] = global_i+1 +  global_j   *nx;
00175     // (i,j+1) entry
00176     if (global_j < ny-1 && ny > 1)
00177       Indices[NumEntries++] = global_i   + (global_j+1)*nx;
00178 
00179     // Insert the global indices
00180     A.InsertGlobalIndices( MyGlobalElements[i], NumEntries, &Indices[0] );
00181   } // end i loop
00182 
00183   // Finish up graph construction
00184   A.FillComplete();
00185 
00186   EpetraExt::CrsGraph_MapColoring
00187     Greedy0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00188                      0, false, verbose );
00189   Epetra_MapColoring & Greedy0ColorMap = Greedy0MapColoringTransform( A );
00190   printColoring(Greedy0ColorMap, &A,verbose);
00191 
00192   EpetraExt::CrsGraph_MapColoring
00193     Greedy1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00194                      1, false, verbose );
00195   Epetra_MapColoring & Greedy1ColorMap = Greedy1MapColoringTransform( A );
00196   printColoring(Greedy1ColorMap, &A,verbose);
00197 
00198   EpetraExt::CrsGraph_MapColoring
00199     Greedy2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
00200                      2, false, verbose );
00201   Epetra_MapColoring & Greedy2ColorMap = Greedy2MapColoringTransform( A );
00202   printColoring(Greedy2ColorMap, &A,verbose);
00203 
00204   EpetraExt::CrsGraph_MapColoring
00205     Lubi0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00206                    0, false, verbose );
00207   Epetra_MapColoring & Lubi0ColorMap = Lubi0MapColoringTransform( A );
00208   printColoring(Lubi0ColorMap, &A,verbose);
00209 
00210   EpetraExt::CrsGraph_MapColoring
00211     Lubi1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00212                    1, false, verbose );
00213   Epetra_MapColoring & Lubi1ColorMap = Lubi1MapColoringTransform( A );
00214   printColoring(Lubi1ColorMap, &A,verbose);
00215 
00216   EpetraExt::CrsGraph_MapColoring
00217     Lubi2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
00218                    2, false, verbose );
00219   Epetra_MapColoring & Lubi2ColorMap = Lubi2MapColoringTransform( A );
00220   printColoring(Lubi2ColorMap, &A,verbose);
00221 
00222 #ifdef EPETRA_MPI
00223   if( verbose ) cout << "Parallel Map Coloring 1!\n";
00224   EpetraExt::CrsGraph_MapColoring
00225     Parallel1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::PSEUDO_PARALLEL,
00226                        0, false, verbose );
00227   Epetra_MapColoring & Parallel1ColorMap = Parallel1MapColoringTransform( A );
00228   printColoring(Parallel1ColorMap, &A,verbose);
00229 
00230   if( verbose ) cout << "Parallel Map Coloring 2!\n";
00231   EpetraExt::CrsGraph_MapColoring
00232     Parallel2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN,
00233                        0, false, verbose );
00234   Epetra_MapColoring & Parallel2ColorMap = Parallel2MapColoringTransform( A );
00235   printColoring(Parallel2ColorMap, &A,verbose);
00236 #endif
00237 
00238 
00239 #ifdef EPETRA_MPI
00240   MPI_Finalize();
00241 #endif
00242 
00243   return 0;
00244 }
00245 
00246 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph * A, bool verbose) {
00247 
00248   int NumColors = ColorMap.NumColors();
00249   int * ListOfColors = ColorMap.ListOfColors();
00250 
00251   EpetraExt::CrsGraph_MapColoringIndex MapColoringIndexTransform( ColorMap );
00252   vector<Epetra_IntVector> & ColIndices = MapColoringIndexTransform( *A );
00253 
00254   if( verbose )
00255   {
00256 
00257     cout << endl;
00258     cout << "***************************************\n";
00259     cout << "Column Indexing by Color:\n";
00260     cout << "***************************************\n";
00261     cout << endl;
00262     for( int i = 0; i < NumColors; ++i )
00263     {
00264       cout << "COLOR: " << ListOfColors[i] << endl;
00265       cout << ColIndices[i];
00266     }
00267     cout << endl;
00268   }
00269 
00270   return;
00271 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines