IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_METISPartitioner.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_Partitioner.h"
45 #include "Ifpack_OverlappingPartitioner.h"
46 #include "Ifpack_METISPartitioner.h"
47 #include "Ifpack_Graph.h"
48 #include "Ifpack_Graph_Epetra_CrsGraph.h"
49 #include "Epetra_Comm.h"
50 #include "Epetra_CrsGraph.h"
51 #include "Epetra_Map.h"
52 #include "Teuchos_ParameterList.hpp"
53 #include "Teuchos_RefCountPtr.hpp"
54 
55 // may need to change this for wierd installations
56 typedef int idxtype;
57 #ifdef HAVE_IFPACK_METIS
58 extern "C" {
59  void METIS_EstimateMemory(int *, idxtype *, idxtype *, int *, int *, int *);
60  void METIS_PartGraphKway(int *, idxtype *, idxtype *, idxtype *,
61  idxtype *, int *, int *, int *, int *, int *,
62  idxtype *);
63  void METIS_PartGraphRecursive(int *, idxtype *, idxtype *,
64  idxtype *, idxtype *, int *, int *, int *,
65  int *, int *, idxtype *);
66 
67 }
68 #endif
69 
70 //==============================================================================
71 // NOTE:
72 // - matrix is supposed to be localized, and passes through the
73 // singleton filter. This means that I do not have to look
74 // for Dirichlet nodes (singletons). Also, all rows and columns are
75 // local.
77 {
78 
79  int ierr;
80 #ifdef HAVE_IFPACK_METIS
81  int nbytes = 0;
82  int edgecut;
83 #endif
84 
85  Teuchos::RefCountPtr<Epetra_CrsGraph> SymGraph ;
86  Teuchos::RefCountPtr<Epetra_Map> SymMap;
87  Teuchos::RefCountPtr<Ifpack_Graph_Epetra_CrsGraph> SymIFPACKGraph;
88  Teuchos::RefCountPtr<Ifpack_Graph> IFPACKGraph = Teuchos::rcp( (Ifpack_Graph*)Graph_, false );
89 
90  int Length = 2 * MaxNumEntries();
91  int NumIndices;
92  vector<int> Indices;
93  Indices.resize(Length);
94 
95  /* construct the CSR graph information of the LOCAL matrix
96  using the get_row function */
97 
98  vector<idxtype> wgtflag;
99  wgtflag.resize(4);
100 
101  vector<int> options;
102  options.resize(4);
103 
104  int numflag;
105 
106  if (UseSymmetricGraph_) {
107 
108  // need to build a symmetric graph.
109  // I do this in two stages:
110  // 1.- construct an Epetra_CrsMatrix, symmetric
111  // 2.- convert the Epetra_CrsMatrix into METIS format
112  SymMap = Teuchos::rcp( new Epetra_Map(NumMyRows(),0,Graph_->Comm()) );
113  SymGraph = Teuchos::rcp( new Epetra_CrsGraph(Copy,*SymMap,0) );
114 
115  for (int i = 0; i < NumMyRows() ; ++i) {
116 
117  ierr = Graph_->ExtractMyRowCopy(i, Length, NumIndices,
118  &Indices[0]);
119  IFPACK_CHK_ERR(ierr);
120 
121  for (int j = 0 ; j < NumIndices ; ++j) {
122  int jj = Indices[j];
123  if (jj != i) {
124  SymGraph->InsertGlobalIndices(i,1,&jj);
125  SymGraph->InsertGlobalIndices(jj,1,&i);
126  }
127  }
128  }
129  IFPACK_CHK_ERR(SymGraph->FillComplete());
130  SymIFPACKGraph = Teuchos::rcp( new Ifpack_Graph_Epetra_CrsGraph(SymGraph) );
131  IFPACKGraph = SymIFPACKGraph;
132  }
133 
134  // now work on IFPACKGraph, that can be the symmetric or
135  // the non-symmetric one
136 
137  /* set parameters */
138 
139  wgtflag[0] = 0; /* no weights */
140  numflag = 0; /* C style */
141  options[0] = 0; /* default options */
142 
143  vector<idxtype> xadj;
144  xadj.resize(NumMyRows() + 1);
145 
146  vector<idxtype> adjncy;
147  adjncy.resize(NumMyNonzeros());
148 
149  int count = 0;
150  int count2 = 0;
151  xadj[0] = 0;
152 
153  for (int i = 0; i < NumMyRows() ; ++i) {
154 
155  xadj[count2+1] = xadj[count2]; /* nonzeros in row i-1 */
156 
157  ierr = IFPACKGraph->ExtractMyRowCopy(i, Length, NumIndices, &Indices[0]);
158  IFPACK_CHK_ERR(ierr);
159 
160  for (int j = 0 ; j < NumIndices ; ++j) {
161  int jj = Indices[j];
162  if (jj != i) {
163  adjncy[count++] = jj;
164  xadj[count2+1]++;
165  }
166  }
167  count2++;
168  }
169 
170  vector<idxtype> NodesInSubgraph;
171  NodesInSubgraph.resize(NumLocalParts_);
172 
173  // some cases can be handled separately
174 
175  int ok;
176 
177  if (NumLocalParts() == 1) {
178 
179  for (int i = 0 ; i < NumMyRows() ; ++i)
180  Partition_[i] = 0;
181 
182  } else if (NumLocalParts() == NumMyRows()) {
183 
184  for (int i = 0 ; i < NumMyRows() ; ++i)
185  Partition_[i] = i;
186 
187  } else {
188 
189  ok = 0;
190 
191  // sometimes METIS creates less partitions than specified.
192  // ok will check this problem, and recall metis, asking
193  // for NumLocalParts_/2 partitions
194  while (ok == 0) {
195 
196  for (int i = 0 ; i < NumMyRows() ; ++i)
197  Partition_[i] = -1;
198 
199 #ifdef HAVE_IFPACK_METIS
200  int j = NumMyRows();
201  if (NumLocalParts_ < 8) {
202 
203  int i = 1; /* optype in the METIS manual */
204  numflag = 0;
205  METIS_EstimateMemory(&j, &xadj[0], &adjncy[0],
206  &numflag, &i, &nbytes );
207 
208  METIS_PartGraphRecursive(&j, &xadj[0], &adjncy[0],
209  NULL, NULL,
210  &wgtflag[0], &numflag, &NumLocalParts_,
211  &options[0], &edgecut, &Partition_[0]);
212  } else {
213 
214  numflag = 0;
215 
216  METIS_PartGraphKway (&j, &xadj[0], &adjncy[0],
217  NULL,
218  NULL, &wgtflag[0], &numflag,
219  &NumLocalParts_, &options[0],
220  &edgecut, &Partition_[0]);
221  }
222 #else
223  numflag = numflag * 2; // avoid warning for unused variable
224  if (Graph_->Comm().MyPID() == 0) {
225  cerr << "METIS was not linked; now I put all" << endl;
226  cerr << "the local nodes in the same partition." << endl;
227  }
228  for (int i = 0 ; i < NumMyRows() ; ++i)
229  Partition_[i] = 0;
230  NumLocalParts_ = 1;
231 #endif
232 
233  ok = 1;
234 
235  for (int i = 0 ; i < NumLocalParts() ; ++i)
236  NodesInSubgraph[i] = 0;
237 
238  for (int i = 0 ; i < NumMyRows() ; ++i) {
239  int j = Partition_[i];
240  if ((j < 0) || (j>= NumLocalParts())) {
241  ok = 0;
242  break;
243  }
244  else NodesInSubgraph[j]++;
245  }
246 
247  for (int i = 0 ; i < NumLocalParts() ; ++i) {
248  if( NodesInSubgraph[i] == 0 ) {
249  ok = 0;
250  break;
251  }
252  }
253 
254  if (ok == 0) {
255  cerr << "Specified number of subgraphs ("
256  << NumLocalParts_ << ") generates empty subgraphs." << endl;
257  cerr << "Now I recall METIS with NumLocalParts_ = "
258  << NumLocalParts_ / 2 << "..." << endl;
260  }
261 
262  if (NumLocalParts() == 0) {
263  IFPACK_CHK_ERR(-10); // something went wrong
264  }
265 
266  if (NumLocalParts() == 1) {
267  for (int i = 0 ; i < NumMyRows() ; ++i)
268  Partition_[i] = 0;
269  ok = 1;
270  }
271 
272  } /* while( ok == 0 ) */
273 
274  } /* if( NumLocalParts_ == 1 ) */
275 
276  return(0);
277 }
virtual int ExtractMyRowCopy(int MyRow, int LenOfIndices, int &NumIndices, int *Indices) const =0
Extracts a copy of input local row.
int MaxNumEntries() const
Returns the max number of local entries in a row.
int NumMyNonzeros() const
Returns the number of local nonzero elements.
int NumMyRows() const
Returns the number of local rows.
std::vector< int > Partition_
Partition_[i] contains the ID of non-overlapping part it belongs to.
Ifpack_Graph_Epetra_CrsGraph: a class to define Ifpack_Graph as a light-weight conversion of Epetra_C...
virtual const Epetra_Comm & Comm() const =0
Returns the communicator object of the graph.
const Ifpack_Graph * Graph_
Reference to the graph to be partitioned.
int NumLocalParts_
Number of local subgraphs.
int NumLocalParts() const
Returns the number of computed local partitions.
Ifpack_Graph: a pure virtual class that defines graphs for IFPACK.
Definition: Ifpack_Graph.h:59
int ComputePartitions()
Computes the partitions. Returns 0 if successful.