HavoqGT
single_source_shortest_path.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Lawrence Livermore National Security, LLC.
3  * Produced at the Lawrence Livermore National Laboratory.
4  * Written by Roger Pearce <rpearce@llnl.gov>.
5  * LLNL-CODE-644630.
6  * All rights reserved.
7  *
8  * This file is part of HavoqGT, Version 0.1.
9  * For details, see https://computation.llnl.gov/casc/dcca-pub/dcca/Downloads.html
10  *
11  * Please also read this link – Our Notice and GNU Lesser General Public License.
12  * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
13  *
14  * This program is free software; you can redistribute it and/or modify it under
15  * the terms of the GNU Lesser General Public License (as published by the Free
16  * Software Foundation) version 2.1 dated February 1999.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A
20  * PARTICULAR PURPOSE. See the terms and conditions of the GNU General Public
21  * License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * OUR NOTICE AND TERMS AND CONDITIONS OF THE GNU GENERAL PUBLIC LICENSE
28  *
29  * Our Preamble Notice
30  *
31  * A. This notice is required to be provided under our contract with the
32  * U.S. Department of Energy (DOE). This work was produced at the Lawrence
33  * Livermore National Laboratory under Contract No. DE-AC52-07NA27344 with the DOE.
34  *
35  * B. Neither the United States Government nor Lawrence Livermore National
36  * Security, LLC nor any of their employees, makes any warranty, express or
37  * implied, or assumes any liability or responsibility for the accuracy,
38  * completeness, or usefulness of any information, apparatus, product, or process
39  * disclosed, or represents that its use would not infringe privately-owned rights.
40  *
41  * C. Also, reference herein to any specific commercial products, process, or
42  * services by trade name, trademark, manufacturer or otherwise does not
43  * necessarily constitute or imply its endorsement, recommendation, or favoring by
44  * the United States Government or Lawrence Livermore National Security, LLC. The
45  * views and opinions of authors expressed herein do not necessarily state or
46  * reflect those of the United States Government or Lawrence Livermore National
47  * Security, LLC, and shall not be used for advertising or product endorsement
48  * purposes.
49  *
50  */
51 
52 #ifndef HAVOQGT_MPI_SINGLE_SOURCE_SHORTEST_PATH_HPP_INCLUDED
53 #define HAVOQGT_MPI_SINGLE_SOURCE_SHORTEST_PATH_HPP_INCLUDED
54 
55 
56 
58 #include <queue>
59 
60 namespace havoqgt { namespace mpi {
61 
62 
63 template <typename Visitor>
65 {
66 
67 protected:
68  std::priority_queue< Visitor, std::vector<Visitor>,
69  std::greater<Visitor> > m_data;
70 public:
71  sssp_queue() { }
72 
73  bool push(Visitor const & task)
74  {
75  m_data.push(task);
76  return true;
77  }
78 
79  void pop()
80  {
81  m_data.pop();
82  }
83 
84  Visitor const & top() //const
85  {
86  return m_data.top();
87  }
88 
89  size_t size() const
90  {
91  return m_data.size();;
92  }
93 
94  bool empty() const
95  {
96  return m_data.empty();
97  }
98 
99  void clear()
100  {
101  m_data.clear();
102  }
103 };
104 
105 
106 template<typename Graph, typename PathData, typename EdgeWeight>
108 public:
109  typedef typename Graph::vertex_locator vertex_locator;
110  typedef typename PathData::value_type path_type;
111  sssp_visitor(): m_path(std::numeric_limits<path_type>::max()) { }
112  sssp_visitor(vertex_locator _vertex, path_type _level):
113  vertex(_vertex), m_path(_level) { }
114 
115  sssp_visitor(vertex_locator _vertex) :
116  vertex(_vertex), m_path(0) { }
117 
118 
119  bool pre_visit() const {
120  bool do_visit = (*path_data())[vertex] > m_path;
121  if(do_visit) {
122  (*path_data())[vertex] = m_path;
123  }
124  return do_visit;
125  }
126 
127  template<typename VisitorQueueHandle>
128  bool visit(Graph& g, VisitorQueueHandle vis_queue) const {
129  if(m_path == (*path_data())[vertex])
130  {
131  typedef typename Graph::edge_iterator eitr_type;
132  for(eitr_type eitr = g.edges_begin(vertex); eitr != g.edges_end(vertex); ++eitr) {
133  vertex_locator neighbor = eitr.target();
134  path_type weight = (*edge_data())[eitr];
135  //std::cout << "Visiting neighbor: " << g.locator_to_label(neighbor) << std::endl;
136  sssp_visitor new_visitor( neighbor, weight + m_path);
137  vis_queue->queue_visitor(new_visitor);
138  }
139  return true;
140  }
141  return false;
142  }
143 
144  friend inline bool operator>(const sssp_visitor& v1, const sssp_visitor& v2) {
145  return v1.m_path > v2.m_path;
146  }
147 
148  friend inline bool operator<(const sssp_visitor& v1, const sssp_visitor& v2) {
149  return v1.m_path < v2.m_path;
150  }
151 
152  static void set_path_data(PathData* _data) { path_data() = _data; }
153 
154  static PathData*& path_data() {
155  static PathData* data;
156  return data;
157  }
158 
159  static void set_edge_weight(EdgeWeight* _data) { edge_data() = _data; }
160 
161  static EdgeWeight*& edge_data() {
162  static EdgeWeight* data;
163  return data;
164  }
165 
166  vertex_locator vertex;
167  path_type m_path;
168 };
169 
170 
171 template <typename TGraph, typename PathData, typename EdgeWeight>
173  PathData& path_data,
174  EdgeWeight& edge_data,
175  typename TGraph::vertex_locator s) {
176 
177  typedef sssp_visitor<TGraph, PathData, EdgeWeight> visitor_type;
178  visitor_type::set_path_data(&path_data);
179  visitor_type::set_edge_weight(&edge_data);
180  typedef visitor_queue< visitor_type, sssp_queue, TGraph > visitor_queue_type;
181 
182  visitor_queue_type vq(&g);
183  vq.init_visitor_traversal(s);
184 }
185 
186 
187 
188 }} //end namespace havoqgt::mpi
189 
190 
191 
192 
193 #endif //HAVOQGT_MPI_SINGLE_SOURCE_SHORTEST_PATH_HPP_INCLUDED
friend bool operator<(const sssp_visitor &v1, const sssp_visitor &v2)
sssp_visitor(vertex_locator _vertex, path_type _level)
friend bool operator>(const sssp_visitor &v1, const sssp_visitor &v2)
void single_source_shortest_path(TGraph &g, PathData &path_data, EdgeWeight &edge_data, typename TGraph::vertex_locator s)
std::priority_queue< Visitor, std::vector< Visitor >, std::greater< Visitor > > m_data
bool visit(Graph &g, VisitorQueueHandle vis_queue) const
static void set_edge_weight(EdgeWeight *_data)
static void set_path_data(PathData *_data)