Debian Codegraph @

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