Zoltan2  Version of the Day
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_GraphModel.hpp>
50 #include <queue>
51 
52 
56 
57 
58 namespace Zoltan2{
59 
60 template <typename Adapter>
61 int AlgRCM(
62  const RCP<GraphModel<Adapter> > &model,
63  const RCP<OrderingSolution<typename Adapter::gid_t,
64  typename Adapter::lno_t> > &solution,
65  const RCP<Teuchos::ParameterList> &pl,
66  const RCP<Teuchos::Comm<int> > &comm
67 )
68 {
69  typedef typename Adapter::lno_t lno_t;
70  typedef typename Adapter::gno_t gno_t;
71  typedef typename Adapter::scalar_t scalar_t;
72 
73  int ierr= 0;
74 
75  HELLO;
76 
77  // Check size of communicator: serial only.
78  // TODO: Remove this test when RCM works on local graph.
79  if (comm->getSize() > 1){
80  throw std::runtime_error("RCM currently only works in serial.");
81  }
82 
83  // Get local graph.
84  ArrayView<const gno_t> edgeIds;
85  ArrayView<const lno_t> offsets;
86  ArrayView<StridedData<lno_t, scalar_t> > wgts;
87 
88  // TODO: edgeIds should be of type lno_t for getLocalEdgeList. Needs revisit.
89  //model->getLocalEdgeList(edgeIds, offsets, wgts); // BUGGY!
90  // Use global graph for now. This only works in serial!
91  ArrayView<const int> procIds;
92  size_t numEdges = model->getEdgeList( edgeIds, procIds, offsets, wgts);
93 
94  //cout << "Debug: Local graph from getLocalEdgeList" << endl;
95  //cout << "edgeIds: " << edgeIds << endl;
96  //cout << "offsets: " << offsets << endl;
97 
98  const size_t nVtx = model->getLocalNumVertices();
99  ArrayRCP<lno_t> perm = solution->getPermutationRCP();
100 
101  // Check if there are actually edges to reorder.
102  // If there are not, then just use the natural ordering.
103  if (numEdges == 0) {
104  for (size_t i = 0; i < nVtx; ++i) {
105  perm[i] = i;
106  }
107  return 0;
108  }
109 
110  // Set the label of each vertex to invalid.
111  Tpetra::global_size_t INVALID = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
112  for (size_t i = 0; i < nVtx; ++i) {
113  perm[i] = INVALID;
114  }
115 
116  // TODO: Find min-degree (or pseudo-peripheral) root vertex.
117  lno_t root = 0;
118 
119  // Do BFS from root
120  std::queue<lno_t> Q;
121  size_t count = 0; // CM label, reversed later
122  size_t next = 0;
123 
124  while (count < nVtx) {
125 
126  // Label connected component starting at root
127  Q.push(root);
128  //cout << "Debug: perm[" << root << "] = " << count << endl;
129  perm[root] = count++;
130 
131  while (Q.size()){
132  // Get a vertex from the queue
133  lno_t v = Q.front();
134  Q.pop();
135  //cout << "Debug: v= " << v << ", offsets[v] = " << offsets[v] << endl;
136 
137  // Add unmarked nbors to queue
138  // TODO: If edge weights, sort nbors by decreasing weight,
139  // TODO: Else, sort nbors by increasing degree
140  for (lno_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
141  lno_t nbor = edgeIds[ptr];
142  if (static_cast<Tpetra::global_size_t>(perm[nbor]) == INVALID){
143  //cout << "Debug: perm[" << nbor << "] = " << count << endl;
144  perm[nbor] = count++; // Label as we push on Q
145  Q.push(nbor);
146  }
147  }
148  }
149 
150  // Find an unmarked vertex, use as new root
151  while ((next < nVtx) && (static_cast<Tpetra::global_size_t>(perm[next]) != INVALID)) next++;
152  root = next;
153  }
154 
155  // Reverse labels for RCM
156  bool reverse = true; // TODO: Make parameter
157  if (reverse) {
158  lno_t temp;
159  for (size_t i=0; i < nVtx/2; ++i) {
160  // Swap (perm[i], perm[nVtx-i])
161  temp = perm[i];
162  perm[i] = perm[nVtx-1-i];
163  perm[nVtx-1-i] = temp;
164  }
165  }
166 
167  return ierr;
168 }
169 
170 }
171 #endif
#define HELLO
#define INVALID(STR)
size_t global_size_t
Defines the OrderingSolution class.
int AlgRCM(const RCP< GraphModel< Adapter > > &model, const RCP< OrderingSolution< typename Adapter::gid_t, typename Adapter::lno_t > > &solution, const RCP< Teuchos::ParameterList > &pl, const RCP< Teuchos::Comm< int > > &comm)
double scalar_t
GraphModel defines the interface required for graph models.
Defines the GraphModel interface.
The class containing ordering solutions and metrics.