EpetraExt Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
example/MapColoring/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_CrsMatrix.h"
56#include "Epetra_IntVector.h"
57#include "Epetra_MapColoring.h"
63
64#include <vector>
65
66//prototype
67void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph *A, bool verbose);
68
69int main(int argc, char *argv[]) {
70
71
72#ifdef EPETRA_MPI
73 // Initialize MPI
74 MPI_Init( &argc, &argv );
75#endif
76
77 bool verbose = false;
78
79 if( argc < 2 || argc > 3 )
80 {
81 cout << "Usage: " << argv[0] << " [-v] base_name \n"
82 << "\t{ Files: base_name_matrix contains matrix-market data and \n"
83 << "\t base_name_map contains the needed map.\n" << endl;
84 exit(1);
85 }
86
87 int loc = 1;
88 // Check if we should print results to standard out
89 if(argv[loc][0]=='-' && argv[loc][1]=='v')
90 { verbose = true; ++loc; }
91
92#ifdef EPETRA_MPI
93 Epetra_MpiComm Comm(MPI_COMM_WORLD);
94#else
96#endif
97
98 int MyPID = Comm.MyPID();
99
100 bool verbose1 = false;
101 if(verbose) verbose1 = (MyPID==0);
102
103 if(verbose1)
104 cout << EpetraExt::EpetraExt_Version() << endl << endl;
105
106 Comm.Barrier();
107
108 string filename1(argv[loc++]);
109 string filename2 = filename1 + "_map";
110 filename1 += "_matrix";
111
112 if(verbose1) cout << "Reading Epetra_BlockMap from file : " << filename2;
113 Epetra_BlockMap* BlkMap(0);
114 EpetraExt::MatrixMarketFileToBlockMap(filename2.c_str(), Comm, BlkMap);
115 if(verbose1) cout << " Done." << endl;
116
117 if(verbose1) cout << "Converting Epetra_BlockMap to Epetra_Map ... ";
118 Epetra_Map* Map = new Epetra_Map( BlkMap->NumGlobalElements(),
119 BlkMap->NumMyElements(),
120 BlkMap->MyGlobalElements(),
121 BlkMap->IndexBase(),
122 BlkMap->Comm());
123 if(verbose1) cout << " Done." << endl;
124
125 if(verbose1) cout << "Reading Epetra_CrsMatrix from file : " << filename1;
126 Epetra_CrsMatrix* A_Matrix(0);
127 EpetraExt::MatrixMarketFileToCrsMatrix( filename1.c_str(), *Map, A_Matrix);
128 if(verbose1) cout << " Done." << endl;
129
130 A_Matrix->FillComplete();
131
132 if(verbose) cout << Comm << endl << flush;
133 Comm.Barrier();
134
135 // Obtain the Epetra Compressed Row Sparse Graph
136 const Epetra_CrsGraph & constA = A_Matrix->Graph();
137 Epetra_CrsGraph & A = const_cast<Epetra_CrsGraph&>(constA);
138
139 // Finish up graph construction
140 assert(A.FillComplete() == 0);
141
143 Greedy0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
144 0, false, verbose );
145 Epetra_MapColoring & Greedy0ColorMap = Greedy0MapColoringTransform( A );
146 printColoring(Greedy0ColorMap, &A,verbose);
147
149 Greedy1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
150 1, false, verbose );
151 Epetra_MapColoring & Greedy1ColorMap = Greedy1MapColoringTransform( A );
152 printColoring(Greedy1ColorMap, &A,verbose);
153
155 Greedy2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::GREEDY,
156 2, false, verbose );
157 Epetra_MapColoring & Greedy2ColorMap = Greedy2MapColoringTransform( A );
158 printColoring(Greedy2ColorMap, &A,verbose);
159
161 Lubi0MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
162 0, false, verbose );
163 Epetra_MapColoring & Lubi0ColorMap = Lubi0MapColoringTransform( A );
164 printColoring(Lubi0ColorMap, &A,verbose);
165
167 Lubi1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
168 1, false, verbose );
169 Epetra_MapColoring & Lubi1ColorMap = Lubi1MapColoringTransform( A );
170 printColoring(Lubi1ColorMap, &A,verbose);
171
173 Lubi2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::LUBY,
174 2, false, verbose );
175 Epetra_MapColoring & Lubi2ColorMap = Lubi2MapColoringTransform( A );
176 printColoring(Lubi2ColorMap, &A,verbose);
177
178#ifdef EPETRA_MPI
179 if( verbose ) cout << "Parallel Map Coloring 1!\n";
181 Parallel1MapColoringTransform( EpetraExt::CrsGraph_MapColoring::PSEUDO_PARALLEL,
182 0, false, verbose );
183 Epetra_MapColoring & Parallel1ColorMap = Parallel1MapColoringTransform( A );
184 printColoring(Parallel1ColorMap, &A,verbose);
185
186 if( verbose ) cout << "Parallel Map Coloring 2!\n";
188 Parallel2MapColoringTransform( EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN,
189 0, false, verbose );
190 Epetra_MapColoring & Parallel2ColorMap = Parallel2MapColoringTransform( A );
191 printColoring(Parallel2ColorMap, &A,verbose);
192#endif
193
194#ifdef EPETRA_MPI
195 MPI_Finalize();
196#endif
197
198 return 0;
199}
200
201void printColoring (const Epetra_MapColoring & ColorMap, Epetra_CrsGraph * A, bool verbose) {
202
203 int NumColors = ColorMap.NumColors();
204 int * ListOfColors = ColorMap.ListOfColors();
205
206 EpetraExt::CrsGraph_MapColoringIndex MapColoringIndexTransform( ColorMap );
207 vector<Epetra_IntVector> & ColIndices = MapColoringIndexTransform( *A );
208
209 if( verbose )
210 {
211
212 cout << endl;
213 cout << "***************************************\n";
214 cout << "Column Indexing by Color:\n";
215 cout << "***************************************\n";
216 cout << endl;
217 for( int i = 0; i < NumColors; ++i )
218 {
219 cout << "COLOR: " << ListOfColors[i] << endl;
220 cout << ColIndices[i];
221 }
222 cout << endl;
223 }
224
225 return;
226}
Map Coloring of independent columns in a Graph.
void Barrier() const
int MyPID() const
int main(int argc, char *argv[])
void printColoring(const Epetra_MapColoring &ColorMap, Epetra_CrsGraph *A, bool verbose)
int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
std::string EpetraExt_Version()
int MatrixMarketFileToBlockMap(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.