Debian Codegraph @

packages/belos/tpetra/test/TFQMR/test_tfqmr_diag.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 // This test generates diagonal matrices for TFQMR to solve.
11 //
12 // NOTE: No preconditioner is used in this case.
13 //
14
15 // Teuchos
16 #include <Teuchos_Time.hpp>
17 #include <Teuchos_StandardCatchMacros.hpp>
18
19 // Tpetra
20 #include <Tpetra_Core.hpp>
21 #include <Tpetra_Map.hpp>
22 #include <Tpetra_MultiVector.hpp>
23 #include <Tpetra_Vector.hpp>
24 #include <Tpetra_CrsMatrix.hpp>
25
26 // Belos
27 #include "BelosConfigDefs.hpp"
28 #include "BelosLinearProblem.hpp"
29 #include "BelosTpetraAdapter.hpp"
30 #include "BelosTpetraOperator.hpp"
31 #include "BelosTFQMRSolMgr.hpp"
32
33 using namespace Belos;
34
35 //************************************************************************************************
36
37 template<class MV>
38 class VectorOperator
39 {
40 public:
41
42 VectorOperator(int m_in, int n_in) : m(m_in), n(n_in) {};
43
44 virtual ~VectorOperator() {};
45
46 virtual void operator () (const MV &x, MV &y) = 0;
47
48 int size (int dim) const { return (dim == 1) ? m : n; };
49
50 protected:
51
52 int m, n; // an (m x n) operator
53
54 private:
55
56 // Not allowing copy construction.
57 VectorOperator( const VectorOperator& ): m(0), n(0) {};
58 VectorOperator* operator=( const VectorOperator& ) { return NULL; };
59
60 };
61
62 //************************************************************************************************
63
64 template<class ST, class MV>
65 class DiagonalOperator : public VectorOperator<MV>
66 {
67 public:
68
69 DiagonalOperator(int n_in, ST v_in) : VectorOperator<MV>(n_in, n_in), v(v_in) { };
70
71 ~DiagonalOperator() { };
72
73 void operator () (const MV &x, MV &y)
74 {
75 y.scale(v, x);
76 };
77
78 private:
79
80 ST v;
81 };
82
83 //************************************************************************************************
84
85 template<class ST, class MV>
86 class DiagonalOperator2 : public VectorOperator<MV>
87 {
88 public:
89
90 DiagonalOperator2<ST,MV>(int n_in, int min_gid_in, ST v_in)
91 : VectorOperator<MV>(n_in, n_in), min_gid(min_gid_in), v(v_in) {}
92
93 ~DiagonalOperator2() { };
94
95 void operator () (const MV &x, MV &y)
96 {
97 auto yLocalData = y.getLocalViewHost(Tpetra::Access::ReadWrite);
98 auto xLocalData = x.getLocalViewHost(Tpetra::Access::ReadOnly);
99
100 for (size_t j = 0; j < x.getNumVectors(); ++j) {
101 for (size_t i = 0; i < x.getLocalLength(); ++i) {
102 yLocalData(i, j) = (min_gid + i + 1) * v * xLocalData(i, j); // NOTE: square operator!
103 }
104 }
105 }
106
107 private:
108
109 int min_gid;
110 ST v;
111 };
112
113 //************************************************************************************************
114
115 template<class MV>
116 class ComposedOperator : public VectorOperator<MV>
117 {
118 public:
119
120 ComposedOperator(int n,
121 const Teuchos::RCP<VectorOperator<MV>>& pA_in,
122 const Teuchos::RCP<VectorOperator<MV>>& pB_in);
123
124 virtual ~ComposedOperator() {};
125
126 virtual void operator () (const MV &x, MV &y);
127
128 private:
129
130 Teuchos::RCP<VectorOperator<MV>> pA;
131 Teuchos::RCP<VectorOperator<MV>> pB;
132 };
133
134 template<class MV>
135 ComposedOperator<MV>::ComposedOperator(int n_in,
136 const Teuchos::RCP<VectorOperator<MV>>& pA_in,
137 const Teuchos::RCP<VectorOperator<MV>>& pB_in)
138 : VectorOperator<MV>(n_in, n_in), pA(pA_in), pB(pB_in)
139 {
140 }
141
142 template<class MV>
143 void ComposedOperator<MV>::operator () (const MV &x, MV &y)
144 {
145 MV ytemp(y.getMap(), y.getNumVectors(), false);
146 (*pB)( x, ytemp );
147 (*pA)( ytemp, y );
148 }
149
150 //************************************************************************************************
151
152 template<class OP, class ST, class MP, class MV>
153 class TrilinosInterface : public OP
154 {
155 public:
156
157 TrilinosInterface(const Teuchos::RCP<VectorOperator<MV>> pA_in,
158 const Teuchos::RCP<const Teuchos::Comm<int>> pComm_in,
159 const Teuchos::RCP<const MP> pMap_in)
160 : pA (pA_in),
161 pComm (pComm_in),
162 pMap (pMap_in),
163 use_transpose (false)
164 {}
165
166 void apply (const MV &X,
167 MV &Y,
168 Teuchos::ETransp mode = Teuchos::NO_TRANS,
169 ST alpha = Teuchos::ScalarTraits<ST>::one(),
170 ST beta = Teuchos::ScalarTraits<ST>::zero()) const override;
171
172 virtual ~TrilinosInterface() {};
173
174 bool hasTransposeApply() const {return(use_transpose);};
175
176 Teuchos::RCP<const MP> getDomainMap() const override {return pMap; }
177
178 Teuchos::RCP<const MP> getRangeMap() const override {return pMap; }
179
180 private:
181
182 Teuchos::RCP<VectorOperator<MV>> pA;
183 Teuchos::RCP<const Teuchos::Comm<int>> pComm;
184 Teuchos::RCP<const MP> pMap;
185
186 bool use_transpose;
187 };
188
189 template<class OP, class ST, class MP, class MV>
190 void TrilinosInterface<OP, ST, MP, MV>::apply (const MV &X, MV &Y,
191 Teuchos::ETransp mode, ST alpha, ST beta) const
192 {
193 (*pA)(X,Y);
194 }
195
196 //************************************************************************************************
197
198 template<class OP, class ST, class MP, class MV>
199 class IterativeInverseOperator : public VectorOperator<MV>
200 {
201 public:
202
203 IterativeInverseOperator(int n_in, int blocksize,
204 const Teuchos::RCP<VectorOperator<MV>>& pA_in,
205 std::string opString="Iterative Solver", bool print_in=false);
206
207 virtual ~IterativeInverseOperator() {}
208
209 virtual void operator () (const MV &b, MV &x);
210
211 private:
212
213 Teuchos::RCP<VectorOperator<MV>> pA; // operator which will be inverted
214 // supplies a matrix std::vector multiply
215 const bool print;
216
217 Teuchos::Time timer;
218 Teuchos::RCP<const Teuchos::Comm<int>> pComm;
219 Teuchos::RCP<MP> pMap;
220
221 Teuchos::RCP<OP> pPE;
222 Teuchos::RCP<Teuchos::ParameterList> pList;
223 Teuchos::RCP<LinearProblem<ST,MV,OP> > pProb;
224 Teuchos::RCP<TFQMRSolMgr<ST,MV,OP> > pBelos;
225 };
226
227 template<class OP, class ST, class MP, class MV>
228 IterativeInverseOperator<OP, ST, MP, MV>::IterativeInverseOperator(int n_in, int blocksize,
229 const Teuchos::RCP<VectorOperator<MV>>& pA_in,
230 std::string opString, bool print_in)
231 : VectorOperator<MV>(n_in, n_in), // square operator
232 pA(pA_in),
233 print(print_in),
234 timer(opString)
235 {
236 int nGlobal;
237
238 pComm = Tpetra::getDefaultComm();
239 Teuchos::reduceAll<int, int>(*pComm, Teuchos::REDUCE_SUM, 1, &n_in, &nGlobal);
240
241 pMap = Teuchos::rcp( new MP(nGlobal, n_in, 0, pComm) );
242 pPE = Teuchos::rcp( new TrilinosInterface<OP, ST, MP, MV>(pA, pComm, pMap ) );
243
244 pProb = Teuchos::rcp( new LinearProblem<ST,MV,OP>() );
245 pProb->setOperator( pPE );
246
247 int max_iter = 100;
248 ST tol = sqrt(std::numeric_limits<ST>::epsilon());
249 int verbosity = Belos::Errors + Belos::Warnings;
250 if (print)
251 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
252
253 pList = Teuchos::rcp( new Teuchos::ParameterList );
254 pList->set( "Maximum Iterations", max_iter );
255 pList->set( "Convergence Tolerance", tol );
256 pList->set( "Verbosity", verbosity );
257
258 pBelos = Teuchos::rcp( new TFQMRSolMgr<ST,MV,OP>(pProb, pList) );
259 }
260
261 template<class OP, class ST, class MP, class MV>
262 void IterativeInverseOperator<OP, ST, MP, MV>::operator () (const MV &b, MV &x)
263 {
264 int pid = pComm->getRank();
265
266 // Initialize the solution to zero
267 x.putScalar( 0.0 );
268
269 // Reset the solver, problem, and status test for next solve (HKT)
270 pProb->setProblem( Teuchos::rcp(&x, false), Teuchos::rcp(&b, false) );
271
272 timer.start();
273 Belos::ReturnType ret = pBelos->solve();
274 timer.stop();
275
276 if (pid == 0 && print) {
277 if (ret == Belos::Converged)
278 {
279 std::cout << std::endl << "pid[" << pid << "] TFQMR converged" << std::endl;
280 std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl;
281
282 }
283 else
284 std::cout << std::endl << "pid[" << pid << "] TFQMR did not converge" << std::endl;
285 }
286 }
287
288 //************************************************************************************************
289 //************************************************************************************************
290
291 template<class ScalarType>
292 int run(int argc, char *argv[])
293 {
294 // Get default Tpetra template types
295 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
296 using LO = typename Tpetra::MultiVector<>::local_ordinal_type;
297 using GO = typename Tpetra::MultiVector<>::global_ordinal_type;
298 using NT = typename Tpetra::MultiVector<>::node_type;
299
300 // Init Tpetra types
301 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
302 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
303 using MP = typename Tpetra::Map<LO,GO,NT>;
304
305 using Teuchos::RCP;
306 using Teuchos::rcp;
307
308 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
309 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
310 int pid = rank(*comm);
311
312 bool verbose = false;
313 bool success = true;
314
315 ST tol = sqrt(std::numeric_limits<ST>::epsilon());
316
317 try {
318 int n(10);
319 int numRHS=1;
320
321 RCP<MP> map = RCP(new MP(n, 0, comm));
322 MV X(map, numRHS), Y(map, numRHS);
323
324 X.putScalar(1.0);
325
326 // Inner computes inv(D2)*y
327 RCP<DiagonalOperator2<ST,MV>> D2 = rcp(new DiagonalOperator2<ST,MV>(n, map->getMinGlobalIndex(), 1.0));
328 IterativeInverseOperator<OP, ST, MP, MV> A2(n, 1, D2, "Belos (inv(D2))", true);
329
330 // should return x=(1, 1/2, 1/3, ..., 1/10)
331 A2(X,Y);
332
333 if (pid==0) {
334 std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl;
335 }
336 Y.print(std::cout);
337
338 // Inner computes inv(D)*x
339 RCP<DiagonalOperator<ST,MV>> D = rcp(new DiagonalOperator<ST,MV>(n, 4.0));
340 RCP<IterativeInverseOperator<OP, ST, MP, MV>> Inner =
341 rcp(new IterativeInverseOperator<OP, ST, MP, MV>(n, 1, D, "Belos (inv(D))", false));
342
343 // Composed_Operator computed inv(D)*B*x
344 RCP<DiagonalOperator<ST,MV>> B = rcp(new DiagonalOperator<ST,MV>(n, 4.0));
345 RCP<ComposedOperator<MV>> C = rcp(new ComposedOperator<MV>(n, Inner, B));
346
347 // Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x
348 RCP<IterativeInverseOperator<OP, ST, MP, MV>> Outer =
349 rcp(new IterativeInverseOperator<OP, ST, MP, MV>(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true));
350
351 // should return x=1/4
352 (*Inner)(X,Y);
353
354 if (pid==0) {
355 std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl;
356 }
357 Y.print(std::cout);
358
359 // should return x=1
360 (*Outer)(X,Y);
361
362 if (pid==0) {
363 std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl;
364 }
365 Y.print(std::cout);
366
367 // Compute the norm of Y - 1.0
368 std::vector<ST> norm_Y(Y.getNumVectors());
369 Y.update(-1.0, X, 1.0);
370 Y.norm2(norm_Y);
371
372 if (pid==0)
373 std::cout << "Two-norm of std::vector (Y-1.0) : "<< norm_Y[0] << std::endl;
374
375 success = (norm_Y[0] < tol && !Teuchos::ScalarTraits<ST>::isnaninf( norm_Y[0] ) );
376
377 if (success) {
378 if (pid==0)
379 std::cout << "End Result: TEST PASSED" << std::endl;
380 } else {
381 if (pid==0)
382 std::cout << "End Result: TEST FAILED" << std::endl;
383 }
384
385 }
386 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
387
388 return success ? EXIT_SUCCESS : EXIT_FAILURE;
389 }
390
391 int main(int argc, char *argv[]) {
392 return run<double>(argc, argv);
393
394 // wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON
395 // run<float>(argc, argv);
396 }