Debian Codegraph @

packages/muelu/research/caglusa/Tpetra_HierarchicalOperator_decl.hpp

raw
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 TPETRA_HIERARCHICALOPERATOR_DECL_HPP
11 #define TPETRA_HIERARCHICALOPERATOR_DECL_HPP
12
13 #include <Teuchos_RCP.hpp>
14 #include <Tpetra_Map.hpp>
15 #include <Tpetra_MultiVector.hpp>
16 #include <Tpetra_CrsMatrix.hpp>
17 #include <Tpetra_RowMatrix.hpp>
18 #include <Tpetra_OperatorWithDiagonal.hpp>
19 #include <Tpetra_BlockedMap_decl.hpp>
20 #include <Tpetra_BlockedMatrix_decl.hpp>
21 #include "Teuchos_VerbosityLevel.hpp"
22
23 namespace Tpetra {
24
25 /*
26
27 A container class for hierarchical matrices of different types.
28 In particular, both H- and H2-matrices are supported.
29
30 The unknowns of the kernel approximations are collected in the clusterMap.
31 For H-matrices, this is just a concatenation.
32 For H2-matrices, the map also contains the intermediate clusters that might be needed in upward/downward pass.
33
34
35 H = nearField
36 + basisMatrix *
37 ((I+transferMatrices[K-1]^T) * ... * (I+transferMatrices[0]^T)) *
38 kernelApproximations *
39 ((I+transferMatrices[0]) * ... * (I+transferMatrices[K-1])) *
40 basisMatrix^T
41
42 nearField and basisMatrix are standard (point) CRS matrices.
43 kernelApproximations and transferMatrices[.] are blocked CRS matrices
44
45 I is the identity matrix and is not explicitely saved.
46
47 Maps:
48 map (standard): domain and range of H;
49 domain, range, row of nearField
50 clusterMap (blocked map): domain, range, row, column of transferMatrices;
51 domain, range, row of kernelApproximations
52 ghosted_clusterMap (blocked map): column of kernelApproximations
53
54
55 For H-matrices:
56 K = 0, i.e. there are no transfer matrices
57
58 For H2-matrices:
59 upward and downward pass in the cluster hierarchy are encoded in transfer matrices
60 */
61
62 template <class Scalar = Tpetra::Operator<>::scalar_type,
63 class LocalOrdinal = typename Tpetra::Operator<Scalar>::local_ordinal_type,
64 class GlobalOrdinal = typename Tpetra::Operator<Scalar, LocalOrdinal>::global_ordinal_type,
65 class Node = typename Tpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
66 class HierarchicalOperator : public Tpetra::OperatorWithDiagonal<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
67 public:
68 using matrix_type = Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
69 using mv_type = Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
70 using map_type = Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node>;
71
72 //! The RowMatrix representing the base class of CrsMatrix
73 using row_matrix_type = RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
74
75 using impl_scalar_type = typename row_matrix_type::impl_scalar_type;
76 using mag_type = typename Kokkos::ArithTraits<impl_scalar_type>::mag_type;
77
78 using local_inds_device_view_type =
79 typename row_matrix_type::local_inds_device_view_type;
80 using local_inds_host_view_type =
81 typename row_matrix_type::local_inds_host_view_type;
82 using nonconst_local_inds_host_view_type =
83 typename row_matrix_type::nonconst_local_inds_host_view_type;
84
85 using global_inds_device_view_type =
86 typename row_matrix_type::global_inds_device_view_type;
87 using global_inds_host_view_type =
88 typename row_matrix_type::global_inds_host_view_type;
89 using nonconst_global_inds_host_view_type =
90 typename row_matrix_type::nonconst_global_inds_host_view_type;
91
92 using values_device_view_type =
93 typename row_matrix_type::values_device_view_type;
94 using values_host_view_type =
95 typename row_matrix_type::values_host_view_type;
96 using nonconst_values_host_view_type =
97 typename row_matrix_type::nonconst_values_host_view_type;
98
99 using blocked_matrix_type = BlockedMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
100 using blocked_map_type = BlockedMap<LocalOrdinal, GlobalOrdinal, Node>;
101
102 //! @name Constructor/Destructor
103 //@{
104
105 //! Constructor
106 HierarchicalOperator(const Teuchos::RCP<matrix_type>& nearField,
107 const Teuchos::RCP<blocked_matrix_type>& kernelApproximations,
108 const Teuchos::RCP<matrix_type>& basisMatrix,
109 std::vector<Teuchos::RCP<blocked_matrix_type> >& transferMatrices,
110 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
111
112 //! Returns the Tpetra::Map object associated with the domain of this operator.
113 Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
114 return nearField_->getDomainMap();
115 }
116
117 //! Returns the Tpetra::Map object associated with the range of this operator.
118 Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
119 return nearField_->getRangeMap();
120 }
121
122 //! Returns in Y the result of a Tpetra::Operator applied to a Tpetra::MultiVector X.
123 /*!
124 \param[in] X - Tpetra::MultiVector of dimension NumVectors to multiply with matrix.
125 \param[out] Y -Tpetra::MultiVector of dimension NumVectors containing result.
126 */
127 void apply(const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X,
128 Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y,
129 Teuchos::ETransp mode = Teuchos::NO_TRANS,
130 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
131 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
132
133 Teuchos::RCP<HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > restrict(const Teuchos::RCP<matrix_type>& P);
134
135 Teuchos::RCP<matrix_type> toMatrix();
136
137 double getCompression() {
138 size_t nnz = (nearField_->getGlobalNumEntries() +
139 kernelApproximations_->pointA_->getGlobalNumEntries() +
140 basisMatrix_->getGlobalNumEntries());
141 for (size_t i = 0; i < transferMatrices_.size(); i++)
142 nnz += transferMatrices_[i]->pointA_->getGlobalNumEntries();
143 return Teuchos::as<double>(nnz) / (getDomainMap()->getGlobalNumElements() * getDomainMap()->getGlobalNumElements());
144 }
145
146 size_t numClusterPairsOnLevelGlobal(size_t level) const {
147 using vec_type = typename Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
148 using Teuchos::RCP;
149 const Scalar ONE = Teuchos::ScalarTraits<Scalar>::one();
150 const Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
151 if (level == 0) {
152 // RCP<vec_type> tempV = Teuchos::rcp(new vec_type(kernelApproximations_->blockMap_->blockMap_, false));
153 // RCP<vec_type> tempV2 = Teuchos::rcp(new vec_type(kernelApproximations_->blockMap_->blockMap_, false));
154
155 // tempV->putScalar(ONE);
156 // transferMatrices_[0]->blockA_->apply(*tempV, *tempV2, Teuchos::TRANS);
157 // tempV->putScalar(ZERO);
158 // kernelApproximations_->blockA_->apply(*tempV2, *tempV);
159 // return Teuchos::as<size_t>(tempV->dot(*tempV2));
160 return 0;
161 } else if (level <= transferMatrices_.size()) {
162 RCP<vec_type> tempV = Teuchos::rcp(new vec_type(kernelApproximations_->blockMap_->blockMap_, false));
163 RCP<vec_type> tempV2 = Teuchos::rcp(new vec_type(kernelApproximations_->blockMap_->blockMap_, false));
164
165 tempV->putScalar(ONE);
166 transferMatrices_[level - 1]->blockA_->apply(*tempV, *tempV2, Teuchos::TRANS);
167 tempV->putScalar(ZERO);
168 kernelApproximations_->blockA_->apply(*tempV2, *tempV);
169 return Teuchos::as<size_t>(tempV->dot(*tempV2));
170 } else {
171 TEUCHOS_ASSERT(false);
172 }
173 }
174
175 size_t numClustersOnLevelGlobal(size_t level) const {
176 if (level > 0)
177 return Teuchos::as<size_t>(transferMatrices_[level - 1]->blockA_->getGlobalNumEntries());
178 else {
179 const double treeCoarseningFactor = params_->get<double>("treeCoarseningFactor");
180 return Teuchos::as<size_t>(transferMatrices_[0]->blockA_->getGlobalNumEntries() / treeCoarseningFactor);
181 }
182 }
183
184 size_t numClustersOnLevelLocal(size_t level) const {
185 if (level > 0)
186 return Teuchos::as<size_t>(transferMatrices_[level - 1]->blockA_->getLocalNumEntries());
187 else {
188 const double treeCoarseningFactor = params_->get<double>("treeCoarseningFactor");
189 return Teuchos::as<size_t>(transferMatrices_[0]->blockA_->getLocalNumEntries() / treeCoarseningFactor);
190 }
191 }
192
193 size_t numLevels() const {
194 return transferMatrices_.size() + 1;
195 }
196
197 void setDebugOutput(const bool debugOutput) {
198 debugOutput_ = debugOutput;
199 }
200
201 Teuchos::RCP<matrix_type> nearFieldMatrix() {
202 return nearField_;
203 }
204
205 // Fake RowMatrix interface
206 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getRowMap() const {
207 return nearField_->getRowMap();
208 }
209
210 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getColMap() const {
211 return nearField_->getColMap();
212 }
213
214 Teuchos::RCP<const Teuchos::Comm<int> > getComm() const {
215 return nearField_->getDomainMap()->getComm();
216 }
217
218 Teuchos::RCP<const RowGraph<LocalOrdinal, GlobalOrdinal, Node> > getGraph() const {
219 return nearField_->getCrsGraph();
220 }
221
222 global_size_t getGlobalNumRows() const {
223 return nearField_->getGlobalNumRows();
224 }
225
226 global_size_t getGlobalNumCols() const {
227 return nearField_->getGlobalNumCols();
228 }
229
230 size_t getLocalNumRows() const {
231 return nearField_->getLocalNumRows();
232 }
233
234 size_t getLocalNumCols() const {
235 return nearField_->getLocalNumCols();
236 }
237
238 GlobalOrdinal getIndexBase() const {
239 return nearField_->getIndexBase();
240 }
241
242 global_size_t getGlobalNumEntries() const {
243 return nearField_->getGlobalNumEntries();
244 }
245
246 size_t getLocalNumEntries() const {
247 return nearField_->getLocalNumEntries();
248 }
249
250 void getLocalDiagCopy(Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& diag) const {
251 nearField_->getLocalDiagCopy(diag);
252 }
253
254 void describe(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const {
255 describe(out, verbLevel, true);
256 }
257
258 void describe(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel, const bool printHeader) const {
259 using std::endl;
260 using std::setw;
261 const size_t numRows = nearField_->getRowMap()->getGlobalNumElements();
262 const size_t nnzNearField = nearField_->getGlobalNumEntries();
263 const double nnzNearPerRow = Teuchos::as<double>(nnzNearField) / numRows;
264 const size_t nnzKernelApprox = kernelApproximations_->pointA_->getGlobalNumEntries();
265 const size_t numClusterPairs = kernelApproximations_->blockA_->getGlobalNumEntries();
266 const size_t nnzBasis = basisMatrix_->getGlobalNumEntries();
267 size_t numTransfers = transferMatrices_.size();
268 size_t nnzTransfer = 0;
269 for (size_t i = 0; i < transferMatrices_.size(); i++)
270 nnzTransfer += transferMatrices_[i]->pointA_->getGlobalNumEntries();
271 const size_t nnzTotal = nnzNearField + nnzKernelApprox + nnzBasis + nnzTransfer;
272 const double nnzTotalPerRow = Teuchos::as<double>(nnzTotal) / numRows;
273 std::ostringstream oss;
274 oss << std::left;
275 if (printHeader)
276 oss << setw(9) << "rows" << setw(12)
277 << "nnz(near)" << setw(14)
278 << "nnz(near)/row" << setw(12)
279 << "nnz(basis)" << setw(15)
280 << "#cluster pairs" << setw(12)
281 << "nnz(kernel)" << setw(14)
282 << "#transfers" << setw(14)
283 << "nnz(transfer)" << setw(12)
284 << "nnz(total)" << setw(14)
285 << "nnz(total)/row" << endl;
286 oss << setw(9) << numRows << setw(12)
287 << nnzNearField << setw(14)
288 << nnzNearPerRow << setw(12)
289 << nnzBasis << setw(15)
290 << numClusterPairs << setw(12)
291 << nnzKernelApprox << setw(14)
292 << numTransfers << setw(14)
293 << nnzTransfer << setw(12)
294 << nnzTotal << setw(14)
295 << nnzTotalPerRow << endl;
296
297 if (Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_EXTREME)) {
298 oss << setw(9) << "level" << setw(9) << "numClusters" << setw(9) << "numClusterPairs" << endl;
299 for (size_t lvl = 0; lvl < numLevels(); ++lvl) {
300 size_t numClustersLvl_gbl = numClustersOnLevelGlobal(lvl);
301 size_t numClusterPairsLvl = numClusterPairsOnLevelGlobal(lvl);
302 // size_t numClustersLvl_lcl = numClustersOnLevelLocal(lvl);
303
304 oss << setw(9) << lvl << setw(9) << numClustersLvl_gbl << setw(9) << numClusterPairsLvl << endl;
305 }
306 }
307
308 out << oss.str();
309 }
310
311 bool hasFarField() const {
312 return kernelApproximations_->blockA_->getGlobalNumEntries() > 0;
313 }
314
315 bool hasTransferMatrices() const {
316 return !transferMatrices_.empty();
317 }
318
319 bool denserThanDenseMatrix() const {
320 const size_t numRows = nearField_->getRowMap()->getGlobalNumElements();
321 const size_t nnzNearField = nearField_->getGlobalNumEntries();
322 // const double nnzNearPerRow = Teuchos::as<double>(nnzNearField)/numRows;
323 const size_t nnzKernelApprox = kernelApproximations_->pointA_->getGlobalNumEntries();
324 // const size_t numClusterPairs = kernelApproximations_->blockA_->getGlobalNumEntries();
325 const size_t nnzBasis = basisMatrix_->getGlobalNumEntries();
326 size_t nnzTransfer = 0;
327 for (size_t i = 0; i < transferMatrices_.size(); i++)
328 nnzTransfer += transferMatrices_[i]->pointA_->getGlobalNumEntries();
329 const size_t nnzTotal = nnzNearField + nnzKernelApprox + nnzBasis + nnzTransfer;
330 const double nnzTotalPerRow = Teuchos::as<double>(nnzTotal) / numRows;
331
332 return (nnzTotalPerRow >= numRows);
333 }
334
335 private:
336 void allocateMemory(size_t numVectors) const;
337
338 void applyWithTransposes(const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X,
339 Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y,
340 Teuchos::ETransp mode = Teuchos::NO_TRANS,
341 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
342 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
343
344 void applyWithoutTransposes(const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X,
345 Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y,
346 Teuchos::ETransp mode = Teuchos::NO_TRANS,
347 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
348 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
349
350 bool canApplyWithoutTransposes_;
351 std::string coarseningCriterion_;
352 bool debugOutput_;
353
354 Teuchos::RCP<matrix_type> nearField_;
355 Teuchos::RCP<blocked_matrix_type> kernelApproximations_;
356 Teuchos::RCP<matrix_type> basisMatrix_;
357 Teuchos::RCP<matrix_type> basisMatrixT_;
358 std::vector<Teuchos::RCP<blocked_matrix_type> > transferMatrices_;
359 std::vector<Teuchos::RCP<blocked_matrix_type> > transferMatricesT_;
360 Teuchos::RCP<const map_type> clusterCoeffMap_;
361 mutable Teuchos::RCP<mv_type> coefficients_, coefficients2_;
362 mutable Teuchos::RCP<mv_type> X_colmap_, coefficients_colmap_;
363
364 Teuchos::RCP<Teuchos::ParameterList> params_;
365 };
366 } // namespace Tpetra
367
368 #endif // TPETRA_HIERARCHICALOPERATOR_DECL_HPP