IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_OverlappingPartitioner.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_Graph.h"
47 
48 #include "Epetra_Comm.h"
49 #include "Epetra_BlockMap.h"
50 #include "Epetra_Map.h"
51 #include "Teuchos_ParameterList.hpp"
52 
53 static const string PrintMsg_ = "(Ifpack_OvPartitioner) ";
54 
55 //==============================================================================
58  NumLocalParts_(1),
59  Graph_(Graph),
60  OverlappingLevel_(0),
61  IsComputed_(false),
62  verbose_(false)
63 {
64 }
65 
66 //==============================================================================
68 {
69 }
70 
71 //==============================================================================
72 int Ifpack_OverlappingPartitioner::SetParameters(Teuchos::ParameterList& List)
73 {
74 
75  NumLocalParts_ = List.get("partitioner: local parts", NumLocalParts_);
76  OverlappingLevel_ = List.get("partitioner: overlap", OverlappingLevel_);
77  verbose_ = List.get("partitioner: print level", verbose_);
78 
79  if (NumLocalParts_ < 0)
81  if (NumLocalParts_ == 0)
82  NumLocalParts_ = 1;
83  if (NumLocalParts_ < 0)
84  IFPACK_CHK_ERR(-1);
86  IFPACK_CHK_ERR(-1);
87 
88  if (OverlappingLevel_ < 0)
89  IFPACK_CHK_ERR(-1);
90 
92 
93  return(0);
94 }
95 
96 //==============================================================================
98 {
99 
100  if (NumLocalParts_ < 1)
101  IFPACK_CHK_ERR(-1); // incorrect value
102 
103  if (OverlappingLevel_ < 0)
104  IFPACK_CHK_ERR(-1); // incorrect value
105 
106  // some output
107 
108  if (verbose_ && (Comm().MyPID() == 0)) {
109  cout << PrintMsg_ << "Number of local parts = " << NumLocalParts_ << endl;
110  cout << PrintMsg_ << "Number of global parts = "
111  << NumLocalParts_ * Comm().NumProc() << endl;
112  cout << PrintMsg_ << "Amount of overlap = " << OverlappingLevel_ << endl;
113  }
114 
115  // 1.- allocate memory
116 
117  Partition_.resize(NumMyRows());
118  Parts_.resize(NumLocalParts());
119 
120  // 2.- sanity checks on input graph
121 
122  if (Graph_->Filled() == false)
123  IFPACK_CHK_ERR(-4); // need FillComplete() called
124 
126  IFPACK_CHK_ERR(-3); // can partition square matrices only
127 
128  if (NumLocalParts_ < 1)
129  IFPACK_CHK_ERR(-2); // value not valid
130 
131  // 3.- perform non-overlapping partition
132 
133  IFPACK_CHK_ERR(ComputePartitions());
134 
135  // 4.- compute the partitions with overlapping
136 
137  IFPACK_CHK_ERR(ComputeOverlappingPartitions());
138 
139  // 5.- return to the user
140 
141  IsComputed_ = true;
142 
143  return(0);
144 }
145 
146 // ======================================================================
148 {
149 
150  // FIXME: the first part of this function should be elsewhere
151  // start defining the subgraphs for no overlap
152 
153  vector<int> sizes;
154  sizes.resize(NumLocalParts_);
155 
156  // 1.- compute how many rows are in each subgraph
157  for (int i = 0 ; i < NumLocalParts_ ; ++i)
158  sizes[i] = 0;
159 
160  for (int i = 0 ; i < NumMyRows() ; ++i) {
161  if (Partition_[i] >= NumLocalParts_) {
162  cerr << "ERROR: Partition[" << i << "] = "<< Partition_[i]
163  << ", NumLocalParts = " << NumLocalParts_ << endl;
164  cerr << "(file = " << __FILE__ << ", line = "
165  << __LINE__ << ")" << endl;
166  IFPACK_CHK_ERR(-10);
167  }
168  // no singletons should be here, as the matrix is
169  // supposed to be filtered through Ifpack_SingletonFilter
170  if (Partition_[i] == -1)
171  IFPACK_CHK_ERR(-1);
172  sizes[Partition_[i]]++;
173  }
174 
175  // 2.- allocate space for each subgraph
176  for (int i = 0 ; i < NumLocalParts_ ; ++i)
177  Parts_[i].resize(sizes[i]);
178 
179  // 3.- cycle over all rows and populate the vectors
180 
181  for (int i = 0 ; i < NumLocalParts_ ; ++i)
182  sizes[i] = 0;
183 
184  for (int i = 0 ; i < NumMyRows() ; ++i) {
185  int part = Partition_[i];
186  int count = sizes[part];
187  Parts_[part][count] = i;
188  sizes[part]++;
189  }
190 
191  if (OverlappingLevel_ == 0)
192  return(0);
193 
194  // wider overlap requires further computations
195  for (int level = 1 ; level <= OverlappingLevel_ ; ++level) {
196 
197  vector<vector<int> > tmp;
198  tmp.resize(NumLocalParts_);
199 
200  // cycle over all rows in the local graph (that is the overlapping
201  // graph). For each row, all columns will belong to the subgraph of
202  // row `i'.
203 
204  int MaxNumEntries_tmp = Graph_->MaxMyNumEntries();
205  vector<int> Indices;
206  Indices.resize(MaxNumEntries_tmp);
207 
208  for (int part = 0 ; part < NumLocalParts_ ; ++part) {
209 
210  for (int i = 0; i < (int)Parts_[part].size() ; ++i) {
211 
212  int LRID = Parts_[part][i];
213  int NumIndices;
214  int ierr = Graph_->ExtractMyRowCopy(LRID, MaxNumEntries_tmp,
215  NumIndices, &Indices[0]);
216  IFPACK_CHK_ERR(ierr);
217 
218  for (int j = 0 ; j < NumIndices ; ++j) {
219 
220  // use *local* indices
221  int col = Indices[j];
222  if (col >= NumMyRows())
223  continue;
224 
225  // has this column already been inserted?
226  vector<int>::iterator
227  where = find(tmp[part].begin(), tmp[part].end(), col);
228 
229  if (where == tmp[part].end()) {
230  tmp[part].push_back(col);
231  }
232  }
233  }
234  }
235 
236  // now I convert the STL vectors into Epetra_IntSerialDenseVectors.
237  for (int i = 0 ; i < NumLocalParts_ ; ++i) {
238  Parts_[i].resize(tmp[i].size());
239  for (int j = 0 ; j < (int)tmp[i].size() ; ++j)
240  Parts_[i][j] = tmp[i][j];
241  }
242  }
243 
244  return(0);
245 
246 }
247 
248 //============================================================================
250 {
251  return(Graph_->NumMyRows());
252 }
253 
254 //============================================================================
256 {
257  return(Graph_->NumMyNonzeros());
258 }
259 
260 //============================================================================
262 {
263  return(Graph_->NumGlobalRows());
264 }
265 
266 //============================================================================
268 {
269  return(Graph_->MaxMyNumEntries());
270 }
271 
272 //============================================================================
273 const Epetra_Comm& Ifpack_OverlappingPartitioner::Comm() const
274 {
275  return(Graph_->Comm());
276 }
277 
278 // ======================================================================
279 ostream& Ifpack_OverlappingPartitioner::Print(ostream & os) const
280 {
281 
282  if (Comm().MyPID())
283  return(os);
284 
285  os << "================================================================================" << endl;
286  os << "Ifpack_OverlappingPartitioner" << endl;
287  os << "Number of local rows = " << Graph_->NumMyRows() << endl;
288  os << "Number of global rows = " << Graph_->NumGlobalRows() << endl;
289  os << "Number of local parts = " << NumLocalParts_ << endl;
290  os << "Overlapping level = " << OverlappingLevel_ << endl;
291  os << "Is computed = " << IsComputed_ << endl;
292  os << "================================================================================" << endl;
293 
294  return(os);
295 }
296 
virtual int SetParameters(Teuchos::ParameterList &List)
Sets all the parameters for the partitioner.
virtual int Compute()
Computes the partitions. Returns 0 if successful.
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 SetPartitionParameters(Teuchos::ParameterList &List)=0
Sets all the parameters for the partitioner.
int MaxNumEntries() const
Returns the max number of local entries in a row.
Ifpack_OverlappingPartitioner(const Ifpack_Graph *Graph)
Constructor.
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.
virtual int ComputeOverlappingPartitions()
Computes the partitions. Returns 0 if successful.
virtual ostream & Print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator&lt;&lt;.
virtual int NumMyNonzeros() const =0
Returns the number of local nonzero entries.
virtual bool Filled() const =0
Returns true is graph is filled.
bool IsComputed_
If true, the graph has been successfully partitioned.
virtual int NumGlobalRows() const =0
Returns the number of global rows.
bool verbose_
If true, information are reported on cout.
virtual const Epetra_Comm & Comm() const =0
Returns the communicator object of the graph.
virtual int NumGlobalCols() const =0
Returns the number of global columns.
const Ifpack_Graph * Graph_
Reference to the graph to be partitioned.
int NumLocalParts_
Number of local subgraphs.
virtual int MaxMyNumEntries() const =0
Returns the maximun number of entries for row.
virtual int ComputePartitions()=0
Computes the partitions. Returns 0 if successful.
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
const Epetra_Comm & Comm() const
Returns the communicator object of Graph.
std::vector< std::vector< int > > Parts_
Parts_[i][j] is the ID of the j-th row contained in the (overlapping)
int NumGlobalRows() const
Returns the number of local rows.