MueLu  Version of the Day
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 ordering = 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  const LO numRows = graph.GetNodeNumVertices();
80  const int myRank = graph.GetComm()->getRank();
81 
82  ArrayRCP<LO> vertex2AggId = aggregates.GetVertex2AggId()->getDataNonConst(0);
83  ArrayRCP<LO> procWinner = aggregates.GetProcWinner() ->getDataNonConst(0);
84 
85  LO numLocalAggregates = aggregates.GetNumAggregates();
86 
87  ArrayRCP<LO> randomVector;
88  if (ordering == "random") {
89  randomVector = arcp<LO>(numRows);
90  for (LO i = 0; i < numRows; i++)
91  randomVector[i] = i;
92  RandomReorder(randomVector);
93  }
94 
95  int aggIndex = -1;
96  size_t aggSize = 0;
97  std::vector<int> aggList(graph.getNodeMaxNumRowEntries());
98 
99  std::queue<LO> graphOrderQueue;
100 
101  // Main loop over all local rows of graph(A)
102  for (LO i = 0; i < numRows; i++) {
103  // Step 1: pick the next node to aggregate
104  LO rootCandidate = 0;
105  if (ordering == "natural") rootCandidate = i;
106  else if (ordering == "random") rootCandidate = randomVector[i];
107  else if (ordering == "graph") {
108 
109  if (graphOrderQueue.size() == 0) {
110  // Current queue is empty for "graph" ordering, populate with one READY node
111  for (LO jnode = 0; jnode < numRows; jnode++)
112  if (aggStat[jnode] == READY) {
113  graphOrderQueue.push(jnode);
114  break;
115  }
116  }
117  if (graphOrderQueue.size() == 0) {
118  // There are no more ready nodes, end the phase
119  break;
120  }
121  rootCandidate = graphOrderQueue.front(); // take next node from graph ordering queue
122  graphOrderQueue.pop(); // delete this node in list
123  }
124 
125  if (aggStat[rootCandidate] != READY)
126  continue;
127 
128  // Step 2: build tentative aggregate
129  aggSize = 0;
130  aggList[aggSize++] = rootCandidate;
131 
132  ArrayView<const LO> neighOfINode = graph.getNeighborVertices(rootCandidate);
133 
134  // If the number of neighbors is less than the minimum number of nodes
135  // per aggregate, we know this is not going to be a valid root, and we
136  // may skip it, but only for "natural" and "random" (for "graph" we still
137  // need to fetch the list of local neighbors to continue)
138  if ((ordering == "natural" || ordering == "random") &&
139  neighOfINode.size() < minNodesPerAggregate) {
140  continue;
141  }
142 
143  LO numAggregatedNeighbours = 0;
144 
145  for (int j = 0; j < neighOfINode.size(); j++) {
146  LO neigh = neighOfINode[j];
147 
148  if (neigh != rootCandidate && graph.isLocalNeighborVertex(neigh)) {
149 
150  if (aggStat[neigh] == READY || aggStat[neigh] == NOTSEL) {
151  // If aggregate size does not exceed max size, add node to the
152  // tentative aggregate
153  // NOTE: We do not exit the loop over all neighbours since we have
154  // still to count all aggregated neighbour nodes for the
155  // aggregation criteria
156  // NOTE: We check here for the maximum aggregation size. If we
157  // would do it below with all the other check too big aggregates
158  // would not be accepted at all.
159  if (aggSize < as<size_t>(maxNodesPerAggregate))
160  aggList[aggSize++] = neigh;
161 
162  } else {
163  numAggregatedNeighbours++;
164  }
165  }
166  }
167 
168  // Step 3: check if tentative aggregate is acceptable
169  if ((numAggregatedNeighbours <= maxNeighAlreadySelected) && // too many connections to other aggregates
170  (aggSize >= as<size_t>(minNodesPerAggregate))) { // too few nodes in the tentative aggregate
171  // Accept new aggregate
172  // rootCandidate becomes the root of the newly formed aggregate
173  aggregates.SetIsRoot(rootCandidate);
174  aggIndex = numLocalAggregates++;
175 
176  for (size_t k = 0; k < aggSize; k++) {
177  aggStat [aggList[k]] = AGGREGATED;
178  vertex2AggId[aggList[k]] = aggIndex;
179  procWinner [aggList[k]] = myRank;
180  }
181 
182  numNonAggregatedNodes -= aggSize;
183 
184  } else {
185  // Aggregate is not accepted
186  aggStat[rootCandidate] = NOTSEL;
187 
188  // Need this for the "graph" ordering below
189  // The original candidate is always aggList[0]
190  aggSize = 1;
191  }
192 
193  if (ordering == "graph") {
194  // Add candidates to the list of nodes
195  // NOTE: the code have slightly different meanings depending on context:
196  // - if aggregate was accepted, we add neighbors of neighbors of the original candidate
197  // - if aggregate was not accepted, we add neighbors of the original candidate
198  for (size_t k = 0; k < aggSize; k++) {
199  ArrayView<const LO> neighOfJNode = graph.getNeighborVertices(aggList[k]);
200 
201  for (int j = 0; j < neighOfJNode.size(); j++) {
202  LO neigh = neighOfJNode[j];
203 
204  if (graph.isLocalNeighborVertex(neigh) && aggStat[neigh] == READY)
205  graphOrderQueue.push(neigh);
206  }
207  }
208  }
209  }
210 
211  // Reset all NOTSEL vertices to READY
212  // This simplifies other algorithms
213  for (LO i = 0; i < numRows; i++)
214  if (aggStat[i] == NOTSEL)
215  aggStat[i] = READY;
216 
217  // update aggregate object
218  aggregates.SetNumAggregates(numLocalAggregates);
219  }
220 
221  template <class LocalOrdinal, class GlobalOrdinal, class Node>
223  //TODO: replace int
224  int n = list.size();
225  for(int i = 0; i < n-1; i++)
226  std::swap(list[i], list[RandomOrdinal(i,n-1)]);
227  }
228 
229  template <class LocalOrdinal, class GlobalOrdinal, class Node>
231  return min + as<int>((max-min+1) * (static_cast<double>(std::rand()) / (RAND_MAX + 1.0)));
232  }
233 
234 } // end namespace
235 
236 
237 #endif /* MUELU_AGGREGATIONPHASE1ALGORITHM_DEF_HPP_ */
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
virtual size_t GetNodeNumVertices() const =0
Return number of vertices owned by the calling node.
Namespace for MueLu classes and methods.
const RCP< LOVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
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).
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.
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.