Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraPartitioner2D.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) 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 //************************************************************************
38 //@HEADER
39
40 // Assumes we have Epetra and Zoltan support
41 #include <Isorropia_EpetraPartitioner2D.hpp>
42 #include <Isorropia_Zoltan_Repartition.hpp>
43 #include <Isorropia_EpetraZoltanLib.hpp>
44 #include <Isorropia_Exception.hpp>
45 #include <Isorropia_Epetra.hpp>
46 #include <Teuchos_RCP.hpp>
47 #include <Teuchos_ParameterList.hpp>
48
49 #include <Epetra_Comm.h>
50 #include <Epetra_Map.h>
51 #include <Epetra_Import.h>
52 #include <Epetra_Vector.h>
53 #include <Epetra_MultiVector.h>
54 #include <Epetra_CrsGraph.h>
55 #include <Epetra_CrsMatrix.h>
56 #include <Epetra_LinearProblem.h>
57
58 #include <cstring>
59 #include <iostream>
60 #include <sstream>
61 #include <string>
62 #include <ctype.h>
63
64 namespace Isorropia {
65
66 namespace Epetra {
67
68
69 /** Constructor that accepts an Epetra_CrsGraph object, called by
70 API function create_partitioner().
71
72 \param input_graph Matrix-graph object for which a new partitioning
73 is to be computed. A Teuchos::RCP is used here because a
74 reference to the input object may be held by this object after
75 this constructor completes and returns.
76
77 \param paramlist Teuchos::ParameterList which will be copied to an
78 internal ParameterList attribute. No reference to this input
79 object is held after this constructor completes.<br>
80 If the ParameterList object contains a sublist named "Zoltan", then
81 the Zoltan library is used to perform the balancing. Also, any
82 parameters in the "Zoltan" sublist will be relayed directly to Zoltan.
83 Refer to the Zoltan users guide for specific parameters that Zoltan
84 recognizes. A couple of important ones are "LB_METHOD" (valid values
85 include "GRAPH", "HYPERGRAPH"), "DEBUG_LEVEL" (valid values are
86 0 to 10, default is 1), etc.
87
88 \param compute_partitioning_now Optional argument defaults to true.
89 If true, the method compute_partitioning() will be called before
90 this constructor returns.
91 */
92 Partitioner2D::Partitioner2D(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
93 const Teuchos::ParameterList& paramlist,
94 bool compute_partitioning_now):
95 Operator (input_graph, paramlist, 0)
96 {
97 if (compute_partitioning_now)
98 partition(true);
99 }
100
101 /**
102 Constructor that accepts an Epetra_RowMatrix object, called by
103 API function create_partitioner().
104
105 \param input_matrix Matrix object for which a new partitioning is
106 to be computed. A Teuchos::RCP is used here because a
107 reference to the input object may be held by this object after
108 this constructor completes and returns.
109
110 \param paramlist Teuchos::ParameterList which will be copied to an
111 internal ParameterList attribute. No reference to this input
112 object is held after this constructor completes.<br>
113 If the ParameterList object contains a sublist named "Zoltan", then
114 the Zoltan library is used to perform the balancing. Also, any
115 parameters in the "Zoltan" sublist will be relayed directly to Zoltan.
116 Refer to the Zoltan users guide for specific parameters that Zoltan
117 recognizes. A couple of important ones are "LB_METHOD" (valid values
118 include "GRAPH", "HYPERGRAPH"), "DEBUG_LEVEL" (valid values are
119 0 to 10, default is 1), etc.
120
121 \param compute_partitioning_now Optional argument defaults to true.
122 If true, the method compute_partitioning() will be called before
123 this constructor returns.
124 */
125 Partitioner2D::Partitioner2D(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
126 const Teuchos::ParameterList& paramlist,
127 bool compute_partitioning_now):
128 Operator (input_matrix, paramlist, 0)
129 {
130 if (compute_partitioning_now)
131 partition(true);
132 }
133
134
135 /** Destructor */
136 Partitioner2D::~Partitioner2D(){}
137
138 ////////////////////////////////////////////////////////////////////////////////
139 /** setParameters() is an internal Partitioner2D method which handles
140 the parameters from a Teuchos::ParameterList object.
141
142 The input
143 ParameterList object is copied into an internal ParameterList
144 attribute, and no reference to the input object is held after
145 this function returns. (Thus, the input paramlist object may be
146 altered or destroyed as soon as this method returns.)<br>
147 If the ParameterList object contains a sublist named "Zoltan", then
148 the Zoltan library is used to perform the balancing. Also, any
149 parameters in the "Zoltan" sublist will be relayed directly to Zoltan.
150 Refer to the Zoltan users guide for specific parameters that Zoltan
151 recognizes. A couple of important ones are "LB_METHOD" (valid values
152 include "GRAPH", "HYPERGRAPH"), "DEBUG_LEVEL" (valid values are
153 0 to 10, default is 1), etc.
154 */
155
156 /** partition is a method that computes
157 a rebalanced partitioning for the data in the object
158 that this class was constructed with.
159
160 \param force_repartitioning Optional argument defaults to false. By
161 default, compute_partitioning() only does anything the first time
162 it is called, and subsequent repeated calls are no-ops. If the user's
163 intent is to re-compute the partitioning (e.g., if parameters
164 or other inputs have been changed), then setting this flag to
165 true will force a new partitioning to be computed.
166 */
167 ////////////////////////////////////////////////////////////////////////////////
168 void Partitioner2D::
169 partition(bool force_repartitioning)
170 {
171 std::string partitioning_method_str("PARTITIONING METHOD");
172 std::string partitioning_method =
173 paramlist_.get(partitioning_method_str, "UNSPECIFIED");
174
175 if(partitioning_method == "UNSPECIFIED")
176 {
177 throw Isorropia::Exception("PARTITIONING_METHOD parameter must be specified.");
178 }
179
180 if(partitioning_method != "HYPERGRAPH2D")
181 {
182 throw Isorropia::Exception("PARTITIONING_METHOD parameter must be HYPERGRAPH2D.");
183 }
184
185 if (alreadyComputed() && !force_repartitioning)
186 return;
187
188 // Determine whether graph input or matrix input is used
189 if (input_graph_.get() != 0)
190 lib_ = Teuchos::rcp(new ZoltanLibClass(input_graph_, costs_,
191 Library::hgraph2d_finegrain_input_));
192 else
193 lib_ = Teuchos::rcp(new ZoltanLibClass(input_matrix_, costs_,
194 Library::hgraph2d_finegrain_input_));
195
196 std::string zoltan("ZOLTAN");
197 Teuchos::ParameterList &sublist = paramlist_.sublist(zoltan);
198
199 if (paramlist_.isParameter("NUM PARTS"))
200 {
201 sublist.set("NUM_GLOBAL_PARTS", paramlist_.get<std::string>("NUM PARTS"));
202 }
203 if (paramlist_.isParameter("IMBALANCE TOL"))
204 {
205 sublist.set("IMBALANCE_TOL", paramlist_.get<std::string>("IMBALANCE TOL"));
206 }
207
208 lib_->repartition(sublist, properties_, exportsSize_, imports_);
209 computeNumberOfProperties();
210 operation_already_computed_ = true;
211 }
212 ////////////////////////////////////////////////////////////////////////////////
213
214 ////////////////////////////////////////////////////////////////////////////////
215 void Partitioner2D::
216 compute(bool force_repartitioning)
217 {
218 partition(force_repartitioning);
219 }
220 ////////////////////////////////////////////////////////////////////////////////
221
222 // /** An internal method which determines whether the
223 // method compute_partitioning() has already been
224 // called on this class instance.
225 // */
226 //bool Partitioner2D::partitioning_already_computed() const
227 //{
228 // return (alreadyComputed());
229 //}
230
231 // /** An internal method which returns the new partition ID for a given element that
232 // resided locally in the old partitioning.
233 // */
234 //int Partitioner2D::newPartitionNumber(int myElem) const
235 //{
236 // return ((*this)[myElem]);
237 //}
238
239 /** An internal method which returns the number of elements in a given partition.
240
241 (Currently only implemented for the case where 'partition' is local.)
242 */
243 int Partitioner2D::numElemsInPart(int part) const
244 {
245 return (numElemsWithProperty(part));
246 }
247
248 ////////////////////////////////////////////////////////////////////////////////
249 /** An internal method which fills caller-allocated list (of length len) with the
250 global element ids to be located in the given partition.
251
252 (Currently only implemented for the case where 'partition' is local.)
253 */
254 ////////////////////////////////////////////////////////////////////////////////
255 void Partitioner2D::elemsInPart(int partition, int* elementList, int len) const
256 {
257 //MMW
258 std::cout << "MMW::NEED to reimplement" << std::endl;
259
260 return (elemsWithProperty(partition, elementList, len));
261 }
262 ////////////////////////////////////////////////////////////////////////////////
263
264 ////////////////////////////////////////////////////////////////////////////////
265 /** Returns indx for nonzero A(i,j) assuming this nonzero is located on this
266 processor. This is useful in getting a part number for this nonzero since
267 the [] operator needs this index. Returns -1 if nonzero not found locally.
268 */
269 ////////////////////////////////////////////////////////////////////////////////
270 int Partitioner2D::getNZIndx(int row, int column) const
271 {
272
273
274 if (input_graph_.get() != 0) // Graph
275 {
276 const Epetra_BlockMap *rowMap_ = &(input_graph_->RowMap());
277 const Epetra_BlockMap *colMap_ = &(input_graph_->ColMap());
278 int numRows = rowMap_->NumMyElements();
279
280 int nzIndx = 0;
281
282 for (int rowNum=0; rowNum<numRows; rowNum++)
283 {
284 int rowSize;
285 rowSize = input_graph_->NumMyIndices(rowNum);
286
287 if(row == rowNum)
288 {
289 int *tmprowCols = new int[rowSize];
290 int numEntries;
291
292 input_graph_->ExtractMyRowCopy (rowNum, rowSize, numEntries, tmprowCols);
293
294 for(int colIndx=0;colIndx<rowSize;colIndx++)
295 {
296
297 if (column == colMap_->GID(tmprowCols[colIndx]))
298 {
299 return nzIndx;
300 }
301 else
302 {
303 nzIndx++;
304 }
305 }
306
307 delete [] tmprowCols;
308 }
309 else
310 {
311 nzIndx += rowSize;
312 }
313
314
315 }
316
317
318
319 }
320 else // matrix
321 {
322
323 const Epetra_BlockMap *rowMap_ = &(input_matrix_->RowMatrixRowMap());
324 const Epetra_BlockMap *colMap_ = &(input_matrix_->RowMatrixColMap());
325 int numRows = rowMap_->NumMyElements();
326
327 int nzIndx = 0;
328 for (int rowNum=0; rowNum<numRows; rowNum++)
329 {
330 int rowSize;
331 input_matrix_->NumMyRowEntries(rowNum,rowSize);
332
333 if(row == rowNum)
334 {
335 int *tmprowCols = new int[rowSize];
336 double *tmprowVals = new double[rowSize];
337 int numEntries;
338
339 input_matrix_->ExtractMyRowCopy (rowNum, rowSize, numEntries, tmprowVals, tmprowCols);
340
341 for(int colIndx=0; colIndx<rowSize; colIndx++)
342 {
343 if (column == colMap_->GID(tmprowCols[colIndx]))
344 {
345 return nzIndx;
346 }
347 else
348 {
349 nzIndx++;
350 }
351 }
352 delete [] tmprowCols;
353 delete [] tmprowVals;
354 }
355 else
356 {
357 nzIndx += rowSize;
358 }
359 }
360 }
361
362 return -1;
363 }
364 ////////////////////////////////////////////////////////////////////////////////
365
366
367 ////////////////////////////////////////////////////////////////////////////////
368 ////////////////////////////////////////////////////////////////////////////////
369 int Partitioner2D::createDomainAndRangeMaps(Epetra_Map *domainMap,
370 Epetra_Map *rangeMap)
371 {
372 std::cout << "MMW::NEED to implement" << std::endl;
373
374 return 0;
375 }
376 ////////////////////////////////////////////////////////////////////////////////
377
378 ////////////////////////////////////////////////////////////////////////////////
379 ////////////////////////////////////////////////////////////////////////////////
380 int Partitioner2D::createColumnMap(Epetra_Map *colMap)
381 {
382 std::cout << "MMW::NEED to implement" << std::endl;
383
384 return 0;
385 }
386 ////////////////////////////////////////////////////////////////////////////////
387
388 ////////////////////////////////////////////////////////////////////////////////
389 ////////////////////////////////////////////////////////////////////////////////
390 int Partitioner2D::createRowMap(Epetra_Map *rowMap)
391 {
392 std::cout << "MMW::NEED to implement" << std::endl;
393
394 return 0;
395 }
396 ////////////////////////////////////////////////////////////////////////////////
397
398 } // namespace EPETRA
399
400 }//namespace Isorropia
401