EpetraExt Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test/MapColoring_LL/cxx_main.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
42 // CrsMatrix_MapColoring Test routine
43 #include <Epetra_ConfigDefs.h>
44 #include "EpetraExt_Version.h"
45 
46 #ifdef EPETRA_MPI
47 #include "Epetra_MpiComm.h"
48 #include <mpi.h>
49 #endif
50 
51 #include "Epetra_SerialComm.h"
52 #include "Epetra_Time.h"
53 #include "Epetra_Map.h"
54 #include "Epetra_CrsGraph.h"
55 #include "Epetra_LongLongVector.h"
56 #include "Epetra_MapColoring.h"
57 #include "EpetraExt_MapColoring.h"
59 #include "../epetra_test_err.h"
60 
61 #include <vector>
62 
63 // prototype
64 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph *A, bool verbose);
65 
66 int main(int argc, char *argv[]) {
67 
68 
69 #ifdef EPETRA_MPI
70 
71  // Initialize MPI
72 
73  MPI_Init( &argc, &argv );
74  //int size, rank; // Number of MPI processes, My process ID
75 
76  //MPI_Comm_size(MPI_COMM_WORLD, &size);
77  //MPI_Comm_rank(MPI_COMM_WORLD, &rank);
78 
79 #else
80 
81  //int size = 1; // Serial case (not using MPI)
82  //int rank = 0;
83 
84 #endif
85 
86  bool verbose = false;
87 
88  long long nx = 5;
89  long long ny = 5;
90 
91  if( argc > 1 )
92  {
93  if( argc > 4 )
94  {
95  cout << "Usage: " << argv[0] << " [-v [nx [ny]]]" << endl;
96  exit(1);
97  }
98 
99  int loc = 1;
100  // Check if we should print results to standard out
101  if(argv[loc][0]=='-' && argv[loc][1]=='v')
102  { verbose = true; ++loc; }
103 
104  if (loc < argc) nx = atoi( argv[loc++] );
105  if( loc < argc) ny = atoi( argv[loc] );
106  }
107 
108 #ifdef EPETRA_MPI
109  Epetra_MpiComm Comm(MPI_COMM_WORLD);
110 #else
111  Epetra_SerialComm Comm;
112 #endif
113 
114  int MyPID = Comm.MyPID();
115  int NumProc = Comm.NumProc();
116 
117  bool verbose1 = false;
118  if(verbose) verbose1 = (MyPID==0);
119 
120  if(verbose1)
121  cout << EpetraExt::EpetraExt_Version() << endl << endl;
122 
123  Comm.Barrier();
124 
125  if(verbose) cout << Comm << endl << flush;
126  Comm.Barrier();
127 
128  long long NumGlobalElements = nx * ny;
129  if( NumGlobalElements < NumProc )
130  {
131  cout << "NumGlobalElements = " << NumGlobalElements <<
132  " cannot be < number of processors = " << NumProc;
133  exit(1);
134  }
135 
136  int IndexBase = 0;
137  Epetra_Map Map( NumGlobalElements, IndexBase, Comm );
138 
139  // Extract the global indices of the elements local to this processor
140  int NumMyElements = Map.NumMyElements();
141  std::vector<long long> MyGlobalElements( NumMyElements );
142  Map.MyGlobalElements( &MyGlobalElements[0] );
143  if( verbose ) cout << Map;
144 
145  // Create the number of non-zeros for a tridiagonal (1D problem) or banded
146  // (2D problem) matrix
147  std::vector<int> NumNz( NumMyElements, 5 );
148  long long global_i;
149  long long global_j;
150  for (int i = 0; i < NumMyElements; ++i)
151  {
152  global_j = MyGlobalElements[i] / nx;
153  global_i = MyGlobalElements[i] - global_j * nx;
154  if (global_i == 0) NumNz[i] -= 1; // By having separate statements,
155  if (global_i == nx-1) NumNz[i] -= 1; // this works for 2D as well as 1D
156  if (global_j == 0) NumNz[i] -= 1; // systems (i.e. nx x 1 or 1 x ny)
157  if (global_j == ny-1) NumNz[i] -= 1; // or even a 1 x 1 system
158  }
159  if(verbose)
160  {
161  cout << endl << "NumNz: ";
162  for (int i = 0; i < NumMyElements; i++) cout << NumNz[i] << " ";
163  cout << endl;
164  } // end if
165 
166  // Create the Epetra Compressed Row Sparse Graph
167  Epetra_CrsGraph A( Copy, Map, &NumNz[0] );
168 
169  std::vector<long long> Indices(5);
170  int NumEntries;
171 
172  for (int i = 0; i < NumMyElements; ++i )
173  {
174  global_j = MyGlobalElements[i] / nx;
175  global_i = MyGlobalElements[i] - global_j * nx;
176  NumEntries = 0;
177  // (i,j-1) entry
178  if (global_j > 0 && ny > 1)
179  Indices[NumEntries++] = global_i + (global_j-1)*nx;
180  // (i-1,j) entry
181  if (global_i > 0)
182  Indices[NumEntries++] = global_i-1 + global_j *nx;
183  // (i,j) entry
184  Indices[NumEntries++] = MyGlobalElements[i];
185  // (i+1,j) entry
186  if (global_i < nx-1)
187  Indices[NumEntries++] = global_i+1 + global_j *nx;
188  // (i,j+1) entry
189  if (global_j < ny-1 && ny > 1)
190  Indices[NumEntries++] = global_i + (global_j+1)*nx;
191 
192  // Insert the global indices
193  A.InsertGlobalIndices( MyGlobalElements[i], NumEntries, &Indices[0] );
194  } // end i loop
195 
196  // Finish up graph construction
197  A.FillComplete();
198 
200  Greedy0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
201  0, false, verbose );
202  Epetra_MapColoring & Greedy0ColorMap = Greedy0MapColoringTransform( A );
203  printColoring(Greedy0ColorMap, &A,verbose);
204 
206  Greedy1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
207  1, false, verbose );
208  Epetra_MapColoring & Greedy1ColorMap = Greedy1MapColoringTransform( A );
209  printColoring(Greedy1ColorMap, &A,verbose);
210 
212  Greedy2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
213  2, false, verbose );
214  Epetra_MapColoring & Greedy2ColorMap = Greedy2MapColoringTransform( A );
215  printColoring(Greedy2ColorMap, &A,verbose);
216 
218  Lubi0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
219  0, false, verbose );
220  Epetra_MapColoring & Lubi0ColorMap = Lubi0MapColoringTransform( A );
221  printColoring(Lubi0ColorMap, &A,verbose);
222 
224  Lubi1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
225  1, false, verbose );
226  Epetra_MapColoring & Lubi1ColorMap = Lubi1MapColoringTransform( A );
227  printColoring(Lubi1ColorMap, &A,verbose);
228 
230  Lubi2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
231  2, false, verbose );
232  Epetra_MapColoring & Lubi2ColorMap = Lubi2MapColoringTransform( A );
233  printColoring(Lubi2ColorMap, &A,verbose);
234 
235 #ifdef EPETRA_MPI
236  if( verbose ) cout << "Parallel Map Coloring 1!\n";
238  Parallel1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::PSEUDO_PARALLEL,
239  0, false, verbose );
240  Epetra_MapColoring & Parallel1ColorMap = Parallel1MapColoringTransform( A );
241  printColoring(Parallel1ColorMap, &A,verbose);
242 
243  if( verbose ) cout << "Parallel Map Coloring 2!\n";
245  Parallel2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN,
246  0, false, verbose );
247  Epetra_MapColoring & Parallel2ColorMap = Parallel2MapColoringTransform( A );
248  printColoring(Parallel2ColorMap, &A,verbose);
249 #endif
250 
251 
252 #ifdef EPETRA_MPI
253  MPI_Finalize();
254 #endif
255 
256  return 0;
257 }
258 
259 void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph * A, bool verbose) {
260 
261  int NumColors = ColorMap.NumColors();
262  int * ListOfColors = ColorMap.ListOfColors();
263 
264  EpetraExt::CrsGraph_MapColoringIndex64 MapColoringIndexTransform( ColorMap );
265  vector<Epetra_LongLongVector> & ColIndices = MapColoringIndexTransform( *A );
266 
267  if( verbose )
268  {
269 
270  cout << endl;
271  cout << "***************************************\n";
272  cout << "Column Indexing by Color:\n";
273  cout << "***************************************\n";
274  cout << endl;
275  for( int i = 0; i < NumColors; ++i )
276  {
277  cout << "COLOR: " << ListOfColors[i] << endl;
278  cout << ColIndices[i];
279  }
280  cout << endl;
281  }
282 
283  return;
284 }
int MyGlobalElements(int *MyGlobalElementList) const
std::string EpetraExt_Version()
int MyPID() const
int InsertGlobalIndices(int_type GlobalRow, int NumIndices, int_type *Indices)
int main(int argc, char **argv)
Definition: HDF5_IO.cpp:67
int NumMyElements() const
int * ListOfColors() const
int NumProc() const
int NumColors() const
void Barrier() const
void printColoring(const Epetra_MapColoring &ColorMap, Epetra_CrsGraph *A, bool verbose)
Map Coloring of independent columns in a Graph.