Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraZoltanLib.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
41 #ifdef HAVE_ISORROPIA_ZOLTAN
42
43 #endif
44 #include <Isorropia_Exception.hpp>
45 #include <Isorropia_Epetra.hpp>
46 #include <Isorropia_EpetraCostDescriber.hpp>
47
48 #include <Teuchos_RCP.hpp>
49 #include <Teuchos_ParameterList.hpp>
50
51 #include <Isorropia_EpetraZoltanLib.hpp>
52
53 #ifdef HAVE_EPETRA
54 #include <Epetra_Comm.h>
55 #include <Epetra_Map.h>
56 #include <Epetra_Import.h>
57 #include <Epetra_Vector.h>
58 #include <Epetra_MultiVector.h>
59 #include <Epetra_CrsGraph.h>
60 #include <Epetra_CrsMatrix.h>
61 #include <Epetra_LinearProblem.h>
62 #ifdef HAVE_MPI
63 #include <Epetra_MpiComm.h>
64 #endif
65 #endif
66
67
68 #include <cstring>
69 #include <iostream>
70 #include <sstream>
71 #include <string>
72 #include <ctype.h>
73 #include <exception>
74
75 /* TODO: clean up the code */
76
77 namespace Isorropia {
78
79 #ifdef HAVE_EPETRA
80
81 namespace Epetra {
82
83 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
84 int inputType):
85 Library(input_graph, inputType)
86 {
87 }
88
89 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
90 Teuchos::RCP<const Epetra_MultiVector> input_coords, int inputType):
91 Library(input_graph, input_coords, inputType)
92 {
93 }
94
95 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
96 Teuchos::RCP<CostDescriber> costs,
97 int inputType):
98 Library(input_graph, costs, inputType)
99 {
100 }
101
102 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
103 Teuchos::RCP<CostDescriber> costs,
104 Teuchos::RCP<const Epetra_MultiVector> input_coords,
105 Teuchos::RCP<const Epetra_MultiVector> weights, int inputType):
106 Library(input_graph, costs, input_coords, weights, inputType)
107 {
108 int weightDim = weights->NumVectors();
109
110 if (weightDim > 1){
111 if (input_coords->Comm().MyPID() == 0){
112 std::cout << "WARNING: Zoltan will only use the first weight of the "<< weightDim << " supplied for each object" << std::endl;
113 }
114 }
115 }
116
117 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
118 int inputType):
119 Library(input_matrix, inputType)
120 {
121 }
122
123 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
124 Teuchos::RCP<const Epetra_MultiVector> input_coords,
125 int inputType):
126 Library(input_matrix, input_coords, inputType)
127 {
128 }
129
130 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
131 Teuchos::RCP<CostDescriber> costs, int inputType):
132 Library(input_matrix, costs, inputType)
133 {
134 }
135
136 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
137 Teuchos::RCP<CostDescriber> costs,
138 Teuchos::RCP<const Epetra_MultiVector> input_coords,
139 Teuchos::RCP<const Epetra_MultiVector> weights, int inputType):
140 Library(input_matrix, costs, input_coords, weights, inputType)
141 {
142 int weightDim = weights->NumVectors();
143
144 if (weightDim > 1){
145 if (input_coords->Comm().MyPID() == 0){
146 std::cout << "WARNING: Zoltan will only use the first weight of the "<< weightDim << " supplied for each object" << std::endl;
147 }
148 }
149 }
150
151 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_MultiVector> input_coords,
152 int inputType):
153 Library(input_coords, inputType)
154 {
155 }
156
157 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_MultiVector> input_coords,
158 Teuchos::RCP<const Epetra_MultiVector> weights,
159 int inputType):
160 Library(input_coords, weights, inputType)
161 {
162 int weightDim = weights->NumVectors();
163
164 if (weightDim > 1){
165 if (input_coords->Comm().MyPID() == 0){
166 std::cout << "WARNING: Zoltan will only use the first weight of the "<< weightDim << " supplied for each object" << std::endl;
167 }
168 }
169 }
170
171 ZoltanLibClass::ZoltanLibClass(Teuchos::RCP<const Epetra_BlockMap> input_map,
172 int inputType):
173 Library(input_map, inputType)
174 {
175 }
176
177
178
179 int ZoltanLibClass::precompute()
180 {
181 std::string str1("Isorropia::ZoltanLibClass::precompute ");
182 MPI_Comm mpicomm = zoltan_get_global_comm();
183 #ifdef HAVE_MPI
184 MPI_Comm default_mpicomm = zoltan_get_global_comm();
185 #endif // HAVE_MPI
186 int itype;
187
188 Library::precompute(); // assumes input_type_ is set
189
190 if (input_graph_.get() || input_matrix_.get())
191 {
192 if (input_type_ != hgraph2d_finegrain_input_){
193 computeCost(); // graph or hypergraph weights
194 }
195 }
196
197 if (input_type_ == graph_input_)
198 itype = ZoltanLib::QueryObject::graph_input_;
199 else if (input_type_ == hgraph_input_)
200 itype = ZoltanLib::QueryObject::hgraph_input_;
201 else if (input_type_ == hgraph2d_finegrain_input_)
202 itype = ZoltanLib::QueryObject::hgraph2d_finegrain_input_;
203 else if (input_type_ == geometric_input_)
204 itype = ZoltanLib::QueryObject::geometric_input_;
205 else if (input_type_ == simple_input_)
206 itype = ZoltanLib::QueryObject::simple_input_;
207 else if (input_type_ == hgraph_graph_input_) // hierarchical partitioning
208 itype = ZoltanLib::QueryObject::hgraph_graph_input_;
209 else if (input_type_ == hgraph_geometric_input_) // hierarchical partitioning
210 itype = ZoltanLib::QueryObject::hgraph_geometric_input_;
211 else if (input_type_ == graph_geometric_input_) // hierarchical partitioning
212 itype = ZoltanLib::QueryObject::graph_geometric_input_;
213 else if (input_type_ == hgraph_graph_geometric_input_) // hierarchical partitioning
214 itype = ZoltanLib::QueryObject::hgraph_graph_geometric_input_;
215 else
216 itype = ZoltanLib::QueryObject::unspecified_input_;
217
218
219 if (input_graph_.get() !=0 && input_coords_.get()!=0) //geometric and graph inputs
220 {
221 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_graph_, costs_, input_coords_, weights_, itype));
222 #ifdef HAVE_MPI
223 const Epetra_Comm &ecomm = input_graph_->RowMap().Comm();
224 try
225 {
226 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
227 mpicomm = empicomm.Comm();
228 }
229 catch (std::exception& e)
230 {
231 // Serial Comm with MPI
232 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
233 }
234 #endif
235 }
236 else if (input_matrix_.get() !=0 && input_coords_.get()!=0) //geometric and matrix inputs
237 {
238 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_matrix_, costs_, input_coords_, weights_, itype));
239 #ifdef HAVE_MPI
240 const Epetra_Comm &ecomm = input_matrix_->RowMatrixRowMap().Comm();
241 try
242 {
243 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
244 mpicomm = empicomm.Comm();
245 }
246 catch (std::exception& e)
247 {
248 // Serial Comm with MPI
249 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
250 }
251 #endif
252 }
253 else if (input_graph_.get() != 0) //graph inputs
254 {
255 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_graph_, costs_, itype));
256 #ifdef HAVE_MPI
257 const Epetra_Comm &ecomm = input_graph_->RowMap().Comm();
258 try
259 {
260 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
261 mpicomm = empicomm.Comm();
262 }
263 catch (std::exception& e)
264 {
265 // Serial Comm with MPI
266 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
267 }
268 #endif
269 }
270 else if (input_matrix_.get() != 0) //matrix inputs
271 {
272 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_matrix_, costs_, itype));
273 #ifdef HAVE_MPI
274 const Epetra_Comm &ecomm = input_matrix_->RowMatrixRowMap().Comm();
275 try
276 {
277 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
278 mpicomm = empicomm.Comm();
279 }
280 catch (std::exception& e)
281 {
282 // Serial Comm with MPI
283 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
284 }
285 #endif
286 }
287 else if (input_coords_.get() != 0) // coord inputs
288 {
289 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_coords_, weights_));
290 #ifdef HAVE_MPI
291 const Epetra_Comm &ecomm = input_coords_->Map().Comm();
292 try
293 {
294 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
295 mpicomm = empicomm.Comm();
296 }
297 catch (std::exception& e)
298 {
299 // Serial Comm with MPI
300 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
301 }
302 #endif
303 }
304 else // BlockMap inputs
305 {
306 queryObject_ = Teuchos::rcp(new ZoltanLib::QueryObject(input_map_, itype));
307 #ifdef HAVE_MPI
308 const Epetra_Comm &ecomm = input_map_->Comm();
309 try
310 {
311 const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm);
312 mpicomm = empicomm.Comm();
313 }
314 catch (std::exception& e)
315 {
316 // Serial Comm with MPI
317 MPI_Comm_split(default_mpicomm, ecomm.MyPID(), 0, &mpicomm);
318 }
319 #endif
320 }
321
322
323
324 float version;
325 int argcTmp=0;
326 char *argvTmp[1];
327 std::string lb_method_str("LB_METHOD");
328
329 // create a Zoltan problem
330
331 argvTmp[0] = NULL;
332 Zoltan_Initialize(argcTmp, argvTmp, &version);
333
334 zz_ = new Zoltan(mpicomm);
335
336 if (zz_ == NULL){
337 throw Isorropia::Exception("Error creating Zoltan object");
338 return (-1);
339 }
340
341 //////////////////////////
342 // set problem parameters
343 //////////////////////////
344
345 std::string dbg_level_str("DEBUG_LEVEL");
346 if (!zoltanParamList_.isParameter(dbg_level_str))
347 {
348 zoltanParamList_.set(dbg_level_str, "0");
349 }
350
351 if (!zoltanParamList_.isParameter(lb_method_str)) //set default parameters
352 {
353 if (input_type_ == graph_input_)
354 {
355 zoltanParamList_.set(lb_method_str, "GRAPH");
356 }
357 else if (input_type_ == geometric_input_)
358 {
359 if (!zoltanParamList_.isParameter(lb_method_str)) //MMW: Don't think this if is needed
360 zoltanParamList_.set(lb_method_str, "RCB");
361 }
362 else if (input_type_ == simple_input_) //not sure this is needed
363 {
364 zoltanParamList_.set(lb_method_str, "BLOCK");
365 }
366 else if (input_type_ == hgraph_graph_input_ || input_type_ == hgraph_geometric_input_ ||
367 input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_ )
368 {
369 zoltanParamList_.set(lb_method_str, "HIER");
370 }
371 else
372 {
373 zoltanParamList_.set(lb_method_str, "HYPERGRAPH");
374 }
375 }
376
377 // Make LB_APPROACH = PARTITION the default in Isorropia
378 std::string lb_approach_str("LB_APPROACH");
379 if (!zoltanParamList_.isParameter(lb_approach_str)) {
380 zoltanParamList_.set(lb_approach_str, "PARTITION");
381 }
382
383 // For fine-grain hypergraph, we don't want obj or (hyper)edge weights
384 if (input_type_ == hgraph2d_finegrain_input_)
385 {
386 zoltanParamList_.set("OBJ_WEIGHT_DIM", "0");
387 zoltanParamList_.set("EDGE_WEIGHT_DIM", "0");
388 }
389 else if (input_type_ == geometric_input_)
390 {
391 // We always overwrite user choice.
392 // if (!zoltanParamList_.isParameter("OBJ_WEIGHT_DIM")) {
393 if (weights_.get())
394 {
395 zoltanParamList_.set("OBJ_WEIGHT_DIM", "1");
396 }
397 else
398 {
399 zoltanParamList_.set("OBJ_WEIGHT_DIM", "0");
400 }
401 //}
402 }
403 else if(input_type_ != simple_input_) //graph or hypergraph
404 {
405 if (queryObject_->haveVertexWeights())
406 {
407 if (!zoltanParamList_.isParameter("OBJ_WEIGHT_DIM"))
408 {
409 zoltanParamList_.set("OBJ_WEIGHT_DIM", "1");
410 }
411 }
412
413 if (queryObject_->haveGraphEdgeWeights() ||
414 queryObject_->haveHypergraphEdgeWeights())
415 {
416 if (!zoltanParamList_.isParameter("EDGE_WEIGHT_DIM"))
417 {
418 zoltanParamList_.set("EDGE_WEIGHT_DIM", "1");
419 }
420 }
421 }
422
423 // For fine-grain hypergraph, we will use (row, col) of nz for
424 // vertex GIDs. Don't need LIDs.
425
426 if (input_type_ == hgraph2d_finegrain_input_)
427 {
428 zoltanParamList_.set("NUM_GID_ENTRIES", "2");
429 zoltanParamList_.set("NUM_LID_ENTRIES", "1");
430 }
431
432 Teuchos::ParameterList::ConstIterator
433 iter = zoltanParamList_.begin(),
434 iter_end = zoltanParamList_.end();
435
436 for(; iter != iter_end; ++iter)
437 {
438 const std::string& name = iter->first;
439 const std::string& value = Teuchos::getValue<std::string>(iter->second);
440 zz_->Set_Param(name, value);
441 }
442
443 // Set the query functions
444
445 zz_->Set_Num_Obj_Fn(ZoltanLib::QueryObject::Number_Objects, (void *)queryObject_.get());
446 zz_->Set_Obj_List_Fn(ZoltanLib::QueryObject::Object_List, (void *)queryObject_.get());
447
448 int ierr;
449 num_obj_ = ZoltanLib::QueryObject::Number_Objects((void *)queryObject_.get(), &ierr);
450
451
452 if (input_type_ == hgraph2d_finegrain_input_)
453 {
454 zz_->Set_HG_Size_CS_Fn(ZoltanLib::QueryObject::HG_Size_CS, (void *)queryObject_.get());
455 zz_->Set_HG_CS_Fn(ZoltanLib::QueryObject::HG_CS, (void *)queryObject_.get());
456 }
457 if (input_type_ == hgraph_input_ || input_type_ == hgraph_graph_input_ ||
458 input_type_ == hgraph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
459 {
460 zz_->Set_HG_Size_CS_Fn(ZoltanLib::QueryObject::HG_Size_CS, (void *)queryObject_.get());
461 zz_->Set_HG_CS_Fn(ZoltanLib::QueryObject::HG_CS, (void *)queryObject_.get());
462 zz_->Set_HG_Size_Edge_Wts_Fn(ZoltanLib::QueryObject::HG_Size_Edge_Weights,
463 (void *)queryObject_.get());
464 zz_->Set_HG_Edge_Wts_Fn(ZoltanLib::QueryObject::HG_Edge_Weights, (void *)queryObject_.get());
465 }
466 if (input_type_ == graph_input_ || input_type_ == hgraph_graph_input_ ||
467 input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
468 {
469 zz_->Set_Num_Edges_Multi_Fn(ZoltanLib::QueryObject::Number_Edges_Multi, (void *)queryObject_.get());
470 zz_->Set_Edge_List_Multi_Fn(ZoltanLib::QueryObject::Edge_List_Multi, (void *)queryObject_.get());
471 }
472 if (input_type_ == geometric_input_ || input_type_ == hgraph_geometric_input_ ||
473 input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
474 {
475 zz_->Set_Num_Geom_Fn(ZoltanLib::QueryObject::Number_Geom, (void *)queryObject_.get());
476 zz_->Set_Geom_Multi_Fn(ZoltanLib::QueryObject::Geom_Multi, (void *)queryObject_.get());
477 }
478
479 return (ierr);
480 }
481
482 void ZoltanLibClass::computeCost()
483 {
484 std::string str1("Isorropia::ZoltanLibClass::computeCost ");
485 std::string str2;
486
487
488 const Epetra_Comm &comm = input_map_->Comm();
489
490 // If vertex/edge costs have been set, do a global operation to find
491 // out how many weights were given. Some processes may provide no
492 // weights - they need to be informed that weights are being provided
493 // by the application. Do some sanity checks.
494
495 int err = 0;
496 int gerr = 0;
497 int base = input_map_->IndexBase();
498
499 int numMyVWeights = 0;
500 int numMyGWeights = 0;
501 int numMyHGWeights = 0;
502 int globalNumCols = 0;
503 int myNZ = 0;
504 int globalNZ = 0;
505 int mySelfEdges = 0;
506 int globalSelfEdges = 0;
507
508 int myRows = input_map_->NumMyElements();
509 int globalNumRows = input_map_->NumGlobalElements();
510
511 if (input_graph_.get() == 0) //matrix
512 {
513 myNZ = input_matrix_->NumMyNonzeros();
514 mySelfEdges = input_matrix_->NumMyDiagonals();
515 globalNZ = input_matrix_->NumGlobalNonzeros();
516 globalSelfEdges = input_matrix_->NumGlobalDiagonals();
517 globalNumCols = input_matrix_->NumGlobalCols();
518 }
519 else //graph
520 {
521 myNZ = input_graph_->NumMyNonzeros();
522 mySelfEdges = input_graph_->NumMyDiagonals();
523 globalNZ = input_graph_->NumGlobalNonzeros();
524 globalSelfEdges = input_graph_->NumGlobalDiagonals();
525 globalNumCols = input_graph_->NumGlobalCols();
526 }
527
528 if (costs_.get() != 0)
529 {
530
531 numMyVWeights = costs_->getNumVertices();
532
533 if (costs_->haveGraphEdgeWeights()){
534 for (int i=0; i<numMyVWeights; i++){
535 int gid = input_map_->GID(i);
536 if (gid >= base){
537 numMyGWeights += costs_->getNumGraphEdges(gid);
538 }
539 }
540 }
541 numMyHGWeights = costs_->getNumHypergraphEdgeWeights();
542
543 if ((numMyVWeights > 0) && (numMyVWeights != myRows)){
544 str2 = "Number of my vertex weights != number of my rows";
545 err = 1;
546 }
547 else if ((numMyGWeights > 0) && (numMyGWeights != (myNZ - mySelfEdges))){
548 str2 = "Number of my graph edge weights != number of my nonzeros";
549 err = 1;
550 }
551 }
552 else{
553 costs_ = Teuchos::rcp(new CostDescriber());
554 }
555
556 comm.SumAll(&err, &gerr ,1);
557
558 if (gerr > 0){
559 throw Isorropia::Exception(str1+str2);
560 }
561
562 int lval[4], gval[4];
563 lval[0] = numMyVWeights;
564 lval[1] = numMyGWeights;
565 lval[2] = numMyHGWeights;
566
567 comm.SumAll(lval, gval, 3);
568
569 int numVWeights = gval[0];
570 int numGWeights = gval[1];
571 int numHGWeights = gval[2];
572
573 if ((numVWeights > 0) && (numVWeights != globalNumRows)){
574 str2 = "Number of vertex weights supplied by application != number of rows";
575 throw Isorropia::Exception(str1+str2);
576 }
577 if ((numGWeights > 0) && (numGWeights != (globalNZ - globalSelfEdges))){
578 str2 = "Number of graph edge weights supplied by application != number of edges";
579 throw Isorropia::Exception(str1+str2);
580 }
581 if ((numHGWeights > 0) && (numHGWeights < globalNumCols)){
582 str2 = "Number of hyperedge weights supplied by application < number of columns";
583 throw Isorropia::Exception(str1+str2);
584 }
585
586 costs_->setNumGlobalVertexWeights(numVWeights);
587 costs_->setNumGlobalGraphEdgeWeights(numGWeights);
588 costs_->setNumGlobalHypergraphEdgeWeights(numHGWeights);
589 }
590
591
592 void ZoltanLibClass::preCheckPartition()
593 {
594 std::string str1("Isorropia::ZoltanLibClass::precheckPartition ");
595 std::string str2;
596
597 const Epetra_Comm &comm = input_map_->Comm();
598 int localProc = comm.MyPID();
599 int nprocs = comm.NumProc();
600
601 // Checks for Zoltan parameters NUM_GLOBAL_PARTS and NUM_LOCAL_PARTS.
602
603 std::string gparts_str("NUM_GLOBAL_PARTS");
604 std::string lparts_str("NUM_LOCAL_PARTS");
605 std::string gparts("0");
606 std::string lparts("0");
607
608 if (zoltanParamList_.isParameter(gparts_str)){
609 gparts = zoltanParamList_.get<std::string>(gparts_str);
610 }
611 if (zoltanParamList_.isParameter(lparts_str)){
612 lparts = zoltanParamList_.get<std::string>(lparts_str);
613 }
614
615 int myGparts = atoi(gparts.c_str());
616 int myLparts = atoi(lparts.c_str());
617 int maxGparts, maxLparts, sumLparts;
618 int fixGparts = -1;
619 int fixLparts = -1;
620 int numrows = input_map_->NumGlobalElements();
621
622 int myParts[] = {myGparts, myLparts};
623 int maxParts[2];
624
625 comm.MaxAll(myParts, maxParts, 2);
626
627 maxGparts = maxParts[0];
628 maxLparts = maxParts[1];
629
630 // Fix problem if the number of rows is less than the number
631 // of processes. We need to set NUM_GLOBAL_PARTS to
632 // the number of rows, unless it was already set to something
633 // greater than 0 but less than the number of rows.
634
635 if (numrows < nprocs){
636 if ((maxGparts == 0) || (maxGparts > numrows)){
637 maxGparts = numrows;
638 }
639 }
640
641 if (maxLparts > 0)
642 comm.SumAll(&myLparts, &sumLparts, 1);
643 else
644 sumLparts = 0;
645
646 if ((maxGparts > 0) || (maxLparts > 0)){
647 // One or more processes set NGP or NLP so we need to check
648 // them for validity.
649
650 if (maxGparts != myGparts){
651 fixGparts = maxGparts; // all procs should have same NGP
652 }
653
654 if (maxGparts > 0){
655 if (maxGparts > numrows){
656 // This is an error because we can't split rows among partitions
657 str2 = "NUM_GLOBAL_PARTS exceeds number of rows (objects to be partitioned)";
658 throw Isorropia::Exception(str1+str2);
659 }
660
661 if ((sumLparts > 0) && (sumLparts != maxGparts)){
662 // This is an error because local partitions must sum to number of global
663 str2 = "NUM_GLOBAL_PARTS not equal to sum of NUM_LOCAL_PARTS";
664 throw Isorropia::Exception(str1+str2);
665 }
666
667 if ((sumLparts == 0) && (maxGparts < nprocs)){
668 // Set NUM_LOCAL_PARTS to 1 or 0, because Zoltan will divide
669 // a partition across 2 or more processes when the number of
670 // partitions is less than the number of processes. This doesn't
671 // work for Epetra matrices, where rows are not owned by more than
672 // one process.
673
674 fixLparts = (localProc < maxGparts) ? 1 : 0;
675 }
676 }
677 else if (maxLparts > 0){
678
679 // Set NUM_GLOBAL_PARTS to sum of local partitions.
680 // Zoltan does this already, but just to be safe...
681
682 fixGparts = sumLparts;
683 }
684 if (fixGparts > 0){
685 std::ostringstream os;
686 os << fixGparts;
687 std::string s = os.str();
688 zoltanParamList_.set(gparts_str, s);
689 }
690 if (fixLparts >= 0){
691 std::ostringstream os;
692 os << fixLparts;
693 std::string s = os.str();
694 zoltanParamList_.set(lparts_str, s);
695 }
696 }
697 }
698
699 int ZoltanLibClass::
700 repartition(Teuchos::ParameterList& zoltanParamList,
701 std::vector<int>& properties,
702 int& exportsSize,
703 std::vector<int>& imports)
704 {
705 zoltanParamList_ = zoltanParamList;
706
707 // Avoid to construct import list.
708 // Perhaps "PARTITION ASSIGNMENTS" will be better in term of performance.
709 zoltanParamList_.set("RETURN_LISTS", "EXPORT AND IMPORT");
710
711 preCheckPartition();
712 precompute();
713
714 // Set part sizes
715
716 if (numPartSizes > 0){
717 int err;
718 int *wgtIdx = NULL;
719
720 err = zz_->LB_Set_Part_Sizes(1, numPartSizes, partGIDs, wgtIdx, partSizes);
721
722 if (err != ZOLTAN_OK){
723 throw Isorropia::Exception("Error in LB_Set_Part_Sizes");
724 return -1;
725 }
726 }
727
728 //Generate Load Balance
729 int changes=0, num_gid_entries=0, num_lid_entries=0, num_import=0, num_export=0;
730 ZOLTAN_ID_PTR import_global_ids=NULL, import_local_ids=NULL;
731 ZOLTAN_ID_PTR export_global_ids=NULL, export_local_ids=NULL;
732 int * import_procs=NULL, * export_procs=NULL;
733 int *import_to_part=NULL, *export_to_part=NULL;
734
735 int err = zz_->LB_Partition(changes, num_gid_entries, num_lid_entries,
736 num_import, import_global_ids, import_local_ids, import_procs, import_to_part,
737 num_export, export_global_ids, export_local_ids, export_procs, export_to_part );
738
739 if (err != ZOLTAN_OK){
740 throw Isorropia::Exception("Error computing partitioning with Zoltan");
741 return -1;
742 }
743
744 exportsSize = num_export;
745 imports.clear();
746 imports.assign(import_global_ids, import_global_ids + num_import);
747
748 properties.assign(num_obj_, queryObject_->RowMap().Comm().MyPID());
749
750 for( int i = 0; i < num_export; ++i )
751 {
752 properties[export_local_ids[i]] = export_to_part[i];
753 }
754
755 //Free Zoltan Data
756 zz_->LB_Free_Part(&import_global_ids, &import_local_ids,
757 &import_procs, &import_to_part);
758 zz_->LB_Free_Part(&export_global_ids, &export_local_ids,
759 &export_procs, &export_to_part);
760
761 postcompute();
762
763 return (0);
764 }
765
766 int ZoltanLibClass::
767 color(Teuchos::ParameterList& zoltanParamList,
768 std::vector<int>& properties)
769 {
770 zoltanParamList_ = zoltanParamList;
771 // Distance 2 coloring by default.
772 if (!zoltanParamList_.isParameter("COLORING_PROBLEM"))
773 zoltanParamList_.set("COLORING_PROBLEM", "DISTANCE-2");
774
775 precompute();
776
777 //Generate Load Balance
778 int num_gid_entries = 1;
779
780 // Use ColMap to have directly answers for columns !
781 properties.resize(num_obj_);
782
783 // TODO64 - This only works if the global IDs fit in an int.
784
785 ZOLTAN_ID_TYPE *gids=NULL;
786
787 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
788 gids = new ZOLTAN_ID_TYPE [num_obj_];
789 if (num_obj_ && ! gids){
790 throw Isorropia::Exception("Out of memory in ZoltanLibClass::color()");
791 return -1;
792 }
793
794 int *intIds = queryObject_->RowMap().MyGlobalElements();
795 for (int i=0; i < num_obj_; i++){
796 gids[i] = (ZOLTAN_ID_TYPE)intIds[i];
797 }
798 }
799 else{
800 gids = (ZOLTAN_ID_PTR)queryObject_->RowMap().MyGlobalElements();
801 }
802
803 int err = zz_->Color(num_gid_entries, num_obj_, gids, &properties[0]);
804
805 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
806 delete [] gids;
807 }
808
809 if (err != ZOLTAN_OK){
810 throw Isorropia::Exception("Error computing coloring with Zoltan");
811 return -1;
812 }
813
814 postcompute();
815 return (0);
816 }
817
818 int ZoltanLibClass::
819 order(Teuchos::ParameterList& zoltanParamList,
820 std::vector<int>& properties)
821 {
822 zoltanParamList_ = zoltanParamList;
823
824 precompute();
825
826 /* Note : this works because epetra ordinal type is int */
827 int num_gid_entries = 1;
828
829 properties.resize(num_obj_);
830
831 // TODO64 - This only works if the global IDs fit in an int.
832
833 ZOLTAN_ID_TYPE *gids=NULL;
834
835 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
836 gids = new ZOLTAN_ID_TYPE [num_obj_];
837 if (num_obj_ && ! gids){
838 throw Isorropia::Exception("Out of memory in ZoltanLibClass::order()");
839 return -1;
840 }
841
842 int *intIds = queryObject_->RowMap().MyGlobalElements();
843 for (int i=0; i < num_obj_; i++){
844 gids[i] = (ZOLTAN_ID_TYPE)intIds[i];
845 }
846 }
847 else{
848 gids = (ZOLTAN_ID_PTR)queryObject_->RowMap().MyGlobalElements();
849 }
850
851 int err = zz_->Order(num_gid_entries, num_obj_, gids, &properties[0], NULL);
852
853 if (sizeof(ZOLTAN_ID_TYPE) != sizeof(int)){
854 delete [] gids;
855 }
856
857 if (err != ZOLTAN_OK){
858 throw Isorropia::Exception("Error computing ordering with Zoltan");
859 return -1;
860 }
861
862 postcompute();
863 return (0);
864 }
865
866
867
868 int ZoltanLibClass::postcompute()
869 {
870 if (zz_)
871 delete zz_;
872 zz_ = NULL;
873
874 return (0);
875 }
876
877
878
879
880 } // namespace EPETRA
881
882 #endif //HAVE_EPETRA
883
884 }//namespace Isorropia