Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraCostDescriber.hpp

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 #ifndef _Isorropia_EpetraCostDescriber_hpp_
41 #define _Isorropia_EpetraCostDescriber_hpp_
42
43 #include <Isorropia_ConfigDefs.hpp>
44 #include <Isorropia_CostDescriber.hpp>
45 #include <Teuchos_RCP.hpp>
46 #include <Teuchos_ParameterList.hpp>
47
48 #include <map>
49 #include <list>
50 #include <set>
51 #include <iostream>
52
53 #ifdef HAVE_EPETRA
54 class Epetra_Vector;
55 class Epetra_CrsMatrix;
56 class Epetra_RowMatrix;
57 class Epetra_MultiVector;
58 class Epetra_Import;
59 class Epetra_CrsGraph;
60
61 namespace Isorropia {
62
63 /** The Isorropia::Epetra namespace implements a parallel partitioner
64 that operates on Epetra objects.
65
66 The objects to be partitioned may be the rows of an Epetra matrix
67 or linear system. There are two modes of operation.
68
69 In one, the user supplies
70 an Epetra object to Isorropia::Epetra::create_balanced_copy() and a
71 new, rebalanced Epetra object is returned.
72
73 In the other mode, the user supplies
74 an Epetra object to Isorropia::Epetra::create_partitioner() and a
75 Isorropia::Epetra::Partitioner object is returned. This Partitioner
76 object may be used to create an Isorropia::Epetra::Redistributor object.
77 Finally the "redistribute" method of the Redistributor object
78 may be used multiple times to redistribute Epetra objects with the
79 same distribution as the Epetra object given to create_partitioner().
80
81 In both cases, the application may supply row and edge weights with
82 an Isorropia::Epetra::CostDescriber and may supply parameters with
83 an Teuchos::ParameterList.
84
85 If Trilinos has been built with the Zoltan parallel dynamic load
86 balancing library (http://www.cs.sandia.gov/Zoltan),
87 then Isorropia will use Zoltan to partition the Epetra object.
88 The application can set Zoltan parameters with the ParameterList object.
89
90 If Zoltan is not available, or the application has set the parameter
91 "PARTITIONING_METHOD" to "SIMPLE_LINEAR", Isorropia will
92 perform a simple linear partitioning
93 of the object. If the application supplied vertex weights with a
94 CostDescriber, the linear partitioner will observe the vertex (row) weights.
95 Otherwise the linear partitioner will use the number of non-zeroes in
96 the row as the row weight. The linear partitioner does not
97 consider edge weights in the partitioning.
98 */
99
100 class Operator;
101
102 namespace Epetra {
103
104 /** The CostDescriber class describes the vertex, edge and/or hyperedge weights.
105
106 It is instantiated by the application to define
107 weights, and then supplied to Isorropia with the
108 Isorropia::Epetra::create_balanced_copy method or the
109 Isorropia::Epetra::create_partitioner method.
110
111 The CostDescriber can hold vertex (row) weights.
112 For graph partitioning with Zoltan it may
113 also define graph edge (nonzero) weights. For hypergraph partitioning
114 with Zoltan it may describe hyperedge (column) weights.
115
116 If the application does not provide weights, reasonable defaults will
117 be used. Zoltan parameters (supplied with an Isorropia::Epetra::ParameterList)
118 are available to override those defaults.
119 */
120
121 // Forward declarations of friends
122
123 namespace ZoltanLib{
124 class QueryObject;
125 }
126 class ZoltanLibClass;
127
128 class CostDescriber : public Isorropia::CostDescriber {
129
130 // public methods are part of API, private methods are used by different
131 // classes in isorropia
132
133 friend class Isorropia::Operator;
134 friend class Isorropia::Epetra::ZoltanLib::QueryObject;
135 friend class Isorropia::Epetra::ZoltanLibClass;
136
137 public:
138 /** Constructor */
139 CostDescriber();
140
141 /** Destructor */
142 ~CostDescriber();
143
144 /** Copy constructor */
145 CostDescriber(const CostDescriber &costs);
146
147 /** Overloaded << operator for CostDescriber object
148 */
149 friend std::ostream& operator <<(std::ostream &, const Isorropia::Epetra::CostDescriber &cd);
150
151 /** setVertexWeights is called by a process to supply the
152 weight of each vertex (row) in the graph or hypergraph.
153 If the object to be partitioned is instead an Epetra_MultiVector
154 representing real
155 coordinates, then the weights represent the weight assigned to each coordinate.
156
157 \param vwgts [in] vector of weights, one for each vertex
158 */
159 void setVertexWeights(Teuchos::RCP<const Epetra_Vector> vwgts);
160
161 /** setGraphEdgeWeights is called by a process to supply the weights for
162 each of the edges of its vertices. An edge corresponds to a non-zero
163 in the row representing the vertex.
164
165 This method is called only when performing graph partitioning with a
166 square symmetric matrix. For hypergraph partitioning call the equivalent
167 hypergraph method.
168
169 \param gewts an Epetra_CrsMatrix supplied by the application, each non-zero
170 represents a weight for an edge
171 */
172 void setGraphEdgeWeights(Teuchos::RCP<const Epetra_CrsMatrix> gewts);
173
174 /** setHypergraphEdgeWeights is called by processes in an application to
175 supply weights for the hyperedges, which are represented by the columns
176 of the matrix. (A hyperedge can in general link more than one vertex.)
177
178 Matrices that represent hypergraphs are not in general square.
179 There may be more or fewer hyperedges (columns) than vertices (rows).
180
181 \param hgewts an Epetra_Vector containing the weights for each hyperedge.
182 */
183 void setHypergraphEdgeWeights(Teuchos::RCP<const Epetra_Vector> hgewts);
184
185 /** setHypergraphEdgeWeights is called by processes in an application to
186 supply weights for the hyperedges, which are represented by the columns
187 of the matrix. (A hyperedge can in general link more than one vertex.)
188
189 Matrices that represent hypergraphs are not in general square.
190 There may be more or fewer hyperedges (columns) than vertices (rows).
191
192 More than one process can supply a weight for the same column. (So
193 there is no concept of a process owning a hyperedge.) Zoltan
194 combines these weights according to the setting of the
195 PHG_EDGE_WEIGHT_OPERATION parameter.
196
197 \param numHGedges the length of the hgGIDs and heEwgts arrays
198 \param hgGIDs the global ID for each hyperedge this process will supply a weight for
199 \param hgEwgts the hyperedge weight corresponding to each hyperedge listed in hgGIDs
200 */
201 void setHypergraphEdgeWeights(int numHGedges, const int *hgGIDs, const float *hgEwgts);
202
203 /** \copydoc Isorropia::Epetra::CostDescriber::setHypergraphEdgeWeights(int numHGedges, const int *hgGIDs, const float *hgEwgts)
204 */
205
206 void setHypergraphEdgeWeights(int numHGedges, const int *hgGIDs, const double *hgEwgts);
207
208 /** Get the contents of this CostDescriber
209
210 \param vertexWeights is set to a mapping from vertex global IDs to their weights
211 \param graphEdgeWeights is a mapping from vertex global IDs to a map from neighboring
212 IDs to edge weights
213 \param hypergraphEdgeWeights is a mapping from hyperedge (column) global IDs to hyperedge weights
214
215 */
216 void getCosts(std::map<int, float > &vertexWeights,
217 std::map<int, std::map<int, float > > &graphEdgeWeights,
218 std::map<int, float > &hypergraphEdgeWeights) const;
219
220 /** Print out the contents of this CostDescriber
221 */
222 void show_cd(std::ostream &) const;
223
224 // Add documentation
225
226 #ifdef USE_UTILS
227 int compareBeforeAndAfterHypergraph( const Epetra_RowMatrix &in_m, const Epetra_RowMatrix &out_m,
228 const Epetra_Import &importer,
229 std::vector<double> &balance, std::vector<double> &cutn, std::vector<double> &cutl) const;
230
231 int compareBeforeAndAfterGraph( const Epetra_RowMatrix &in_m, const Epetra_RowMatrix &out_m,
232 const Epetra_Import &importer,
233 std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt,
234 std::vector<double> &cutn, std::vector<double> &cutl) const;
235
236 int compareBeforeAndAfterGraph( const Epetra_CrsGraph &in_g, const Epetra_CrsGraph &out_g,
237 const Epetra_Import &importer,
238 std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt,
239 std::vector<double> &cutn, std::vector<double> &cutl) const;
240
241 int compareBeforeAndAfterImbalance(const Epetra_MultiVector &mv, const Epetra_Import &importer,
242 std::vector<double> &min, std::vector<double> &max, std::vector<double> &avg) const;
243 #endif
244
245 private:
246
247 void _transformWeights(const Epetra_Import &importer);
248
249 int _compareBeforeAndAfterGraph( const Epetra_RowMatrix *in_m, const Epetra_RowMatrix *out_m,
250 const Epetra_CrsGraph *in_g, const Epetra_CrsGraph *out_g,
251 const Epetra_Import &importer,
252 std::vector<double> &balance, std::vector<int> &numCuts, std::vector<double> &cutWgt,
253 std::vector<double> &cutn, std::vector<double> &cutl) const;
254
255 /** \copydoc Isorropia::CostDescriber::setParameters
256 */
257 void setParameters(const Teuchos::ParameterList& paramlist);
258
259 /** \copydoc Isorropia::CostDescriber::haveVertexWeights
260 */
261 bool haveVertexWeights() const;
262 /** \copydoc Isorropia::CostDescriber::getNumVertices
263 */
264 int getNumVertices() const;
265 /** \copydoc Isorropia::CostDescriber::getVertexWeights
266 */
267 void getVertexWeights(int numVertices,
268 int* global_ids, float* weights) const;
269 /** \copydoc Isorropia::CostDescriber::haveGraphEdgeWeights
270 */
271 bool haveGraphEdgeWeights() const;
272 /** \copydoc Isorropia::CostDescriber::getNumGraphEdges
273 */
274 int getNumGraphEdges(int vertex_global_id) const;
275
276 /** Get the set of global IDs for the vertices that we have edge information for.
277
278 \param gids will be set to the global IDs of the vertices for which neighbor and edge weight information have been provided
279 */
280 int getGraphEdgeVertices(std::set<int> &gids) const;
281
282
283 /** \copydoc Isorropia::CostDescriber::getGraphEdgeWeights
284 */
285 void getGraphEdgeWeights(int vertex_global_id,
286 int num_neighbors,
287 int* neighbor_global_ids,
288 float* weights) const;
289 /** \copydoc Isorropia::CostDescriber::haveHypergraphEdgeWeights
290 */
291 bool haveHypergraphEdgeWeights() const;
292 /** \copydoc Isorropia::CostDescriber::getNumHypergraphEdgeWeights
293 */
294 int getNumHypergraphEdgeWeights() const;
295 /** \copydoc Isorropia::CostDescriber::getHypergraphEdgeWeights
296 */
297 void getHypergraphEdgeWeights(int numEdges,
298 int* global_ids,
299 float* weights) const;
300
301 /** Return the CostDescribers hypergraph edge weights as a map from hyperedge (column)
302 global ID to weight.
303
304 \param wgtMap will be set to a map from hyperedge global ID to hyperedge weight
305 */
306 int getHypergraphEdgeWeights(std::map<int, float> &wgtMap) const;
307
308
309 /** Get vertex weights in the form of a map from vertex global ID to vertex weight.
310
311 \param wgtMap a map supplied by the caller, the vertex weights will be added to this map
312 \return the size of wgtMap
313 */
314 int getVertexWeights(std::map<int, float> &wgtMap) const;
315
316 // TODO documentation
317 const Epetra_Vector &getVertexWeights() { return *vertex_weights_;}
318
319
320 /** getGraphEdgeWeights is called to obtain
321 the graph edge weights for a given vertex (row).
322
323 \param vertex_global_id the global ID of the vertex the caller wants edge weights for
324 \param wgtMap a map from the global ID of each vertex neighbor to the weight of the edge formed by the vertex and this neighbor
325 \return the count of the neighbors of vertex_global_id
326 */
327 int getGraphEdgeWeights(int vertex_global_id, std::map<int, float> &wgtMap) const;
328
329
330
331 /** haveGlobalVertexWeights returns true if any process in the application has
332 supplied vertex weights, it returns false otherwise.
333 */
334 bool haveGlobalVertexWeights() const;
335
336 /** setNumGlobalVertexWeights may be used to set the count of the
337 global number of vertex weights supplied to the CostDescriber
338 */
339 void setNumGlobalVertexWeights(int num);
340
341 /** haveGlobalGraphEdgeWeights returns true if any process in the application has
342 supplied graph edge weights, it returns false otherwise.
343 */
344 bool haveGlobalGraphEdgeWeights() const;
345
346 /** setNumGlobalGraphEdgeWeights may be used to set the count of the
347 global number of graph edge weights supplied to the CostDescriber
348 */
349 void setNumGlobalGraphEdgeWeights(int num);
350
351 /** haveGlobalHypergraphEdgeWeights returns true if any process in the application has
352 supplied hyperedge weights, it returns false otherwise.
353 */
354 bool haveGlobalHypergraphEdgeWeights() const;
355
356 /** setNumGlobalHypergraphEdgeWeights may be used to set the count of the
357 global number of hyperedge weights supplied to the CostDescriber
358 */
359 void setNumGlobalHypergraphEdgeWeights(int num);
360
361 /** Dynamically allocate storage for hypergraph edge weights.
362 */
363 void allocate_hg_edge_weights_(int n);
364
365 /** Free storage used by hypergraph edge weights.
366 */
367 void free_hg_edge_weights_();
368
369 Teuchos::RCP<const Epetra_Vector> vertex_weights_;
370 Teuchos::RCP<const Epetra_CrsMatrix> graph_edge_weights_;
371 std::set<int> graph_self_edges_;
372
373 Teuchos::ParameterList paramlist_;
374
375 int *hg_edge_gids_;
376 float *hg_edge_weights_;
377 int num_hg_edge_weights_;
378
379 int numGlobalVertexWeights_;
380 int numGlobalGraphEdgeWeights_;
381 int numGlobalHypergraphEdgeWeights_;
382
383 /** getEdges creates an array of the neighbors and edge weights for given vertex.
384 Self edges are not included.
385
386 \param vertexGID the global ID of the vertex (must be one owned by calling process)
387 \param len of preallocated nborGID and weights arrays
388 \param nborGID on return contains the global ID of each vertex neighboring vertexGID,
389 allocated by caller
390 \param weights on return contains the weight for each edge formed by the vertices in nborGID
391
392 \return the number of neighbors in nborGID is returned
393
394 */
395 int getEdges(int vertexGID, int len, int *nborGID, float *weights) const;
396
397 };//class CostDescriber
398
399 }//namespace Epetra
400 }//namespace Isorropia
401
402 #endif //HAVE_EPETRA
403
404 #endif
405
406
407 #if defined(Isorropia_SHOW_DEPRECATED_WARNINGS)
408 #ifdef __GNUC__
409 #warning "The Isorropia package is deprecated"
410 #endif
411 #endif
412