Zoltan2
Loading...
Searching...
No Matches
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
57namespace Zoltan2 {
58
64};
65
98template <typename User, typename UserCoord=User>
99 class GraphAdapter : public AdapterWithCoordsWrapper<User, UserCoord> {
100private:
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
115public:
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;
125 typedef GraphAdapter<User, UserCoord> base_adapter_t;
126#endif
127
128 enum BaseAdapterType adapterType() const override {return GraphAdapterType;}
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
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 override {
308 return getLocalNumVertices();
309 else
310 return getLocalNumEdges();
311 }
312
313 void getIDsView(const gno_t *&Ids) const override {
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 override {
329 return getNumWeightsPerVertex();
330 else
331 return getNumWeightsPerEdge();
332 }
333
334 void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const override {
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::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:74
#define Z2_THROW_NOT_IMPLEMENTED
Defines the VectorAdapter interface.
InputTraits< User >::node_t node_t
InputTraits< User >::offset_t offset_t
InputTraits< User >::scalar_t scalar_t
InputTraits< User >::lno_t lno_t
InputTraits< User >::gno_t gno_t
GraphAdapter defines the interface for graph-based user data.
virtual ~GraphAdapter()
Destructor.
virtual size_t getLocalNumVertices() const =0
Returns the number of vertices on this process.
void setCoordinateInput(VectorAdapter< UserCoord > *coordData) override
Allow user to provide additional data that contains coordinate info associated with the MatrixAdapter...
void setAdjacencyEntityType(std::string typestr)
Sets the adjacency entity type. Called by algorithm based on parameter value in parameter list from a...
void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const override
virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride, int=0) const
Provide a pointer to the edge weights, if any.
virtual bool useDegreeAsVertexWeight(int) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
VectorAdapter< UserCoord > * getCoordinateInput() const override
Obtain the coordinate data registered by the user.
size_t getLocalNumIDs() const override
Returns the number of objects on this process.
enum GraphEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_E...
virtual int getNumWeightsPerEdge() const
Returns the number (0 or greater) of edge weights.
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.
virtual void getVertexIDsView(const gno_t *&vertexIds) const =0
Sets pointers to this process' graph entries.
virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, int=0) const
Provide a pointer to the vertex weights, if any.
int getNumWeightsPerID() const override
Returns the number of weights per object. Number of weights per object should be zero or greater....
bool coordinatesAvailable() const
Indicate whether coordinate information has been set for this MatrixAdapter.
enum GraphEntityType getAdjacencyEntityType() const
Returns the entity that describes adjacencies between the entities to be partitioned,...
void getIDsView(const gno_t *&Ids) const override
Provide a pointer to this process' identifiers.
void setPrimaryEntityType(std::string typestr)
Sets the primary entity type. Called by algorithm based on parameter value in parameter list from app...
bool useDegreeAsWeight(int idx) const
virtual int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
enum BaseAdapterType adapterType() const override
Returns the type of adapter.
virtual size_t getLocalNumEdges() const =0
Returns the number of edges on this process.
VectorAdapter defines the interface for vector input.
Created by mbenlioglu on Aug 31, 2020.
BaseAdapterType
An enum to identify general types of adapters.
@ GraphAdapterType
graph data
GraphEntityType
Enumerated entity type for graphs: Vertices or Edges.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
default_offset_t offset_t
The data type to represent offsets.
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.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices.
default_scalar_t scalar_t
The data type for weights and coordinates.