10 #ifndef MUELU_CLASSICALMAPFACTORY_DEF_HPP_
11 #define MUELU_CLASSICALMAPFACTORY_DEF_HPP_
14 #include <Teuchos_ArrayRCP.hpp>
21 #include <Xpetra_StridedMapFactory.hpp>
28 #include "MueLu_LWGraph.hpp"
32 #include "MueLu_LWGraph.hpp"
34 #ifdef HAVE_MUELU_ZOLTAN2
36 #include <Zoltan2_XpetraCrsGraphAdapter.hpp>
37 #include <Zoltan2_ColoringProblem.hpp>
38 #include <Zoltan2_ColoringSolution.hpp>
42 #include "MueLu_LWGraph_kokkos.hpp"
43 #include <KokkosGraph_Distance1ColorHandle.hpp>
44 #include <KokkosGraph_Distance1Color.hpp>
48 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
51 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
55 #undef SET_VALID_ENTRY
57 validParamList->
set<
RCP<const FactoryBase> >(
"UnAmalgamationInfo", Teuchos::null,
"Generating factory of UnAmalgamationInfo");
60 validParamList->
set<
RCP<const FactoryBase> >(
"DofsPerNode", null,
"Generating factory for variable \'DofsPerNode\', usually the same as for \'Graph\'");
62 return validParamList;
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 Input(currentLevel,
"A");
68 Input(currentLevel,
"UnAmalgamationInfo");
69 Input(currentLevel,
"Graph");
72 bool use_color_graph = pL.
get<
bool>(
"aggregation: coloring: use color graph");
74 Input(currentLevel,
"Coloring Graph");
77 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
84 bool use_color_graph = pL.
get<
bool>(
"aggregation: coloring: use color graph");
86 graph = Get<RCP<LWGraph> >(currentLevel,
"Coloring Graph");
88 graph = Get<RCP<LWGraph> >(currentLevel,
"Graph");
97 std::string coloringAlgo = pL.
get<std::string>(
"aggregation: coloring algorithm");
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";
109 int rank = graph->GetDomainMap()->getComm()->getRank();
111 printf(
"[%d,%d] graph local size = %dx%d\n", rank, currentLevel.GetLevelID(), (int)graph->GetDomainMap()->getLocalNumElements(), (int)graph->GetImportMap()->getLocalNumElements());
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);
115 graph->print(*fancy,
Debug);
118 A->getRowMap()->getComm()->barrier();
125 coloringAlgo =
"MIS";
127 if (coloringAlgo ==
"file") {
130 std::string map_file = std::string(
"map_fcsplitting_") + std::to_string(currentLevel.GetLevelID()) + std::string(
".m");
131 std::string color_file = std::string(
"fcsplitting_") + std::to_string(currentLevel.GetLevelID()) + std::string(
".m");
133 FILE* mapfile = fopen(map_file.c_str(),
"r");
138 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;
142 TEUCHOS_TEST_FOR_EXCEPTION(!colorMap->isCompatible(*A->getRowMap()), std::invalid_argument,
"Coloring on disk has incompatible map with A");
150 fc_splitting = LocalOrdinalVectorFactory::Build(A->getRowMap());
151 TEUCHOS_TEST_FOR_EXCEPTION(mv->getLocalLength() != fc_splitting->getLocalLength(), std::invalid_argument,
"Coloring map mismatch");
154 auto boundaryNodes = graph->GetBoundaryNodeMap();
156 ArrayRCP<LO> fc_data = fc_splitting->getDataNonConst(0);
157 for (
LO i = 0; i < (
LO)fc_data.
size(); i++) {
158 if (boundaryNodes[i])
159 fc_data[i] = DIRICHLET_PT;
161 fc_data[i] = Teuchos::as<LO>(mv_data[i]);
164 #ifdef HAVE_MUELU_ZOLTAN2
165 else if (coloringAlgo.find(
"Zoltan2") != std::string::npos && graph->GetDomainMap()->lib() ==
Xpetra::UseTpetra) {
167 DoDistributedGraphColoring(graph, myColors, numColors);
170 else if (coloringAlgo ==
"MIS" || graph->GetDomainMap()->lib() ==
Xpetra::UseTpetra) {
172 TEUCHOS_TEST_FOR_EXCEPTION(A->getRowMap()->getComm()->getSize() != 1, std::invalid_argument,
"MIS on more than 1 MPI rank is not supported");
173 DoMISNaive(*graph, myColors, numColors);
176 TEUCHOS_TEST_FOR_EXCEPTION(A->getRowMap()->getComm()->getSize() != 1, std::invalid_argument,
"KokkosKernels graph coloring on more than 1 MPI rank is not supported");
177 DoGraphColoring(*graph, myColors, numColors);
182 int rank = graph->GetDomainMap()->getComm()->getRank();
184 printf(
"[%d,%d] num colors %d\n", rank, currentLevel.GetLevelID(), numColors);
186 std::ofstream ofs(std::string(
"m_colors_") + std::to_string(currentLevel.GetLevelID()) + std::string(
"_") + std::to_string(rank) + std::string(
".dat"), std::ofstream::out);
188 *fancy << myColors();
191 A->getRowMap()->getComm()->barrier();
199 LO num_c_points = 0, num_d_points = 0, num_f_points = 0;
202 auto boundaryNodes = graph->GetBoundaryNodeMap();
203 fc_splitting = LocalOrdinalVectorFactory::Build(A->getRowMap());
204 ArrayRCP<LO> myPointType = fc_splitting->getDataNonConst(0);
205 for (
LO i = 0; i < (
LO)myColors.size(); i++) {
206 if (boundaryNodes[i]) {
207 myPointType[i] = DIRICHLET_PT;
209 }
else if ((
LO)myColors[i] == 1) {
210 myPointType[i] = C_PT;
213 myPointType[i] = F_PT;
215 num_f_points = (
LO)myColors.size() - num_d_points - num_c_points;
218 ArrayRCP<LO> myPointType = fc_splitting->getDataNonConst(0);
220 for (
LO i = 0; i < (
LO)myPointType.
size(); i++) {
221 if (myPointType[i] == DIRICHLET_PT)
223 else if (myPointType[i] == C_PT)
226 num_f_points = (
LO)myPointType.
size() - num_d_points - num_c_points;
232 GO l_counts[] = {(
GO)num_c_points, (
GO)num_f_points, (
GO)num_d_points};
237 GetOStream(
Statistics1) <<
"ClassicalMapFactory(" << coloringAlgo <<
"): C/F/D = " << g_counts[0] <<
"/" << g_counts[1] <<
"/" << g_counts[2] << std::endl;
244 GenerateCoarseMap(*A->getRowMap(), num_c_points, coarseMap);
247 Set(currentLevel,
"FC Splitting", fc_splitting);
248 Set(currentLevel,
"CoarseMap", coarseMap);
252 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
256 std::vector<size_t> stridingInfo_(1);
257 stridingInfo_[0] = 1;
258 GO domainGIDOffset = 0;
260 coarseMap = StridedMapFactory::Build(fineMap.lib(),
263 fineMap.getIndexBase(),
270 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
275 using KernelHandle = KokkosKernels::Experimental::
276 KokkosKernelsHandle<
typename graph_t::row_map_type::value_type,
277 typename graph_t::entries_type::value_type,
278 typename graph_t::entries_type::value_type,
279 typename graph_t::device_type::execution_space,
280 typename graph_t::device_type::memory_space,
281 typename graph_t::device_type::memory_space>;
285 kh.create_graph_coloring_handle();
288 auto coloringHandle = kh.get_graph_coloring_handle();
291 if (pL.
get<
bool>(
"aggregation: deterministic") ==
true) {
292 coloringHandle->set_algorithm(KokkosGraph::COLORING_SERIAL);
294 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"serial") {
295 coloringHandle->set_algorithm(KokkosGraph::COLORING_SERIAL);
297 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"vertex based") {
298 coloringHandle->set_algorithm(KokkosGraph::COLORING_VB);
300 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"vertex based bit array") {
301 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBBIT);
303 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"vertex based color set") {
304 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBCS);
306 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"vertex based deterministic") {
307 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBD);
308 if (IsPrint(
Statistics1)) GetOStream(
Statistics1) <<
" algorithm: vertex based deterministic" << std::endl;
309 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"vertex based deterministic bit array") {
310 coloringHandle->set_algorithm(KokkosGraph::COLORING_VBDBIT);
311 if (IsPrint(
Statistics1)) GetOStream(
Statistics1) <<
" algorithm: vertex based deterministic bit array" << std::endl;
312 }
else if (pL.
get<std::string>(
"aggregation: coloring algorithm") ==
"edge based") {
313 coloringHandle->set_algorithm(KokkosGraph::COLORING_EB);
322 auto graphLW =
dynamic_cast<const LWGraph*
>(&graph);
336 auto rowptrs = graphLW->getRowPtrs();
337 auto entries = graphLW->getEntries();
338 KokkosGraph::Experimental::graph_color(&kh,
347 auto myColors_d = coloringHandle->get_vertex_colors();
348 numColors =
static_cast<LO>(coloringHandle->get_num_colors());
351 auto myColors_h = Kokkos::create_mirror_view(myColors_d);
352 myColors_out.
resize(myColors_h.size());
353 Kokkos::View<LO*, Kokkos::LayoutLeft, Kokkos::HostSpace> myColors_v(&myColors_out[0], myColors_h.
size());
354 Kokkos::deep_copy(myColors_v, myColors_h);
357 kh.destroy_graph_coloring_handle();
362 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
377 for (
LO row = 0; row < Nrows; row++) {
378 if (boundaryNodes[row])
381 bool has_colored_neighbor =
false;
382 for (
LO j = 0; !has_colored_neighbor && j < (
LO)indices.length; j++) {
384 if (myColors[indices(j)] == MIS)
385 has_colored_neighbor =
true;
387 if (!has_colored_neighbor)
394 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
397 #ifdef HAVE_MUELU_ZOLTAN2
400 params.
set(
"color_choice",
"FirstFit");
401 params.
set(
"color_method",
"D1");
410 GraphAdapter z_adapter(graph);
413 Zoltan2::ColoringProblem<GraphAdapter> problem(&z_adapter, ¶ms, graph->
GetDomainMap()->getComm());
415 Zoltan2::ColoringSolution<GraphAdapter>* soln = problem.getSolution();
417 numColors = (
LO)soln->getNumColors();
421 if (std::is_same<LO, int>::value)
422 myColors_out = colors;
425 for (
LO i = 0; i < (
LO)myColors_out.
size(); i++)
426 myColors_out[i] = (
LO)colors[i];
437 #endif // ifdef HAVE_MUELU_ZOLTAN2
void Build(Level ¤tLevel) const override
Build an object with this factory.
#define SET_VALID_ENTRY(name)
typename std::conditional< OnHost, typename local_graph_device_type::HostMirror, local_graph_device_type >::type local_graph_type
T & get(const std::string &name, T def_value)
void DeclareInput(Level ¤tLevel) const override
Specifies the data that this class needs, and the factories that generate that data.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void DoDistributedGraphColoring(RCP< const LWGraph > &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
Print additional debugging information.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual void DoMISNaive(const LWGraph &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
KOKKOS_INLINE_FUNCTION size_type GetNodeNumVertices() const
Return number of graph vertices.
KOKKOS_INLINE_FUNCTION const boundary_nodes_type GetBoundaryNodeMap() const
Returns map with global ids of boundary nodes.
void resize(const size_type n, const T &val=T())
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
static RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map)
KOKKOS_INLINE_FUNCTION neighbor_vertices_type getNeighborVertices(LO i) const
Return the list of vertices adjacent to the vertex 'v'.
virtual void DoGraphColoring(const LWGraph &graph, Teuchos::ArrayRCP< LO > &myColors, LO &numColors) const
virtual void GenerateCoarseMap(const Map &fineMap, LO num_c_points, Teuchos::RCP< const Map > &coarseMap) const
const RCP< const Map > GetDomainMap() const
Lightweight MueLu representation of a compressed row storage graph.
RCP< const ParameterList > GetValidParameterList() const override
Return a const parameter list of valid parameters that setParameterList() will accept.