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;
125 #endif
126 
128 
131  virtual ~GraphAdapter() {};
132 
133  // Default GraphEntityType is GRAPH_VERTEX.
134  GraphAdapter() : primaryEntityType(GRAPH_VERTEX),
135  adjacencyEntityType(GRAPH_EDGE),
136  coordinateInput_(),
137  haveCoordinateInput_(false) {}
138 
140  // Methods to be defined in derived classes.
141 
144  virtual size_t getLocalNumVertices() const = 0;
145 
148  virtual size_t getLocalNumEdges() const = 0;
149 
153  virtual void getVertexIDsView(const gno_t *&vertexIds) const = 0;
154 
164  virtual void getEdgesView(const lno_t *&offsets,
165  const gno_t *&adjIds) const = 0;
166 
169  virtual int getNumWeightsPerVertex() const { return 0; }
170 
177  virtual void getVertexWeightsView(const scalar_t *&weights, int &stride,
178  int idx = 0) const
179  {
180  weights = NULL;
181  stride = 0;
183  }
184 
185 
189  virtual bool useDegreeAsVertexWeight(int idx) const
190  {
191  return false;
192  }
193 
196  virtual int getNumWeightsPerEdge() const { return 0; }
197 
204  virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride,
205  int idx = 0) const
206  {
207  weights = NULL;
208  stride = 0;
210  }
211 
212 
222  {
223  coordinateInput_ = coordData;
224  haveCoordinateInput_ = true;
225  }
226 
230  bool coordinatesAvailable() const { return haveCoordinateInput_; }
231 
236  {
237  return coordinateInput_;
238  }
239 
241  // Implementations of base-class methods
242 
246  inline enum GraphEntityType getPrimaryEntityType() const {
247  return this->primaryEntityType;
248  }
249 
255  void setPrimaryEntityType(std::string typestr) {
256  if (typestr == "vertex") {
257  this->primaryEntityType = GRAPH_VERTEX;
258  this->adjacencyEntityType = GRAPH_EDGE;
259  }
260  else if (typestr == "edge") {
261  this->primaryEntityType = GRAPH_EDGE;
262  this->adjacencyEntityType = GRAPH_VERTEX;
263  }
264  else {
265  std::ostringstream emsg;
266  emsg << __FILE__ << "," << __LINE__
267  << " error: Invalid GraphEntityType " << typestr << std::endl;
268  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
269  throw std::runtime_error(emsg.str());
270  }
271  }
272 
278  return this->adjacencyEntityType;
279  }
280 
286  void setAdjacencyEntityType(std::string typestr) {
287  if (typestr == "vertex") {
288  this->adjacencyEntityType = GRAPH_VERTEX;
289  this->primaryEntityType = GRAPH_EDGE;
290  }
291  else if (typestr == "edge") {
292  this->adjacencyEntityType = GRAPH_EDGE;
293  this->primaryEntityType = GRAPH_VERTEX;
294  }
295  else {
296  std::ostringstream emsg;
297  emsg << __FILE__ << "," << __LINE__
298  << " error: Invalid GraphEntityType " << typestr << std::endl;
299  emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
300  throw std::runtime_error(emsg.str());
301  }
302  }
303 
304  // Functions from the BaseAdapter interface
305  size_t getLocalNumIDs() const {
307  return getLocalNumVertices();
308  else
309  return getLocalNumEdges();
310  }
311 
312  void getIDsView(const gno_t *&Ids) const {
314  getVertexIDsView(Ids);
315  else {
316  // TODO: Need getEdgeIDsView? What is an Edge ID?
317  // TODO: std::pair<gno_t, gno_t>?
318  std::ostringstream emsg;
319  emsg << __FILE__ << "," << __LINE__
320  << " error: getIDsView not yet supported for graph edges."
321  << std::endl;
322  throw std::runtime_error(emsg.str());
323  }
324  }
325 
326  int getNumWeightsPerID() const {
328  return getNumWeightsPerVertex();
329  else
330  return getNumWeightsPerEdge();
331  }
332 
333  void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const {
335  getVertexWeightsView(wgt, stride, idx);
336  else {
337  // TODO: Need getEdgeWeightsView that lets Edges be primary object?
338  // TODO: That is, get edge weights based on some Edge ID.
339  std::ostringstream emsg;
340  emsg << __FILE__ << "," << __LINE__
341  << " error: getWeightsView not yet supported for graph edges."
342  << std::endl;
343  throw std::runtime_error(emsg.str());
344  }
345  }
346 
347  bool useDegreeAsWeight(int idx) const
348  {
349  if (this->getPrimaryEntityType() == GRAPH_VERTEX)
350  return useDegreeAsVertexWeight(idx);
351  else {
352  std::ostringstream emsg;
353  emsg << __FILE__ << "," << __LINE__
354  << " error: useDegreeAsWeight is supported only for vertices"
355  << std::endl;
356  throw std::runtime_error(emsg.str());
357  }
358  }
359 };
360 
361 } //namespace Zoltan2
362 
363 #endif
bool useDegreeAsWeight(int idx) const
InputTraits< User >::scalar_t scalar_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
virtual int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per 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.
virtual bool useDegreeAsVertexWeight(int idx) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
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.
size_t getLocalNumIDs() const
Returns the number of objects on this process.
GraphAdapter defines the interface for graph-based user data.
GraphEntityType
Enumerated entity type for graphs: Vertices or Edges.
InputTraits< User >::lno_t lno_t
Defines the VectorAdapter interface.
#define Z2_THROW_NOT_IMPLEMENTED
static ArrayRCP< ArrayRCP< zscalar_t > > weights
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
enum BaseAdapterType adapterType() const
Returns the type of adapter.
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Provide pointer to a weight array with stride.
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.
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
void setAdjacencyEntityType(std::string typestr)
Sets the adjacency entity type. Called by algorithm based on parameter value in parameter list from a...
virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the vertex weights, if any.
BaseAdapter defines methods required by all Adapters.
enum GraphEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_E...
VectorAdapter< UserCoord > * getCoordinateInput() const
Obtain the coordinate data registered by the user.
void setCoordinateInput(VectorAdapter< UserCoord > *coordData)
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
virtual int getNumWeightsPerEdge() const
Returns the number (0 or greater) of edge weights.
virtual ~GraphAdapter()
Destructor.
virtual size_t getLocalNumVertices() const =0
Returns the number of vertices on this process.
virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the edge weights, if any.
void getIDsView(const gno_t *&Ids) const
Provide a pointer to this process&#39; identifiers.
default_scalar_t scalar_t
The data type for weights and coordinates.