-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy pathMueLu_AggregationPhase1Algorithm_kokkos_def.hpp
430 lines (368 loc) · 16.9 KB
/
MueLu_AggregationPhase1Algorithm_kokkos_def.hpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// @HEADER
//
// ***********************************************************************
//
// MueLu: A package for multigrid based preconditioning
// 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
// Jonathan Hu (jhu@sandia.gov)
// Andrey Prokopenko (aprokop@sandia.gov)
// Ray Tuminaro (rstumin@sandia.gov)
//
// ***********************************************************************
//
// @HEADER
#ifndef MUELU_AGGREGATIONPHASE1ALGORITHM_KOKKOS_DEF_HPP
#define MUELU_AGGREGATIONPHASE1ALGORITHM_KOKKOS_DEF_HPP
#ifdef HAVE_MUELU_KOKKOS_REFACTOR
#include <queue>
#include <vector>
#include <Teuchos_Comm.hpp>
#include <Teuchos_CommHelpers.hpp>
#include <Xpetra_Vector.hpp>
#include "MueLu_AggregationPhase1Algorithm_kokkos_decl.hpp"
#include "MueLu_Aggregates_kokkos.hpp"
#include "MueLu_Exceptions.hpp"
#include "MueLu_LWGraph_kokkos.hpp"
#include "MueLu_Monitor.hpp"
#include "KokkosGraph_Distance2ColorHandle.hpp"
#include "KokkosGraph_Distance2Color.hpp"
namespace MueLu {
template <class LocalOrdinal, class GlobalOrdinal, class Node>
void AggregationPhase1Algorithm_kokkos<LocalOrdinal, GlobalOrdinal, Node>::
BuildAggregates(const ParameterList& params, const LWGraph_kokkos& graph, Aggregates_kokkos& aggregates, std::vector<unsigned>& aggStat,
LO& numNonAggregatedNodes) const {
Monitor m(*this, "BuildAggregates");
std::string orderingStr = params.get<std::string>("aggregation: ordering");
int maxNeighAlreadySelected = params.get<int> ("aggregation: max selected neighbors");
int minNodesPerAggregate = params.get<int> ("aggregation: min agg size");
int maxNodesPerAggregate = params.get<int> ("aggregation: max agg size");
Algorithm algorithm = Algorithm::Serial;
std::string algoParamName = "aggregation: phase 1 algorithm";
if(params.isParameter(algoParamName))
{
algorithm = algorithmFromName(params.get<std::string>("aggregation: phase 1 algorithm"));
}
TEUCHOS_TEST_FOR_EXCEPTION(maxNodesPerAggregate < minNodesPerAggregate, Exceptions::RuntimeError,
"MueLu::UncoupledAggregationAlgorithm::BuildAggregates: minNodesPerAggregate must be smaller or equal to MaxNodePerAggregate!");
//Distance-2 gives less control than serial uncoupled phase 1
//no custom row reordering because would require making deep copy of local matrix entries and permuting it
//can only enforce max aggregate size
if(algorithm == Algorithm::Distance2)
{
BuildAggregatesDistance2(graph, aggregates, aggStat, numNonAggregatedNodes, maxNodesPerAggregate);
}
else
{
BuildAggregatesSerial(graph, aggregates, aggStat, numNonAggregatedNodes,
minNodesPerAggregate, maxNodesPerAggregate, maxNeighAlreadySelected, orderingStr);
}
}
template <class LocalOrdinal, class GlobalOrdinal, class Node>
void AggregationPhase1Algorithm_kokkos<LocalOrdinal, GlobalOrdinal, Node>::
BuildAggregatesSerial(const LWGraph_kokkos& graph, Aggregates_kokkos& aggregates,
std::vector<unsigned>& aggStat, LO& numNonAggregatedNodes,
LO minNodesPerAggregate, LO maxNodesPerAggregate,
LO maxNeighAlreadySelected, std::string& orderingStr) const
{
enum {
O_NATURAL,
O_RANDOM,
O_GRAPH
} ordering;
ordering = O_NATURAL; // initialize variable (fix CID 143665)
if (orderingStr == "natural") ordering = O_NATURAL;
if (orderingStr == "random" ) ordering = O_RANDOM;
if (orderingStr == "graph" ) ordering = O_GRAPH;
const LO numRows = graph.GetNodeNumVertices();
const int myRank = graph.GetComm()->getRank();
ArrayRCP<LO> vertex2AggId = aggregates.GetVertex2AggId()->getDataNonConst(0);
ArrayRCP<LO> procWinner = aggregates.GetProcWinner() ->getDataNonConst(0);
LO numLocalAggregates = aggregates.GetNumAggregates();
ArrayRCP<LO> randomVector;
if (ordering == O_RANDOM) {
randomVector = arcp<LO>(numRows);
for (LO i = 0; i < numRows; i++)
randomVector[i] = i;
RandomReorder(randomVector);
}
int aggIndex = -1;
size_t aggSize = 0;
std::vector<int> aggList(graph.getNodeMaxNumRowEntries());
std::queue<LO> graphOrderQueue;
// Main loop over all local rows of graph(A)
for (LO i = 0; i < numRows; i++) {
// Step 1: pick the next node to aggregate
LO rootCandidate = 0;
if (ordering == O_NATURAL) rootCandidate = i;
else if (ordering == O_RANDOM) rootCandidate = randomVector[i];
else if (ordering == O_GRAPH) {
if (graphOrderQueue.size() == 0) {
// Current queue is empty for "graph" ordering, populate with one READY node
for (LO jnode = 0; jnode < numRows; jnode++)
if (aggStat[jnode] == READY) {
graphOrderQueue.push(jnode);
break;
}
}
if (graphOrderQueue.size() == 0) {
// There are no more ready nodes, end the phase
break;
}
rootCandidate = graphOrderQueue.front(); // take next node from graph ordering queue
graphOrderQueue.pop(); // delete this node in list
}
if (aggStat[rootCandidate] != READY)
continue;
// Step 2: build tentative aggregate
aggSize = 0;
aggList[aggSize++] = rootCandidate;
auto neighOfINode = graph.getNeighborVertices(rootCandidate);
// If the number of neighbors is less than the minimum number of nodes
// per aggregate, we know this is not going to be a valid root, and we
// may skip it, but only for "natural" and "random" (for "graph" we still
// need to fetch the list of local neighbors to continue)
if ((ordering == O_NATURAL || ordering == O_RANDOM) &&
as<int>(neighOfINode.length) < minNodesPerAggregate) {
continue;
}
LO numAggregatedNeighbours = 0;
for (int j = 0; j < as<int>(neighOfINode.length); j++) {
LO neigh = neighOfINode(j);
if (neigh != rootCandidate && graph.isLocalNeighborVertex(neigh)) {
if (aggStat[neigh] == READY || aggStat[neigh] == NOTSEL) {
// If aggregate size does not exceed max size, add node to the
// tentative aggregate
// NOTE: We do not exit the loop over all neighbours since we have
// still to count all aggregated neighbour nodes for the
// aggregation criteria
// NOTE: We check here for the maximum aggregation size. If we
// would do it below with all the other check too big aggregates
// would not be accepted at all.
if (aggSize < as<size_t>(maxNodesPerAggregate))
aggList[aggSize++] = neigh;
} else {
numAggregatedNeighbours++;
}
}
}
// Step 3: check if tentative aggregate is acceptable
if ((numAggregatedNeighbours <= maxNeighAlreadySelected) && // too many connections to other aggregates
(aggSize >= as<size_t>(minNodesPerAggregate))) { // too few nodes in the tentative aggregate
// Accept new aggregate
// rootCandidate becomes the root of the newly formed aggregate
aggregates.SetIsRoot(rootCandidate);
aggIndex = numLocalAggregates++;
for (size_t k = 0; k < aggSize; k++) {
aggStat [aggList[k]] = AGGREGATED;
vertex2AggId[aggList[k]] = aggIndex;
procWinner [aggList[k]] = myRank;
}
numNonAggregatedNodes -= aggSize;
} else {
// Aggregate is not accepted
aggStat[rootCandidate] = NOTSEL;
// Need this for the "graph" ordering below
// The original candidate is always aggList[0]
aggSize = 1;
}
if (ordering == O_GRAPH) {
// Add candidates to the list of nodes
// NOTE: the code have slightly different meanings depending on context:
// - if aggregate was accepted, we add neighbors of neighbors of the original candidate
// - if aggregate was not accepted, we add neighbors of the original candidate
for (size_t k = 0; k < aggSize; k++) {
auto neighOfJNode = graph.getNeighborVertices(aggList[k]);
for (int j = 0; j < as<int>(neighOfJNode.length); j++) {
LO neigh = neighOfJNode(j);
if (graph.isLocalNeighborVertex(neigh) && aggStat[neigh] == READY)
graphOrderQueue.push(neigh);
}
}
}
}
// Reset all NOTSEL vertices to READY
// This simplifies other algorithms
for (LO i = 0; i < numRows; i++)
if (aggStat[i] == NOTSEL)
aggStat[i] = READY;
// update aggregate object
aggregates.SetNumAggregates(numLocalAggregates);
}
template <class LocalOrdinal, class GlobalOrdinal, class Node>
void AggregationPhase1Algorithm_kokkos<LocalOrdinal, GlobalOrdinal, Node>::
BuildAggregatesDistance2(const LWGraph_kokkos& graph, Aggregates_kokkos& aggregates,
std::vector<unsigned>& aggStat, LO& numNonAggregatedNodes, LO maxAggSize) const
{
const LO numRows = graph.GetNodeNumVertices();
const int myRank = graph.GetComm()->getRank();
ArrayRCP<LO> vertex2AggId = aggregates.GetVertex2AggId()->getDataNonConst(0);
ArrayRCP<LO> procWinner = aggregates.GetProcWinner() ->getDataNonConst(0);
LO numLocalAggregates = aggregates.GetNumAggregates();
//get the sparse local graph in CRS
std::vector<LocalOrdinal> rowptrs;
rowptrs.reserve(numRows + 1);
std::vector<LocalOrdinal> colinds;
colinds.reserve(graph.GetNodeNumEdges());
rowptrs.push_back(0);
for(LocalOrdinal row = 0; row < numRows; row++)
{
auto entries = graph.getNeighborVertices(row);
for(LocalOrdinal i = 0; i < entries.length; i++)
{
colinds.push_back(entries.colidx(i));
}
rowptrs.push_back(colinds.size());
}
//the local CRS graph to Kokkos device views, then compute graph squared
typedef typename Tpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node>::local_graph_type graph_t;
typedef typename graph_t::device_type device_t;
typedef typename device_t::memory_space memory_space;
typedef typename device_t::execution_space execution_space;
typedef typename graph_t::row_map_type::non_const_type rowptrs_view;
typedef typename rowptrs_view::HostMirror host_rowptrs_view;
typedef typename graph_t::entries_type::non_const_type colinds_view;
typedef typename colinds_view::HostMirror host_colinds_view;
//note: just using colinds_view in place of scalar_view_t type (it won't be used at all by symbolic SPGEMM)
typedef KokkosKernels::Experimental::KokkosKernelsHandle<
typename rowptrs_view::const_value_type, typename colinds_view::const_value_type, typename colinds_view::const_value_type,
execution_space, memory_space, memory_space> KernelHandle;
KernelHandle kh;
//leave gc algorithm choice as the default
kh.create_distance2_graph_coloring_handle();
// get the distance-2 graph coloring handle
auto coloringHandle = kh.get_distance2_graph_coloring_handle();
// Set the distance-2 graph coloring algorithm to use.
// Options:
// COLORING_D2_DEFAULT - Let the kernel handle pick the variation
// COLORING_D2_SERIAL - Use the legacy serial-only implementation
// COLORING_D2_MATRIX_SQUARED - Use the SPGEMM + D1GC method
// COLORING_D2_SPGEMM - Same as MATRIX_SQUARED
// COLORING_D2_VB - Use the parallel vertex based direct method
// COLORING_D2_VB_BIT - Same as VB but using the bitvector forbidden array
// COLORING_D2_VB_BIT_EF - Add experimental edge-filtering to VB_BIT
coloringHandle->set_algorithm( KokkosGraph::COLORING_D2_SERIAL );
//Create device views for graph rowptrs/colinds
rowptrs_view aRowptrs("A device rowptrs", rowptrs.size());
colinds_view aColinds("A device colinds", colinds.size());
// Populate A in temporary host views, then copy to device
{
host_rowptrs_view aHostRowptrs = Kokkos::create_mirror_view(aRowptrs);
for(size_t i = 0; i < rowptrs.size(); i++)
{
aHostRowptrs(i) = rowptrs[i];
}
Kokkos::deep_copy(aRowptrs, aHostRowptrs);
host_colinds_view aHostColinds = Kokkos::create_mirror_view(aColinds);
for(size_t i = 0; i < colinds.size(); i++)
{
aHostColinds(i) = colinds[i];
}
Kokkos::deep_copy(aColinds, aHostColinds);
}
//run d2 graph coloring
//graph is symmetric so row map/entries and col map/entries are the same
KokkosGraph::Experimental::graph_compute_distance2_color(&kh, numRows, numRows, aRowptrs, aColinds, aRowptrs, aColinds);
// extract the colors
auto colorsDevice = coloringHandle->get_vertex_colors();
auto colors = Kokkos::create_mirror_view(colorsDevice);
Kokkos::deep_copy(colors, colorsDevice);
//clean up coloring handle
kh.destroy_distance2_graph_coloring_handle();
//have color 1 (first color) be the aggregate roots (add those to mapping first)
LocalOrdinal aggCount = 0;
for(LocalOrdinal i = 0; i < numRows; i++)
{
if(colors(i) == 1 && aggStat[i] == READY)
{
vertex2AggId[i] = aggCount++;
aggStat[i] = AGGREGATED;
numLocalAggregates++;
procWinner[i] = myRank;
}
}
numNonAggregatedNodes = 0;
std::vector<LocalOrdinal> aggSizes(numLocalAggregates, 0);
for(int i = 0; i < numRows; i++)
{
if(vertex2AggId[i] >= 0)
aggSizes[vertex2AggId[i]]++;
}
//now assign every READY vertex to a directly connected root
for(LocalOrdinal i = 0; i < numRows; i++)
{
if(colors(i) != 1 && (aggStat[i] == READY || aggStat[i] == NOTSEL))
{
//get neighbors of vertex i and
//look for local, aggregated, color 1 neighbor (valid root)
auto neighbors = graph.getNeighborVertices(i);
for(LocalOrdinal j = 0; j < neighbors.length; j++)
{
auto nei = neighbors.colidx(j);
LocalOrdinal agg = vertex2AggId[nei];
if(graph.isLocalNeighborVertex(nei) && colors(nei) == 1 && aggStat[nei] == AGGREGATED && aggSizes[agg] < maxAggSize)
{
//assign vertex i to aggregate with root j
vertex2AggId[i] = agg;
aggSizes[agg]++;
aggStat[i] = AGGREGATED;
procWinner[i] = myRank;
break;
}
}
}
if(aggStat[i] != AGGREGATED)
{
numNonAggregatedNodes++;
if(aggStat[i] == NOTSEL)
aggStat[i] = READY;
}
}
// update aggregate object
aggregates.SetNumAggregates(numLocalAggregates);
}
template <class LocalOrdinal, class GlobalOrdinal, class Node>
void AggregationPhase1Algorithm_kokkos<LocalOrdinal, GlobalOrdinal, Node>::RandomReorder(ArrayRCP<LO> list) const {
//TODO: replace int
int n = list.size();
for(int i = 0; i < n-1; i++)
std::swap(list[i], list[RandomOrdinal(i,n-1)]);
}
template <class LocalOrdinal, class GlobalOrdinal, class Node>
int AggregationPhase1Algorithm_kokkos<LocalOrdinal, GlobalOrdinal, Node>::RandomOrdinal(int min, int max) const {
return min + as<int>((max-min+1) * (static_cast<double>(std::rand()) / (RAND_MAX + 1.0)));
}
} // end namespace
#endif // HAVE_MUELU_KOKKOS_REFACTOR
#endif // MUELU_AGGREGATIONPHASE1ALGORITHM_KOKKOS_DEF_HPP