MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_AggregationPhase1Algorithm_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_AGGREGATIONPHASE1ALGORITHM_DEF_HPP_
47 #define MUELU_AGGREGATIONPHASE1ALGORITHM_DEF_HPP_
48 
49 #include <queue>
50 
51 #include <Teuchos_Comm.hpp>
52 #include <Teuchos_CommHelpers.hpp>
53 
54 #include <Xpetra_Vector.hpp>
55 
57 
58 #include "MueLu_GraphBase.hpp"
59 #include "MueLu_Aggregates.hpp"
60 #include "MueLu_Exceptions.hpp"
61 #include "MueLu_Monitor.hpp"
62 
63 namespace MueLu {
64 
65  template <class LocalOrdinal, class GlobalOrdinal, class Node>
67  BuildAggregates(const ParameterList& params, const GraphBase& graph, Aggregates& aggregates, std::vector<unsigned>& aggStat,
68  LO& numNonAggregatedNodes) const {
69  Monitor m(*this, "BuildAggregates");
70 
71  std::string orderingStr = params.get<std::string>("aggregation: ordering");
72  int maxNeighAlreadySelected = params.get<int> ("aggregation: max selected neighbors");
73  int minNodesPerAggregate = params.get<int> ("aggregation: min agg size");
74  int maxNodesPerAggregate = params.get<int> ("aggregation: max agg size");
75 
76  TEUCHOS_TEST_FOR_EXCEPTION(maxNodesPerAggregate < minNodesPerAggregate, Exceptions::RuntimeError,
77  "MueLu::UncoupledAggregationAlgorithm::BuildAggregates: minNodesPerAggregate must be smaller or equal to MaxNodePerAggregate!");
78 
79  enum {
80  O_NATURAL,
81  O_RANDOM,
82  O_GRAPH
83  } ordering;
84  ordering = O_NATURAL; // initialize variable (fix CID 143665)
85  if (orderingStr == "natural") ordering = O_NATURAL;
86  if (orderingStr == "random" ) ordering = O_RANDOM;
87  if (orderingStr == "graph" ) ordering = O_GRAPH;
88 
89  const LO numRows = graph.GetNodeNumVertices();
90  const int myRank = graph.GetComm()->getRank();
91 
92  ArrayRCP<LO> vertex2AggId = aggregates.GetVertex2AggId()->getDataNonConst(0);
93  ArrayRCP<LO> procWinner = aggregates.GetProcWinner() ->getDataNonConst(0);
94 
95  LO numLocalAggregates = aggregates.GetNumAggregates();
96 
97  ArrayRCP<LO> randomVector;
98  if (ordering == O_RANDOM) {
99  randomVector = arcp<LO>(numRows);
100  for (LO i = 0; i < numRows; i++)
101  randomVector[i] = i;
102  RandomReorder(randomVector);
103  }
104 
105  int aggIndex = -1;
106  size_t aggSize = 0;
107  std::vector<int> aggList(graph.getNodeMaxNumRowEntries());
108 
109  std::queue<LO> graphOrderQueue;
110 
111  // Main loop over all local rows of graph(A)
112  for (LO i = 0; i < numRows; i++) {
113  // Step 1: pick the next node to aggregate
114  LO rootCandidate = 0;
115  if (ordering == O_NATURAL) rootCandidate = i;
116  else if (ordering == O_RANDOM) rootCandidate = randomVector[i];
117  else if (ordering == O_GRAPH) {
118 
119  if (graphOrderQueue.size() == 0) {
120  // Current queue is empty for "graph" ordering, populate with one READY node
121  for (LO jnode = 0; jnode < numRows; jnode++)
122  if (aggStat[jnode] == READY) {
123  graphOrderQueue.push(jnode);
124  break;
125  }
126  }
127  if (graphOrderQueue.size() == 0) {
128  // There are no more ready nodes, end the phase
129  break;
130  }
131  rootCandidate = graphOrderQueue.front(); // take next node from graph ordering queue
132  graphOrderQueue.pop(); // delete this node in list
133  }
134 
135  if (aggStat[rootCandidate] != READY)
136  continue;
137 
138  // Step 2: build tentative aggregate
139  aggSize = 0;
140  aggList[aggSize++] = rootCandidate;
141 
142  ArrayView<const LO> neighOfINode = graph.getNeighborVertices(rootCandidate);
143 
144  // If the number of neighbors is less than the minimum number of nodes
145  // per aggregate, we know this is not going to be a valid root, and we
146  // may skip it, but only for "natural" and "random" (for "graph" we still
147  // need to fetch the list of local neighbors to continue)
148  if ((ordering == O_NATURAL || ordering == O_RANDOM) &&
149  neighOfINode.size() < minNodesPerAggregate) {
150  continue;
151  }
152 
153  LO numAggregatedNeighbours = 0;
154 
155  for (int j = 0; j < neighOfINode.size(); j++) {
156  LO neigh = neighOfINode[j];
157 
158  if (neigh != rootCandidate && graph.isLocalNeighborVertex(neigh)) {
159 
160  if (aggStat[neigh] == READY || aggStat[neigh] == NOTSEL) {
161  // If aggregate size does not exceed max size, add node to the
162  // tentative aggregate
163  // NOTE: We do not exit the loop over all neighbours since we have
164  // still to count all aggregated neighbour nodes for the
165  // aggregation criteria
166  // NOTE: We check here for the maximum aggregation size. If we
167  // would do it below with all the other check too big aggregates
168  // would not be accepted at all.
169  if (aggSize < as<size_t>(maxNodesPerAggregate))
170  aggList[aggSize++] = neigh;
171 
172  } else {
173  numAggregatedNeighbours++;
174  }
175  }
176  }
177 
178  // Step 3: check if tentative aggregate is acceptable
179  if ((numAggregatedNeighbours <= maxNeighAlreadySelected) && // too many connections to other aggregates
180  (aggSize >= as<size_t>(minNodesPerAggregate))) { // too few nodes in the tentative aggregate
181  // Accept new aggregate
182  // rootCandidate becomes the root of the newly formed aggregate
183  aggregates.SetIsRoot(rootCandidate);
184  aggIndex = numLocalAggregates++;
185 
186  for (size_t k = 0; k < aggSize; k++) {
187  aggStat [aggList[k]] = AGGREGATED;
188  vertex2AggId[aggList[k]] = aggIndex;
189  procWinner [aggList[k]] = myRank;
190  }
191 
192  numNonAggregatedNodes -= aggSize;
193 
194  } else {
195  // Aggregate is not accepted
196  aggStat[rootCandidate] = NOTSEL;
197 
198  // Need this for the "graph" ordering below
199  // The original candidate is always aggList[0]
200  aggSize = 1;
201  }
202 
203  if (ordering == O_GRAPH) {
204  // Add candidates to the list of nodes
205  // NOTE: the code have slightly different meanings depending on context:
206  // - if aggregate was accepted, we add neighbors of neighbors of the original candidate
207  // - if aggregate was not accepted, we add neighbors of the original candidate
208  for (size_t k = 0; k < aggSize; k++) {
209  ArrayView<const LO> neighOfJNode = graph.getNeighborVertices(aggList[k]);
210 
211  for (int j = 0; j < neighOfJNode.size(); j++) {
212  LO neigh = neighOfJNode[j];
213 
214  if (graph.isLocalNeighborVertex(neigh) && aggStat[neigh] == READY)
215  graphOrderQueue.push(neigh);
216  }
217  }
218  }
219  }
220 
221  // Reset all NOTSEL vertices to READY
222  // This simplifies other algorithms
223  for (LO i = 0; i < numRows; i++)
224  if (aggStat[i] == NOTSEL)
225  aggStat[i] = READY;
226 
227  // update aggregate object
228  aggregates.SetNumAggregates(numLocalAggregates);
229  }
230 
231  template <class LocalOrdinal, class GlobalOrdinal, class Node>
233  //TODO: replace int
234  int n = list.size();
235  for(int i = 0; i < n-1; i++)
236  std::swap(list[i], list[RandomOrdinal(i,n-1)]);
237  }
238 
239  template <class LocalOrdinal, class GlobalOrdinal, class Node>
241  return min + as<int>((max-min+1) * (static_cast<double>(std::rand()) / (RAND_MAX + 1.0)));
242  }
243 
244 } // end namespace
245 
246 
247 #endif /* MUELU_AGGREGATIONPHASE1ALGORITHM_DEF_HPP_ */
LocalOrdinal RandomOrdinal(LocalOrdinal min, LocalOrdinal max)
const RCP< LOVector > & GetProcWinner() const
Returns constant vector that maps local node IDs to owning processor IDs.
Container class for aggregation information.
virtual size_t getNodeMaxNumRowEntries() const =0
T & get(const std::string &name, T def_value)
virtual size_t GetNodeNumVertices() const =0
Return number of vertices owned by the calling node.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
size_type size() const
void SetIsRoot(LO i, bool value=true)
Set root node information.
LO GetNumAggregates() const
returns the number of aggregates of the current processor. Note: could/should be renamed to GetNumLoc...
virtual bool isLocalNeighborVertex(LocalOrdinal v) const =0
Return true if vertex with local id &#39;v&#39; is on current process.
void RandomReorder(ArrayRCP< LO > list) const
Utility to take a list of integers and reorder them randomly (by using a local permutation).
const RCP< LOMultiVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
virtual const RCP< const Teuchos::Comm< int > > GetComm() const =0
MueLu representation of a graph.
void BuildAggregates(const ParameterList &params, const GraphBase &graph, Aggregates &aggregates, std::vector< unsigned > &aggStat, LO &numNonAggregatedNodes) const
Local aggregation.
Timer to be used in non-factories.
void RandomReorder(Teuchos::Array< LocalOrdinal > &list)
Exception throws to report errors in the internal logical of the program.
int RandomOrdinal(int min, int max) const
Generate a random number in the range [min, max].
virtual Teuchos::ArrayView< const LocalOrdinal > getNeighborVertices(LocalOrdinal v) const =0
Return the list of vertices adjacent to the vertex &#39;v&#39;.
void SetNumAggregates(LO nAggregates)
Set number of local aggregates on current processor.