MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_ClassicalMapFactory_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_CLASSICALMAPFACTORY_DEF_HPP_
11#define MUELU_CLASSICALMAPFACTORY_DEF_HPP_
12
13#include <Teuchos_Array.hpp>
14#include <Teuchos_ArrayRCP.hpp>
15
16#ifdef HAVE_MPI
17#include <Teuchos_DefaultMpiComm.hpp>
18#endif
19
20#include <Xpetra_Vector.hpp>
21#include <Xpetra_StridedMapFactory.hpp>
22#include <Xpetra_VectorFactory.hpp>
23#include <Xpetra_Import.hpp>
24#include <Xpetra_IO.hpp>
25
27#include "MueLu_Level.hpp"
28#include "MueLu_LWGraph.hpp"
29#include "MueLu_MasterList.hpp"
30#include "MueLu_Monitor.hpp"
31
32#include "MueLu_LWGraph.hpp"
33
34#ifdef HAVE_MUELU_ZOLTAN2
36#include <Zoltan2_XpetraCrsGraphAdapter.hpp>
37#include <Zoltan2_ColoringProblem.hpp>
38#include <Zoltan2_ColoringSolution.hpp>
39
40#endif
41
42#include "MueLu_LWGraph_kokkos.hpp"
43#include <KokkosGraph_Distance1ColorHandle.hpp>
44#include <KokkosGraph_Distance1Color.hpp>
45
46namespace MueLu {
47
48template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
50 RCP<ParameterList> validParamList = rcp(new ParameterList());
51#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
52 SET_VALID_ENTRY("aggregation: deterministic");
53 SET_VALID_ENTRY("aggregation: coloring algorithm");
54 SET_VALID_ENTRY("aggregation: coloring: use color graph");
55#undef SET_VALID_ENTRY
56 validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A");
57 validParamList->set<RCP<const FactoryBase> >("UnAmalgamationInfo", Teuchos::null, "Generating factory of UnAmalgamationInfo");
58 validParamList->set<RCP<const FactoryBase> >("Graph", null, "Generating factory of the graph");
59 validParamList->set<RCP<const FactoryBase> >("Coloring Graph", null, "Generating factory of the graph");
60 validParamList->set<RCP<const FactoryBase> >("DofsPerNode", null, "Generating factory for variable \'DofsPerNode\', usually the same as for \'Graph\'");
61
62 return validParamList;
63}
64
65template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
67 Input(currentLevel, "A");
68 Input(currentLevel, "UnAmalgamationInfo");
69 Input(currentLevel, "Graph");
70
71 const ParameterList& pL = GetParameterList();
72 bool use_color_graph = pL.get<bool>("aggregation: coloring: use color graph");
73 if (use_color_graph)
74 Input(currentLevel, "Coloring Graph");
75}
76
77template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
79 FactoryMonitor m(*this, "Build", currentLevel);
80 const ParameterList& pL = GetParameterList();
81 RCP<const Matrix> A = Get<RCP<Matrix> >(currentLevel, "A");
82
83 RCP<const LWGraph> graph;
84 bool use_color_graph = pL.get<bool>("aggregation: coloring: use color graph");
85 if (use_color_graph)
86 graph = Get<RCP<LWGraph> >(currentLevel, "Coloring Graph");
87 else
88 graph = Get<RCP<LWGraph> >(currentLevel, "Graph");
89
90 /* ============================================================= */
91 /* Phase 1 : Compute an initial MIS */
92 /* ============================================================= */
93 ArrayRCP<LO> myColors;
94 LO numColors = 0;
95
96 RCP<LocalOrdinalVector> fc_splitting;
97 std::string coloringAlgo = pL.get<std::string>("aggregation: coloring algorithm");
98
99 // Switch to Zoltan2 if we're parallel and Tpetra (and not file)
100#ifdef HAVE_MUELU_ZOLTAN2
101 int numProcs = A->getRowMap()->getComm()->getSize();
102 if (coloringAlgo != "file" && numProcs > 1 && graph->GetDomainMap()->lib() == Xpetra::UseTpetra)
103 coloringAlgo = "Zoltan2";
104#endif
105
106 //#define CMS_DUMP
107#ifdef CMS_DUMP
108 {
109 int rank = graph->GetDomainMap()->getComm()->getRank();
110
111 printf("[%d,%d] graph local size = %dx%d\n", rank, currentLevel.GetLevelID(), (int)graph->GetDomainMap()->getLocalNumElements(), (int)graph->GetImportMap()->getLocalNumElements());
112
113 std::ofstream ofs(std::string("m_dropped_graph_") + std::to_string(currentLevel.GetLevelID()) + std::string("_") + std::to_string(rank) + std::string(".dat"), std::ofstream::out);
114 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(ofs));
115 graph->print(*fancy, Debug);
116 }
117 {
118 A->getRowMap()->getComm()->barrier();
119 }
120
121#endif
122
123 if (coloringAlgo == "file") {
124 // Read the CF splitting from disk
125 // NOTE: For interoperability reasons, this is dependent on the point_type enum not changing
126 std::string map_file = std::string("map_fcsplitting_") + std::to_string(currentLevel.GetLevelID()) + std::string(".m");
127 std::string color_file = std::string("fcsplitting_") + std::to_string(currentLevel.GetLevelID()) + std::string(".m");
128
129 FILE* mapfile = fopen(map_file.c_str(), "r");
130 using real_type = typename Teuchos::ScalarTraits<SC>::magnitudeType;
131 using RealValuedMultiVector = typename Xpetra::MultiVector<real_type, LO, GO, NO>;
132 RCP<RealValuedMultiVector> mv;
133
134 GetOStream(Statistics1) << "Reading FC splitting from " << color_file << ", using map file " << map_file << ". On rank " << A->getRowMap()->getComm()->getRank() << " local size is " << A->getRowMap()->getLocalNumElements() << std::endl;
135 if (mapfile) {
136 fclose(mapfile);
137 RCP<const Map> colorMap = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ReadMap(map_file, A->getRowMap()->lib(), A->getRowMap()->getComm());
138 TEUCHOS_TEST_FOR_EXCEPTION(!colorMap->isCompatible(*A->getRowMap()), std::invalid_argument, "Coloring on disk has incompatible map with A");
139
140 mv = Xpetra::IO<real_type, LocalOrdinal, GlobalOrdinal, Node>::ReadMultiVector(color_file, colorMap);
141 } else {
142 // Use A's rowmap and hope it matches
143 mv = Xpetra::IO<real_type, LocalOrdinal, GlobalOrdinal, Node>::ReadMultiVector(color_file, A->getRowMap());
144 }
145 TEUCHOS_TEST_FOR_EXCEPTION(mv.is_null(), std::invalid_argument, "Coloring on disk cannot be read");
146 fc_splitting = LocalOrdinalVectorFactory::Build(A->getRowMap());
147 TEUCHOS_TEST_FOR_EXCEPTION(mv->getLocalLength() != fc_splitting->getLocalLength(), std::invalid_argument, "Coloring map mismatch");
148
149 // Overlay the Dirichlet Points (and copy out the rest)
150 auto boundaryNodes = graph->GetBoundaryNodeMap();
151 ArrayRCP<const real_type> mv_data = mv->getData(0);
152 ArrayRCP<LO> fc_data = fc_splitting->getDataNonConst(0);
153 for (LO i = 0; i < (LO)fc_data.size(); i++) {
154 if (boundaryNodes[i])
155 fc_data[i] = DIRICHLET_PT;
156 else
157 fc_data[i] = Teuchos::as<LO>(mv_data[i]);
158 }
159 }
160#ifdef HAVE_MUELU_ZOLTAN2
161 else if (coloringAlgo.find("Zoltan2") != std::string::npos && graph->GetDomainMap()->lib() == Xpetra::UseTpetra) {
162 SubFactoryMonitor sfm(*this, "DistributedGraphColoring", currentLevel);
163 DoDistributedGraphColoring(graph, myColors, numColors);
164 }
165#endif
166 else if (coloringAlgo == "MIS" || graph->GetDomainMap()->lib() == Xpetra::UseTpetra) {
167 SubFactoryMonitor sfm(*this, "MIS", currentLevel);
168 TEUCHOS_TEST_FOR_EXCEPTION(A->getRowMap()->getComm()->getSize() != 1, std::invalid_argument, "MIS on more than 1 MPI rank is not supported");
169 DoMISNaive(*graph, myColors, numColors);
170 } else {
171 SubFactoryMonitor sfm(*this, "GraphColoring", currentLevel);
172 TEUCHOS_TEST_FOR_EXCEPTION(A->getRowMap()->getComm()->getSize() != 1, std::invalid_argument, "KokkosKernels graph coloring on more than 1 MPI rank is not supported");
173 DoGraphColoring(*graph, myColors, numColors);
174 }
175
176#ifdef CMS_DUMP
177 {
178 int rank = graph->GetDomainMap()->getComm()->getRank();
179
180 printf("[%d,%d] num colors %d\n", rank, currentLevel.GetLevelID(), numColors);
181
182 std::ofstream ofs(std::string("m_colors_") + std::to_string(currentLevel.GetLevelID()) + std::string("_") + std::to_string(rank) + std::string(".dat"), std::ofstream::out);
183 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(ofs));
184 *fancy << myColors();
185 }
186 {
187 A->getRowMap()->getComm()->barrier();
188 }
189
190#endif
191
192 /* ============================================================= */
193 /* Phase 2 : Mark the C-Points */
194 /* ============================================================= */
195 LO num_c_points = 0, num_d_points = 0, num_f_points = 0;
196 if (fc_splitting.is_null()) {
197 // We just have a coloring, so we need to generate a splitting
198 auto boundaryNodes = graph->GetBoundaryNodeMap();
199 fc_splitting = LocalOrdinalVectorFactory::Build(A->getRowMap());
200 ArrayRCP<LO> myPointType = fc_splitting->getDataNonConst(0);
201 for (LO i = 0; i < (LO)myColors.size(); i++) {
202 if (boundaryNodes[i]) {
203 myPointType[i] = DIRICHLET_PT;
204 num_d_points++;
205 } else if ((LO)myColors[i] == 1) {
206 myPointType[i] = C_PT;
207 num_c_points++;
208 } else
209 myPointType[i] = F_PT;
210 }
211 num_f_points = (LO)myColors.size() - num_d_points - num_c_points;
212 } else {
213 // If we read the splitting off disk, we just need to count
214 ArrayRCP<LO> myPointType = fc_splitting->getDataNonConst(0);
215
216 for (LO i = 0; i < (LO)myPointType.size(); i++) {
217 if (myPointType[i] == DIRICHLET_PT)
218 num_d_points++;
219 else if (myPointType[i] == C_PT)
220 num_c_points++;
221 }
222 num_f_points = (LO)myPointType.size() - num_d_points - num_c_points;
223 }
224
225 /* Output statistics on c/f/d points */
226 if (GetVerbLevel() & Statistics1) {
227 // NOTE: We batch the communication here
228 GO l_counts[] = {(GO)num_c_points, (GO)num_f_points, (GO)num_d_points};
229 GO g_counts[3];
230
231 RCP<const Teuchos::Comm<int> > comm = A->getRowMap()->getComm();
232 Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 3, l_counts, g_counts);
233 GetOStream(Statistics1) << "ClassicalMapFactory(" << coloringAlgo << "): C/F/D = " << g_counts[0] << "/" << g_counts[1] << "/" << g_counts[2] << std::endl;
234 }
235
236 /* Generate the Coarse map */
237 RCP<const Map> coarseMap;
238 {
239 SubFactoryMonitor sfm(*this, "Coarse Map", currentLevel);
240 GenerateCoarseMap(*A->getRowMap(), num_c_points, coarseMap);
241 }
242
243 Set(currentLevel, "FC Splitting", fc_splitting);
244 Set(currentLevel, "CoarseMap", coarseMap);
245}
246
247/* ************************************************************************* */
248template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
250 GenerateCoarseMap(const Map& fineMap, LO num_c_points, RCP<const Map>& coarseMap) const {
251 // FIXME: Assumes scalar PDE
252 std::vector<size_t> stridingInfo_(1);
253 stridingInfo_[0] = 1;
254 GO domainGIDOffset = 0;
255
256 coarseMap = StridedMapFactory::Build(fineMap.lib(),
257 Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(),
258 num_c_points,
259 fineMap.getIndexBase(),
260 stridingInfo_,
261 fineMap.getComm(),
262 domainGIDOffset);
263}
264
265/* ************************************************************************* */
266template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
268 DoGraphColoring(const LWGraph& graph, ArrayRCP<LO>& myColors_out, LO& numColors) const {
269 const ParameterList& pL = GetParameterList();
270 using graph_t = typename LWGraph_kokkos::local_graph_type;
271 using KernelHandle = KokkosKernels::Experimental::
272 KokkosKernelsHandle<typename graph_t::row_map_type::value_type,
273 typename graph_t::entries_type::value_type,
274 typename graph_t::entries_type::value_type,
275 typename graph_t::device_type::execution_space,
276 typename graph_t::device_type::memory_space,
277 typename graph_t::device_type::memory_space>;
278 KernelHandle kh;
279
280 // Leave gc algorithm choice as the default
281 kh.create_graph_coloring_handle();
282
283 // Get the distance-1 graph coloring handle
284 auto coloringHandle = kh.get_graph_coloring_handle();
285
286 // Set the distance-1 coloring algorithm to use
287 if (pL.get<bool>("aggregation: deterministic") == true) {
288 coloringHandle->set_algorithm(KokkosGraph::COLORING_SERIAL);
289 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: serial" << std::endl;
290 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "serial") {
291 coloringHandle->set_algorithm(KokkosGraph::COLORING_SERIAL);
292 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: serial" << std::endl;
293 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "vertex based") {
294 coloringHandle->set_algorithm(KokkosGraph::COLORING_VB);
295 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: vertex based" << std::endl;
296 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "vertex based bit array") {
297 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBBIT);
298 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: vertex based bit array" << std::endl;
299 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "vertex based color set") {
300 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBCS);
301 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: vertex based color set" << std::endl;
302 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "vertex based deterministic") {
303 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBD);
304 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: vertex based deterministic" << std::endl;
305 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "vertex based deterministic bit array") {
306 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBDBIT);
307 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: vertex based deterministic bit array" << std::endl;
308 } else if (pL.get<std::string>("aggregation: coloring algorithm") == "edge based") {
309 coloringHandle->set_algorithm(KokkosGraph::COLORING_EB);
310 if (IsPrint(Statistics1)) GetOStream(Statistics1) << " algorithm: edge based" << std::endl;
311 } else {
312 TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, "Unrecognized distance 1 coloring algorithm");
313 }
314
315 // Create device views for graph rowptrs/colinds
316 size_t numRows = graph.GetNodeNumVertices();
317 // auto graphLWK = dynamic_cast<const LWGraph_kokkos*>(&graph);
318 auto graphLW = dynamic_cast<const LWGraph*>(&graph);
319 TEUCHOS_TEST_FOR_EXCEPTION(!graphLW, std::invalid_argument, "Graph is not a LWGraph object");
320 // Run d1 graph coloring
321 // Assume that the graph is symmetric so row map/entries and col map/entries are the same
322
323 // if (graphLWK) {
324 // KokkosGraph::Experimental::graph_color(&kh,
325 // numRows,
326 // numRows, // FIXME: This should be the number of columns
327 // graphLWK->getRowPtrs(),
328 // graphLWK->getEntries(),
329 // true);
330 // } else
331 if (graphLW) {
332 auto rowptrs = graphLW->getRowPtrs();
333 auto entries = graphLW->getEntries();
334 KokkosGraph::Experimental::graph_color(&kh,
335 numRows,
336 numRows, // FIXME: This should be the number of columns
337 rowptrs,
338 entries,
339 true);
340 }
341
342 // Extract the colors and store them in the aggregates
343 auto myColors_d = coloringHandle->get_vertex_colors();
344 numColors = static_cast<LO>(coloringHandle->get_num_colors());
345
346 // Copy back to host
347 auto myColors_h = Kokkos::create_mirror_view(myColors_d);
348 myColors_out.resize(myColors_h.size());
349 Kokkos::View<LO*, Kokkos::LayoutLeft, Kokkos::HostSpace> myColors_v(&myColors_out[0], myColors_h.size());
350 Kokkos::deep_copy(myColors_v, myColors_h);
351
352 // clean up coloring handle
353 kh.destroy_graph_coloring_handle();
354
355} // end DoGraphColoring
356
357/* ************************************************************************* */
358template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
360 DoMISNaive(const LWGraph& graph, ArrayRCP<LO>& myColors, LO& numColors) const {
361 // This is a fall-back routine for when we don't have Kokkos or when it isn't initialized
362 // We just do greedy MIS because this is easy to write.
363
364 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
365 LO MIS = Teuchos::ScalarTraits<LO>::one();
366
367 // FIXME: Not efficient
368 myColors.resize(0);
369 myColors.resize(graph.GetNodeNumVertices(), LO_INVALID);
370 auto boundaryNodes = graph.GetBoundaryNodeMap();
371 LO Nrows = (LO)graph.GetNodeNumVertices();
372
373 for (LO row = 0; row < Nrows; row++) {
374 if (boundaryNodes[row])
375 continue;
376 auto indices = graph.getNeighborVertices(row);
377 bool has_colored_neighbor = false;
378 for (LO j = 0; !has_colored_neighbor && j < (LO)indices.length; j++) {
379 // FIXME: This does not handle ghosting correctly
380 if (myColors[indices(j)] == MIS)
381 has_colored_neighbor = true;
382 }
383 if (!has_colored_neighbor)
384 myColors[row] = MIS;
385 }
386 numColors = 1;
387}
388
389/* ************************************************************************* */
390template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
392 DoDistributedGraphColoring(RCP<const LWGraph>& graph, ArrayRCP<LO>& myColors_out, LO& numColors) const {
393#ifdef HAVE_MUELU_ZOLTAN2
394 // const ParameterList& pL = GetParameterList();
395 Teuchos::ParameterList params;
396 params.set("color_choice", "FirstFit");
397 params.set("color_method", "D1");
398 // params.set("color_choice", colorMethod);
399 // params.set("color_method", colorAlg);
400 // params.set("verbose", verbose);
401 // params.set("serial_threshold",serialThreshold);
402 // params.set("recolor_degrees",recolorDegrees);
403
404 // Do the coloring via Zoltan2
405 using GraphAdapter = MueLuGraphBaseAdapter<LWGraph>;
406 GraphAdapter z_adapter(graph);
407
408 // We need to provide the MPI Comm, or else we wind up using the default (eep!)
409 Zoltan2::ColoringProblem<GraphAdapter> problem(&z_adapter, &params, graph->GetDomainMap()->getComm());
410 problem.solve();
411 Zoltan2::ColoringSolution<GraphAdapter>* soln = problem.getSolution();
412 ArrayRCP<int> colors = soln->getColorsRCP();
413 numColors = (LO)soln->getNumColors();
414
415 // Assign the Array RCP or Copy Out
416 // FIXME: This probably won't work if LO!=int
417 if (std::is_same<LO, int>::value)
418 myColors_out = colors;
419 else {
420 myColors_out.resize(colors.size());
421 for (LO i = 0; i < (LO)myColors_out.size(); i++)
422 myColors_out[i] = (LO)colors[i];
423 }
424
425 /*
426
427 printf("CMS: numColors = %d\ncolors = ",numColors);
428 for(int i=0;i<colors.size(); i++)
429 printf("%d ",colors[i]);
430 printf("\n");
431
432 */
433#endif // ifdef HAVE_MUELU_ZOLTAN2
434}
435
436} // namespace MueLu
437
438#endif /* MUELU_CLASSICALMAPFACTORY_DEF_HPP_ */
#define SET_VALID_ENTRY(name)
RCP< const ParameterList > GetValidParameterList() const override
Return a const parameter list of valid parameters that setParameterList() will accept.
virtual void GenerateCoarseMap(const Map &fineMap, LO num_c_points, Teuchos::RCP< const Map > &coarseMap) const
virtual void DoGraphColoring(const LWGraph &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
void DeclareInput(Level &currentLevel) const override
Specifies the data that this class needs, and the factories that generate that data.
virtual void DoDistributedGraphColoring(RCP< const LWGraph > &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
virtual void DoMISNaive(const LWGraph &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
void Build(Level &currentLevel) const override
Build an object with this factory.
Timer to be used in factories. Similar to Monitor but with additional timers.
KOKKOS_INLINE_FUNCTION size_type GetNodeNumVertices() const
Return number of graph vertices.
typename std::conditional< OnHost, typename local_graph_device_type::host_mirror_type, local_graph_device_type >::type local_graph_type
KOKKOS_INLINE_FUNCTION const boundary_nodes_type GetBoundaryNodeMap() const
Returns map with global ids of boundary nodes.
KOKKOS_INLINE_FUNCTION neighbor_vertices_type getNeighborVertices(LO i) const
Return the list of vertices adjacent to the vertex 'v'.
Lightweight MueLu representation of a compressed row storage graph.
Class that holds all level-specific information.
int GetLevelID() const
Return level number.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
Namespace for MueLu classes and methods.
@ Debug
Print additional debugging information.
@ Statistics1
Print more statistics.