Tpetra Matrix/Vector Services  Version of the Day
MultiPrecCG.hpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 */
43 
44 #ifndef MULTIPRECCG_HPP_
45 #define MULTIPRECCG_HPP_
46 
47 #include <Teuchos_TimeMonitor.hpp>
51 #include <Teuchos_FancyOStream.hpp>
52 
53 #include <Tpetra_CrsMatrix.hpp>
54 #include <Tpetra_Vector.hpp>
55 #include <Tpetra_RTI.hpp>
56 #include <Tpetra_MatrixIO.hpp>
57 
58 #include <iostream>
59 #include <functional>
60 
61 #ifdef HAVE_TPETRA_QD
62 # include <qd/qd_real.h>
63 #endif
64 
65 namespace Tpetra {
66  namespace RTI {
67  // specialization for pair
68  template <class T1, class T2>
69  class ZeroOp<std::pair<T1,T2>> {
70  public:
71  static inline std::pair<T1,T2> identity() {
72  return std::make_pair( Teuchos::ScalarTraits<T1>::zero(),
74  }
75  };
76  }
77 }
78 
79 namespace TpetraExamples {
80 
81  using Teuchos::RCP;
83  using Teuchos::Time;
84  using Teuchos::null;
85  using std::binary_function;
86  using std::pair;
87  using std::make_pair;
88  using std::plus;
89  using std::multiplies;
90 
91  struct trivial_fpu_fix {
92  void fix() {}
93  void unfix() {}
94  };
95 #ifdef HAVE_TPETRA_QD
96  struct nontrivial_fpu_fix {
97  unsigned int old_cw;
98  void fix() {fpu_fix_start(&old_cw);}
99  void unfix() {fpu_fix_end(&old_cw);}
100  };
101 #endif
102  // implementations
103  template <class T> struct fpu_fix : trivial_fpu_fix {};
104 #ifdef HAVE_TPETRA_QD
105  template <> struct fpu_fix<qd_real> : nontrivial_fpu_fix {};
106  template <> struct fpu_fix<dd_real> : nontrivial_fpu_fix {};
107 #endif
108 
110  template <class Tout, class Tin, class LO, class GO, class Node>
111  struct convertHelp {
112  static RCP<const Tpetra::CrsMatrix<Tout,LO,GO,Node>> doit(const RCP<const Tpetra::CrsMatrix<Tin,LO,GO,Node>> &A)
113  {
114  return A->template convert<Tout>();
115  }
116  };
117 
119  template <class T, class LO, class GO, class Node>
120  struct convertHelp<T,T,LO,GO,Node> {
121  static RCP<const Tpetra::CrsMatrix<T,LO,GO,Node>> doit(const RCP<const Tpetra::CrsMatrix<T,LO,GO,Node>> &A)
122  {
123  return A;
124  }
125  };
126 
127 
131  template <class T1, class T2, class Op>
132  class pair_op : public binary_function<pair<T1,T2>,pair<T1,T2>,pair<T1,T2>> {
133  private:
134  Op op_;
135  public:
136  pair_op(Op op) : op_(op) {}
137  inline pair<T1,T2> operator()(const pair<T1,T2>& a, const pair<T1,T2>& b) const {
138  return make_pair(op_(a.first,b.first),op_(a.second,b.second));
139  }
140  };
141 
143  template <class T1, class T2, class Op>
144  pair_op<T1,T2,Op> make_pair_op(Op op) { return pair_op<T1,T2,Op>(op); }
145 
147  template <class S, class LO, class GO, class Node>
148  class RFPCGInit
149  {
150  private:
154 
155  public:
156 
157  RFPCGInit(RCP<Tpetra::CrsMatrix<S,LO,GO,Node>> Atop) : A(Atop) {}
158 
159  template <class T>
160  RCP<ParameterList> initDB(ParameterList &params)
161  {
162  fpu_fix<T> ff;
163  ff.fix();
164  typedef Tpetra::Vector<T,LO,GO,Node> Vector;
167  RCP<const Map> map = A->getDomainMap();
168  RCP<ParameterList> db = Teuchos::parameterList();
170  //
171  db->set<RCP<const Op>>("A", AT );
172  db->set("numIters", params.get<int>("numIters",A->getGlobalNumRows()) );
173  db->set("tolerance",params.get<double>("tolerance",1e-7));
174  db->set("verbose", params.get<int>("verbose",0) );
175  db->set("bx", Tpetra::createVector<T>(map) );
176  db->set("r", Tpetra::createVector<T>(map) );
177  db->set("z", Tpetra::createVector<T>(map) );
178  db->set("p", Tpetra::createVector<T>(map) );
179  db->set("Ap", Tpetra::createVector<T>(map) );
180  db->set("rold", Tpetra::createVector<T>(map) );
181  if (params.get<bool>("Extract Diagonal",false)) {
182  RCP<Vector> diag = Tpetra::createVector<T>(map);
183  AT->getLocalDiagCopy(*diag);
184  db->set("diag", diag);
185  }
186  ff.unfix();
187  return db;
188  }
189  };
190 
191  /******************************
192  * Somewhat flexible CG
193  * Golub and Ye, 1999
194  *
195  * r = b
196  * z = M*r
197  * p = z
198  * do
199  * alpha = r'*z / p'*A*p
200  * x = x + alpha*p
201  * r = r - alpha*A*p
202  * if outermost, check r for convergence
203  * z = M*r
204  * beta = z'*(r_new - r_old) / z'*r
205  * p = z + beta*p
206  * enddo
207  ******************************/
208 
210  template <class TS, class LO, class GO, class Node>
212  {
213  using Teuchos::as;
214  using Tpetra::RTI::ZeroOp;
215  typedef typename TS::type T;
216  typedef typename TS::next::type T2;
217  typedef Tpetra::Vector<T ,LO,GO,Node> VectorT1;
218  typedef Tpetra::Vector<T2,LO,GO,Node> VectorT2;
219  typedef Tpetra::Operator<T,LO,GO,Node> OpT1;
220  typedef Teuchos::ScalarTraits<T> ST;
221  // get objects from level database
222  const int numIters = db.get<int>("numIters");
223  auto x = db.get<RCP<VectorT1>>("bx");
224  auto r = db.get<RCP<VectorT1>>("r");
225  auto z = db.get<RCP<VectorT1>>("z");
226  auto p = db.get<RCP<VectorT1>>("p");
227  auto Ap = db.get<RCP<VectorT1>>("Ap");
228  auto rold = db.get<RCP<VectorT1>>("rold");
229  auto A = db.get<RCP<const OpT1>>("A");
230  RCP<const VectorT1> diag;
231  if (TS::bottom) {
232  diag = db.get<RCP<VectorT1>>("diag");
233  }
234  const T tolerance = db.get<double>("tolerance", 0.0);
235  const int verbose = db.get<int>("verbose",0);
236  static RCP<Time> timer, Atimer;
237  if (timer == null) {
239  "recursiveFPCG<"+Teuchos::TypeNameTraits<T>::name()+">"
240  );
241  }
242  if (Atimer == null) {
245  );
246  }
247 
248  fpu_fix<T> ff;
249  ff.fix();
250 
251  Teuchos::OSTab tab(out);
252 
253  if (verbose) {
254  *out << "Beginning recursiveFPCG<" << Teuchos::TypeNameTraits<T>::name() << ">" << std::endl;
255  }
256 
257  timer->start();
258  const T r2 = TPETRA_BINARY_PRETRANSFORM_REDUCE(
259  r, x, // fused:
260  x, // : r = x
261  r*r, ZeroOp<T>, plus<T>() ); // : sum r'*r
262  // b comes in, x goes out. now we're done with b, so zero the solution.
263  TPETRA_UNARY_TRANSFORM( x, ST::zero() ); // x = 0
264  if (TS::bottom) {
265  TPETRA_TERTIARY_TRANSFORM( z, diag, r, r/diag ); // z = D\r
266  }
267  else {
268  ff.unfix();
269  timer->stop();
270  ParameterList &db_T2 = db.sublist("child");
271  auto bx_T2 = db_T2.get< RCP<VectorT2>>("bx");
272  TPETRA_BINARY_TRANSFORM( bx_T2, r, as<T2>(r) ); // b_T2 = (T2)r
273  recursiveFPCG<typename TS::next,LO,GO,Node>(out,db_T2); // x_T2 = A_T2 \ b_T2
274  TPETRA_BINARY_TRANSFORM( z, bx_T2, as<T>(bx_T2) ); // z = (T)bx_T2
275  timer->start();
276  ff.fix();
277  }
278  T zr = TPETRA_TERTIARY_PRETRANSFORM_REDUCE(
279  p, z, r, // fused:
280  z, // : p = z
281  z*r, ZeroOp<T>, plus<T>() ); // : z*r
283  int k;
284  for (k=0; k<numIters; ++k)
285  {
286  Atimer->start();
287  A->apply(*p,*Ap); // Ap = A*p
288  Atimer->stop();
289  T pAp = TPETRA_REDUCE2( p, Ap,
290  p*Ap, ZeroOp<T>, plus<T>() ); // p'*Ap
291  const T alpha = zr / pAp;
292  TPETRA_BINARY_TRANSFORM( x, p, x + alpha*p ); // x = x + alpha*p
293  TPETRA_BINARY_TRANSFORM( rold, r, r ); // rold = r
294  T rr = TPETRA_BINARY_PRETRANSFORM_REDUCE(
295  r, Ap, // fused:
296  r - alpha*Ap, // : r - alpha*Ap
297  r*r, ZeroOp<T>, plus<T>() ); // : sum r'*r
298  if (verbose > 1) *out << "|res|/|res_0|: " << ST::squareroot(rr/r2)
299  << std::endl;
300  if (rr/r2 < tolerance*tolerance) {
301  if (verbose) {
302  *out << "Convergence detected!" << std::endl;
303  }
304  break;
305  }
306  if (TS::bottom) {
307  TPETRA_TERTIARY_TRANSFORM( z, diag, r, r/diag ); // z = D\r
308  }
309  else {
310  ff.unfix();
311  timer->stop();
312  ParameterList &db_T2 = db.sublist("child");
313  auto bx_T2 = db_T2.get< RCP<VectorT2>>("bx");
314  TPETRA_BINARY_TRANSFORM( bx_T2, r, as<T2>(r) ); // b_T2 = (T2)r
315  recursiveFPCG<typename TS::next,LO,GO,Node>(out,db_T2); // x_T2 = A_T2 \ b_T2
316  TPETRA_BINARY_TRANSFORM( z, bx_T2, as<T>(bx_T2) ); // z = (T)bx_T2
317  timer->start();
318  ff.fix();
319  }
320  const T zoro = zr;
321  typedef ZeroOp<pair<T,T>> ZeroPTT;
322  auto plusTT = make_pair_op<T,T>(plus<T>());
323  pair<T,T> both = TPETRA_REDUCE3( z, r, rold, // fused: z'*r and z'*r_old
324  make_pair(z*r, z*rold), ZeroPTT, plusTT );
325  zr = both.first; // this is used on the next iteration as well
326  const T znro = both.second;
327  const T beta = (zr - znro) / zoro;
328  TPETRA_BINARY_TRANSFORM( p, z, z + beta*p ); // p = z + beta*p
329  }
330  timer->stop();
331  ff.unfix();
332  if (verbose) {
333  *out << "Leaving recursiveFPCG<" << Teuchos::TypeNameTraits<T>::name()
334  << "> after " << k << " iterations." << std::endl;
335  }
336  }
337 
339  template <class TS, class LO, class GO, class Node>
341  {
345  using Tpetra::RTI::reduce;
347  using Tpetra::RTI::ZeroOp;
348  using Teuchos::as;
349  typedef typename TS::type T;
350  typedef typename TS::next::type T2;
351  typedef Tpetra::Vector<T ,LO,GO,Node> VectorT1;
352  typedef Tpetra::Vector<T2,LO,GO,Node> VectorT2;
353  typedef Tpetra::Operator<T,LO,GO,Node> OpT1;
354  typedef Teuchos::ScalarTraits<T> ST;
355  // get objects from level database
356  const int numIters = db.get<int>("numIters");
357  auto x = db.get<RCP<VectorT1>>("bx");
358  auto r = db.get<RCP<VectorT1>>("r");
359  auto z = db.get<RCP<VectorT1>>("z");
360  auto p = db.get<RCP<VectorT1>>("p");
361  auto Ap = db.get<RCP<VectorT1>>("Ap");
362  auto rold = db.get<RCP<VectorT1>>("rold");
363  auto A = db.get<RCP<const OpT1>>("A");
364  static RCP<Time> timer;
365  if (timer == null) {
367  "recursiveFPCGUnfused<"+Teuchos::TypeNameTraits<T>::name()+">"
368  );
369  }
370  RCP<const VectorT1> diag;
371  if (TS::bottom) {
372  diag = db.get<RCP<VectorT1>>("diag");
373  }
374  const T tolerance = db.get<double>("tolerance", 0.0);
375  const int verbose = db.get<int>("verbose",0);
376 
377  fpu_fix<T> ff;
378  ff.fix();
379 
380  Teuchos::OSTab tab(out);
381 
382  if (verbose) {
383  *out << "Beginning recursiveFPCGUnfused<" << Teuchos::TypeNameTraits<T>::name() << ">" << std::endl;
384  }
385 
386  timer->start();
387  binary_transform( *r, *x, [](T, T bi) {return bi;}); // r = b (b is stored in x)
388  const T r2 = reduce( *r, *r, reductionGlob<ZeroOp<T>>(multiplies<T>(), // r'*r
389  plus<T>()));
390  unary_transform( *x, [](T) {return ST::zero();}); // set x = 0 (now that we don't need b)
391  if (TS::bottom) {
392  tertiary_transform( *z, *diag, *r, [](T, T di, T ri) {return ri/di;}); // z = D\r
393  }
394  else {
395  ff.unfix();
396  timer->stop();
397  ParameterList &db_T2 = db.sublist("child");
398  auto bx_T2 = db_T2.get< RCP<VectorT2>>("bx");
399  binary_transform( *bx_T2, *r, [](T2, T ri) {return as<T2>(ri);}); // b_T2 = (T2)r
400  recursiveFPCGUnfused<typename TS::next,LO,GO,Node>(out,db_T2); // x_T2 = A_T2 \ b_T2
401  binary_transform( *z, *bx_T2, [](T, T2 xi) {return as<T>(xi);}); // z = (T)x_T2
402  timer->start();
403  ff.fix();
404  }
405  binary_transform( *p, *z, [](T, T zi) {return zi;}); // p = z
406  T zr = reduce( *z, *r, reductionGlob<ZeroOp<T>>(multiplies<T>(), // z'*r
407  plus<T>()));
409  int k;
410  for (k=0; k<numIters; ++k)
411  {
412  A->apply(*p,*Ap); // Ap = A*p
413  T pAp = reduce( *p, *Ap, reductionGlob<ZeroOp<T>>(multiplies<T>(), // p'*Ap
414  plus<T>()));
415  const T alpha = zr / pAp;
416  binary_transform( *x, *p, [alpha](T xi, T pi) {return xi + alpha*pi;}); // x = x + alpha*p
417  binary_transform( *rold, *r, [](T, T ri) {return ri;}); // rold = r
418  binary_transform( *r, *Ap,[alpha](T ri, T Api) {return ri - alpha*Api;}); // r = r - alpha*Ap
419  T rr = reduce( *r, *r, reductionGlob<ZeroOp<T>>(multiplies<T>(), // r'*r
420  plus<T>()));
421  if (verbose > 1) *out << "|res|/|res_0|: " << ST::squareroot(rr/r2) << std::endl;
422  if (rr/r2 < tolerance*tolerance) {
423  if (verbose) {
424  *out << "Convergence detected!" << std::endl;
425  }
426  break;
427  }
428  if (TS::bottom) {
429  tertiary_transform( *z, *diag, *r, [](T, T di, T ri) {return ri/di;}); // z = D\r
430  }
431  else {
432  ff.unfix();
433  timer->stop();
434  ParameterList &db_T2 = db.sublist("child");
435  auto bx_T2 = db_T2.get< RCP<VectorT2>>("bx");
436  binary_transform( *bx_T2, *r, [](T2, T ri) {return as<T2>(ri);}); // b_T2 = (T2)r
437  recursiveFPCGUnfused<typename TS::next,LO,GO,Node>(out,db_T2); // x_T2 = A_T2 \ b_T2
438  binary_transform( *z, *bx_T2, [](T, T2 xi) {return as<T>(xi);}); // z = (T)x_T2
439  timer->start();
440  ff.fix();
441  }
442  const T zoro = zr;
443  zr = reduce( *z, *r, reductionGlob<ZeroOp<T>>(multiplies<T>(), // z'*r
444  plus<T>())); // this is loop-carried
445  const T znro = reduce( *z, *rold, reductionGlob<ZeroOp<T>>(multiplies<T>(),// z'*r_old
446  plus<T>()));
447  const T beta = (zr - znro) / zoro;
448  binary_transform( *p, *z, [beta](T pi, T zi) {return zi + beta*pi;}); // p = z + beta*p
449  }
450  timer->stop();
451  ff.unfix();
452  if (verbose) {
453  *out << "Leaving recursiveFPCGUnfused<" << Teuchos::TypeNameTraits<T>::name() << "> after " << k << " iterations." << std::endl;
454  }
455  }
456 
457 } // end of namespace TpetraExamples
458 
459 #endif // MULTIPRECCG_HPP_
Initialization for a database stack for the recursiveFPCG() algorithm, to be used with Tpetra::Ext::i...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Sparse matrix that presents a compressed sparse row interface.
pair_op< T1, T2, Op > make_pair_op(Op op)
Non-member constructor to create a pair_op.
Definition: RTIExample.cpp:121
void binary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
Transform values of vec_inout using vec_inout, vec_in2 and operator op.
Definition: Tpetra_RTI.hpp:167
T & get(const std::string &name, T def_value)
Namespace for Tpetra example classes and methods.
void recursiveFPCGUnfused(const RCP< Teuchos::FancyOStream > &out, ParameterList &db)
Recursive, self-preconditioning flexible CG.
static RCP< Time > getNewTimer(const std::string &name)
void recursiveFPCG(const RCP< Teuchos::FancyOStream > &out, ParameterList &db)
Recursive, self-preconditioning flexible CG.
Abstract interface for linear operators accepting Tpetra MultiVector objects.
Glob::RedOP::result_type reduce(const Vector< S, LO, GO, Node > &vec_in, Glob glob)
Reduce values of vec_in using the operators instantiated in glob.
Definition: Tpetra_RTI.hpp:201
ReductionGlob< GOP, ROP, IOP > reductionGlob(GOP gop, ROP rop)
Non-member constructor to instantiate a type glob of a static identity functor and generation and red...
Definition: Tpetra_RTI.hpp:137
Helper function to call CrsMatrix<T1>::convert<T2> if T1 != T2, and return the original matrix otherw...
Describes a parallel distribution of objects over processes.
TypeTo as(const TypeFrom &t)
A distributed dense vector.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
A static identity functor, providing a static method identity() that returns zero.
Definition: Tpetra_RTI.hpp:67
void unary_transform(Vector< S, LO, GO, Node > &vec_inout, OP op)
Transform values of vec_inout using via operator op.
Definition: Tpetra_RTI.hpp:155
void tertiary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, const Vector< S3, LO, GO, Node > &vec_in3, OP op)
Transform values of vec_inout using vec_inout, vec_in2, vec_in3 and operator op.
Definition: Tpetra_RTI.hpp:183
static std::string name()