Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraRedistributor.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 #include <Isorropia_EpetraRedistributor.hpp>
41 #include <Isorropia_Exception.hpp>
42 #include <Isorropia_Epetra.hpp>
43 #include <Isorropia_EpetraPartitioner.hpp>
44
45 #ifdef USE_UTILS
46 #include <../../utils/ispatest_epetra_utils.hpp>
47 #include <../../utils/ispatest_lbeval_utils.hpp>
48 using namespace ispatest;
49 #endif
50
51 #include <Teuchos_RCP.hpp>
52
53 #ifdef HAVE_EPETRA
54 #include <Epetra_Map.h>
55 #include <Epetra_Import.h>
56 #include <Epetra_Vector.h>
57 #include <Epetra_MultiVector.h>
58 #include <Epetra_CrsGraph.h>
59 #include <Epetra_CrsMatrix.h>
60 #include <Epetra_LinearProblem.h>
61 #include <Epetra_Comm.h>
62 #endif
63
64
65 namespace Isorropia {
66
67 namespace Epetra {
68
69 #ifdef HAVE_EPETRA
70
71 Redistributor::Redistributor(Teuchos::RCP<Isorropia::Epetra::Partitioner> partitioner)
72 : partitioner_(partitioner),
73 importer_(),
74 target_map_()
75 {
76 if (!partitioner_->alreadyComputed()) {
77 partitioner_->partition();
78 }
79 }
80
81 Redistributor::Redistributor(Isorropia::Epetra::Partitioner *partitioner)
82 : partitioner_(Teuchos::RCP<Isorropia::Epetra::Partitioner>(partitioner,false)),
83 importer_(),
84 target_map_()
85 {
86 if (!partitioner_->alreadyComputed()) {
87 partitioner_->partition();
88 }
89 }
90
91 Redistributor::Redistributor(Teuchos::RCP<Epetra_Map> target_map)
92 : partitioner_(),
93 importer_(),
94 target_map_(target_map)
95 {
96 // Do not partition, target map is given.
97 }
98
99 Redistributor::Redistributor(Epetra_Map *target_map)
100 : partitioner_(),
101 importer_(),
102 target_map_(Teuchos::RCP<Epetra_Map>(target_map, false))
103 {
104 // Do not partition, target map is given.
105 }
106
107 Redistributor::~Redistributor()
108 {
109 }
110
111 void
112 Redistributor::redistribute(const Epetra_SrcDistObject& src,
113 Epetra_DistObject& target)
114 {
115 create_importer(src.Map());
116
117 target.Import(src, *importer_, Insert);
118 }
119
120 Teuchos::RCP<Epetra_CrsGraph>
121 Redistributor::redistribute(const Epetra_CrsGraph& input_graph, bool callFillComplete)
122 {
123 Epetra_CrsGraph *outputGraphPtr=0;
124 redistribute(input_graph, outputGraphPtr, callFillComplete);
125
126 return Teuchos::RCP<Epetra_CrsGraph>(outputGraphPtr);
127 }
128
129 void
130 Redistributor::redistribute(const Epetra_CrsGraph& input_graph, Epetra_CrsGraph * &outputGraphPtr, bool callFillComplete)
131 {
132 create_importer(input_graph.RowMap());
133
134 // First obtain the length of each of my new rows
135
136 int myOldRows = input_graph.NumMyRows();
137 int myNewRows = target_map_->NumMyElements();
138
139 double *nnz = new double [myOldRows];
140 for (int i=0; i < myOldRows; i++){
141 nnz[i] = input_graph.NumMyIndices(i);
142 }
143
144 Epetra_Vector oldRowSizes(Copy, input_graph.RowMap(), nnz);
145
146 if (myOldRows)
147 delete [] nnz;
148
149 Epetra_Vector newRowSizes(*target_map_);
150
151 newRowSizes.Import(oldRowSizes, *importer_, Insert);
152
153 int *rowSize = new int [myNewRows];
154 for (int i=0; i< myNewRows; i++){
155 rowSize[i] = static_cast<int>(newRowSizes[i]);
156 }
157
158 // Receive new rows, send old rows
159
160 outputGraphPtr = new Epetra_CrsGraph(Copy, *target_map_, rowSize, true);
161
162 if (myNewRows)
163 delete [] rowSize;
164
165 outputGraphPtr->Import(input_graph, *importer_, Insert);
166
167 // Set the new domain map such that
168 // (a) if old DomainMap == old RangeMap, preserve this property,
169 // (b) otherwise, let the new DomainMap be the old DomainMap
170 const Epetra_BlockMap *newDomainMap;
171 if (input_graph.DomainMap().SameAs(input_graph.RangeMap()))
172 newDomainMap = &(outputGraphPtr->RangeMap());
173 else
174 newDomainMap = &(input_graph.DomainMap());
175
176 if (callFillComplete && (!outputGraphPtr->Filled()))
177 outputGraphPtr->FillComplete(*newDomainMap, *target_map_);
178
179 return;
180 }
181
182 Teuchos::RCP<Epetra_CrsMatrix>
183 Redistributor::redistribute(const Epetra_CrsMatrix& inputMatrix, bool callFillComplete)
184 {
185 Epetra_CrsMatrix *outputMatrix=0;
186 redistribute(inputMatrix, outputMatrix, callFillComplete);
187
188 return Teuchos::RCP<Epetra_CrsMatrix>(outputMatrix);
189 }
190
191 void
192 Redistributor::redistribute(const Epetra_CrsMatrix& inputMatrix, Epetra_CrsMatrix * &outputMatrix, bool callFillComplete)
193 {
194 create_importer(inputMatrix.RowMap());
195
196 // First obtain the length of each of my new rows
197
198 int myOldRows = inputMatrix.NumMyRows();
199 int myNewRows = target_map_->NumMyElements();
200
201 double *nnz = new double [myOldRows];
202 for (int i=0; i < myOldRows; i++){
203 nnz[i] = inputMatrix.NumMyEntries(i);
204 }
205
206 Epetra_Vector oldRowSizes(Copy, inputMatrix.RowMap(), nnz);
207
208 if (myOldRows)
209 delete [] nnz;
210
211 Epetra_Vector newRowSizes(*target_map_);
212
213 newRowSizes.Import(oldRowSizes, *importer_, Insert);
214
215 int *rowSize=0;
216 if(myNewRows){
217 rowSize = new int [myNewRows];
218 for (int i=0; i< myNewRows; i++){
219 rowSize[i] = static_cast<int>(newRowSizes[i]);
220 }
221 }
222
223 // Receive new rows, send old rows
224
225 outputMatrix = new Epetra_CrsMatrix(Copy, *target_map_, rowSize, true);
226
227 if (myNewRows)
228 delete [] rowSize;
229
230 outputMatrix->Import(inputMatrix, *importer_, Insert);
231
232 // Set the new domain map such that
233 // (a) if old DomainMap == old RangeMap, preserve this property,
234 // (b) otherwise, let the new DomainMap be the old DomainMap
235 const Epetra_Map *newDomainMap;
236 if (inputMatrix.DomainMap().SameAs(inputMatrix.RangeMap()))
237 newDomainMap = &(outputMatrix->RangeMap());
238 else
239 newDomainMap = &(inputMatrix.DomainMap());
240
241 if (callFillComplete && (!outputMatrix->Filled()))
242 outputMatrix->FillComplete(*newDomainMap, *target_map_);
243
244 #ifdef USE_UTILS
245 // TODO: "PRINT ZOLTAN METRICS" should specify graph, hypergraph, or imbalance
246 // instead of just amount of output
247 // Metrics should be printed for each redistribute() method
248
249 if (!Teuchos::is_null(partitioner_) &&
250 partitioner_->printZoltanMetrics() > 0){
251 std::vector<double> balance(2), cutWgt(2), cutn(2), cutl(2);
252 std::vector<int> numCuts(2);
253 CostDescriber *defaultCosts = NULL;
254
255 Teuchos::RCP<CostDescriber> &cptr = partitioner_->getCosts();
256
257 if (Teuchos::is_null(cptr)){
258 defaultCosts = new CostDescriber;
259 cptr = Teuchos::rcp(defaultCosts);
260 }
261
262 int fail = cptr->compareBeforeAndAfterGraph( inputMatrix, *outputMatrix, *importer_,
263 balance, numCuts, cutWgt, cutn, cutl);
264
265 if (!fail){
266
267 if (partitioner_->printZoltanMetrics() > 1){
268 ispatest::printRowMatrix(inputMatrix, std::cout, "BEFORE:", true);
269 }
270 inputMatrix.Comm().Barrier();
271 if (inputMatrix.Comm().MyPID() == 0){
272 std::cout << "BEFORE: Imbalance " << balance[0] << ", cuts " << numCuts[0];
273 std::cout << ", cut weight " << cutWgt[0];
274 std::cout << ", CUTN " << cutn[0];
275 std::cout << ", CUTL " << cutl[0] << std::endl;
276 }
277
278 if (partitioner_->printZoltanMetrics() > 1){
279 ispatest::printRowMatrix(*outputMatrix, std::cout, "AFTER:", true);
280 }
281 inputMatrix.Comm().Barrier();
282 if (inputMatrix.Comm().MyPID() == 0){
283 std::cout << " AFTER: Imbalance " << balance[1] << ", cuts " << numCuts[1];
284 std::cout << ", cut weight " << cutWgt[1];
285 std::cout << ", CUTN " << cutn[1];
286 std::cout << ", CUTL " << cutl[1] << std::endl;
287 }
288 }
289 else{
290 if (inputMatrix.Comm().MyPID() == 0){
291 std::cout << " Error computing Zoltan quality metrics" << std::endl;
292 }
293 }
294 inputMatrix.Comm().Barrier();
295 }
296 #endif
297
298 return;
299 }
300
301 Teuchos::RCP<Epetra_CrsMatrix>
302 Redistributor::redistribute(const Epetra_RowMatrix& inputMatrix, bool callFillComplete)
303 {
304 Epetra_CrsMatrix *outputMatrix = 0;
305 redistribute(inputMatrix,outputMatrix,callFillComplete);
306 return Teuchos::RCP<Epetra_CrsMatrix>(outputMatrix);
307 }
308
309 void
310 Redistributor::redistribute(const Epetra_RowMatrix& inputMatrix, Epetra_CrsMatrix * &outputMatrix, bool callFillComplete)
311 {
312
313
314 create_importer(inputMatrix.RowMatrixRowMap());
315 // First obtain the length of each of my new rows
316
317 int myOldRows = inputMatrix.NumMyRows();
318 int myNewRows = target_map_->NumMyElements();
319
320
321 double *nnz = new double [myOldRows];
322 int val;
323 for (int i=0; i < myOldRows; i++){
324 inputMatrix.NumMyRowEntries(i, val);
325 nnz[i] = static_cast<double>(val);
326 }
327
328 Epetra_Vector oldRowSizes(Copy, inputMatrix.RowMatrixRowMap(), nnz);
329
330 if (myOldRows)
331 delete [] nnz;
332
333 Epetra_Vector newRowSizes(*target_map_);
334
335 newRowSizes.Import(oldRowSizes, *importer_, Insert);
336
337 int *rowSize = new int [myNewRows];
338 for (int i=0; i< myNewRows; i++){
339 rowSize[i] = static_cast<int>(newRowSizes[i]);
340 }
341
342 // Receive new rows, send old rows
343
344 outputMatrix = new Epetra_CrsMatrix(Copy, *target_map_, rowSize, true);
345
346 if (myNewRows)
347 delete [] rowSize;
348
349 outputMatrix->Import(inputMatrix, *importer_, Insert);
350
351 // Set the new domain map such that
352 // (a) if old DomainMap == old RangeMap, preserve this property,
353 // (b) otherwise, use the original OperatorDomainMap
354 //if (inputMatrix.NumGlobalRows() == inputMatrix.NumGlobalCols())
355 if (inputMatrix.OperatorDomainMap().SameAs(inputMatrix.OperatorRangeMap())){
356 if (callFillComplete && (!outputMatrix->Filled()))
357 outputMatrix->FillComplete();
358 }
359 else {
360 if (callFillComplete && (!outputMatrix->Filled()))
361 outputMatrix->FillComplete(inputMatrix.OperatorDomainMap(), *target_map_);
362 }
363
364 #ifdef USE_UTILS
365 // TODO: "PRINT ZOLTAN METRICS" should specify graph, hypergraph, or imbalance
366 // instead of just amount of output
367 // Metrics should be printed for each redistribute() method
368
369 if (!Teuchos::is_null(partitioner_) &&
370 partitioner_->printZoltanMetrics() > 0){
371 std::vector<double> balance(2), cutWgt(2), cutn(2), cutl(2);
372 std::vector<int> numCuts(2);
373 CostDescriber *defaultCosts = NULL;
374
375 Teuchos::RCP<CostDescriber> &cptr = partitioner_->getCosts();
376
377 if (Teuchos::is_null(cptr)){
378 defaultCosts = new CostDescriber;
379 cptr = Teuchos::rcp(defaultCosts);
380 }
381
382 int fail = cptr->compareBeforeAndAfterGraph( inputMatrix, *outputMatrix, *importer_,
383 balance, numCuts, cutWgt, cutn, cutl);
384 if (!fail){
385
386 if (partitioner_->printZoltanMetrics() > 1){
387 ispatest::printRowMatrix(inputMatrix, std::cout, "BEFORE:", true);
388 }
389 inputMatrix.Comm().Barrier();
390 if (inputMatrix.Comm().MyPID() == 0){
391 std::cout << "BEFORE: Imbalance " << balance[0] << ", cuts " << numCuts[0];
392 std::cout << ", cut weight " << cutWgt[0];
393 std::cout << ", CUTN " << cutn[0];
394 std::cout << ", CUTL " << cutl[0] << std::endl;
395 }
396
397 if (partitioner_->printZoltanMetrics() > 1){
398 ispatest::printRowMatrix(*outputMatrix, std::cout, "AFTER:", true);
399 }
400 inputMatrix.Comm().Barrier();
401 if (inputMatrix.Comm().MyPID() == 0){
402 std::cout << " AFTER: Imbalance " << balance[1] << ", cuts " << numCuts[1];
403 std::cout << ", cut weight " << cutWgt[1];
404 std::cout << ", CUTN " << cutn[1];
405 std::cout << ", CUTL " << cutl[1] << std::endl;
406 }
407 }
408 else{
409 if (inputMatrix.Comm().MyPID() == 0){
410 std::cout << " Error computing Zoltan quality metrics" << std::endl;
411 }
412 }
413 inputMatrix.Comm().Barrier();
414 }
415 #endif
416
417 return;
418 }
419
420 Teuchos::RCP<Epetra_Vector>
421 Redistributor::redistribute(const Epetra_Vector& input_vector)
422 {
423 Epetra_Vector *outputVector = 0;
424 redistribute(input_vector,outputVector);
425
426 return Teuchos::RCP<Epetra_Vector>(outputVector);
427 }
428
429 void
430 Redistributor::redistribute(const Epetra_Vector& inputVector, Epetra_Vector * &outputVector)
431 {
432 create_importer(inputVector.Map());
433
434 outputVector = new Epetra_Vector(*target_map_);
435
436 outputVector->Import(inputVector, *importer_, Insert);
437
438 return;
439 }
440
441 Teuchos::RCP<Epetra_MultiVector>
442 Redistributor::redistribute(const Epetra_MultiVector& input_vector)
443 {
444 Epetra_MultiVector *outputVector=0;
445 redistribute(input_vector,outputVector);
446
447 return Teuchos::RCP<Epetra_MultiVector>(outputVector);
448 }
449
450
451 void
452 Redistributor::redistribute(const Epetra_MultiVector& inputVector, Epetra_MultiVector * &outputVector)
453 {
454 create_importer(inputVector.Map());
455
456 outputVector = new Epetra_MultiVector(*target_map_, inputVector.NumVectors());
457
458 outputVector->Import(inputVector, *importer_, Insert);
459
460 return;
461 }
462
463
464 // Reverse redistribute methods (for vectors).
465
466 void
467 Redistributor::redistribute_reverse(const Epetra_Vector& input_vector, Epetra_Vector& output_vector)
468 {
469 create_importer(input_vector.Map());
470
471 // Export using the importer
472 output_vector.Export(input_vector, *importer_, Insert);
473
474 }
475
476 void
477 Redistributor::redistribute_reverse(const Epetra_MultiVector& input_vector, Epetra_MultiVector& output_vector)
478 {
479 create_importer(input_vector.Map());
480
481 // Export using the importer
482 output_vector.Export(input_vector, *importer_, Insert);
483
484 }
485
486 void Redistributor::create_importer(const Epetra_BlockMap& src_map)
487 {
488
489 if (!Teuchos::is_null(partitioner_) && partitioner_->numProperties() >
490 src_map.Comm().NumProc()) {
491 throw Isorropia::Exception("Cannot redistribute: Too many parts for too few processors.");
492 }
493
494 if (Teuchos::is_null(target_map_) && !Teuchos::is_null(partitioner_))
495 target_map_ = partitioner_->createNewMap();
496
497 importer_ = Teuchos::rcp(new Epetra_Import(*target_map_, src_map));
498
499 }
500
501 #endif //HAVE_EPETRA
502
503 }//namespace Epetra
504
505 }//namespace Isorropia
506