Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraOperator.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_EpetraOperator.hpp>
41 #include <Isorropia_Exception.hpp>
42 #include <Isorropia_Epetra.hpp>
43 #include <Isorropia_EpetraCostDescriber.hpp>
44
45 #include <Teuchos_RCP.hpp>
46 #include <Teuchos_ParameterList.hpp>
47
48 #ifdef HAVE_EPETRA
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 #else /* HAVE_EPETRA */
58 #error "This module needs Epetra"
59 #endif /* HAVE_EPETRA */
60
61
62
63 #include <cstring>
64 #include <iostream>
65 #include <sstream>
66 #include <string>
67 #include <ctype.h>
68
69 #ifdef MIN
70 #undef MIN
71 #endif
72
73 #define MIN(a,b) ((a)<(b)?(a):(b))
74
75 namespace Isorropia {
76
77 #ifdef HAVE_EPETRA
78
79 namespace Epetra {
80
81 // Operator::
82 // Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph, int base)
83 // : input_graph_(input_graph),
84 // input_matrix_(0),
85 // input_coords_(0),
86 // costs_(0),
87 // weights_(0),
88 // operation_already_computed_(false),
89 // lib_(0),
90 // base_(base)
91 // {
92 // input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
93 // }
94
95 // Operator::
96 // Operator(Teuchos::RCP<const Epetra_BlockMap> input_map, int base)
97 // : input_map_(input_map),
98 // input_graph_(0),
99 // input_matrix_(0),
100 // input_coords_(0),
101 // costs_(0),
102 // weights_(0),
103 // operation_already_computed_(false),
104 // lib_(0),
105 // base_(base)
106 // {
107
108 // }
109
110 // Operator::
111 // Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
112 // Teuchos::RCP<const Epetra_MultiVector> input_coords,int base)
113 // : input_graph_(input_graph),
114 // input_matrix_(0),
115 // input_coords_(input_coords),
116 // costs_(0),
117 // weights_(0),
118 // operation_already_computed_(false),
119 // lib_(0),
120 // base_(base)
121 // {
122 // input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
123 // }
124
125
126 Operator::
127 Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
128 const Teuchos::ParameterList& paramlist, int base)
129 : input_graph_(input_graph), input_matrix_(0),
130 input_coords_(0),
131 costs_(0), weights_(0),
132 operation_already_computed_(false),
133 lib_(0), base_(base)
134 {
135 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
136
137 if(paramlist.name() != "EmptyParameterList")
138 {
139 setParameters(paramlist);
140 }
141
142 }
143
144 Operator::
145 Operator(Teuchos::RCP<const Epetra_BlockMap> input_map,
146 const Teuchos::ParameterList& paramlist, int base)
147 : input_map_(input_map),input_graph_(0), input_matrix_(0),
148 input_coords_(0),
149 costs_(0), weights_(0),
150 operation_already_computed_(false),
151 lib_(0), base_(base)
152 {
153 if(paramlist.name() != "EmptyParameterList")
154 {
155 setParameters(paramlist);
156 }
157 }
158
159 Operator::
160 Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
161 Teuchos::RCP<const Epetra_MultiVector> input_coords,
162 const Teuchos::ParameterList& paramlist, int base)
163 : input_graph_(input_graph), input_matrix_(0),
164 input_coords_(input_coords),
165 costs_(0), weights_(0),
166 operation_already_computed_(false),
167 lib_(0), base_(base)
168 {
169 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
170
171 if(paramlist.name() != "EmptyParameterList")
172 {
173 setParameters(paramlist);
174 }
175
176 }
177
178
179
180 Operator::
181 Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
182 Teuchos::RCP<CostDescriber> costs,
183 const Teuchos::ParameterList& paramlist, int base)
184 : input_graph_(input_graph), input_matrix_(0),
185 input_coords_(0),
186 costs_(costs), weights_(0),
187 operation_already_computed_(false),
188 lib_(0), base_(base)
189 {
190
191 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
192
193 if(paramlist.name() != "EmptyParameterList")
194 {
195 setParameters(paramlist);
196 }
197 }
198
199 Operator::
200 Operator(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
201 Teuchos::RCP<CostDescriber> costs,
202 Teuchos::RCP<const Epetra_MultiVector> input_coords,
203 Teuchos::RCP<const Epetra_MultiVector> weights,
204 const Teuchos::ParameterList& paramlist, int base)
205 : input_graph_(input_graph), input_matrix_(0),
206 input_coords_(input_coords),
207 costs_(costs), weights_(weights),
208 operation_already_computed_(false),
209 lib_(0), base_(base)
210 {
211
212 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
213
214 if(paramlist.name() != "EmptyParameterList")
215 {
216 setParameters(paramlist);
217 }
218 }
219
220 // Operator::
221 // Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix, int base)
222 // : input_graph_(0),
223 // input_matrix_(input_matrix),
224 // input_coords_(0),
225 // costs_(0),
226 // weights_(0),
227 // operation_already_computed_(false),
228 // lib_(0),
229 // base_(base)
230 // {
231 // input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()), false);
232 // }
233
234 // Operator::
235 // Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
236 // Teuchos::RCP<const Epetra_MultiVector> input_coords, int base)
237 // : input_graph_(0),
238 // input_matrix_(input_matrix),
239 // input_coords_(input_coords),
240 // costs_(0),
241 // weights_(0),
242 // operation_already_computed_(false),
243 // lib_(0),
244 // base_(base)
245 // {
246 // input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()), false);
247 // }
248
249 Operator::
250 Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
251 const Teuchos::ParameterList& paramlist, int base)
252 : input_graph_(0), input_matrix_(input_matrix),
253 input_coords_(0),
254 costs_(0), weights_(0),
255 operation_already_computed_(false),
256 lib_(0),
257 base_(base)
258 {
259 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
260
261 if(paramlist.name() != "EmptyParameterList")
262 {
263 setParameters(paramlist);
264 }
265 }
266
267 Operator::
268 Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
269 Teuchos::RCP<const Epetra_MultiVector> input_coords,
270 const Teuchos::ParameterList& paramlist, int base)
271 : input_graph_(0), input_matrix_(input_matrix),
272 input_coords_(input_coords),
273 costs_(0), weights_(0),
274 operation_already_computed_(false),
275 lib_(0),
276 base_(base)
277 {
278 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
279
280 if(paramlist.name() != "EmptyParameterList")
281 {
282 setParameters(paramlist);
283 }
284 }
285
286 Operator::
287 Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
288 Teuchos::RCP<CostDescriber> costs,
289 const Teuchos::ParameterList& paramlist, int base)
290 : input_graph_(0),
291 input_matrix_(input_matrix),
292 input_coords_(0),
293 costs_(costs),
294 weights_(0),
295 operation_already_computed_(false),
296 lib_(0),
297 base_(base)
298 {
299 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
300
301 if(paramlist.name() != "EmptyParameterList")
302 {
303 setParameters(paramlist);
304 }
305 }
306
307 Operator::
308 Operator(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
309 Teuchos::RCP<CostDescriber> costs,
310 Teuchos::RCP<const Epetra_MultiVector> input_coords,
311 Teuchos::RCP<const Epetra_MultiVector> weights,
312 const Teuchos::ParameterList& paramlist, int base)
313 : input_graph_(0),
314 input_matrix_(input_matrix),
315 input_coords_(input_coords),
316 costs_(costs),
317 weights_(weights),
318 operation_already_computed_(false),
319 lib_(0),
320 base_(base)
321 {
322 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
323
324 if(paramlist.name() != "EmptyParameterList")
325 {
326 setParameters(paramlist);
327 }
328 }
329
330 // Operator::
331 // Operator(Teuchos::RCP<const Epetra_MultiVector> input_coords, int base)
332 // : input_graph_(0),
333 // input_matrix_(0),
334 // input_coords_(input_coords),
335 // costs_(0),
336 // weights_(0),
337 // operation_already_computed_(false),
338 // lib_(0),
339 // base_(base)
340 // {
341 // input_map_ = Teuchos::rcp(&(input_coords->Map()), false);
342 // }
343
344 Operator::
345 Operator(Teuchos::RCP<const Epetra_MultiVector> input_coords,
346 const Teuchos::ParameterList& paramlist, int base)
347 : input_graph_(0),
348 input_matrix_(0),
349 input_coords_(input_coords),
350 costs_(0),
351 weights_(0),
352 operation_already_computed_(false),
353 lib_(0),
354 base_(base)
355 {
356 input_map_ = Teuchos::rcp(&(input_coords->Map()),false);
357
358 if(paramlist.name() != "EmptyParameterList")
359 {
360 setParameters(paramlist);
361 }
362 }
363
364 Operator::
365 Operator(Teuchos::RCP<const Epetra_MultiVector> input_coords,
366 Teuchos::RCP<const Epetra_MultiVector> weights,
367 const Teuchos::ParameterList& paramlist, int base)
368 : input_graph_(0),
369 input_matrix_(0),
370 input_coords_(input_coords),
371 costs_(0),
372 weights_(weights),
373 operation_already_computed_(false),
374 lib_(0),
375 base_(base)
376 {
377 input_map_ = Teuchos::rcp(&(input_coords->Map()),false);
378
379 if(paramlist.name() != "EmptyParameterList")
380 {
381 setParameters(paramlist);
382 }
383 }
384
385 Operator::~Operator()
386 {
387 }
388
389 void Operator::setParameters(const Teuchos::ParameterList& paramlist)
390 {
391 int changed;
392 paramlist_ = paramlist;
393 paramsToUpper(paramlist_, changed);
394
395 Teuchos::ParameterList &sublist = paramlist_.sublist("ZOLTAN");
396 std::string str_sym="STRUCTURALLY SYMMETRIC";
397 sublist.set("GRAPH_SYMMETRIZE", "TRANSPOSE");
398 if (paramlist_.isParameter(str_sym)
399 && paramlist_.get<std::string>(str_sym) == "YES") {
400 sublist.set("GRAPH_SYMMETRIZE", "NONE");
401 }
402
403 }
404
405 const int& Operator::operator[](int myElem) const
406 {
407 return (properties_[myElem]);
408 }
409
410 int Operator::numElemsWithProperty(int property) const
411 {
412 if ((unsigned) property < numberElemsByProperties_.size())
413 return numberElemsByProperties_[property];
414 return (0);
415 }
416
417 void
418 Operator::elemsWithProperty(int property, int* elementList, int len) const
419 {
420 int length = 0;
421 std::vector<int>::const_iterator elemsIter;
422
423 for (elemsIter = properties_.begin() ; (length < len) && (elemsIter != properties_.end()) ;
424 elemsIter ++) {
425 if (*elemsIter == property)
426 elementList[length++] = elemsIter - properties_.begin();
427 }
428 }
429
430
431 void
432 Operator::computeNumberOfProperties()
433 {
434 std::vector<int>::const_iterator elemsIter;
435 std::vector<int>::iterator numberIter;
436 const Epetra_Comm& input_comm = input_map_->Comm();
437
438 int max = 0;
439 numberElemsByProperties_.assign(properties_.size(), 0);
440
441 numberIter = numberElemsByProperties_.begin();
442 for(elemsIter = properties_.begin() ; elemsIter != properties_.end() ; elemsIter ++) {
443 int property;
444 property = *elemsIter;
445 if (max < property) {
446 max = property;
447 int toAdd = max - numberElemsByProperties_.size() + 1;
448 if (toAdd > 0) {
449 numberElemsByProperties_.insert(numberElemsByProperties_.end(), toAdd, 0);
450 numberIter = numberElemsByProperties_.begin();
451 }
452 }
453 (*(numberIter + property)) ++;
454 }
455
456 input_comm.MaxAll(&max, &numberOfProperties_, 1);
457
458 numberOfProperties_ = numberOfProperties_ - base_ + 1;
459
460 localNumberOfProperties_ = max - base_ + 1;
461 }
462
463 void Operator::stringToUpper(std::string &s, int &changed, bool rmUnderscore)
464 {
465 std::string::iterator siter;
466 changed = 0;
467
468 for (siter = s.begin(); siter != s.end() ; siter++)
469 {
470 if (islower(*siter)){
471 *siter = toupper(*siter);
472 changed++;
473 }
474 if (rmUnderscore && (*siter == '_')) {
475 *siter = ' ';
476 changed ++;
477 }
478 }
479 }
480
481 void Operator::paramsToUpper(Teuchos::ParameterList &plist, int &changed, bool rmUnderscore)
482 {
483 changed = 0;
484
485 // get a list of all parameter names in the list
486
487 std::vector<std::string> paramNames ;
488 Teuchos::ParameterList::ConstIterator pIter;
489
490 pIter = plist.begin();
491
492 while (1){
493 //////////////////////////////////////////////////////////////////////
494 // Compiler considered this while statement an error
495 // for ( pIter = plist.begin() ; pIter != plist.end() ; pIter++ ){
496 // }
497 //////////////////////////////////////////////////////////////////////
498 if (pIter == plist.end()) break;
499 const std::string & nm = plist.name(pIter);
500 paramNames.push_back(nm);
501 pIter++;
502 }
503
504 // Change parameter names and values to upper case
505
506 for (unsigned int i=0; i < paramNames.size(); i++){
507
508 std::string origName(paramNames[i]);
509 int paramNameChanged = 0;
510 stringToUpper(paramNames[i], paramNameChanged, rmUnderscore);
511
512 if (plist.isSublist(origName)){
513 Teuchos::ParameterList &sublist = plist.sublist(origName);
514
515 int sublistChanged=0;
516 paramsToUpper(sublist, sublistChanged, false);
517
518 if (paramNameChanged){
519
520 // this didn't work, so I need to remove the old sublist
521 // and create a new one
522 //
523 //sublist.setName(paramNames[i]);
524
525 Teuchos::ParameterList newlist(sublist);
526 plist.remove(origName);
527 plist.set(paramNames[i], newlist);
528 }
529 }
530 else if (plist.isType<std::string>(origName)){
531
532 std::string paramVal(plist.get<std::string>(origName));
533
534 int paramValChanged=0;
535 stringToUpper(paramVal, paramValChanged);
536
537 if (paramNameChanged || paramValChanged){
538 if (paramNameChanged){
539 plist.remove(origName);
540 }
541 plist.set(paramNames[i], paramVal);
542 changed++;
543 }
544 }
545 } // next parameter or sublist
546 }
547
548 int Operator::extractPropertiesCopy(int len,
549 int& size,
550 int* array) const
551 {
552 int ierr;
553 const int *ptr;
554
555 ierr = extractPropertiesView(size, ptr);
556 if (ierr)
557 return (ierr);
558
559 size = MIN(size, len);
560 memcpy (array, ptr, size * sizeof(int));
561
562 return (0);
563 }
564
565 int Operator::extractPropertiesView(int& size,
566 const int*& array) const
567 {
568 size = properties_.size();
569 if (size)
570 array = &(properties_[0]);
571 else
572 array = NULL;
573 return (0);
574 }
575
576
577
578 } // namespace EPETRA
579
580 #endif //HAVE_EPETRA
581
582 }//namespace Isorropia
583