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 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
Zoltan2::GraphAdapter::getLocalNumIDs
size_t getLocalNumIDs() const
Returns the number of objects on this process.
Definition: Zoltan2_GraphAdapter.hpp:306
Zoltan2::InputTraits::scalar_t
default_scalar_t scalar_t
The data type for weights and coordinates.
Definition: Zoltan2_InputTraits.hpp:181
Z2_THROW_NOT_IMPLEMENTED
#define Z2_THROW_NOT_IMPLEMENTED
Definition: Zoltan2_Exceptions.hpp:92
Zoltan2::VectorAdapter< UserCoord >
Zoltan2::BaseAdapter::offset_t
InputTraits< User >::offset_t offset_t
Definition: Zoltan2_Adapter.hpp:108
Zoltan2::GraphEntityType
GraphEntityType
Enumerated entity type for graphs: Vertices or Edges.
Definition: Zoltan2_GraphAdapter.hpp:61
Zoltan2::GraphAdapter::GraphAdapter
GraphAdapter()
Definition: Zoltan2_GraphAdapter.hpp:135
Zoltan2::GraphAdapter::useDegreeAsWeight
bool useDegreeAsWeight(int idx) const
Definition: Zoltan2_GraphAdapter.hpp:348
Zoltan2::GraphAdapter::getLocalNumEdges
virtual size_t getLocalNumEdges() const =0
Returns the number of edges on this process.
Zoltan2::GraphAdapter::getNumWeightsPerVertex
virtual int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
Definition: Zoltan2_GraphAdapter.hpp:170
Zoltan2_TestingFramework::base_adapter_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Definition: Zoltan2_Typedefs.hpp:168
Zoltan2::InputTraits::offset_t
default_offset_t offset_t
The data type to represent offsets.
Definition: Zoltan2_InputTraits.hpp:195
Zoltan2::GraphAdapter::getEdgesView
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.
Zoltan2::GraphAdapter::useDegreeAsVertexWeight
virtual bool useDegreeAsVertexWeight(int idx) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
Definition: Zoltan2_GraphAdapter.hpp:190
Zoltan2_VectorAdapter.hpp
Defines the VectorAdapter interface.
Zoltan2::GraphAdapter::coordinatesAvailable
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
Definition: Zoltan2_GraphAdapter.hpp:231
Zoltan2::GRAPH_VERTEX
Definition: Zoltan2_GraphAdapter.hpp:62
Zoltan2::GraphAdapter::getWeightsView
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Definition: Zoltan2_GraphAdapter.hpp:334
Zoltan2::GraphAdapter::setPrimaryEntityType
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
Definition: Zoltan2_GraphAdapter.hpp:256
Zoltan2::GraphAdapter::setAdjacencyEntityType
void setAdjacencyEntityType(std::string typestr)
Sets the adjacency entity type. Called by algorithm based on parameter value in parameter list from a...
Definition: Zoltan2_GraphAdapter.hpp:287
Zoltan2::BaseAdapter::lno_t
InputTraits< User >::lno_t lno_t
Definition: Zoltan2_Adapter.hpp:104
Zoltan2::BaseAdapter::gno_t
InputTraits< User >::gno_t gno_t
Definition: Zoltan2_Adapter.hpp:105
Zoltan2_Adapter.hpp
Zoltan2::InputTraits::node_t
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
Definition: Zoltan2_InputTraits.hpp:204
Zoltan2::InputTraits::lno_t
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices.
Definition: Zoltan2_InputTraits.hpp:186
Zoltan2::GraphAdapter::adapterType
enum BaseAdapterType adapterType() const
Returns the type of adapter.
Definition: Zoltan2_GraphAdapter.hpp:128
Zoltan2::GraphAdapter::getPrimaryEntityType
enum GraphEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_E...
Definition: Zoltan2_GraphAdapter.hpp:247
Zoltan2::BaseAdapterType
BaseAdapterType
An enum to identify general types of adapters.
Definition: Zoltan2_Adapter.hpp:63
Zoltan2::InputTraits::gno_t
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
Definition: Zoltan2_InputTraits.hpp:191
Zoltan2::GraphAdapter::getLocalNumVertices
virtual size_t getLocalNumVertices() const =0
Returns the number of vertices on this process.
Zoltan2::GraphAdapterType
graph data
Definition: Zoltan2_Adapter.hpp:68
Zoltan2::GraphAdapter::getVertexIDsView
virtual void getVertexIDsView(const gno_t *&vertexIds) const =0
Sets pointers to this process' graph entries.
Zoltan2::GraphAdapter::getNumWeightsPerID
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater....
Definition: Zoltan2_GraphAdapter.hpp:327
Zoltan2::BaseAdapter
Definition: Zoltan2_Adapter.hpp:101
weights
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Definition: rcbPerformanceZ1.cpp:82
Zoltan2::GraphAdapter::getNumWeightsPerEdge
virtual int getNumWeightsPerEdge() const
Returns the number (0 or greater) of edge weights.
Definition: Zoltan2_GraphAdapter.hpp:197
Zoltan2
Definition: Zoltan2_AlgSerialGreedy.hpp:56
Zoltan2::GraphAdapter::getIDsView
void getIDsView(const gno_t *&Ids) const
Provide a pointer to this process' identifiers.
Definition: Zoltan2_GraphAdapter.hpp:313
Zoltan2::GraphAdapter::setCoordinateInput
void setCoordinateInput(VectorAdapter< UserCoord > *coordData)
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
Definition: Zoltan2_GraphAdapter.hpp:222
Zoltan2::GraphAdapter::~GraphAdapter
virtual ~GraphAdapter()
Destructor.
Definition: Zoltan2_GraphAdapter.hpp:132
Zoltan2::GraphAdapter
GraphAdapter defines the interface for graph-based user data.
Definition: Zoltan2_GraphAdapter.hpp:99
Zoltan2::BaseAdapter::scalar_t
InputTraits< User >::scalar_t scalar_t
Definition: Zoltan2_Adapter.hpp:106
Zoltan2::GraphAdapter::getVertexWeightsView
virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the vertex weights, if any.
Definition: Zoltan2_GraphAdapter.hpp:178
Zoltan2::GRAPH_EDGE
Definition: Zoltan2_GraphAdapter.hpp:63
Zoltan2::GraphAdapter::getCoordinateInput
VectorAdapter< UserCoord > * getCoordinateInput() const
Obtain the coordinate data registered by the user.
Definition: Zoltan2_GraphAdapter.hpp:236
Zoltan2::GraphAdapter::getAdjacencyEntityType
enum GraphEntityType getAdjacencyEntityType() const
Returns the entity that describes adjacencies between the entities to be partitioned,...
Definition: Zoltan2_GraphAdapter.hpp:278
Zoltan2::GraphAdapter::getEdgeWeightsView
virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the edge weights, if any.
Definition: Zoltan2_GraphAdapter.hpp:205