Zoltan2
Loading...
Searching...
No Matches
Zoltan2_TpetraCrsColorer_Zoltan2.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Teuchos_ArrayRCP.hpp"
4#include "Teuchos_TestForException.hpp"
5
6#include "TpetraExt_MatrixMatrix.hpp"
7#include "Tpetra_CrsMatrix_decl.hpp"
8#include "Tpetra_RowMatrixTransposer.hpp"
9
13
14namespace Zoltan2 {
15
16// Implementation of CrsColorer<> using Zoltan2. Currently this is a distance-1
17// algorithm, which requires forming A^T*A, and only works in serial
18template <typename CrsMatrixType> class Zoltan2CrsColorer {
19public:
20 using matrix_t = CrsMatrixType;
21 using graph_t = typename matrix_t::crs_graph_type;
22 using node_t = typename matrix_t::node_type;
23 using device_t = typename node_t::device_type;
24 using list_of_colors_t = Kokkos::View<int *, device_t>;
25 using list_of_colors_host_t = typename list_of_colors_t::HostMirror;
26 using SC = typename matrix_t::scalar_type;
27 using LO = typename matrix_t::local_ordinal_type;
28 using GO = typename matrix_t::global_ordinal_type;
29 using vector_t = typename Tpetra::Vector<SC, LO, GO, node_t>;
30
31 // Constructor
32 Zoltan2CrsColorer(const Teuchos::RCP<matrix_t> &matrix_)
33 : matrix(matrix_), graph(Impl::get_graph(matrix_)),
34 colorVecLocal_(matrix_->getRowMap()),
35 colorVecGlobal_(matrix_->getColMap()) {}
36
37 // Compute coloring data
38 void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors,
39 list_of_colors_host_t &list_of_colors_host,
40 list_of_colors_t &list_of_colors) {
41 auto inputMat = this->matrix;
42
43 const auto matrixType = coloring_params.get("matrixType", "Jacobian");
44 const auto symmetric = coloring_params.get(
45 "symmetric", (matrixType == "Jacobian" ? false : true));
46
47 // Force symmetrize when input matrix is not symmetric (we can change that
48 // once we implement Bipartate symmetrization)
49 if (!symmetric) {
50 // Transpose symmetrize A+A^T
51 const auto nzpr = this->matrix->getGlobalMaxNumRowEntries();
52
53 inputMat =
54 Teuchos::rcp(new CrsMatrixType(matrix->getRowMap(), nzpr * nzpr));
55
56 Tpetra::MatrixMatrix::Add(*(this->matrix), false, 1.0, *(this->matrix),
57 true, 1.0, inputMat);
58
59 inputMat->fillComplete();
60 }
61
62 // Create Zoltan2 coloring problem and solve
64 Z2Adapter_t z2_adapter(inputMat);
65
66 auto z2_params = coloring_params.sublist("Zoltan2");
67
68 // Once we implement
69 z2_params.set("color_method", "D2");
70
71 Zoltan2::ColoringProblem<Z2Adapter_t> z2_problem(&z2_adapter, &z2_params);
72 z2_problem.solve();
73
74 // Extract colors
75 auto z2_solution = z2_problem.getSolution();
76 auto local_num_colors = z2_solution->getNumColors();
77
78 auto local_list_of_colors = z2_solution->getColorsRCP();
79 const auto len = local_list_of_colors.size();
80
81 // Compute global number of colors
82 auto comm = this->graph->getRowMap()->getComm();
83 Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, 1, &local_num_colors,
84 &num_colors);
85
86 list_of_colors_host_t list_of_colors_tmp(local_list_of_colors.getRawPtr(),
87 len);
88 {
89 auto colors_mv =
90 colorVecLocal_.getLocalViewHost(Tpetra::Access::ReadWrite);
91 auto local_colors = Kokkos::subview(colors_mv, Kokkos::ALL(), 0);
92
93 TEUCHOS_TEST_FOR_EXCEPTION(
94 local_colors.extent(0) != list_of_colors_tmp.extent(0),
95 std::logic_error, "Incorrect length of color list!");
96
97 for (size_t i = 0; i < local_colors.extent(0); ++i) {
98 local_colors(i) = list_of_colors_tmp(i);
99 }
100 }
101
102 using Import_t = Tpetra::Import<typename matrix_t::local_ordinal_type,
103 typename matrix_t::global_ordinal_type,
104 typename matrix_t::node_type>;
105
106 // Create an importer from RowMap to ColMap
107 Import_t importer(matrix->getRowMap(), matrix->getColMap());
108
109 colorVecGlobal_.doImport(colorVecLocal_, importer, Tpetra::INSERT);
110
111 {
112 auto colors_mv =
113 colorVecGlobal_.getLocalViewHost(Tpetra::Access::ReadOnly);
114 auto local_colors = Kokkos::subview(colors_mv, Kokkos::ALL(), 0);
115 const auto num_cols = this->matrix->getLocalNumCols();
116
117 TEUCHOS_TEST_FOR_EXCEPTION(local_colors.extent(0) != num_cols,
118 std::logic_error,
119 "Incorrect length of color list!");
120
121 list_of_colors = list_of_colors_t("list_of_colors", num_cols);
122 list_of_colors_host = Kokkos::create_mirror_view(list_of_colors);
123
124 Kokkos::deep_copy(list_of_colors_host, local_colors);
125 Kokkos::deep_copy(list_of_colors, list_of_colors_host);
126 }
127 }
128
129private:
130 const Teuchos::RCP<matrix_t> matrix;
131 const Teuchos::RCP<const graph_t> graph;
132 vector_t colorVecLocal_;
133 vector_t colorVecGlobal_;
134};
135
137// Specialization of Tpetra::Zoltan2CrsColorer for BlockCrsMatrix
138// Zoltan2 does not directly support BlockCrs, so this implementation
139// creates a point matrix from the graph of the BlockCrs matrix
140template <typename SC, typename LO, typename GO, typename NO>
141class Zoltan2CrsColorer<Tpetra::BlockCrsMatrix<SC, LO, GO, NO>> {
142public:
143 typedef Tpetra::BlockCrsMatrix<SC, LO, GO, NO> matrix_t;
144 typedef typename matrix_t::crs_graph_type graph_t;
145 typedef typename matrix_t::node_type node_t;
146 typedef typename node_t::device_type device_t;
147 typedef Kokkos::View<int *, device_t> list_of_colors_t;
148 typedef typename list_of_colors_t::HostMirror list_of_colors_host_t;
149
150 // Constructor
151 Zoltan2CrsColorer(const Teuchos::RCP<matrix_t> &matrix_)
152 : matrix(matrix_), graph(Teuchos::rcp(&(matrix->getCrsGraph()), false)) {}
153
154 // Destructor
156
157 // Compute coloring data
158 void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors,
159 list_of_colors_host_t &list_of_colors_host,
160 list_of_colors_t &list_of_colors) {
161 using point_matrix_t = Tpetra::CrsMatrix<SC, LO, GO, NO>;
162 Teuchos::RCP<point_matrix_t> point_matrix =
163 Teuchos::rcp(new point_matrix_t(graph));
164 point_matrix->setAllToScalar(1.0);
165 point_matrix->fillComplete();
166 Zoltan2CrsColorer<point_matrix_t> point_colorer(point_matrix);
167 point_colorer.computeColoring(coloring_params, num_colors,
168 list_of_colors_host, list_of_colors);
169 }
170
171private:
172 const Teuchos::RCP<matrix_t> matrix;
173 const Teuchos::RCP<const graph_t> graph;
174};
175} // namespace Zoltan2
Defines the ColoringProblem class.
Defines the ColoringSolution class.
Defines the XpetraCrsMatrixAdapter class.
ColoringProblem sets up coloring problems for the user.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
ColoringSolution< Adapter > * getSolution()
Get the solution to the problem.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors, list_of_colors_host_t &list_of_colors_host, list_of_colors_t &list_of_colors)
typename list_of_colors_t::HostMirror list_of_colors_host_t
Kokkos::View< int *, device_t > list_of_colors_t
void computeColoring(Teuchos::ParameterList &coloring_params, int &num_colors, list_of_colors_host_t &list_of_colors_host, list_of_colors_t &list_of_colors)
typename matrix_t::local_ordinal_type LO
typename Tpetra::Vector< SC, LO, GO, node_t > vector_t
typename matrix_t::crs_graph_type graph_t
typename matrix_t::global_ordinal_type GO
Zoltan2CrsColorer(const Teuchos::RCP< matrix_t > &matrix_)
Created by mbenlioglu on Aug 31, 2020.