/usr/include/trilinos/Zoltan2_AlgRCM.hpp is in libtrilinos-zoltan2-dev 12.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | // @HEADER
//
// ***********************************************************************
//
// Zoltan2: A package of combinatorial algorithms for scientific computing
// Copyright 2012 Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Karen Devine (kddevin@sandia.gov)
// Erik Boman (egboman@sandia.gov)
// Siva Rajamanickam (srajama@sandia.gov)
//
// ***********************************************************************
//
// @HEADER
#ifndef _ZOLTAN2_ALGRCM_HPP_
#define _ZOLTAN2_ALGRCM_HPP_
#include <Zoltan2_Algorithm.hpp>
#include <Zoltan2_GraphModel.hpp>
#include <Zoltan2_OrderingSolution.hpp>
#include <Zoltan2_Sort.hpp>
#include <queue>
////////////////////////////////////////////////////////////////////////
//! \file Zoltan2_AlgRCM.hpp
//! \brief RCM ordering of a graph (serial, local graph only)
namespace Zoltan2{
template <typename Adapter>
class AlgRCM : public Algorithm<Adapter>
{
private:
const RCP<GraphModel<Adapter> > model;
const RCP<Teuchos::ParameterList> pl;
const RCP<const Teuchos::Comm<int> > comm;
public:
typedef typename Adapter::lno_t lno_t;
typedef typename Adapter::gno_t gno_t;
typedef typename Adapter::scalar_t scalar_t;
AlgRCM(
const RCP<GraphModel<Adapter> > &model__,
const RCP<Teuchos::ParameterList> &pl__,
const RCP<const Teuchos::Comm<int> > &comm__
) : model(model__), pl(pl__), comm(comm__)
{
}
int order(const RCP<OrderingSolution<lno_t, gno_t> > &solution)
{
int ierr= 0;
HELLO;
// Get local graph.
ArrayView<const gno_t> edgeIds;
ArrayView<const lno_t> offsets;
ArrayView<StridedData<lno_t, scalar_t> > wgts;
const size_t nVtx = model->getLocalNumVertices();
model->getEdgeList(edgeIds, offsets, wgts);
const int numWeightsPerEdge = model->getNumWeightsPerEdge();
if (numWeightsPerEdge > 1){
throw std::runtime_error("Multiple weights not supported.");
}
#if 0
// Debug
cout << "Debug: Local graph from getLocalEdgeList" << endl;
cout << "rank " << comm->getRank() << ": nVtx= " << nVtx << endl;
cout << "rank " << comm->getRank() << ": edgeIds: " << edgeIds << endl;
cout << "rank " << comm->getRank() << ": offsets: " << offsets << endl;
#endif
// RCM constructs invPerm, not perm
const ArrayRCP<lno_t> invPerm = solution->getPermutationRCP(true);
// Check if there are actually edges to reorder.
// If there are not, then just use the natural ordering.
if (offsets[nVtx] == 0) {
for (size_t i = 0; i < nVtx; ++i) {
invPerm[i] = i;
}
solution->setHaveInverse(true);
return 0;
}
// Set the label of each vertex to invalid.
Tpetra::global_size_t INVALID = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
for (size_t i = 0; i < nVtx; ++i) {
invPerm[i] = INVALID;
}
// Loop over all connected components.
// Do BFS within each component.
gno_t root = 0;
std::queue<gno_t> Q;
size_t count = 0; // CM label, reversed later
size_t next = 0; // next unmarked vertex
Teuchos::Array<std::pair<gno_t, size_t> > children; // children and their degrees
while (count < nVtx) {
// Find suitable root vertex for this component.
// First find an unmarked vertex, use to find root in next component.
while ((next < nVtx) && (static_cast<Tpetra::global_size_t>(invPerm[next]) != INVALID)) next++;
// Select root method. Pseudoperipheral usually gives the best
// ordering, but the user may choose a faster method.
std::string root_method = pl->get("root_method", "pseudoperipheral");
if (root_method == std::string("first"))
root = next;
else if (root_method == std::string("smallest_degree"))
root = findSmallestDegree(next, nVtx, edgeIds, offsets);
else if (root_method == std::string("pseudoperipheral"))
root = findPseudoPeripheral(next, nVtx, edgeIds, offsets);
else {
// This should never happen if pl was validated.
throw std::runtime_error("invalid root_method");
}
// Label connected component starting at root
Q.push(root);
//cout << "Debug: invPerm[" << root << "] = " << count << endl;
invPerm[root] = count++;
while (Q.size()){
// Get a vertex from the queue
gno_t v = Q.front();
Q.pop();
//cout << "Debug: v= " << v << ", offsets[v] = " << offsets[v] << endl;
// Add unmarked children to list of pairs, to be added to queue.
children.resize(0);
for (lno_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
gno_t child = edgeIds[ptr];
if (static_cast<Tpetra::global_size_t>(invPerm[child]) == INVALID){
// Not visited yet; add child to list of pairs.
std::pair<gno_t,size_t> newchild;
newchild.first = child;
newchild.second = offsets[child+1] - offsets[child];
children.push_back(newchild);
}
}
// Sort children by increasing degree
// TODO: If edge weights, sort children by decreasing weight,
SortPairs<gno_t,size_t> zort;
zort.sort(children);
typename Teuchos::Array<std::pair<gno_t,size_t> >::iterator it = children.begin ();
for ( ; it != children.end(); ++it){
// Push children on the queue in sorted order.
gno_t child = it->first;
invPerm[child] = count++; // Label as we push on Q
Q.push(child);
//cout << "Debug: invPerm[" << child << "] = " << count << endl;
}
}
}
// Reverse labels for RCM
bool reverse = true; // TODO: Make parameter
if (reverse) {
lno_t temp;
for (size_t i=0; i < nVtx/2; ++i) {
// Swap (invPerm[i], invPerm[nVtx-i])
temp = invPerm[i];
invPerm[i] = invPerm[nVtx-1-i];
invPerm[nVtx-1-i] = temp;
}
}
solution->setHaveInverse(true);
return ierr;
}
private:
// Find a smallest degree vertex in component containing v
gno_t findSmallestDegree(
gno_t v,
lno_t nVtx,
ArrayView<const gno_t> edgeIds,
ArrayView<const lno_t> offsets)
{
std::queue<gno_t> Q;
Teuchos::Array<bool> mark(nVtx);
// Do BFS and compute smallest degree as we go
lno_t smallestDegree = nVtx;
gno_t smallestVertex = 0;
// Clear mark array - nothing marked yet
for (int i=0; i<nVtx; i++)
mark[i] = false;
// Start from v
Q.push(v);
while (Q.size()){
// Get first vertex from the queue
v = Q.front();
Q.pop();
// Check degree of v
lno_t deg = offsets[v+1] - offsets[v];
if (deg < smallestDegree){
smallestDegree = deg;
smallestVertex = v;
}
// Add unmarked children to queue
for (lno_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
gno_t child = edgeIds[ptr];
if (!mark[child]){
mark[child] = true;
Q.push(child);
}
}
}
return smallestVertex;
}
// Find a pseudoperipheral vertex in component containing v
gno_t findPseudoPeripheral(
gno_t v,
lno_t nVtx,
ArrayView<const gno_t> edgeIds,
ArrayView<const lno_t> offsets)
{
std::queue<gno_t> Q;
Teuchos::Array<bool> mark(nVtx);
// Do BFS a couple times, pick vertex last visited (furthest away)
const int numBFS = 2;
for (int bfs=0; bfs<numBFS; bfs++){
// Clear mark array - nothing marked yet
for (int i=0; i<nVtx; i++)
mark[i] = false;
// Start from v
Q.push(v);
while (Q.size()){
// Get first vertex from the queue
v = Q.front();
Q.pop();
// Add unmarked children to queue
for (lno_t ptr = offsets[v]; ptr < offsets[v+1]; ++ptr){
gno_t child = edgeIds[ptr];
if (!mark[child]){
mark[child] = true;
Q.push(child);
}
}
}
}
return v;
}
};
}
#endif
|