Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_AlgRCM.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 #ifndef _ZOLTAN2_ALGRCM_HPP_
46 #define _ZOLTAN2_ALGRCM_HPP_
47 
48 #include <Zoltan2_Algorithm.hpp>
49 #include <Zoltan2_GraphModel.hpp>
51 #include <Zoltan2_Sort.hpp>
52 #include <queue>
53 
54 
58 
59 
60 namespace Zoltan2{
61 
62 template <typename Adapter>
63 class AlgRCM : public Algorithm<Adapter>
64 {
65  private:
66 
67  const RCP<GraphModel<Adapter> > model;
68  const RCP<Teuchos::ParameterList> pl;
69  const RCP<const Teuchos::Comm<int> > comm;
70 
71  public:
72 
73  typedef typename Adapter::lno_t lno_t;
74  typedef typename Adapter::gno_t gno_t;
75  typedef typename Adapter::offset_t offset_t;
76  typedef typename Adapter::scalar_t scalar_t;
77 
79  const RCP<GraphModel<Adapter> > &model__,
80  const RCP<Teuchos::ParameterList> &pl__,
81  const RCP<const Teuchos::Comm<int> > &comm__
82  ) : model(model__), pl(pl__), comm(comm__)
83  {
84  }
85 
86  int globalOrder(const RCP<GlobalOrderingSolution<gno_t> > &/* solution */)
87  {
88  throw std::logic_error("AlgRCM does not yet support global ordering.");
89  }
90 
91  int localOrder(const RCP<LocalOrderingSolution<lno_t> > &solution)
92  {
93  int ierr= 0;
94 
95  HELLO;
96 
97  // Get local graph.
98  ArrayView<const gno_t> edgeIds;
99  ArrayView<const offset_t> offsets;
100  ArrayView<StridedData<lno_t, scalar_t> > wgts;
101 
102  const size_t nVtx = model->getLocalNumVertices();
103  model->getEdgeList(edgeIds, offsets, wgts);
104  const int numWeightsPerEdge = model->getNumWeightsPerEdge();
105  if (numWeightsPerEdge > 1){
106  throw std::runtime_error("Multiple weights not supported.");
107  }
108 
109 #if 0
110  // Debug
111  cout << "Debug: Local graph from getLocalEdgeList" << endl;
112  cout << "rank " << comm->getRank() << ": nVtx= " << nVtx << endl;
113  cout << "rank " << comm->getRank() << ": edgeIds: " << edgeIds << endl;
114  cout << "rank " << comm->getRank() << ": offsets: " << offsets << endl;
115 #endif
116 
117  // RCM constructs invPerm, not perm
118  const ArrayRCP<lno_t> invPerm = solution->getPermutationRCP(true);
119 
120  // Check if there are actually edges to reorder.
121  // If there are not, then just use the natural ordering.
122  if (offsets[nVtx] == 0) {
123  for (size_t i = 0; i < nVtx; ++i) {
124  invPerm[i] = i;
125  }
126  solution->setHaveInverse(true);
127  return 0;
128  }
129 
130  // Set the label of each vertex to invalid.
131  Tpetra::global_size_t INVALID = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
132  for (size_t i = 0; i < nVtx; ++i) {
133  invPerm[i] = INVALID;
134  }
135 
136  // Loop over all connected components.
137  // Do BFS within each component.
138  gno_t root = 0;
139  std::queue<gno_t> Q;
140  size_t count = 0; // CM label, reversed later
141  size_t next = 0; // next unmarked vertex
142  Teuchos::Array<std::pair<gno_t, offset_t> > children; // children and their degrees
143 
144  while (count < nVtx) {
145 
146  // Find suitable root vertex for this component.
147  // First find an unmarked vertex, use to find root in next component.
148  while ((next < nVtx) && (static_cast<Tpetra::global_size_t>(invPerm[next]) != INVALID)) next++;
149 
150  // Select root method. Pseudoperipheral usually gives the best
151  // ordering, but the user may choose a faster method.
152  std::string root_method = pl->get("root_method", "pseudoperipheral");
153  if (root_method == std::string("first"))
154  root = next;
155  else if (root_method == std::string("smallest_degree"))
156  root = findSmallestDegree(next, nVtx, edgeIds, offsets);
157  else if (root_method == std::string("pseudoperipheral"))
158  root = findPseudoPeripheral(next, nVtx, edgeIds, offsets);
159  else {
160  // This should never happen if pl was validated.
161  throw std::runtime_error("invalid root_method");
162  }
163 
164  // Label connected component starting at root
165  Q.push(root);
166  //cout << "Debug: invPerm[" << root << "] = " << count << endl;
167  invPerm[root] = count++;
168 
169  while (Q.size()){
170  // Get a vertex from the queue
171  gno_t v = Q.front();
172  Q.pop();
173  //cout << "Debug: v= " << v << ", offsets[v] = " << offsets[v] << endl;
174 
175  // Add unmarked children to list of pairs, to be added to queue.
176  children.resize(0);
177  for (offset_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
178  gno_t child = edgeIds[ptr];
179  if (static_cast<Tpetra::global_size_t>(invPerm[child]) == INVALID){
180  // Not visited yet; add child to list of pairs.
181  std::pair<gno_t,offset_t> newchild;
182  newchild.first = child;
183  newchild.second = offsets[child+1] - offsets[child];
184  children.push_back(newchild);
185  }
186  }
187  // Sort children by increasing degree
188  // TODO: If edge weights, sort children by decreasing weight,
190  zort.sort(children);
191 
192  typename Teuchos::Array<std::pair<gno_t,offset_t> >::iterator it = children.begin ();
193  for ( ; it != children.end(); ++it){
194  // Push children on the queue in sorted order.
195  gno_t child = it->first;
196  invPerm[child] = count++; // Label as we push on Q
197  Q.push(child);
198  //cout << "Debug: invPerm[" << child << "] = " << count << endl;
199  }
200  }
201  }
202 
203  // Reverse labels for RCM
204  bool reverse = true; // TODO: Make parameter
205  if (reverse) {
206  lno_t temp;
207  for (size_t i=0; i < nVtx/2; ++i) {
208  // Swap (invPerm[i], invPerm[nVtx-i])
209  temp = invPerm[i];
210  invPerm[i] = invPerm[nVtx-1-i];
211  invPerm[nVtx-1-i] = temp;
212  }
213  }
214 
215  solution->setHaveInverse(true);
216  return ierr;
217  }
218 
219  private:
220  // Find a smallest degree vertex in component containing v
221  gno_t findSmallestDegree(
222  gno_t v,
223  lno_t nVtx,
224  ArrayView<const gno_t> edgeIds,
225  ArrayView<const offset_t> offsets)
226  {
227  std::queue<gno_t> Q;
228  Teuchos::Array<bool> mark(nVtx);
229 
230  // Do BFS and compute smallest degree as we go
231  offset_t smallestDegree = nVtx;
232  gno_t smallestVertex = 0;
233 
234  // Clear mark array - nothing marked yet
235  for (int i=0; i<nVtx; i++)
236  mark[i] = false;
237 
238  // Start from v
239  Q.push(v);
240  while (Q.size()){
241  // Get first vertex from the queue
242  v = Q.front();
243  Q.pop();
244  // Check degree of v
245  offset_t deg = offsets[v+1] - offsets[v];
246  if (deg < smallestDegree){
247  smallestDegree = deg;
248  smallestVertex = v;
249  }
250  // Add unmarked children to queue
251  for (offset_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
252  gno_t child = edgeIds[ptr];
253  if (!mark[child]){
254  mark[child] = true;
255  Q.push(child);
256  }
257  }
258  }
259  return smallestVertex;
260  }
261 
262  // Find a pseudoperipheral vertex in component containing v
263  gno_t findPseudoPeripheral(
264  gno_t v,
265  lno_t nVtx,
266  ArrayView<const gno_t> edgeIds,
267  ArrayView<const offset_t> offsets)
268  {
269  std::queue<gno_t> Q;
270  Teuchos::Array<bool> mark(nVtx);
271 
272  // Do BFS a couple times, pick vertex last visited (furthest away)
273  const int numBFS = 2;
274  for (int bfs=0; bfs<numBFS; bfs++){
275  // Clear mark array - nothing marked yet
276  for (int i=0; i<nVtx; i++)
277  mark[i] = false;
278  // Start from v
279  Q.push(v);
280  while (Q.size()){
281  // Get first vertex from the queue
282  v = Q.front();
283  Q.pop();
284  // Add unmarked children to queue
285  for (offset_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
286  gno_t child = edgeIds[ptr];
287  if (!mark[child]){
288  mark[child] = true;
289  Q.push(child);
290  }
291  }
292  }
293  }
294  return v;
295  }
296 
297 };
298 }
299 #endif
#define HELLO
#define INVALID(STR)
int localOrder(const RCP< LocalOrderingSolution< lno_t > > &solution)
Ordering method.
Defines the OrderingSolution class.
AlgRCM(const RCP< GraphModel< Adapter > > &model__, const RCP< Teuchos::ParameterList > &pl__, const RCP< const Teuchos::Comm< int > > &comm__)
Adapter::offset_t offset_t
tuple root
Definition: xml2dox.py:168
Algorithm defines the base class for all algorithms.
int globalOrder(const RCP< GlobalOrderingSolution< gno_t > > &)
Ordering method.
Sort vector of pairs (key, value) by value.
Adapter::scalar_t scalar_t
GraphModel defines the interface required for graph models.
Tpetra::global_size_t global_size_t
Defines the GraphModel interface.
Adapter::lno_t lno_t
void sort(Teuchos::Array< std::pair< key_t, value_t > > &listofPairs, bool inc=true)
Adapter::gno_t gno_t