Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_OverlappingPartitioner_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) 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 #ifndef IFPACK2_OVERLAPPINGPARTITIONER_DEF_HPP
44 #define IFPACK2_OVERLAPPINGPARTITIONER_DEF_HPP
45 #include "Ifpack2_ConfigDefs.hpp"
46 #include "Ifpack2_OverlappingPartitioner_decl.hpp"
47 #include "Teuchos_Array.hpp"
48 #include "Teuchos_ArrayRCP.hpp"
49 #include <vector>
50 #include <string>
51 
52 namespace Ifpack2 {
53 //==============================================================================
54 template<class GraphType>
55 OverlappingPartitioner<GraphType>::OverlappingPartitioner(const Teuchos::RCP<const Tpetra::RowGraph<LocalOrdinal,GlobalOrdinal,Node> >& Graph) :
56  NumLocalParts_(1),
57  Graph_(Graph),
58  OverlappingLevel_(0),
59  IsComputed_(false),
60  verbose_(false)
61 {
62 }
63 
64 //==============================================================================
65 template<class GraphType>
66 OverlappingPartitioner<GraphType>::~OverlappingPartitioner()
67 {
68 }
69 
70 //==============================================================================
71 template<class GraphType>
72 typename GraphType::local_ordinal_type OverlappingPartitioner<GraphType>::numLocalParts() const
73 {
74  return( NumLocalParts_);
75 }
76 
77 //==============================================================================
78 template<class GraphType>
79 size_t OverlappingPartitioner<GraphType>::overlappingLevel() const
80 {
81  return(OverlappingLevel_);
82 }
83 
84 //==============================================================================
85 template<class GraphType>
86 typename GraphType::local_ordinal_type OverlappingPartitioner<GraphType>::operator() (LocalOrdinal MyRow) const
87 {
88  TEUCHOS_TEST_FOR_EXCEPTION(((MyRow < 0) || ((size_t)MyRow > Graph_->getNodeNumRows())), std::runtime_error, "Ifpack2::OverlappingPartitioner::operator() invalid row.");
89 
90  return(Partition_[MyRow]);
91 }
92 
93 
94 //==============================================================================
95 template<class GraphType>
96 typename GraphType::local_ordinal_type OverlappingPartitioner<GraphType>::operator() (LocalOrdinal i, LocalOrdinal j) const
97 {
98  TEUCHOS_TEST_FOR_EXCEPTION( ((i < 0) || ((i > NumLocalParts_) || (j < 0) || (j > Parts_[i].size()))),
99  std::runtime_error, "Ifpack2::OverlappingPartitioner::operator() invalid row or node.");
100  return(Parts_[i][j]);
101 }
102 
103 //==============================================================================
104 template<class GraphType>
105 size_t OverlappingPartitioner<GraphType>::numRowsInPart(const LocalOrdinal Part) const
106 {
107  TEUCHOS_TEST_FOR_EXCEPTION( ((Part < 0) || (Part > NumLocalParts_)),
108  std::runtime_error, "Ifpack2::OverlappingPartitioner::numRowsInPart() invalid partition.");
109  return(Parts_[Part].size());
110 }
111 
112 //==============================================================================
113 template<class GraphType>
114 void OverlappingPartitioner<GraphType>::rowsInPart(const LocalOrdinal Part, Teuchos::ArrayRCP<LocalOrdinal> & List) const
115 {
116  // Let numRowsInPart do the sanity checking...
117  size_t numrows= numRowsInPart(Part);
118  for (size_t i = 0 ; i < numrows; i++)
119  List[i] = Parts_[Part][i];
120 }
121 
122 //==============================================================================
123 template<class GraphType>
124 Teuchos::ArrayView<const typename GraphType::local_ordinal_type> OverlappingPartitioner<GraphType>::nonOverlappingPartition() const
125 {
126  return(Partition_.view(0, Graph_->getNodeNumRows()));
127 }
128 
129 //==============================================================================
130 template<class GraphType>
131 void OverlappingPartitioner<GraphType>::setParameters(Teuchos::ParameterList& List)
132 {
133  NumLocalParts_ = List.get("partitioner: local parts", NumLocalParts_);
134  OverlappingLevel_ = List.get("partitioner: overlap", OverlappingLevel_);
135  verbose_ = List.get("partitioner: print level", verbose_);
136 
137  if (NumLocalParts_ < 0)
138  NumLocalParts_ = Graph_->getNodeNumRows() / (-NumLocalParts_);
139  if (NumLocalParts_ == 0)
140  NumLocalParts_ = 1;
141 
142  // Sanity checking
143  TEUCHOS_TEST_FOR_EXCEPTION( ((NumLocalParts_ < 0) || (size_t)NumLocalParts_ > Graph_->getNodeNumRows()),
144  std::runtime_error, "Ifpack2::OverlappingPartitioner::setParameters() invalid NumLocalParts_");
145  TEUCHOS_TEST_FOR_EXCEPTION( (OverlappingLevel_ < 0),
146  std::runtime_error, "Ifpack2::OverlappingPartitioner::setParameters() invalid OverlappingLevel_");
147 
148  setPartitionParameters(List);
149 }
150 
151 //==============================================================================
152 template<class GraphType>
153 void OverlappingPartitioner<GraphType>::compute()
154 {
155  TEUCHOS_TEST_FOR_EXCEPTION( ((NumLocalParts_ < 1) || (OverlappingLevel_ < 0)),
156  std::runtime_error, "Ifpack2::OverlappingPartitioner::compute() invalid NumLocalParts_ or OverlappingLevel_");
157 
158  std::string PrintMsg_("OverlappingPartitioner: ");
159 
160  // some output
161  if (verbose_ && (Graph_->getComm()->getRank() == 0)) {
162  std::cout << PrintMsg_ << "Number of local parts = " << NumLocalParts_ << std::endl;
163  std::cout << PrintMsg_ << "Approx. Number of global parts = "
164  << NumLocalParts_ * Graph_->getComm()->getSize() << std::endl;
165  std::cout << PrintMsg_ << "Amount of overlap = " << OverlappingLevel_ << std::endl;
166  }
167 
168  // 1.- allocate memory
169  Partition_.resize(Graph_->getNodeNumRows());
170  Parts_.resize(NumLocalParts_);
171 
172  // 2.- sanity checks on input graph
173  TEUCHOS_TEST_FOR_EXCEPTION( (!Graph_->isFillComplete() || Graph_->getGlobalNumRows() != Graph_->getGlobalNumCols()),
174  std::runtime_error, "Ifpack2::OverlappingPartitioner::compute() input graph error");
175 
176  // 3.- perform non-overlapping partition
177  computePartitions();
178 
179  // 4.- compute the partitions with overlapping
180  computeOverlappingPartitions();
181 
182  // 5.- mark as computed
183  IsComputed_ = true;
184 }
185 
186 //==============================================================================
187 template<class GraphType>
188 void OverlappingPartitioner<GraphType>::computeOverlappingPartitions()
189 {
190  using std::vector;
191 
192  LocalOrdinal invalid = Teuchos::OrdinalTraits<LocalOrdinal>::invalid();
193 
194  // Old FIXME from Ifpack: the first part of this function should be elsewhere
195 
196  // start defining the subgraphs for no overlap
197 
198  vector<size_t> sizes;
199  sizes.resize(NumLocalParts_);
200 
201  // 1.- compute how many rows are in each subgraph
202  for (int i = 0 ; i < NumLocalParts_ ; ++i)
203  sizes[i] = 0;
204 
205  for (size_t i = 0 ; i < Graph_->getNodeNumRows() ; ++i) {
206  TEUCHOS_TEST_FOR_EXCEPTION( (Partition_[i] >= NumLocalParts_),
207  std::runtime_error, "Ifpack2::OverlappingPartitioner::computeOverlappingPartitions() Partition_[i] > NumLocalParts_");
208 
209  //invalid indicates that this unknown is not in a nonoverlapping partition
210  if (Partition_[i] != invalid)
211  sizes[Partition_[i]]++;
212  }
213 
214  // 2.- allocate space for each subgraph
215  for (int i = 0 ; i < NumLocalParts_ ; ++i)
216  Parts_[i].resize(sizes[i]);
217 
218  // 3.- cycle over all rows and populate the vectors
219  for (int i = 0 ; i < NumLocalParts_ ; ++i)
220  sizes[i] = 0;
221 
222  for (size_t i = 0 ; i < Graph_->getNodeNumRows() ; ++i) {
223  LocalOrdinal part = Partition_[i];
224  if (part != invalid){
225  size_t count = sizes[part];
226  Parts_[part][count] = i;
227  sizes[part]++;
228  }
229  }
230 
231  // If there is no overlap, we're done, so return
232  if (OverlappingLevel_ == 0) return;
233 
234  // wider overlap requires further computations
235  for (size_t level = 1 ; level <= OverlappingLevel_ ; ++level) {
236  vector<vector<size_t> > tmp;
237  tmp.resize(NumLocalParts_);
238 
239  // cycle over all rows in the local graph (that is the overlapping
240  // graph). For each row, all columns will belong to the subgraph of
241  // row `i'.
242 
243  int MaxNumEntries_tmp = Graph_->getNodeMaxNumRowEntries();
244  Teuchos::Array<LocalOrdinal> Indices;
245  Indices.resize(MaxNumEntries_tmp);
246 
247  for (int part = 0 ; part < NumLocalParts_ ; ++part) {
248  for (size_t i = 0; i < (size_t)Parts_[part].size() ; ++i) {
249  LocalOrdinal LRID = Parts_[part][i];
250  tmp[part].push_back(LRID);
251 
252  size_t NumIndices;
253  Graph_->getLocalRowCopy(LRID,Indices(),NumIndices);
254 
255  for (size_t j = 0 ; j < NumIndices ; ++j) {
256  // use *local* indices only
257  LocalOrdinal col = Indices[j];
258  if ((size_t)col >= Graph_->getNodeNumRows())
259  continue;
260 
261  // has this column already been inserted?
262  vector<size_t>::iterator
263  where = std::find(tmp[part].begin(), tmp[part].end(), (size_t) col);
264 
265  if (where == tmp[part].end()) {
266  tmp[part].push_back(col);
267  }
268  }
269  }
270  }
271 
272  // now I convert the STL vectors into Teuchos Array RCP's
273  for (int i = 0 ; i < NumLocalParts_ ; ++i) {
274  Parts_[i].resize(tmp[i].size());
275  for (size_t j = 0 ; j < tmp[i].size() ; ++j)
276  Parts_[i][j] = tmp[i][j];
277  }
278  }
279 }
280 
281 //==============================================================================
282 template<class GraphType>
283 bool OverlappingPartitioner<GraphType>::isComputed() const
284 {
285  return(IsComputed_);
286 }
287 
288 //==============================================================================
289 template<class GraphType>
290 std::ostream& OverlappingPartitioner<GraphType>::print(std::ostream& os) const
291 {
292  Teuchos::FancyOStream fos(Teuchos::rcp(&os,false));
293  fos.setOutputToRootOnly(0);
294  describe(fos);
295  return(os);
296 }
297 
298 //==============================================================================
299 template<class GraphType>
300 std::string OverlappingPartitioner<GraphType>::description() const
301 {
302  std::ostringstream oss;
303  oss << Teuchos::Describable::description();
304  if (isComputed()) {
305  oss << "{status = computed";
306  }
307  else {
308  oss << "{status = is not computed";
309  }
310  oss <<"}";
311  return oss.str();
312 }
313 
314 //==============================================================================
315 template<class GraphType>
316 void OverlappingPartitioner<GraphType>::describe(Teuchos::FancyOStream &os, const Teuchos::EVerbosityLevel verbLevel) const
317 {
318  using std::endl;
319  if(verbLevel==Teuchos::VERB_NONE) return;
320 
321  os << "================================================================================" << endl;
322  os << "Ifpack2::OverlappingPartitioner" << endl;
323  os << "Number of local rows = " << Graph_->getNodeNumRows() << endl;
324  os << "Number of global rows = " << Graph_->getGlobalNumRows() << endl;
325  os << "Number of local parts = " << NumLocalParts_ << endl;
326  os << "Overlapping level = " << OverlappingLevel_ << endl;
327  os << "Is computed = " << IsComputed_ << endl;
328  os << "================================================================================" << endl;
329 }
330 
331 
332 }// namespace Ifpack2
333 
334 #endif // IFPACK2_OVERLAPPINGPARTITIONER_DEF_HPP
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67