Zoltan2 Version of the Day
Zoltan2_GraphInput.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_InputAdapter.hpp>
00055 
00056 namespace Zoltan2 {
00057 
00060 enum GraphEntityType {
00061   GRAPH_VERTEX,
00062   GRAPH_EDGE
00063 };
00064 
00101 template <typename User>
00102   class GraphAdapter : public BaseAdapter<User> {
00103 private:
00104   enum GraphEntityType primaryEntityType; // Entity (vertex or edge) to
00105                                           // be partitioned, ordered,
00106                                           // colored, matched, etc.
00107   enum GraphEntityType adjacencyEntityType; // Entity (edge or vertex)
00108                                             // describing adjacencies;
00109                                             // typically opposite of
00110                                             // primaryEntityType.
00111 
00112 public:
00113 
00114 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00115   typedef typename InputTraits<User>::scalar_t    scalar_t;
00116   typedef typename InputTraits<User>::lno_t    lno_t;
00117   typedef typename InputTraits<User>::gno_t    gno_t;
00118   typedef typename InputTraits<User>::gid_t    gid_t;
00119   typedef typename InputTraits<User>::node_t   node_t;
00120   typedef User user_t;
00121 #endif
00122 
00123   enum BaseAdapterType adapterType() const {return GraphAdapterType;}
00124 
00127   virtual ~GraphAdapter() {};
00128 
00129   // Default GraphEntityType is GRAPH_VERTEX.
00130   GraphAdapter() : primaryEntityType(GRAPH_VERTEX),
00131                    adjacencyEntityType(GRAPH_EDGE) {};
00132 
00134   // Methods to be defined in derived classes.
00135 
00138   virtual size_t getLocalNumVertices() const = 0;
00139 
00142   virtual size_t getLocalNumEdges() const = 0;
00143 
00147   virtual void getVertexIDsView(const gid_t *&vertexIds) const = 0; 
00148 
00158   virtual void getEdgeView(const lno_t *&offsets,
00159                            const gid_t *&adjIds) const = 0;
00160        
00163   virtual int getNumWeightsPerVertex() const = 0;
00164 
00173   virtual void getVertexWeightsView(const scalar_t *&weights, int &stride, 
00174                                     int idx = 0) const = 0;
00175 
00178   virtual int getNumWeightsPerEdge() const = 0;
00179 
00186   virtual void getEdgeWeightsView(const scalar_t *&weights, int &stride,
00187                                   int idx = 0) const = 0;
00188 
00195   virtual int getDimension() const { return 0; }
00196 
00213   virtual void getVertexCoordinatesView(const scalar_t *&coords, int &stride,
00214                                           int idx) const
00215   {
00216     coords = NULL;
00217     stride = 0;
00218   }
00219 
00221   // Implementations of base-class methods
00222 
00226   inline enum GraphEntityType getPrimaryEntityType() const {
00227     return this->primaryEntityType;
00228   }
00229 
00235   void setPrimaryEntityType(string typestr) {
00236     if (typestr == "vertex") {
00237       this->primaryEntityType = GRAPH_VERTEX;
00238       this->adjacencyEntityType = GRAPH_EDGE;
00239     }
00240     else if (typestr == "edge") {
00241       this->primaryEntityType = GRAPH_EDGE;
00242       this->adjacencyEntityType = GRAPH_VERTEX;
00243     }
00244     else {
00245       std::ostringstream emsg;
00246       emsg << __FILE__ << "," << __LINE__
00247            << " error:  Invalid GraphEntityType " << typestr << std::endl;
00248       emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
00249       throw std::runtime_error(emsg.str());
00250     }
00251   }
00252 
00257   inline enum GraphEntityType getAdjacencyEntityType() const {
00258     return this->adjacencyEntityType;
00259   }
00260 
00266   void setAdjacencyEntityType(string typestr) {
00267     if (typestr == "vertex") {
00268       this->adjacencyEntityType = GRAPH_VERTEX;
00269       this->primaryEntityType = GRAPH_EDGE;
00270     }
00271     else if (typestr == "edge") {
00272       this->adjacencyEntityType = GRAPH_EDGE;
00273       this->primaryEntityType = GRAPH_VERTEX;
00274     }
00275     else {
00276       std::ostringstream emsg;
00277       emsg << __FILE__ << "," << __LINE__
00278            << " error:  Invalid GraphEntityType " << typestr << std::endl;
00279       emsg << "Valid values are 'vertex' and 'edge'" << std::endl;
00280       throw std::runtime_error(emsg.str());
00281     }
00282   }
00283 
00284   // Functions from the BaseAdapter interface
00285   size_t getLocalNum() const {
00286     if (getPrimaryEntityType() == GRAPH_VERTEX)
00287       return getLocalNumVertices();
00288     else
00289       return getLocalNumEdges();
00290    }
00291 
00292   void getIDsView(const gid_t *&Ids) const {
00293     if (getPrimaryEntityType() == GRAPH_VERTEX)
00294       getVertexIDsView(Ids);
00295     else {
00296       // TODO:  Need getEdgeIDsView?  What is an Edge ID?  
00297       // TODO:  std::pair<gid_t, gid_t>?
00298       std::ostringstream emsg;
00299       emsg << __FILE__ << "," << __LINE__
00300            << " error:  getIDsView not yet supported for graph edges." 
00301            << std::endl;
00302       throw std::runtime_error(emsg.str());
00303     }
00304   }
00305 
00306   int getNumWeightsPerID() const {
00307     if (getPrimaryEntityType() == GRAPH_VERTEX)
00308       return getNumWeightsPerVertex();
00309     else
00310       return getNumWeightsPerEdge();
00311   }
00312 
00313   void getWeightsView(const scalar_t *&wgt, int &stride, int idx = 0) const {
00314     if (getPrimaryEntityType() == GRAPH_VERTEX)
00315       getVertexWeightsView(wgt, stride, idx);
00316     else {
00317       // TODO:  Need getEdgeWeightsView that lets Edges be primary object? 
00318       // TODO:  That is, get edge weights based on some Edge ID.
00319       std::ostringstream emsg;
00320       emsg << __FILE__ << "," << __LINE__
00321            << " error:  getWeightsView not yet supported for graph edges." 
00322            << std::endl;
00323       throw std::runtime_error(emsg.str());
00324     }
00325   }
00326 };
00327 
00328 }  //namespace Zoltan2
00329 
00330 #endif