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