MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_SparseConstraint_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_SPARSECONSTRAINT_DEF_HPP
11#define MUELU_SPARSECONSTRAINT_DEF_HPP
12
13#include <Xpetra_Map.hpp>
14#include <Xpetra_MapFactory.hpp>
15#include <Xpetra_Matrix.hpp>
16#include <Xpetra_MultiVector.hpp>
17#include <Xpetra_CrsGraph.hpp>
18#include <Xpetra_MatrixMatrix.hpp>
19#include "Kokkos_Pair.hpp"
20#include "Teuchos_ScalarTraits.hpp"
21#include "Teuchos_VerbosityLevel.hpp"
22#include "Xpetra_MatrixFactory.hpp"
23
24#include "MueLu_Exceptions.hpp"
26#include "MueLu_Utilities.hpp"
27#include "MueLu_Monitor.hpp"
28
29namespace MueLu {
30template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
32 SparseConstraint(const RCP<Matrix>& P_nodal,
33 const RCP<Matrix>& D,
34 const RCP<Matrix>& Dc,
35 RCP<const CrsGraph> Ppattern,
36 const std::string& solverType) {
37 this->SetPattern(Ppattern);
38 P_nodal_ = P_nodal;
39 D_ = D;
40 Dc_ = Dc;
41 this->SetProcRankVerbose(Ppattern->getRowMap()->getComm()->getRank());
42 Setup();
43 this->PrepareLeastSquaresSolve(solverType, /*detect_singular_blocks=*/true);
44}
45
46template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48 using graph_t = typename CrsGraph::local_graph_type;
49 using matrix_t = typename CrsMatrix::local_matrix_type;
50 using lno_view_t = typename graph_t::row_map_type::non_const_type;
51 using lno_nnz_view_t = typename graph_t::entries_type::non_const_type;
52 using scalar_view_t = typename matrix_t::values_type::non_const_type;
53 using range_type = Kokkos::RangePolicy<LocalOrdinal, typename Node::execution_space>;
54
55 auto INVALID = Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid();
56
57 Monitor m(*this, "Setup");
58
59 auto D = D_;
60 auto Dc = Dc_;
61 auto Ppattern = this->GetPattern();
62
63 // The constraint on Pe (with graph Ppattern) takes the form
64 //
65 // Pe * Dc = D * Pn.
66 //
67 // This means that we have nnz(Pe * Dc) constraints for nnz(Ppattern)
68 // unknowns.
69 //
70 // A single constraint corresponds to an entry (i,j) of Pe * D0c and is
71 // written out via the sparse matrix-matrix products between Pe and D0c:
72 //
73 // sum_{k} Pe_{i,k} Dc_{k,j} = (D * Pn)_{i,j}
74 //
75 // We map (i,j) to its offset I in (Pe * D0c) and (i,k) to its offset J in Pe.
76 //
77 // The constraint matrix X then has the entry
78 // X_{I,J} = Dc_{k,j}.
79
80 auto lib = Ppattern->getRowMap()->lib();
81
82 // If we rebalanced then Dc lives on a smaller communicator than D.
83 // Since we need to perform matrix-matrix multiplications with Dc, we construct a version of it that lives on the same communicator.
84 auto comm = Ppattern->getRowMap()->getComm();
85 if (Dc.is_null() || Dc->getRowMap()->getComm()->getSize() < comm->getSize()) {
86 if (Dc.is_null()) {
87 Kokkos::View<GlobalOrdinal*, typename Node::memory_space> dummy("", 0);
88 auto big_coarse_nodal_map = MapFactory::Build(lib, INVALID, dummy, 0, comm);
89 auto big_coarse_edge_map = MapFactory::Build(lib, INVALID, dummy, 0, comm);
90 auto big_coarse_nodal_colmap = MapFactory::Build(lib, INVALID, dummy, 0, comm);
91
92 typename Matrix::local_matrix_device_type dummyLocalMatrix;
93 big_Dc_ = MatrixFactory::Build(dummyLocalMatrix, big_coarse_edge_map, big_coarse_nodal_colmap, big_coarse_nodal_map, big_coarse_edge_map);
94
95 } else {
96 auto big_coarse_nodal_map = MapFactory::Build(lib, INVALID, Dc->getDomainMap()->getMyGlobalIndicesDevice(), 0, comm);
97 auto big_coarse_edge_map = MapFactory::Build(lib, INVALID, Dc->getRangeMap()->getMyGlobalIndicesDevice(), 0, comm);
98 auto big_coarse_nodal_colmap = MapFactory::Build(lib, INVALID, Dc->getColMap()->getMyGlobalIndicesDevice(), 0, comm);
99
100 big_Dc_ = MatrixFactory::Build(Dc->getLocalMatrixDevice(), big_coarse_edge_map, big_coarse_nodal_colmap, big_coarse_nodal_map, big_coarse_edge_map);
101 }
102 } else {
103 big_Dc_ = Dc;
104 }
105
106 TEUCHOS_TEST_FOR_EXCEPTION(!D->getRangeMap()->isSameAs(*Ppattern->getRangeMap()),
108 "Maps are incompatible");
109 TEUCHOS_TEST_FOR_EXCEPTION(!big_Dc_->getRangeMap()->isSameAs(*Ppattern->getDomainMap()),
111 "Maps are incompatible");
112
113 // Construct auxiliary graph via Ppattern * Dc
114 RCP<const CrsGraph> auxGraph;
115 {
116 const auto one = Teuchos::ScalarTraits<Scalar>::one();
117 auto absP = MatrixFactory::Build(Ppattern);
118 absP->setAllToScalar(one);
119 absP->fillComplete();
120
121 auto absDc = MatrixFactory::BuildCopy(big_Dc_);
122 absDc->setAllToScalar(one);
123
124 auto P_Dc = MatrixMatrix::Multiply(*absP, false, *absDc, false, this->GetOStream(Statistics2), true, true);
125 auxGraph = P_Dc->getCrsGraph();
126 }
127 RHS_pattern_ = auxGraph;
128
129 GlobalOrdinal indexBase = Ppattern->getRowMap()->getIndexBase();
130 const size_t numUnknowns = Ppattern->getLocalNumEntries();
131 const size_t numRows = Ppattern->getLocalNumRows();
132 Xpetra::global_size_t global_numConstraints = auxGraph->getGlobalNumEntries();
133 Xpetra::global_size_t global_numUnknowns = Ppattern->getGlobalNumEntries();
134 const size_t numConstraints = auxGraph->getLocalNumEntries();
135 auto constraint_rowmap = MapFactory::Build(lib, global_numConstraints, numConstraints, indexBase, comm);
136 auto constraint_domainmap = MapFactory::Build(lib, global_numUnknowns, numUnknowns, indexBase, comm);
137
138 RCP<Matrix> ghostedDc;
139 if (!Ppattern->getImporter().is_null())
140 ghostedDc = MatrixFactory::Build(big_Dc_, *Ppattern->getImporter());
141 else
142 ghostedDc = big_Dc_;
143
144 RCP<Matrix> X;
145 {
146 auto lclPattern = Ppattern->getLocalGraphDevice();
147 auto lclD0 = ghostedDc->getLocalMatrixDevice();
148 auto lclAuxGraph = auxGraph->getLocalGraphDevice();
149
150 // Over-allocate by 1. Makes the logic a bit easier in what follows.
151 lno_view_t rowptr("constraint_rowptr", numConstraints + 2);
152
153 Kokkos::parallel_for(
154 "MueLu::SparseConstraint::sparse_constraint_num_entries_per_row",
155 range_type(0, numRows),
156 KOKKOS_LAMBDA(const size_t pattern_i) {
157 for (size_t pattern_jj = lclPattern.row_map(pattern_i); pattern_jj < lclPattern.row_map(pattern_i + 1); ++pattern_jj) {
158 auto pattern_j = lclPattern.entries(pattern_jj);
159 // entry (pattern_i, pattern_j) in Ppattern
160
161 for (size_t D0_jj = lclD0.graph.row_map(pattern_j); D0_jj < lclD0.graph.row_map(pattern_j + 1); ++D0_jj) {
162 auto D0_j = lclD0.graph.entries(D0_jj);
163 // entry (pattern_j, D0_j) in ghosted D0
164
165 // Find entry (pattern_i, D0_j) in tempGraph
166 size_t constraint_I;
167 for (constraint_I = lclAuxGraph.row_map(pattern_i); constraint_I < lclAuxGraph.row_map(pattern_i + 1); ++constraint_I) {
168 if (lclAuxGraph.entries(constraint_I) == D0_j)
169 break;
170 }
171#ifdef HAVE_MUELU_DEBUG
172 if (lclAuxGraph.entries(constraint_I) != D0_j)
173 ::Kokkos::abort("Did not find entry in row of tempGraph.");
174#endif
175 // Need an entry in row constraint_I.
176 // We offset by 2 since we do not want to compute the final rowptr just yet.
177 // That will happen during fill.
178 Kokkos::atomic_add(&rowptr(constraint_I + 2), 1);
179 }
180 }
181 });
182
183 // The usual prefix sum.
184 size_t nnz = 0;
185 Kokkos::parallel_scan(
186 "MueLu::SparseConstraint::sparse_constraint_prefix_sum",
187 range_type(1, numConstraints + 2),
188 KOKKOS_LAMBDA(const size_t constraint_i, size_t& partial_nnz, bool is_final) {
189 partial_nnz += rowptr(constraint_i);
190 if (is_final)
191 rowptr(constraint_i) = partial_nnz;
192 },
193 nnz);
194
195 // allocate indices and values
196 lno_nnz_view_t colind(Kokkos::ViewAllocateWithoutInitializing("constraint_indices"), nnz);
197 scalar_view_t values(Kokkos::ViewAllocateWithoutInitializing("constraint_values"), nnz);
198
199 // fill indices and values
200 Kokkos::parallel_for(
201 "MueLu::SparseConstraint::sparse_constraint_fill",
202 range_type(0, numRows),
203 KOKKOS_LAMBDA(const size_t pattern_i) {
204 for (size_t pattern_jj = lclPattern.row_map(pattern_i); pattern_jj < lclPattern.row_map(pattern_i + 1); ++pattern_jj) {
205 auto pattern_j = lclPattern.entries(pattern_jj);
206 // entry (pattern_i, pattern_j) in Ppattern
207
208 for (size_t D0_jj = lclD0.graph.row_map(pattern_j); D0_jj < lclD0.graph.row_map(pattern_j + 1); ++D0_jj) {
209 auto D0_j = lclD0.graph.entries(D0_jj);
210 auto D0_val = lclD0.values(D0_jj);
211 // entry (pattern_j, D0_j) in ghosted D0
212
213 // Find entry (pattern_i, D0_j) in tempGraph
214 size_t constraint_I;
215 for (constraint_I = lclAuxGraph.row_map(pattern_i); constraint_I < lclAuxGraph.row_map(pattern_i + 1); ++constraint_I) {
216 if (lclAuxGraph.entries(constraint_I) == D0_j)
217 break;
218 }
219#ifdef HAVE_MUELU_DEBUG
220 if (lclAuxGraph.entries(constraint_I) != D0_j)
221 ::Kokkos::abort("Did not find entry in row of tempGraph.");
222#endif
223 // Enter data into constraint matrix.
224 // (constraint_I, pattern_jj) -> D0_val
225 // After this the rowptr will be correct.
226 // This is why we had to offset the index by 2 earlier on.
227 auto constraint_jj = Kokkos::atomic_fetch_inc(&rowptr(constraint_I + 1));
228 colind(constraint_jj) = pattern_jj;
229 values(constraint_jj) = D0_val;
230 }
231 }
232 });
233
234 auto lclConstraintGraph = graph_t(colind, Kokkos::subview(rowptr, Kokkos::make_pair(size_t(0), numConstraints + 1)));
235 auto lclConstraint = matrix_t("constraint", numUnknowns, values, lclConstraintGraph);
236 X = MatrixFactory::Build(lclConstraint, constraint_rowmap, constraint_domainmap, constraint_domainmap, constraint_rowmap);
237 }
238 this->SetConstraintsMatrix(X);
239}
240
241template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
242typename Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node>::local_graph_type SparseConstraint<Scalar, LocalOrdinal, GlobalOrdinal, Node>::FindBlocks(RCP<const Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node>>& /*XXt*/) {
243 using execution_space = typename Node::execution_space;
244 using range_type = Kokkos::RangePolicy<LocalOrdinal, execution_space>;
245
246 auto lclGraph = RHS_pattern_->getLocalGraphDevice();
247
248 LocalOrdinal numEmptyRows;
249 Kokkos::parallel_reduce(
250 "MueLu::SparseConstraint::FindBlocks::CountEmptyRows", range_type(0, lclGraph.numRows()), KOKKOS_LAMBDA(const LocalOrdinal rowId, LocalOrdinal& emptyRows) {
251 if (lclGraph.row_map(rowId + 1) == lclGraph.row_map(rowId))
252 ++emptyRows;
253 },
254 numEmptyRows);
255
256 auto numConstraints = lclGraph.entries.extent(0);
257 using graph_type = typename CrsGraph::local_graph_type;
258 typename graph_type::row_map_type::non_const_type rowptr("blocks_rowptr", lclGraph.numRows() + 1 - numEmptyRows);
259 typename graph_type::entries_type::non_const_type indices("blocks_indices", numConstraints);
260
261 Kokkos::parallel_scan(
262 "MueLu::SparseConstraint::FindBlocks::GenerateBlockRowPtr", range_type(0, lclGraph.numRows()), KOKKOS_LAMBDA(const LocalOrdinal rowId, LocalOrdinal& rowIdNew, const bool is_final) {
263 if (lclGraph.row_map(rowId + 1) != lclGraph.row_map(rowId)) {
264 if (is_final)
265 rowptr(rowIdNew + 1) = lclGraph.row_map(rowId + 1);
266 ++rowIdNew;
267 }
268 });
269
270 Kokkos::parallel_for(
271 "MueLu::SparseConstraint::FindBlocks::FillBlockIndices", range_type(0, numConstraints), KOKKOS_LAMBDA(const LocalOrdinal constraintId) {
272 indices(constraintId) = constraintId;
273 });
274
275 return graph_type(indices, rowptr);
276}
277
278template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
279typename Teuchos::ScalarTraits<Scalar>::magnitudeType
281 const auto one = Teuchos::ScalarTraits<Scalar>::one();
282
283 // P*Dc
284 RCP<Matrix> temp;
285 temp = MatrixMatrix::Multiply(*P, false,
286 *big_Dc_, false,
287 temp,
288 this->GetOStream(Runtime0), true, true);
289 // D*P_nodal
290 RCP<Matrix> temp2;
291 temp2 = MatrixMatrix::Multiply(*D_, false,
292 *P_nodal_, false,
293 temp2,
294 this->GetOStream(Runtime0), true, true);
295
296 // D*P_nodal - P*Dc
297 RCP<Matrix> residual;
298 MatrixMatrix::TwoMatrixAdd(*temp2, false, one,
299 *temp, false, -one,
300 residual,
301 this->GetOStream(Runtime0));
302 residual->fillComplete();
303 return Teuchos::ScalarTraits<MagnitudeType>::squareroot(Teuchos::ScalarTraits<Scalar>::magnitude(Utilities::Frobenius(*residual, *residual)));
304}
305
306template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
308 MultiVector& vecC) const {
309 this->AssignMatrixEntriesToVector(A, RHS_pattern_, vecC);
310}
311
312} // namespace MueLu
313
314#endif // ifndef MUELU_SPARSECONSTRAINT_DEF_HPP
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Exception throws to report incompatible objects (like maps).
Timer to be used in non-factories.
void AssignMatrixEntriesToConstraintVector(const Matrix &A, MultiVector &vecC) const
CrsGraph::local_graph_type FindBlocks(RCP< const CrsGraph > &) override
MagnitudeType ResidualNorm(RCP< const Matrix > P) const override
static Scalar Frobenius(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B)
Frobenius inner product of two matrices.
Namespace for MueLu classes and methods.
@ Statistics2
Print even more statistics.
@ Runtime0
One-liner description of what is happening.