Debian Codegraph @

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