Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 typename InputTraits<User>::offset_t offset_t;
123  typedef User user_t;
124  typedef UserCoord userCoord_t;
126 #endif
127 
129 
132  virtual ~GraphAdapter() {};
133 
134  // Default GraphEntityType is GRAPH_VERTEX.
135  GraphAdapter() : primaryEntityType(GRAPH_VERTEX),
136  adjacencyEntityType(GRAPH_EDGE),
137  coordinateInput_(),
138  haveCoordinateInput_(false) {}
139 
141  // Methods to be defined in derived classes.
142 
145  virtual size_t getLocalNumVertices() const = 0;
146 
149  virtual size_t getLocalNumEdges() const = 0;
150 
154  virtual void getVertexIDsView(const gno_t *&vertexIds) const = 0;
155 
165  virtual void getEdgesView(const offset_t *&offsets,
166  const gno_t *&adjIds) const = 0;
167 
170  virtual int getNumWeightsPerVertex() const { return 0; }
171 
178  virtual void getVertexWeightsView(const scalar_t *&weights, int &stride,
179  int /* idx */ = 0) const
180  {
181  weights = NULL;
182  stride = 0;
184  }
185 
186 
190  virtual bool useDegreeAsVertexWeight(int /* idx */) const
191  {
192  return false;
193  }
194 
197  virtual int getNumWeightsPerEdge() const { return 0; }
198 
205  virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride,
206  int /* idx */ = 0) const
207  {
208  weights = NULL;
209  stride = 0;
211  }
212 
213 
223  {
224  coordinateInput_ = coordData;
225  haveCoordinateInput_ = true;
226  }
227 
231  bool coordinatesAvailable() const { return haveCoordinateInput_; }
232 
237  {
238  return coordinateInput_;
239  }
240 
242  // Implementations of base-class methods
243 
247  inline enum GraphEntityType getPrimaryEntityType() const {
248  return this->primaryEntityType;
249  }
250 
256  void setPrimaryEntityType(std::string typestr) {
257  if (typestr == "vertex") {
258  this->primaryEntityType = GRAPH_VERTEX;
259  this->adjacencyEntityType = GRAPH_EDGE;
260  }
261  else if (typestr == "edge") {
262  this->primaryEntityType = GRAPH_EDGE;
263  this->adjacencyEntityType = GRAPH_VERTEX;
264  }
265  else {
266  std::ostringstream emsg;
267  emsg << __FILE__ << "," << __LINE__
268  << " error: Invalid GraphEntityType " << typestr << std::endl;
269  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
270  throw std::runtime_error(emsg.str());
271  }
272  }
273 
279  return this->adjacencyEntityType;
280  }
281 
287  void setAdjacencyEntityType(std::string typestr) {
288  if (typestr == "vertex") {
289  this->adjacencyEntityType = GRAPH_VERTEX;
290  this->primaryEntityType = GRAPH_EDGE;
291  }
292  else if (typestr == "edge") {
293  this->adjacencyEntityType = GRAPH_EDGE;
294  this->primaryEntityType = GRAPH_VERTEX;
295  }
296  else {
297  std::ostringstream emsg;
298  emsg << __FILE__ << "," << __LINE__
299  << " error: Invalid GraphEntityType " << typestr << std::endl;
300  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
301  throw std::runtime_error(emsg.str());
302  }
303  }
304 
305  // Functions from the BaseAdapter interface
306  size_t getLocalNumIDs() const {
308  return getLocalNumVertices();
309  else
310  return getLocalNumEdges();
311  }
312 
313  void getIDsView(const gno_t *&Ids) const {
315  getVertexIDsView(Ids);
316  else {
317  // TODO: Need getEdgeIDsView? What is an Edge ID?
318  // TODO: std::pair<gno_t, gno_t>?
319  std::ostringstream emsg;
320  emsg << __FILE__ << "," << __LINE__
321  << " error: getIDsView not yet supported for graph edges."
322  << std::endl;
323  throw std::runtime_error(emsg.str());
324  }
325  }
326 
327  int getNumWeightsPerID() const {
329  return getNumWeightsPerVertex();
330  else
331  return getNumWeightsPerEdge();
332  }
333 
334  void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const {
336  getVertexWeightsView(wgt, stride, idx);
337  else {
338  // TODO: Need getEdgeWeightsView that lets Edges be primary object?
339  // TODO: That is, get edge weights based on some Edge ID.
340  std::ostringstream emsg;
341  emsg << __FILE__ << "," << __LINE__
342  << " error: getWeightsView not yet supported for graph edges."
343  << std::endl;
344  throw std::runtime_error(emsg.str());
345  }
346  }
347 
348  bool useDegreeAsWeight(int idx) const
349  {
350  if (this->getPrimaryEntityType() == GRAPH_VERTEX)
351  return useDegreeAsVertexWeight(idx);
352  else {
353  std::ostringstream emsg;
354  emsg << __FILE__ << "," << __LINE__
355  << " error: useDegreeAsWeight is supported only for vertices"
356  << std::endl;
357  throw std::runtime_error(emsg.str());
358  }
359  }
360 };
361 
362 } //namespace Zoltan2
363 
364 #endif
InputTraits< User >::scalar_t scalar_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
enum BaseAdapterType adapterType() const
Returns the type of adapter.
virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, int=0) const
Provide a pointer to the vertex weights, if any.
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
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.
virtual bool useDegreeAsVertexWeight(int) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
GraphEntityType
Enumerated entity type for graphs: Vertices or Edges.
default_offset_t offset_t
The data type to represent offsets.
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
InputTraits< User >::lno_t lno_t
Defines the VectorAdapter interface.
#define Z2_THROW_NOT_IMPLEMENTED
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.
virtual void getEdgesView(const offset_t *&offsets, const gno_t *&adjIds) const =0
Gets adjacency lists for all vertices in a compressed sparse row (CSR) format.
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...
bool useDegreeAsWeight(int idx) const
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.
InputTraits< User >::offset_t offset_t
default_scalar_t scalar_t
The data type for weights and coordinates.
virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride, int=0) const
Provide a pointer to the edge weights, if any.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:74