Zoltan2
Zoltan2_GraphAdapter.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00051 #ifndef _ZOLTAN2_GRAPHADAPTER_HPP_
00052 #define _ZOLTAN2_GRAPHADAPTER_HPP_
00053 
00054 #include <Zoltan2_Adapter.hpp>
00055 #include <Zoltan2_VectorAdapter.hpp>
00056 
00057 namespace Zoltan2 {
00058 
00061 enum GraphEntityType {
00062   GRAPH_VERTEX,
00063   GRAPH_EDGE
00064 };
00065 
00102 template <typename User, typename UserCoord=User>
00103   class GraphAdapter : public BaseAdapter<User> {
00104 private:
00105   enum GraphEntityType primaryEntityType; // Entity (vertex or edge) to
00106                                           // be partitioned, ordered,
00107                                           // colored, matched, etc.
00108   enum GraphEntityType adjacencyEntityType; // Entity (edge or vertex)
00109                                             // describing adjacencies;
00110                                             // typically opposite of
00111                                             // primaryEntityType.
00112   VectorAdapter<UserCoord> *coordinateInput_;  // A VectorAdapter containing
00113                                                // coordinates of the objects
00114                                                // with primaryEntityType;
00115                                                // optional.
00116   bool haveCoordinateInput_;                   // Flag indicating whether
00117                                                // coordinateInput_ is provided.
00118 
00119 public:
00120 
00121 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00122   typedef typename InputTraits<User>::scalar_t    scalar_t;
00123   typedef typename InputTraits<User>::lno_t    lno_t;
00124   typedef typename InputTraits<User>::gno_t    gno_t;
00125   typedef typename InputTraits<User>::zgid_t    zgid_t;
00126   typedef typename InputTraits<User>::node_t   node_t;
00127   typedef User user_t;
00128   typedef UserCoord userCoord_t;
00129 #endif
00130 
00131   enum BaseAdapterType adapterType() const {return GraphAdapterType;}
00132 
00135   virtual ~GraphAdapter() {};
00136 
00137   // Default GraphEntityType is GRAPH_VERTEX.
00138   GraphAdapter() : primaryEntityType(GRAPH_VERTEX),
00139                    adjacencyEntityType(GRAPH_EDGE),
00140                    coordinateInput_(),
00141                    haveCoordinateInput_(false) {}
00142 
00144   // Methods to be defined in derived classes.
00145 
00148   virtual size_t getLocalNumVertices() const = 0;
00149 
00152   virtual size_t getLocalNumEdges() const = 0;
00153 
00157   virtual void getVertexIDsView(const zgid_t *&vertexIds) const = 0; 
00158 
00168   virtual void getEdgesView(const lno_t *&offsets,
00169                             const zgid_t *&adjIds) const = 0;
00170        
00173   virtual int getNumWeightsPerVertex() const { return 0; }
00174 
00181   virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, 
00182                                     int idx = 0) const
00183   {
00184     weights = NULL;
00185     stride = 0;
00186     Z2_THROW_NOT_IMPLEMENTED_IN_ADAPTER
00187   }
00188 
00189 
00193   virtual bool useDegreeAsVertexWeight(int idx) const
00194   { 
00195     return false;
00196   }
00197 
00200   virtual int getNumWeightsPerEdge() const { return 0; }
00201 
00208   virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride,
00209                                   int idx = 0) const
00210   {
00211     weights = NULL;
00212     stride = 0;
00213     Z2_THROW_NOT_IMPLEMENTED_IN_ADAPTER
00214   }
00215 
00216 
00225   void setCoordinateInput(VectorAdapter<UserCoord> *coordData)
00226   {
00227     coordinateInput_ = coordData;
00228     haveCoordinateInput_ = true;
00229   }
00230 
00234   bool coordinatesAvailable() const { return haveCoordinateInput_; }
00235 
00239   VectorAdapter<UserCoord> *getCoordinateInput() const
00240   {
00241     return coordinateInput_;
00242   }
00243 
00245   // Implementations of base-class methods
00246 
00250   inline enum GraphEntityType getPrimaryEntityType() const {
00251     return this->primaryEntityType;
00252   }
00253 
00259   void setPrimaryEntityType(std::string typestr) {
00260     if (typestr == "vertex") {
00261       this->primaryEntityType = GRAPH_VERTEX;
00262       this->adjacencyEntityType = GRAPH_EDGE;
00263     }
00264     else if (typestr == "edge") {
00265       this->primaryEntityType = GRAPH_EDGE;
00266       this->adjacencyEntityType = GRAPH_VERTEX;
00267     }
00268     else {
00269       std::ostringstream emsg;
00270       emsg << __FILE__ << "," << __LINE__
00271            << " error:  Invalid GraphEntityType " << typestr << std::endl;
00272       emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
00273       throw std::runtime_error(emsg.str());
00274     }
00275   }
00276 
00281   inline enum GraphEntityType getAdjacencyEntityType() const {
00282     return this->adjacencyEntityType;
00283   }
00284 
00290   void setAdjacencyEntityType(std::string typestr) {
00291     if (typestr == "vertex") {
00292       this->adjacencyEntityType = GRAPH_VERTEX;
00293       this->primaryEntityType = GRAPH_EDGE;
00294     }
00295     else if (typestr == "edge") {
00296       this->adjacencyEntityType = GRAPH_EDGE;
00297       this->primaryEntityType = GRAPH_VERTEX;
00298     }
00299     else {
00300       std::ostringstream emsg;
00301       emsg << __FILE__ << "," << __LINE__
00302            << " error:  Invalid GraphEntityType " << typestr << std::endl;
00303       emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
00304       throw std::runtime_error(emsg.str());
00305     }
00306   }
00307 
00308   // Functions from the BaseAdapter interface
00309   size_t getLocalNumIDs() const {
00310     if (getPrimaryEntityType() == GRAPH_VERTEX)
00311       return getLocalNumVertices();
00312     else
00313       return getLocalNumEdges();
00314    }
00315 
00316   void getIDsView(const zgid_t *&Ids) const {
00317     if (getPrimaryEntityType() == GRAPH_VERTEX)
00318       getVertexIDsView(Ids);
00319     else {
00320       // TODO:  Need getEdgeIDsView?  What is an Edge ID?  
00321       // TODO:  std::pair<zgid_t, zgid_t>?
00322       std::ostringstream emsg;
00323       emsg << __FILE__ << "," << __LINE__
00324            << " error:  getIDsView not yet supported for graph edges." 
00325            << std::endl;
00326       throw std::runtime_error(emsg.str());
00327     }
00328   }
00329 
00330   int getNumWeightsPerID() const {
00331     if (getPrimaryEntityType() == GRAPH_VERTEX)
00332       return getNumWeightsPerVertex();
00333     else
00334       return getNumWeightsPerEdge();
00335   }
00336 
00337   void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const {
00338     if (getPrimaryEntityType() == GRAPH_VERTEX)
00339       getVertexWeightsView(wgt, stride, idx);
00340     else {
00341       // TODO:  Need getEdgeWeightsView that lets Edges be primary object? 
00342       // TODO:  That is, get edge weights based on some Edge ID.
00343       std::ostringstream emsg;
00344       emsg << __FILE__ << "," << __LINE__
00345            << " error:  getWeightsView not yet supported for graph edges." 
00346            << std::endl;
00347       throw std::runtime_error(emsg.str());
00348     }
00349   }
00350 
00351   bool useDegreeAsWeight(int idx) const
00352   {
00353     if (this->getPrimaryEntityType() == GRAPH_VERTEX)
00354       return useDegreeAsVertexWeight(idx);
00355     else {
00356       std::ostringstream emsg;
00357       emsg << __FILE__ << "," << __LINE__
00358            << " error:  useDegreeAsWeight is supported only for vertices"
00359            << std::endl;
00360       throw std::runtime_error(emsg.str());
00361     }
00362   }
00363 };
00364 
00365 }  //namespace Zoltan2
00366 
00367 #endif