IFPACK  Development
Ifpack_METISReordering.cpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 
43 #include "Ifpack_ConfigDefs.h"
44 #include "Ifpack_Reordering.h"
45 #include "Ifpack_METISReordering.h"
46 #include "Ifpack_Graph.h"
47 #include "Ifpack_Graph_Epetra_CrsGraph.h"
48 #include "Ifpack_Graph_Epetra_RowMatrix.h"
49 #include "Epetra_Comm.h"
50 #include "Epetra_MultiVector.h"
51 #include "Epetra_CrsGraph.h"
52 #include "Epetra_Map.h"
53 #include "Teuchos_ParameterList.hpp"
54 
55 typedef int idxtype;
56 #ifdef HAVE_IFPACK_METIS
57 extern "C" {
58  void METIS_NodeND(int *n, idxtype *xadj, idxtype *adjncy,
59  int *numflag, int *options, int *perm, int *iperm);
60 }
61 #endif
62 
63 //==============================================================================
65  UseSymmetricGraph_(false),
66  NumMyRows_(0),
67  IsComputed_(false)
68 {}
69 
70 //==============================================================================
71 // Mainly copied from Ifpack_METISPartitioner.cpp
72 //
73 // NOTE:
74 // - matrix is supposed to be localized, and passes through the
75 // singleton filter. This means that I do not have to look
76 // for Dirichlet nodes (singletons). Also, all rows and columns are
77 // local.
79 {
80 
81  NumMyRows_ = Graph.NumMyRows();
82  Reorder_.resize(NumMyRows_);
83  InvReorder_.resize(NumMyRows_);
84 
85  int ierr;
86 
87  Teuchos::RefCountPtr<Epetra_CrsGraph> SymGraph;
88  Teuchos::RefCountPtr<Epetra_Map> SymMap;
89  Teuchos::RefCountPtr<Ifpack_Graph_Epetra_CrsGraph> SymIFPACKGraph;
90  Teuchos::RefCountPtr<Ifpack_Graph> IFPACKGraph = Teuchos::rcp( (Ifpack_Graph*)&Graph, false );
91 
92  int Length = 2 * Graph.MaxMyNumEntries();
93  int NumIndices;
94  vector<int> Indices;
95  Indices.resize(Length);
96 
97  vector<int> options;
98  options.resize(8);
99  options[0] = 0; // default values
100 
101 #ifdef HAVE_IFPACK_METIS
102  int numflag = 0; // C style
103 #endif
104 
105  if (UseSymmetricGraph_) {
106 
107  // need to build a symmetric graph.
108  // I do this in two stages:
109  // 1.- construct an Epetra_CrsMatrix, symmetric
110  // 2.- convert the Epetra_CrsMatrix into METIS format
111  SymMap = Teuchos::rcp( new Epetra_Map(NumMyRows_,0,Graph.Comm()) );
112  SymGraph = Teuchos::rcp( new Epetra_CrsGraph(Copy,*SymMap,0) );
113 
114  for (int i = 0; i < NumMyRows_ ; ++i) {
115 
116  ierr = Graph.ExtractMyRowCopy(i, Length, NumIndices,
117  &Indices[0]);
118  IFPACK_CHK_ERR(ierr);
119 
120  for (int j = 0 ; j < NumIndices ; ++j) {
121  int jj = Indices[j];
122  if (jj != i) {
123  // insert A(i,j), then A(j,i)
124  SymGraph->InsertGlobalIndices(i,1,&jj);
125  SymGraph->InsertGlobalIndices(jj,1,&i);
126  }
127  }
128  }
129  IFPACK_CHK_ERR(SymGraph->OptimizeStorage());
130  IFPACK_CHK_ERR(SymGraph->FillComplete());
131  SymIFPACKGraph = Teuchos::rcp( new Ifpack_Graph_Epetra_CrsGraph(SymGraph) );
132  IFPACKGraph = SymIFPACKGraph;
133  }
134 
135  // convert to METIS format
136  vector<idxtype> xadj;
137  xadj.resize(NumMyRows_ + 1);
138 
139  vector<idxtype> adjncy;
140  adjncy.resize(Graph.NumMyNonzeros());
141 
142  int count = 0;
143  int count2 = 0;
144  xadj[0] = 0;
145 
146  for (int i = 0; i < NumMyRows_ ; ++i) {
147 
148  xadj[count2+1] = xadj[count2]; /* nonzeros in row i-1 */
149 
150  ierr = IFPACKGraph->ExtractMyRowCopy(i, Length, NumIndices, &Indices[0]);
151  IFPACK_CHK_ERR(ierr);
152 
153  for (int j = 0 ; j < NumIndices ; ++j) {
154  int jj = Indices[j];
155  if (jj != i) {
156  adjncy[count++] = jj;
157  xadj[count2+1]++;
158  }
159  }
160  count2++;
161  }
162 
163 #ifdef HAVE_IFPACK_METIS
164  // vectors from METIS. The second last is `perm', the last is `iperm'.
165  // They store the fill-reducing permutation and inverse-permutation.
166  // Let A be the original matrix and A' the permuted matrix. The
167  // arrays perm and iperm are defined as follows. Row (column) i of A'
168  // if the perm[i] row (col) of A, and row (column) i of A is the
169  // iperm[i] row (column) of A'. The numbering starts from 0 in our case.
170  METIS_NodeND(&NumMyRows_, &xadj[0], &adjncy[0],
171  &numflag, &options[0],
172  &InvReorder_[0], &Reorder_[0]);
173 #else
174  cerr << "Please configure with --enable-ifpack-metis" << endl;
175  cerr << "to use METIS Reordering." << endl;
176  exit(EXIT_FAILURE);
177 #endif
178 
179  return(0);
180 }
181 
182 //==============================================================================
183 int Ifpack_METISReordering::Compute(const Epetra_RowMatrix& Matrix)
184 {
185  Ifpack_Graph_Epetra_RowMatrix Graph(Teuchos::rcp(&Matrix, false));
186 
187  IFPACK_CHK_ERR(Compute(Graph));
188 
189  return(0);
190 }
191 
192 //==============================================================================
193 int Ifpack_METISReordering::Reorder(const int i) const
194 {
195 #ifdef IFPACK_ABC
196  if (!IsComputed())
197  IFPACK_CHK_ERR(-1);
198  if ((i < 0) || (i >= NumMyRows_))
199  IFPACK_CHK_ERR(-1);
200 #endif
201 
202  return(Reorder_[i]);
203 }
204 
205 //==============================================================================
207 {
208 #ifdef IFPACK_ABC
209  if (!IsComputed())
210  IFPACK_CHK_ERR(-1);
211  if ((i < 0) || (i >= NumMyRows_))
212  IFPACK_CHK_ERR(-1);
213 #endif
214 
215  return(InvReorder_[i]);
216 }
217 //==============================================================================
218 int Ifpack_METISReordering::P(const Epetra_MultiVector& Xorig,
219  Epetra_MultiVector& X) const
220 {
221  int NumVectors = X.NumVectors();
222 
223  for (int j = 0 ; j < NumVectors ; ++j) {
224  for (int i = 0 ; i < NumMyRows_ ; ++i) {
225  int np = Reorder_[i];
226  X[j][np] = Xorig[j][i];
227  }
228  }
229 
230  return(0);
231 }
232 
233 //==============================================================================
234 int Ifpack_METISReordering::Pinv(const Epetra_MultiVector& Xorig,
235  Epetra_MultiVector& X) const
236 {
237  int NumVectors = X.NumVectors();
238 
239  for (int j = 0 ; j < NumVectors ; ++j) {
240  for (int i = 0 ; i < NumMyRows_ ; ++i) {
241  int np = Reorder_[i];
242  X[j][i] = Xorig[j][np];
243  }
244  }
245 
246  return(0);
247 }
248 
249 //==============================================================================
250 ostream& Ifpack_METISReordering::Print(std::ostream& os) const
251 {
252  os << "*** Ifpack_METISReordering" << endl << endl;
253  if (!IsComputed())
254  os << "*** Reordering not yet computed." << endl;
255 
256  os << "*** Number of local rows = " << NumMyRows_ << endl;
257  os << "Local Row\tReorder[i]\tInvReorder[i]" << endl;
258  for (int i = 0 ; i < NumMyRows_ ; ++i) {
259  os << '\t' << i << "\t\t" << Reorder_[i] << "\t\t" << InvReorder_[i] << endl;
260  }
261 
262  return(os);
263 }
264 
virtual const Epetra_Comm & Comm() const =0
Returns the communicator object of the graph.
virtual int Pinv(const Epetra_MultiVector &Xorig, Epetra_MultiVector &X) const
Applies inverse reordering to multivector Xorig, whose local length equals the number of local rows...
virtual bool IsComputed() const
Returns true is the reordering object has been successfully initialized, false otherwise.
virtual int ExtractMyRowCopy(int MyRow, int LenOfIndices, int &NumIndices, int *Indices) const =0
Extracts a copy of input local row.
virtual int NumMyRows() const =0
Returns the number of local rows.
virtual int Reorder(const int i) const
Returns the reordered index of row i.
Ifpack_Graph_Epetra_CrsGraph: a class to define Ifpack_Graph as a light-weight conversion of Epetra_C...
virtual int InvReorder(const int i) const
Returns the inverse reordered index of row i.
Ifpack_Graph_Epetra_RowMatrix: a class to define Ifpack_Graph as a light-weight conversion of Epetra_...
virtual int NumMyNonzeros() const =0
Returns the number of local nonzero entries.
virtual int Compute(const Ifpack_Graph &Graph)
Computes all it is necessary to initialize the reordering object.
Ifpack_Graph: a pure virtual class that defines graphs for IFPACK.
Definition: Ifpack_Graph.h:59
virtual int MaxMyNumEntries() const =0
Returns the maximun number of entries for row.
virtual ostream & Print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator<<.
virtual int P(const Epetra_MultiVector &Xorig, Epetra_MultiVector &X) const
Applies reordering to multivector Xorig, whose local length equals the number of local rows...