Zoltan2
Zoltan2_componentMetrics.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 
53 #ifndef _ZOLTAN2_COMPONENTMETRICS_HPP_
54 #define _ZOLTAN2_COMPONENTMETRICS_HPP_
55 
56 #include <Zoltan2_Standards.hpp>
57 #include <Zoltan2_GraphModel.hpp>
58 #include <Teuchos_Comm.hpp>
59 #include <queue>
60 
61 namespace Zoltan2
62 {
63 
64 template <typename Adapter>
66 
67 public:
68  typedef typename Adapter::lno_t lno_t;
69  typedef typename Adapter::gno_t gno_t;
70  typedef typename Adapter::scalar_t scalar_t;
71 
72  perProcessorComponentMetrics(const Adapter &ia,
73  const Teuchos::Comm<int> &comm);
74 
75 #ifdef HAVE_ZOLTAN2_MPI
76  // Wrap MPI_Comm as a Teuchos::Comm, then call Teuchos::Comm constructor
77  // Uses delegating constructor feature of C++11.
78  perProcessorComponentMetrics(const Adapter &ia, const MPI_Comm mpicomm) :
80  Teuchos::MpiComm<int>(Teuchos::opaqueWrapper(mpicomm)))
81  {}
82 #endif
83 
85 
86  inline size_t getNumComponents() {return nComponent;}
87  inline size_t getMaxComponentSize() {return maxComponentSize;}
88  inline size_t getMinComponentSize() {return minComponentSize;}
89  inline double getAvgComponentSize() {return avgComponentSize;}
90 
91 private:
92  size_t nComponent; // number of components
93  size_t maxComponentSize; // size of largest component
94  size_t minComponentSize; // size of smalled component
95  double avgComponentSize; // average component size
96 
97  inline void markAndEnqueue(std::queue<gno_t> &q, bool *mark,
98  size_t &nUnmarkedVtx, size_t &cSize, gno_t vtx) {
99  // insert vtx into the queue
100  q.push(vtx);
101 
102  // mark vtx
103  nUnmarkedVtx--;
104  mark[vtx] = true;
105 
106  // increment component size
107  cSize++;
108  }
109 };
110 
112 template <typename Adapter>
114  const Adapter &ia, const Teuchos::Comm<int> &comm) :
115  nComponent(0), maxComponentSize(0), minComponentSize(0),
116  avgComponentSize(0.)
117 {
118  // build local graph model from input adapter
119  std::bitset<NUM_MODEL_FLAGS> graphFlags;
120  graphFlags.set(REMOVE_SELF_EDGES);
121  graphFlags.set(BUILD_LOCAL_GRAPH); // Local graph;
122  // all vertex numbering is 0 to nVtx-1
123 
124  Teuchos::RCP<const Teuchos::Comm<int> > tcomm = rcp(&comm, false);
125  Teuchos::RCP<const Zoltan2::Environment> env =
126  rcp(new Zoltan2::Environment(tcomm));
127 
128  typedef typename Adapter::base_adapter_t base_adapter_t;
129  Teuchos::RCP<const base_adapter_t> ria = rcp(&ia, false);
130  Zoltan2::GraphModel<base_adapter_t> graph(ria, env, tcomm, graphFlags);
131 
132  // get graph from model
133  const size_t nVtx = graph.getLocalNumVertices();
134  ArrayView<const gno_t> adj;
135  ArrayView<const lno_t> offset;
136  ArrayView<StridedData<lno_t, scalar_t> > wgts; // unused
137  graph.getEdgeList(adj, offset, wgts);
138 
139  // do a simple BFS on the graph;
140  // can replace later with KokkosKernels or other TPL
141  size_t nUnmarkedVtx = nVtx;
142  bool *mark = new bool[nUnmarkedVtx];
143  for (size_t i = 0; i < nUnmarkedVtx; i++) mark[i] = false;
144  size_t startVtx = 0;
145 
146  std::queue<gno_t> q;
147 
148  // until all vertices are marked...
149  while (nUnmarkedVtx > 0) {
150 
151  // Find the next component
152  size_t cSize = 0;
153  nComponent++;
154 
155  // Find an unmarked vertex; put it in the queue
156  while (mark[startVtx]) startVtx++;
157  markAndEnqueue(q, mark, nUnmarkedVtx, cSize, startVtx);
158 
159  while (!q.empty()) {
160  gno_t vtx = q.front();
161  q.pop();
162 
163  // Add neighbors of vtx to queue.
164  for (lno_t j = offset[vtx]; j < offset[vtx+1]; j++) {
165  if (!mark[adj[j]]) {
166  markAndEnqueue(q, mark, nUnmarkedVtx, cSize, adj[j]);
167  }
168  }
169  }
170 
171  // update stats
172  if (nComponent == 1) {
173  maxComponentSize = cSize;
174  minComponentSize = cSize;
175  }
176  else {
177  if (cSize > maxComponentSize) maxComponentSize = cSize;
178  if (cSize < minComponentSize) minComponentSize = cSize;
179  }
180  }
181 
182  // update stats
183  if (nComponent) avgComponentSize = double(nVtx) / double(nComponent);
184 
185  delete [] mark;
186 }
187 
188 
189 } // namespace Zoltan2
190 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
size_t getLocalNumVertices() const
Returns the number vertices on this process.
size_t getEdgeList(ArrayView< const gno_t > &edgeIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; edge (neighbor) global Ids, including off-process edges.
algorithm requires no self edges
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
GraphModel defines the interface required for graph models.
Gathering definitions used in software development.
Defines the GraphModel interface.
model represents graph within only one rank
perProcessorComponentMetrics(const Adapter &ia, const Teuchos::Comm< int > &comm)