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