Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_Epetra.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 // We don't want warnings for using deprecated create_balanced_copy.
41 // TODO: remove this line when removing support of create_balanced_copy.
42 #ifdef __deprecated
43 #undef __deprecated
44 #endif
45 #define __deprecated
46
47 #include <Isorropia_Exception.hpp>
48 #include <Isorropia_Utils.hpp>
49 #include <Isorropia_Epetra.hpp>
50 #include <Isorropia_EpetraCostDescriber.hpp>
51 #include <Isorropia_EpetraPartitioner.hpp>
52 #include <Isorropia_EpetraRedistributor.hpp>
53
54 #ifdef HAVE_EPETRA
55 #include <Epetra_Map.h>
56 #include <Epetra_Import.h>
57 #include <Epetra_Vector.h>
58 #include <Epetra_Comm.h>
59 #include <Epetra_IntVector.h>
60 #include <Epetra_CrsGraph.h>
61 #include <Epetra_CrsMatrix.h>
62 #include <Epetra_RowMatrix.h>
63 #include <Epetra_LinearProblem.h>
64 #endif
65
66 #if defined(HAVE_EPETRA) && defined(HAVE_MPI)
67 #include <Epetra_MpiComm.h>
68 #endif
69
70 #ifdef HAVE_MPI
71 #include <mpi.h>
72 #endif
73
74 namespace Isorropia {
75
76 namespace Epetra {
77
78 #ifdef HAVE_EPETRA
79
80 Epetra_MultiVector* create_row_weights_nnz(const Epetra_RowMatrix& input_matrix)
81 {
82 int stride;
83 const Epetra_BlockMap& input_rowmap = input_matrix.RowMatrixRowMap();
84 Epetra_MultiVector* weights = new Epetra_MultiVector(input_rowmap, 1);
85 double* weights_ptr = 0;
86 weights->ExtractView(&weights_ptr, &stride);
87 int local_num_rows = input_rowmap.NumMyElements();
88
89 for(int i=0; i<local_num_rows; ++i) {
90 int nnz;
91 int err = input_matrix.NumMyRowEntries(i, nnz);
92 if (err != 0) {
93 throw Isorropia::Exception("create_row_weights_nnz: error in input_matrix.NumMyRowEntries");
94 }
95
96 weights_ptr[i] = 1.0*nnz;
97 }
98
99 return( weights );
100 }
101
102 Epetra_MultiVector* create_row_weights_nnz(const Epetra_CrsGraph& input_graph)
103 {
104 int stride;
105 const Epetra_BlockMap& input_rowmap = input_graph.RowMap();
106 Epetra_MultiVector* weights = new Epetra_MultiVector(input_rowmap, 1);
107 double* weights_ptr = 0;
108 weights->ExtractView(&weights_ptr, &stride);
109 int local_num_rows = input_rowmap.NumMyElements();
110
111 for(int i=0; i<local_num_rows; ++i) {
112 int nnz = input_graph.NumMyIndices(i);
113
114 weights_ptr[i] = 1.0*nnz;
115 }
116
117 return( weights );
118 }
119
120 Epetra_MultiVector* create_unit_weights(const Epetra_MultiVector& input_coords)
121 {
122 int stride;
123 double* weights_ptr = 0;
124
125 Epetra_MultiVector* weights = new Epetra_MultiVector(input_coords.Map(), 1);
126 weights->ExtractView(&weights_ptr, &stride);
127
128 for(int i=0; i<input_coords.MyLength(); ++i) {
129 weights_ptr[i] = 1.0;
130 }
131
132 return( weights );
133 }
134
135 double compute_imbalance(int nprocs, std::vector<int> &offsets, double *wgts, double target)
136 {
137 double imbalance = 1.0;
138
139 for (int p=0; p < nprocs; p++){
140
141 double pweight = 0.0;
142
143 for (int row=offsets[p]; row < offsets[p+1]; row++){
144 pweight += wgts[row];
145 }
146
147 double ib = 1.0;
148
149 if (pweight <= target)
150 ib += ((target - pweight) / target);
151 else
152 ib += ((pweight - target) / target);
153
154 if (ib > imbalance) imbalance = ib;
155 }
156
157 return imbalance;
158 }
159 int
160 repartition(const Epetra_BlockMap& input_map,
161 const Epetra_MultiVector& weights,
162 std::vector<int>& newPartition, // new partition for each of my objects
163 int& exportsSize, // how many of my objects are exports
164 std::vector<int>& imports) // list of gids I will import
165 {
166 if (!input_map.PointSameAs(weights.Map())) {
167 std::string str1("Epetra::repartition ERROR, input_map not ");
168 std::string str2("equivalent size/layout to weights.Map()");
169 throw Isorropia::Exception(str1+str2);
170 }
171 int stride=0;
172 double *vals = NULL;
173
174 const Epetra_Comm& comm = input_map.Comm();
175 int base = input_map.IndexBase();
176 int myPID = comm.MyPID();
177 int numProcs = comm.NumProc();
178
179 int globalSize = input_map.NumGlobalElements();
180 int localSize = input_map.NumMyElements();
181 int mySize = ((myPID==0) ? globalSize : 0);
182 int myCountSize = ((myPID==0) ? numProcs: 0);
183
184 // Create some maps. The weight vector is not necessarily contiguous by
185 // process rank, so we need to reorganize it.
186
187 // Distributed map of objects that are contiguous by process rank, and those
188 // objects gathered onto process zero
189
190 Epetra_BlockMap distMap(globalSize, localSize, 1, base, comm);
191 Epetra_BlockMap procZeroMap(globalSize, mySize, 1, base, comm);
192
193 // Map of counts for each process, and counts gathered on process 0
194
195 Epetra_BlockMap distCountMap(numProcs, 1, 1, base, comm);
196 Epetra_BlockMap procZeroCountMap(numProcs, myCountSize, 1, base, comm);
197
198 // Some importers
199
200 Epetra_Import DistToProcZero(procZeroMap, distMap);
201 Epetra_Import ProcZeroToDist(distMap, procZeroMap);
202 Epetra_Import CountsToProcZero(procZeroCountMap, distCountMap);
203 Epetra_Import CountsToProcs(distCountMap, procZeroCountMap);
204
205 // Move all weights to process 0
206
207 weights.ExtractView(&vals, &stride);
208 Epetra_Vector distWeights(Copy, distMap, vals);
209 Epetra_Vector weightVal(procZeroMap);
210 weightVal.Import(distWeights, DistToProcZero, Insert);
211
212 // Move the count of weights for each process to process 0
213
214 Epetra_Vector distCount(distCountMap);
215 distCount[0] = localSize;
216 Epetra_Vector weightCount(procZeroCountMap);
217 weightCount.Import(distCount, CountsToProcZero, Insert);
218
219 // Move all global IDs to process 0
220
221 double *myGIDs = new double [localSize];
222 for (int i=0; i<localSize; i++){
223 myGIDs[i] = input_map.GID(i);
224 }
225 Epetra_Vector localGIDs(View, distMap, myGIDs);
226 Epetra_Vector gids(procZeroMap);
227 gids.Import(localGIDs, DistToProcZero, Insert);
228 delete [] myGIDs;
229
230 // Process zero computes a new balance
231
232 double *numImports = new double [numProcs];
233 int totalImports=0;
234 double *importGIDs = NULL;
235 double *ids = NULL;
236
237 if (myPID == 0){
238
239 double total_weight=0.0;
240 weightVal.ExtractView(&vals);
241 for (int i=0; i<globalSize; i++){
242 total_weight += vals[i];
243 }
244
245 double avg_weight = total_weight/numProcs;
246
247 std::vector<int> all_proc_old_offsets(numProcs+1, globalSize);
248 std::vector<int> all_proc_new_offsets(numProcs+1, globalSize);
249
250 for (int i=numProcs-1; i >= 0 ; i--){
251 all_proc_old_offsets[i] = all_proc_old_offsets[i+1] - static_cast<int>(weightCount[i]);
252 }
253
254 double old_imbalance =
255 compute_imbalance(numProcs, all_proc_old_offsets, vals, avg_weight);
256
257 int offset = 0;
258 for(int p=0; p<numProcs; ++p) {
259 all_proc_new_offsets[p] = offset;
260
261 double tmp_weight = 0.0;
262
263 while(offset < globalSize && tmp_weight < avg_weight) {
264 tmp_weight += vals[offset++];
265 }
266 }
267
268 double new_imbalance =
269 compute_imbalance(numProcs, all_proc_new_offsets, vals, avg_weight);
270
271 // Because this is a quick and dirty partitioning, it is possible that
272 // if the balance was good to begin with, that we have just created a
273 // slightly worse balance. In that case, return to the old partitioning.
274
275 if (new_imbalance >= old_imbalance){
276 for (int proc=0; proc <= numProcs; proc++){
277 all_proc_new_offsets[proc] = all_proc_old_offsets[proc];
278 }
279 }
280
281 // Fill weight vector with the new process ID (partition ID) for each object
282
283 for (int proc=0; proc < numProcs; proc++){
284 int len = all_proc_new_offsets[proc+1] - all_proc_new_offsets[proc];
285 for (int j=0; j<len; j++){
286 *vals++ = static_cast<double>(proc);
287 }
288 }
289
290 // Count the number of imports for each process, and create a list of
291 // gids to be imported to each process
292
293 totalImports = 0;
294 importGIDs = new double [globalSize];
295 double *curr = importGIDs;
296 gids.ExtractView(&ids);
297
298 for (int proc=0; proc < numProcs; proc++){
299 numImports[proc] = 0;
300 int from = all_proc_new_offsets[proc];
301 int to = all_proc_new_offsets[proc+1];
302 int old_from = all_proc_old_offsets[proc];
303 int old_to = all_proc_old_offsets[proc+1];
304
305 for (int i=from; i<to ; i++){
306 if ((i< old_from) || (i >= old_to)){
307 *curr++ = ids[i];
308 numImports[proc]++;
309 }
310 }
311 totalImports += static_cast<int>(numImports[proc]);
312 }
313 }
314
315 // The new partition IDs are in the weightVal vector on process zero
316
317 distWeights.Import(weightVal, ProcZeroToDist, Insert);
318
319 newPartition.resize(localSize);
320 for (int i=0; i<localSize; i++){
321 newPartition[i] = static_cast<int>(distWeights[i]);
322 }
323
324 // Get the count of my imports - reuse the weightCount vector
325
326 int *indices = new int [numProcs];
327 for (int i=0; i<numProcs; i++){
328 indices[i] = i + base;
329 }
330
331 weightCount.ReplaceGlobalValues(((myPID==0) ? numProcs : 0), numImports, indices);
332 distCount.Import(weightCount, CountsToProcs, Insert);
333 delete [] indices;
334 if (numImports) delete [] numImports;
335
336 // Get the list of my imports
337
338 int numMyImports = static_cast<int>(distCount[0]);
339 double n1;
340 distCount.Norm1(&n1);
341 totalImports = static_cast<int>(n1);
342 int myImportSize = ((myPID==0) ? totalImports : 0);
343
344 Epetra_BlockMap distImportMap(totalImports, numMyImports, 1, base, comm);
345 Epetra_BlockMap proc0ImportMap(totalImports, myImportSize, 1, base, comm);
346 Epetra_Import importer(distImportMap, proc0ImportMap);
347
348 Epetra_Vector proc0imports(View, proc0ImportMap, importGIDs);
349 Epetra_Vector distImports(distImportMap);
350
351 distImports.Import(proc0imports, importer, Insert);
352
353 if (importGIDs) delete [] importGIDs;
354
355 imports.resize(numMyImports);
356 for (int i=0; i<numMyImports; i++){
357 imports[i] = static_cast<int>(distImports[i]);
358 }
359
360 // Finally, the number of exports
361
362 exportsSize = 0;
363 for (int i=0; i<localSize; i++){
364 if (newPartition[i] != myPID){
365 exportsSize++;
366 }
367 }
368
369 return 0;
370 }
371
372 /* New createBalancedCopy functions */
373
374 Epetra_MultiVector *
375 createBalancedCopy(const Epetra_MultiVector &coords)
376 {
377 Teuchos::ParameterList paramlist;
378
379 return createBalancedCopy(coords, paramlist);
380 }
381
382 Epetra_MultiVector *
383 createBalancedCopy(const Epetra_MultiVector &coords,
384 const Teuchos::ParameterList& paramlist)
385 {
386 Teuchos::RCP<const Epetra_MultiVector> coordRcp = Teuchos::rcp(&coords, false);
387
388 Teuchos::RCP<Partitioner> partitioner =
389 Teuchos::rcp(new Partitioner(coordRcp, paramlist));
390
391 Redistributor rd(partitioner);
392
393 Teuchos::RCP<Epetra_MultiVector> newVec = rd.redistribute(coords);
394
395 newVec.release();
396
397 return newVec.get();
398 }
399
400 Epetra_CrsGraph *
401 createBalancedCopy(const Epetra_CrsGraph& input_graph)
402 {
403 Teuchos::ParameterList paramlist;
404
405 return createBalancedCopy(input_graph, paramlist);
406 }
407
408 Epetra_CrsGraph *
409 createBalancedCopy(const Epetra_CrsGraph& input_graph,
410 const Teuchos::ParameterList& paramlist)
411 {
412 Teuchos::RCP<const Epetra_CrsGraph> rcp_input_graph =
413 Teuchos::rcp(&(input_graph), false);
414
415 Teuchos::RCP<Partitioner> partitioner =
416 Teuchos::rcp(new Partitioner(rcp_input_graph, paramlist));
417
418 Redistributor rd(partitioner);
419
420 Teuchos::RCP<Epetra_CrsGraph> balanced_graph =
421 rd.redistribute(input_graph);
422
423 balanced_graph.release();
424
425 return balanced_graph.get();
426 }
427
428 Epetra_CrsMatrix *
429 createBalancedCopy(const Epetra_CrsMatrix& input_matrix)
430 {
431 Teuchos::ParameterList paramlist;
432
433 return createBalancedCopy(input_matrix, paramlist);
434 }
435
436 Epetra_CrsMatrix *
437 createBalancedCopy(const Epetra_CrsMatrix& input_matrix,
438 const Teuchos::ParameterList& paramlist)
439 {
440 Teuchos::RCP<const Epetra_CrsGraph> input_graph =
441 Teuchos::rcp(&(input_matrix.Graph()), false);
442
443 Teuchos::RCP<Partitioner> partitioner =
444 Teuchos::rcp(new Partitioner(input_graph, paramlist));
445
446 Redistributor rd(partitioner);
447
448 Teuchos::RCP<Epetra_CrsMatrix> balanced_matrix =
449 rd.redistribute(input_matrix);
450
451 balanced_matrix.release();
452 return balanced_matrix.get();
453 }
454
455 Epetra_LinearProblem *
456 createBalancedCopy(const Epetra_LinearProblem& input_problem)
457 {
458 Teuchos::ParameterList paramlist;
459
460 return createBalancedCopy(input_problem, paramlist);
461 }
462
463 Epetra_LinearProblem *
464 createBalancedCopy(const Epetra_LinearProblem& input_problem,
465 const Teuchos::ParameterList& paramlist)
466 {
467 Teuchos::RCP<const Epetra_RowMatrix> rowmat =
468 Teuchos::rcp(input_problem.GetMatrix(), false);
469
470 Teuchos::RCP<Partitioner> partitioner =
471 Teuchos::rcp(new Partitioner(rowmat, paramlist));
472
473 Redistributor rd(partitioner);
474
475 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
476 rd.redistribute(*input_problem.GetMatrix());
477
478 Teuchos::RCP<Epetra_MultiVector> balanced_rhs =
479 rd.redistribute(*input_problem.GetRHS());
480
481 Teuchos::RCP<Epetra_MultiVector> x=
482 Teuchos::rcp(new Epetra_MultiVector(*input_problem.GetLHS()));
483
484 // prevent these from being deallocated on return from createBalancedCopy
485 balanced_matrix.release();
486 balanced_rhs.release();
487 x.release();
488
489 Teuchos::RCP<Epetra_LinearProblem> linprob =
490 Teuchos::rcp(new Epetra_LinearProblem(balanced_matrix.get(), x.get(), balanced_rhs.get()));
491
492 linprob.release();
493
494 return linprob.get();
495 }
496
497 /*
498 ************* Deprecated methods below - remove later ********************
499 */
500
501 Teuchos::RCP<Epetra_RowMatrix>
502 create_balanced_copy(const Epetra_RowMatrix& input_matrix)
503 {
504 CostDescriber costs;
505 Teuchos::ParameterList paramlist;
506
507 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
508 create_balanced_copy(input_matrix, costs, paramlist);
509
510 return balanced_matrix;
511 }
512
513 Teuchos::RCP<Epetra_RowMatrix>
514 create_balanced_copy(const Epetra_RowMatrix& input_matrix,
515 const Epetra_Vector &row_weights)
516 {
517 CostDescriber costs;
518 Teuchos::ParameterList paramlist;
519
520 Teuchos::RCP<const Epetra_Vector> vwgts = Teuchos::rcp(&row_weights);
521 vwgts.release();
522 costs.setVertexWeights(vwgts);
523
524 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
525 create_balanced_copy(input_matrix, costs, paramlist);
526
527 return balanced_matrix;
528 }
529
530 Teuchos::RCP<Epetra_RowMatrix>
531 create_balanced_copy(const Epetra_RowMatrix& input_matrix,
532 const Teuchos::ParameterList& paramlist)
533 {
534 CostDescriber costs;
535
536 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
537 create_balanced_copy(input_matrix, costs, paramlist);
538
539 return balanced_matrix;
540 }
541
542 Teuchos::RCP<Epetra_RowMatrix>
543 create_balanced_copy(const Epetra_RowMatrix& input_matrix,
544 CostDescriber &costs,
545 const Teuchos::ParameterList& paramlist)
546 {
547 Teuchos::RCP<const Epetra_RowMatrix> matrixPtr=
548 Teuchos::rcp(&(input_matrix), false);
549
550 Teuchos::RCP<CostDescriber> costPtr =
551 Teuchos::rcp(&(costs), false);
552
553 Teuchos::RCP<Partitioner> partitioner =
554 Teuchos::rcp(new Partitioner(matrixPtr, costPtr, paramlist));
555
556 Redistributor rd(partitioner);
557
558 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
559 rd.redistribute(input_matrix);
560
561 return balanced_matrix;
562 }
563
564 Teuchos::RCP<Epetra_CrsMatrix>
565 create_balanced_copy(const Epetra_CrsMatrix& input_matrix)
566 {
567 CostDescriber costs;
568 Teuchos::ParameterList paramlist;
569
570 Teuchos::RCP<Epetra_CrsMatrix> balanced_matrix =
571 create_balanced_copy(input_matrix, costs, paramlist);
572
573 return balanced_matrix;
574 }
575
576 Teuchos::RCP<Epetra_CrsMatrix>
577 create_balanced_copy(const Epetra_CrsMatrix& input_matrix,
578 const Epetra_Vector &row_weights)
579 {
580 CostDescriber costs;
581 Teuchos::ParameterList paramlist;
582
583 Teuchos::RCP<const Epetra_Vector> vwgts = Teuchos::rcp(&row_weights);
584 // release of ownership of vwgts, so destruction of stack objects
585 // does not do an extra destruction of row_weights and cause
586 // a fatal fault
587 vwgts.release();
588
589 costs.setVertexWeights(vwgts);
590
591 Teuchos::RCP<Epetra_CrsMatrix> balanced_matrix =
592 create_balanced_copy(input_matrix, costs, paramlist);
593
594 return balanced_matrix;
595 }
596
597 Teuchos::RCP<Epetra_CrsMatrix>
598 create_balanced_copy(const Epetra_CrsMatrix& input_matrix,
599 const Teuchos::ParameterList& paramlist)
600 {
601 CostDescriber costs;
602
603 Teuchos::RCP<Epetra_CrsMatrix> balanced_matrix =
604 create_balanced_copy(input_matrix, costs, paramlist);
605
606 return balanced_matrix;
607 }
608
609 Teuchos::RCP<Epetra_CrsMatrix>
610 create_balanced_copy(const Epetra_CrsMatrix& input_matrix,
611 CostDescriber &costs,
612 const Teuchos::ParameterList& paramlist)
613 {
614 Teuchos::RCP<const Epetra_CrsGraph> input_graph =
615 Teuchos::rcp(&(input_matrix.Graph()), false);
616
617 Teuchos::RCP<CostDescriber> costPtr =
618 Teuchos::rcp(&(costs), false);
619
620 Teuchos::RCP<Partitioner> partitioner =
621 Teuchos::rcp(new Partitioner(input_graph, costPtr, paramlist));
622
623 Redistributor rd(partitioner);
624
625 Teuchos::RCP<Epetra_CrsMatrix> balanced_matrix =
626 rd.redistribute(input_matrix);
627
628 return balanced_matrix;
629 }
630
631 Teuchos::RCP<Epetra_CrsGraph>
632 create_balanced_copy(const Epetra_CrsGraph& input_graph)
633 {
634 CostDescriber costs;
635 Teuchos::ParameterList paramlist;
636
637 Teuchos::RCP<Epetra_CrsGraph> balanced_graph =
638 create_balanced_copy(input_graph, costs, paramlist);
639
640 return balanced_graph;
641 }
642
643 Teuchos::RCP<Epetra_CrsGraph>
644 create_balanced_copy(const Epetra_CrsGraph& input_graph,
645 const Epetra_Vector &row_weights)
646 {
647 CostDescriber costs;
648 Teuchos::ParameterList paramlist;
649
650 Teuchos::RCP<const Epetra_Vector> vwgts = Teuchos::rcp(&row_weights);
651 // release of ownership of vwgts, so destruction of stack objects
652 // does not do an extra destruction of row_weights and cause
653 // a fatal fault
654 vwgts.release();
655
656 costs.setVertexWeights(vwgts);
657
658 Teuchos::RCP<Epetra_CrsGraph> balanced_graph =
659 create_balanced_copy(input_graph, costs, paramlist);
660
661 return balanced_graph;
662 }
663
664 Teuchos::RCP<Epetra_CrsGraph>
665 create_balanced_copy(const Epetra_CrsGraph& input_graph,
666 const Teuchos::ParameterList& paramlist)
667 {
668 CostDescriber costs;
669
670 Teuchos::RCP<Epetra_CrsGraph> balanced_graph =
671 create_balanced_copy(input_graph, costs, paramlist);
672
673 return balanced_graph;
674 }
675
676 Teuchos::RCP<Epetra_CrsGraph>
677 create_balanced_copy(const Epetra_CrsGraph& input_graph,
678 CostDescriber &costs,
679 const Teuchos::ParameterList& paramlist)
680 {
681 Teuchos::RCP<const Epetra_CrsGraph> graphPtr=
682 Teuchos::rcp(&(input_graph), false);
683
684 Teuchos::RCP<CostDescriber> costPtr =
685 Teuchos::rcp(&(costs), false);
686
687 Teuchos::RCP<Partitioner> partitioner =
688 Teuchos::rcp(new Partitioner(graphPtr, costPtr, paramlist));
689
690 Redistributor rd(partitioner);
691
692 Teuchos::RCP<Epetra_CrsGraph> balanced_graph =
693 rd.redistribute(input_graph);
694
695 return balanced_graph;
696 }
697
698 Teuchos::RCP<Epetra_LinearProblem>
699 create_balanced_copy(const Epetra_LinearProblem& input_problem)
700 {
701 CostDescriber costs;
702 Teuchos::ParameterList paramlist;
703
704 Teuchos::RCP<Epetra_LinearProblem> linprob =
705 create_balanced_copy(input_problem, costs, paramlist);
706
707 return linprob;
708 }
709
710 Teuchos::RCP<Epetra_LinearProblem>
711 create_balanced_copy(const Epetra_LinearProblem& input_problem,
712 const Epetra_Vector &row_weights)
713 {
714 CostDescriber costs;
715 Teuchos::ParameterList paramlist;
716
717 Teuchos::RCP<const Epetra_Vector> vwgts = Teuchos::rcp(&row_weights);
718 // release of ownership of vwgts, so destruction of stack objects
719 // does not do an extra destruction of row_weights and cause
720 // a fatal fault
721 vwgts.release();
722 costs.setVertexWeights(vwgts);
723
724 Teuchos::RCP<Epetra_LinearProblem> linprob =
725 create_balanced_copy(input_problem, costs, paramlist);
726
727 return linprob;
728 }
729
730 Teuchos::RCP<Epetra_LinearProblem>
731 create_balanced_copy(const Epetra_LinearProblem& input_problem,
732 const Teuchos::ParameterList& paramlist)
733 {
734 CostDescriber costs;
735
736 Teuchos::RCP<Epetra_LinearProblem> linprob =
737 create_balanced_copy(input_problem, costs, paramlist);
738
739 return linprob;
740 }
741
742 Teuchos::RCP<Epetra_LinearProblem>
743 create_balanced_copy(const Epetra_LinearProblem& input_problem,
744 CostDescriber &costs,
745 const Teuchos::ParameterList& paramlist)
746 {
747 Teuchos::RCP<const Epetra_RowMatrix> rowmat =
748 Teuchos::rcp(input_problem.GetMatrix(), false);
749
750 Teuchos::RCP<CostDescriber> costPtr =
751 Teuchos::rcp(&(costs), false);
752
753 Teuchos::RCP<Partitioner> partitioner =
754 Teuchos::rcp(new Partitioner(rowmat, costPtr, paramlist));
755
756 Redistributor rd(partitioner);
757
758 Teuchos::RCP<Epetra_RowMatrix> balanced_matrix =
759 rd.redistribute(*input_problem.GetMatrix());
760
761 Teuchos::RCP<Epetra_MultiVector> balanced_rhs =
762 rd.redistribute(*input_problem.GetRHS());
763
764 Teuchos::RCP<Epetra_MultiVector> x=
765 Teuchos::rcp(new Epetra_MultiVector(*input_problem.GetLHS()));
766
767 // prevent these from being deallocated on return from create_balanced_copy
768 balanced_matrix.release();
769 balanced_rhs.release();
770 x.release();
771
772 Teuchos::RCP<Epetra_LinearProblem> linprob =
773 Teuchos::rcp(new Epetra_LinearProblem(balanced_matrix.get(), x.get(), balanced_rhs.get()));
774
775 return( linprob );
776 }
777
778 Teuchos::RCP<Epetra_MultiVector>
779 create_balanced_copy(const Epetra_MultiVector &coords,
780 const Teuchos::ParameterList& paramlist)
781 {
782 // TODO make sure weights list with 0 vectors doesn't cause something
783 // to crash
784 Epetra_MultiVector noWeights(coords.Map(), 0);
785 return create_balanced_copy(coords, noWeights, paramlist);
786 }
787
788 Teuchos::RCP<Epetra_MultiVector>
789 create_balanced_copy(const Epetra_MultiVector &coords,
790 const Epetra_MultiVector &weights,
791 const Teuchos::ParameterList& paramlist)
792 {
793 Teuchos::RCP<const Epetra_MultiVector> coordRcp = Teuchos::rcp(&coords, false);
794 Teuchos::RCP<const Epetra_MultiVector> weightRcp = Teuchos::rcp(&weights, false);
795
796 Teuchos::RCP<Partitioner> partitioner =
797 Teuchos::rcp(new Partitioner(coordRcp, weightRcp, paramlist));
798
799 Redistributor rd(partitioner);
800
801 Teuchos::RCP<Epetra_MultiVector> newVec = rd.redistribute(coords);
802
803 return newVec;
804 }
805
806 #endif
807 }//namespace Epetra
808 }//namespace Isorropia