Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraLibrary.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_EpetraLibrary.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
58 #endif
59
60 #include <cstring>
61 #include <iostream>
62 #include <sstream>
63 #include <string>
64 #include <ctype.h>
65
66 namespace Isorropia {
67
68 #ifdef HAVE_EPETRA
69
70 namespace Epetra {
71
72
73 Library::
74 Library(Teuchos::RCP<const Epetra_CrsGraph> input_graph, int itype)
75 : input_type_(itype),
76 numPartSizes(0),
77 partGIDs(NULL),
78 partSizes(NULL),
79 input_graph_(input_graph),
80 input_matrix_(0),
81 input_coords_(),
82 costs_(0),
83 weights_(0)
84 {
85 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
86 }
87
88 Library::
89 Library(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
90 Teuchos::RCP<const Epetra_MultiVector> input_coords, int itype)
91 : input_type_(itype),
92 numPartSizes(0),
93 partGIDs(NULL),
94 partSizes(NULL),
95 input_graph_(input_graph),
96 input_matrix_(0),
97 input_coords_(input_coords),
98 costs_(0),
99 weights_(0)
100 {
101 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
102 }
103
104 Library::
105 Library(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
106 Teuchos::RCP<CostDescriber> costs, int itype)
107 : input_type_(itype),
108 numPartSizes(0),
109 partGIDs(NULL),
110 partSizes(NULL),
111 input_graph_(input_graph),
112 input_matrix_(0),
113 input_coords_(0),
114 costs_(costs),
115 weights_(0)
116 {
117 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
118 }
119
120 Library::
121 Library(Teuchos::RCP<const Epetra_CrsGraph> input_graph, Teuchos::RCP<CostDescriber> costs,
122 Teuchos::RCP<const Epetra_MultiVector> input_coords, Teuchos::RCP<const Epetra_MultiVector> weights,
123 int itype)
124 : input_type_(itype),
125 numPartSizes(0),
126 partGIDs(NULL),
127 partSizes(NULL),
128 input_graph_(input_graph),
129 input_matrix_(0),
130 input_coords_(input_coords),
131 costs_(costs),
132 weights_(weights)
133 {
134 input_map_ = Teuchos::rcp(&(input_graph->RowMap()), false);
135 }
136
137 Library::
138 Library(Teuchos::RCP<const Epetra_RowMatrix> input_matrix, int itype)
139 : input_type_(itype),
140 numPartSizes(0),
141 partGIDs(NULL),
142 partSizes(NULL),
143 input_graph_(0),
144 input_matrix_(input_matrix),
145 input_coords_(0),
146 costs_(0),
147 weights_(0)
148 {
149 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
150 }
151
152 Library::
153 Library(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
154 Teuchos::RCP<const Epetra_MultiVector> input_coords,int itype)
155 : input_type_(itype),
156 numPartSizes(0),
157 partGIDs(NULL),
158 partSizes(NULL),
159 input_graph_(0),
160 input_matrix_(input_matrix),
161 input_coords_(input_coords),
162 costs_(0),
163 weights_(0)
164 {
165 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
166 }
167
168 Library::
169 Library(Teuchos::RCP<const Epetra_RowMatrix> input_matrix,
170 Teuchos::RCP<CostDescriber> costs, int itype)
171 : input_type_(itype),
172 numPartSizes(0),
173 partGIDs(NULL),
174 partSizes(NULL),
175 input_graph_(0),
176 input_matrix_(input_matrix),
177 input_coords_(0),
178 costs_(costs),
179 weights_(0)
180 {
181 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
182 }
183
184 ////////////////////////////////////////////////////////////////////////////////
185 Library::
186 Library(Teuchos::RCP<const Epetra_RowMatrix> input_matrix, Teuchos::RCP<CostDescriber> costs,
187 Teuchos::RCP<const Epetra_MultiVector> input_coords, Teuchos::RCP<const Epetra_MultiVector> weights,
188 int itype)
189 : input_type_(itype),
190 numPartSizes(0),
191 partGIDs(NULL),
192 partSizes(NULL),
193 input_graph_(0),
194 input_matrix_(input_matrix),
195 input_coords_(input_coords),
196 costs_(costs),
197 weights_(weights)
198 {
199 input_map_ = Teuchos::rcp(&(input_matrix->RowMatrixRowMap()),false);
200 }
201
202 Library::
203 Library(Teuchos::RCP<const Epetra_MultiVector> input_coords, int itype)
204 : input_type_(itype),
205 numPartSizes(0),
206 partGIDs(NULL),
207 partSizes(NULL),
208 input_graph_(0),
209 input_matrix_(0),
210 input_coords_(input_coords),
211 costs_(0),
212 weights_(0)
213 {
214 input_map_ = Teuchos::rcp(&(input_coords->Map()), false);
215 }
216
217 ////////////////////////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////////////////////////
219 Library::
220 Library(Teuchos::RCP<const Epetra_MultiVector> input_coords,
221 Teuchos::RCP<const Epetra_MultiVector> weights, int itype)
222 : input_type_(itype),
223 numPartSizes(0),
224 partGIDs(NULL),
225 partSizes(NULL),
226 input_graph_(0),
227 input_matrix_(0),
228 input_coords_(input_coords),
229 costs_(0) ,
230 weights_(weights)
231 {
232 input_map_ = Teuchos::rcp(&(input_coords->Map()), false);
233 }
234 ////////////////////////////////////////////////////////////////////////////////
235
236 ////////////////////////////////////////////////////////////////////////////////
237 ////////////////////////////////////////////////////////////////////////////////
238 Library::
239 Library(Teuchos::RCP<const Epetra_BlockMap> input_map, int itype)
240 : input_type_(itype),
241 numPartSizes(0),
242 partGIDs(NULL),
243 partSizes(NULL),
244 input_map_(input_map),
245 input_graph_(0),
246 input_matrix_(0),
247 input_coords_(0),
248 costs_(0),
249 weights_(0)
250 {
251
252 }
253 ////////////////////////////////////////////////////////////////////////////////
254
255 ////////////////////////////////////////////////////////////////////////////////
256 ////////////////////////////////////////////////////////////////////////////////
257 Library::~Library()
258 {
259 if (partGIDs)
260 delete [] partGIDs;
261 if (partSizes)
262 delete [] partSizes;
263 }
264 ////////////////////////////////////////////////////////////////////////////////
265
266 int Library::precompute()
267 {
268 std::string str1("Isorropia::Epetra::Library::precompute ");
269 std::string str2;
270
271 int inputCount = ((input_graph_.get() == 0) ? 0 : 1);
272 inputCount += ((input_matrix_.get() == 0) ? 0 : 1);
273 inputCount += ((input_coords_.get() == 0) ? 0 : 1);
274 inputCount += ((input_map_.get() == 0) ? 0 : 1);
275
276 if (inputCount < 1)
277 {
278 str2 = "ERROR: not holding valid input.";
279 throw Isorropia::Exception(str1+str2);
280 }
281
282 ///////////////////////////////////
283 // If graph info is needed
284 ///////////////////////////////////
285 if (input_type_ == graph_input_ || input_type_ == hgraph_graph_input_ ||
286 input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
287 {
288 bool square = false;
289 bool symmetric = true; // no easy way to test for this ?? TODO
290 if (input_graph_.get() != 0){
291 if (input_graph_->NumGlobalRows() == input_graph_->NumGlobalCols()){
292 square = true;
293 }
294 }
295 else if (input_matrix_.get() != 0){
296 if (input_matrix_->NumGlobalRows() == input_matrix_->NumGlobalCols()){
297 square = true;
298 }
299 }
300 else{
301 str2 = "Library requires graph or matrix input";
302 throw Isorropia::Exception(str1+str2);
303 }
304 if (!square){
305 str2 = "LB_METHOD=GRAPH, matrix or graph must be square";
306 throw Isorropia::Exception(str1+str2);
307 return (-1);
308 }
309 if (!symmetric){
310 str2 = "LB_METHOD=GRAPH, matrix or graph must be symmetric";
311 throw Isorropia::Exception(str1+str2);
312 return (-1);
313 }
314 }
315
316 ///////////////////////////////////
317 // If hypergraph info is needed
318 ///////////////////////////////////
319 if (input_type_ == hgraph_input_ || input_type_ == hgraph2d_finegrain_input_ ||
320 input_type_ == hgraph_graph_input_ || input_type_ == hgraph_geometric_input_ ||
321 input_type_ == hgraph_graph_geometric_input_)
322 {
323 if ((input_graph_.get() == 0) && (input_matrix_.get() == 0))
324 {
325 str2 = "Library requires graph or matrix input";
326 throw Isorropia::Exception(str1+str2);
327 }
328 }
329
330 ///////////////////////////////////
331 // If geometric info is needed
332 ///////////////////////////////////
333 if (input_type_ == geometric_input_ || input_type_ == hgraph_geometric_input_ ||
334 input_type_ == graph_geometric_input_ || input_type_ == hgraph_graph_geometric_input_)
335 {
336 if ((input_coords_.get() == 0) ||
337 (input_coords_->NumVectors() < 1) || (input_coords_->NumVectors() > 3)){
338 str2 = "Operation requires 1, 2 or 3 dimensional coordinate input";
339 throw Isorropia::Exception(str1+str2);
340 }
341
342 if (weights_.get() != 0){
343 if (weights_->MyLength() != input_coords_->MyLength()){
344 str2 = "Number of weights does not equal number of coordinates";
345 throw Isorropia::Exception(str1+str2);
346 }
347 }
348 }
349 return (0);
350 }
351
352
353 } // namespace EPETRA
354
355 #endif //HAVE_EPETRA
356
357 }//namespace Isorropia
358