Zoltan2
Zoltan2_GraphAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
51 #ifndef _ZOLTAN2_GRAPHADAPTER_HPP_
52 #define _ZOLTAN2_GRAPHADAPTER_HPP_
53 
54 #include <Zoltan2_Adapter.hpp>
56 
57 namespace Zoltan2 {
58 
64 };
65 
98 template <typename User, typename UserCoord=User>
99  class GraphAdapter : public BaseAdapter<User> {
100 private:
101  enum GraphEntityType primaryEntityType; // Entity (vertex or edge) to
102  // be partitioned, ordered,
103  // colored, matched, etc.
104  enum GraphEntityType adjacencyEntityType; // Entity (edge or vertex)
105  // describing adjacencies;
106  // typically opposite of
107  // primaryEntityType.
108  VectorAdapter<UserCoord> *coordinateInput_; // A VectorAdapter containing
109  // coordinates of the objects
110  // with primaryEntityType;
111  // optional.
112  bool haveCoordinateInput_; // Flag indicating whether
113  // coordinateInput_ is provided.
114 
115 public:
116 
117 #ifndef DOXYGEN_SHOULD_SKIP_THIS
118  typedef typename InputTraits<User>::scalar_t scalar_t;
119  typedef typename InputTraits<User>::lno_t lno_t;
120  typedef typename InputTraits<User>::gno_t gno_t;
121  typedef typename InputTraits<User>::node_t node_t;
122  typedef User user_t;
123  typedef UserCoord userCoord_t;
124 #endif
125 
127 
130  virtual ~GraphAdapter() {};
131 
132  // Default GraphEntityType is GRAPH_VERTEX.
133  GraphAdapter() : primaryEntityType(GRAPH_VERTEX),
134  adjacencyEntityType(GRAPH_EDGE),
135  coordinateInput_(),
136  haveCoordinateInput_(false) {}
137 
139  // Methods to be defined in derived classes.
140 
143  virtual size_t getLocalNumVertices() const = 0;
144 
147  virtual size_t getLocalNumEdges() const = 0;
148 
152  virtual void getVertexIDsView(const gno_t *&vertexIds) const = 0;
153 
163  virtual void getEdgesView(const lno_t *&offsets,
164  const gno_t *&adjIds) const = 0;
165 
168  virtual int getNumWeightsPerVertex() const { return 0; }
169 
176  virtual void getVertexWeightsView(const scalar_t *&weights, int &stride,
177  int idx = 0) const
178  {
179  weights = NULL;
180  stride = 0;
182  }
183 
184 
188  virtual bool useDegreeAsVertexWeight(int idx) const
189  {
190  return false;
191  }
192 
195  virtual int getNumWeightsPerEdge() const { return 0; }
196 
203  virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride,
204  int idx = 0) const
205  {
206  weights = NULL;
207  stride = 0;
209  }
210 
211 
221  {
222  coordinateInput_ = coordData;
223  haveCoordinateInput_ = true;
224  }
225 
229  bool coordinatesAvailable() const { return haveCoordinateInput_; }
230 
235  {
236  return coordinateInput_;
237  }
238 
240  // Implementations of base-class methods
241 
245  inline enum GraphEntityType getPrimaryEntityType() const {
246  return this->primaryEntityType;
247  }
248 
254  void setPrimaryEntityType(std::string typestr) {
255  if (typestr == "vertex") {
256  this->primaryEntityType = GRAPH_VERTEX;
257  this->adjacencyEntityType = GRAPH_EDGE;
258  }
259  else if (typestr == "edge") {
260  this->primaryEntityType = GRAPH_EDGE;
261  this->adjacencyEntityType = GRAPH_VERTEX;
262  }
263  else {
264  std::ostringstream emsg;
265  emsg << __FILE__ << "," << __LINE__
266  << " error: Invalid GraphEntityType " << typestr << std::endl;
267  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
268  throw std::runtime_error(emsg.str());
269  }
270  }
271 
277  return this->adjacencyEntityType;
278  }
279 
285  void setAdjacencyEntityType(std::string typestr) {
286  if (typestr == "vertex") {
287  this->adjacencyEntityType = GRAPH_VERTEX;
288  this->primaryEntityType = GRAPH_EDGE;
289  }
290  else if (typestr == "edge") {
291  this->adjacencyEntityType = GRAPH_EDGE;
292  this->primaryEntityType = GRAPH_VERTEX;
293  }
294  else {
295  std::ostringstream emsg;
296  emsg << __FILE__ << "," << __LINE__
297  << " error: Invalid GraphEntityType " << typestr << std::endl;
298  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
299  throw std::runtime_error(emsg.str());
300  }
301  }
302 
303  // Functions from the BaseAdapter interface
304  size_t getLocalNumIDs() const {
306  return getLocalNumVertices();
307  else
308  return getLocalNumEdges();
309  }
310 
311  void getIDsView(const gno_t *&Ids) const {
313  getVertexIDsView(Ids);
314  else {
315  // TODO: Need getEdgeIDsView? What is an Edge ID?
316  // TODO: std::pair<gno_t, gno_t>?
317  std::ostringstream emsg;
318  emsg << __FILE__ << "," << __LINE__
319  << " error: getIDsView not yet supported for graph edges."
320  << std::endl;
321  throw std::runtime_error(emsg.str());
322  }
323  }
324 
325  int getNumWeightsPerID() const {
327  return getNumWeightsPerVertex();
328  else
329  return getNumWeightsPerEdge();
330  }
331 
332  void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const {
334  getVertexWeightsView(wgt, stride, idx);
335  else {
336  // TODO: Need getEdgeWeightsView that lets Edges be primary object?
337  // TODO: That is, get edge weights based on some Edge ID.
338  std::ostringstream emsg;
339  emsg << __FILE__ << "," << __LINE__
340  << " error: getWeightsView not yet supported for graph edges."
341  << std::endl;
342  throw std::runtime_error(emsg.str());
343  }
344  }
345 
346  bool useDegreeAsWeight(int idx) const
347  {
348  if (this->getPrimaryEntityType() == GRAPH_VERTEX)
349  return useDegreeAsVertexWeight(idx);
350  else {
351  std::ostringstream emsg;
352  emsg << __FILE__ << "," << __LINE__
353  << " error: useDegreeAsWeight is supported only for vertices"
354  << std::endl;
355  throw std::runtime_error(emsg.str());
356  }
357  }
358 };
359 
360 } //namespace Zoltan2
361 
362 #endif
InputTraits< User >::scalar_t scalar_t
enum BaseAdapterType adapterType() const
Returns the type of adapter.
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the edge weights, if any.
virtual void getEdgesView(const lno_t *&offsets, const gno_t *&adjIds) const =0
Gets adjacency lists for all vertices in a compressed sparse row (CSR) format.
InputTraits< User >::gno_t gno_t
virtual size_t getLocalNumEdges() const =0
Returns the number of edges on this process.
GraphAdapter defines the interface for graph-based user data.
virtual int getNumWeightsPerEdge() const
Returns the number (0 or greater) of edge weights.
GraphEntityType
Enumerated entity type for graphs: Vertices or Edges.
virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the vertex weights, if any.
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
Defines the VectorAdapter interface.
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Provide pointer to a weight array with stride.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
size_t getLocalNumIDs() const
Returns the number of objects on this process.
void getIDsView(const gno_t *&Ids) const
Provide a pointer to this process&#39; identifiers.
VectorAdapter< UserCoord > * getCoordinateInput() const
Obtain the coordinate data registered by the user.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
BaseAdapterType
An enum to identify general types of adapters.
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
virtual void getVertexIDsView(const gno_t *&vertexIds) const =0
Sets pointers to this process&#39; graph entries.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
virtual int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
void setAdjacencyEntityType(std::string typestr)
Sets the adjacency entity type. Called by algorithm based on parameter value in parameter list from a...
#define Z2_THROW_NOT_IMPLEMENTED_IN_ADAPTER
BaseAdapter defines methods required by all Adapters.
bool useDegreeAsWeight(int idx) const
virtual bool useDegreeAsVertexWeight(int idx) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
enum GraphEntityType getAdjacencyEntityType() const
Returns the entity that describes adjacencies between the entities to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_EDGE.
void setCoordinateInput(VectorAdapter< UserCoord > *coordData)
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
enum GraphEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_E...
virtual ~GraphAdapter()
Destructor.
virtual size_t getLocalNumVertices() const =0
Returns the number of vertices on this process.
default_scalar_t scalar_t
The data type for weights and coordinates.