Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
MultiPrecDriver.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 MULTIPREC_DRIVER_HPP
45 #define MULTIPREC_DRIVER_HPP
46 
47 #include <Teuchos_TypeNameTraits.hpp>
48 #include <Teuchos_TimeMonitor.hpp>
49 #include <Teuchos_ParameterList.hpp>
50 #include <Teuchos_FancyOStream.hpp>
51 
52 #include <Tpetra_Version.hpp>
53 #include <Tpetra_MatrixIO.hpp>
54 #include <Tpetra_RTI.hpp>
55 #include <Tpetra_CrsMatrix.hpp>
56 #include <Tpetra_Vector.hpp>
57 
58 #include "MultiPrecCG.hpp"
59 
60 template <class MPStack>
61 class MultiPrecDriver {
62  public:
63  // input
64  Teuchos::RCP<Teuchos::FancyOStream> out;
65  Teuchos::RCP<Teuchos::ParameterList> params;
66  std::string matrixFile;
67  bool unfusedTest;
68  // output
69  bool testPassed;
70 
71  template <class Node>
72  void run(Teuchos::ParameterList &myMachPL, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
73  {
74  using std::pair;
75  using std::make_pair;
76  using std::plus;
77  using std::endl;
78  using Teuchos::null;
79  using Teuchos::RCP;
80  using Teuchos::ParameterList;
81  using Teuchos::parameterList;
84  using Tpetra::RTI::ZeroOp;
87 
88  // Static types
89  typedef typename MPStack::type S;
90  typedef int LO;
91  typedef int GO;
92  typedef Tpetra::Map<LO,GO,Node> Map;
93  typedef Tpetra::CrsMatrix<S,LO,GO,Node> CrsMatrix;
94  typedef Tpetra::Vector<S,LO,GO,Node> Vector;
95 
96  *out << "Running test with Node==" << Teuchos::typeName(*node) << " on rank " << comm->getRank() << "/" << comm->getSize() << std::endl;
97 
98  // read the matrix
99  RCP<CrsMatrix> A;
100  RCP<const Map> rowMap = null;
101  RCP<ParameterList> fillParams = parameterList();
102  fillParams->set("Preserve Local Graph",true);
103  // must preserve the local graph in order to do convert() calls later
104  Tpetra::Utils::readHBMatrix(matrixFile,comm,node,A,rowMap,fillParams);
105  rowMap = A->getRowMap();
106 
107  // init the solver stack
109  RCP<ParameterList> db = Tpetra::Ext::initStackDB<MPStack>(*params,init);
110 
111  testPassed = true;
112 
113  // choose a solution, compute a right-hand-side
114  auto x = Tpetra::createVector<S>(rowMap),
115  b = Tpetra::createVector<S>(rowMap);
116  x->randomize();
117  A->apply(*x,*b);
118  {
119  // init the rhs
120  auto bx = db->get<RCP<Vector>>("bx");
121  binary_transform( *bx, *b, [](S, S bi) {return bi;}); // bx = b
122  }
123 
124  // call the solve
125  TpetraExamples::recursiveFPCG<MPStack,LO,GO,Node>(out,*db);
126 
127  // check that residual is as requested
128  {
129  auto xhat = db->get<RCP<Vector>>("bx"),
130  bhat = Tpetra::createVector<S>(rowMap);
131  A->apply(*xhat,*bhat);
132  // compute bhat-b, while simultaneously computing |bhat-b|^2 and |b|^2
133  auto nrms = binary_pre_transform_reduce(*bhat, *b,
134  reductionGlob<ZeroOp<pair<S,S>>>(
135  [](S bhati, S bi){ return bi-bhati;}, // bhati = bi-bhat
136  [](S bhati, S bi){ return make_pair(bhati*bhati, bi*bi); },
137  make_pair_op<S,S>(plus<S>())) );
138  const S enrm = Teuchos::ScalarTraits<S>::squareroot(nrms.first),
139  bnrm = Teuchos::ScalarTraits<S>::squareroot(nrms.second);
140  // check that residual is as requested
141  *out << "|b - A*x|/|b|: " << enrm / bnrm << endl;
142  const double tolerance = db->get<double>("tolerance");
143  if (MPStack::bottom) {
144  // give a little slack
145  if (enrm / bnrm > 5*tolerance) testPassed = false;
146  }
147  else {
148  if (enrm / bnrm > tolerance) testPassed = false;
149  }
150  }
151 
152  //
153  // solve again, with the unfused version, just for timings purposes
154  if (unfusedTest)
155  {
156  // init the rhs
157  auto bx = db->get<RCP<Vector>>("bx");
158  binary_transform( *bx, *b, [](S, S bi) {return bi;}); // bx = b
159  // call the solve
160  TpetraExamples::recursiveFPCGUnfused<MPStack,LO,GO,Node>(out,*db);
161  //
162  // test the result
163  auto xhat = db->get<RCP<Vector>>("bx"),
164  bhat = Tpetra::createVector<S>(rowMap);
165  A->apply(*xhat,*bhat);
166  // compute bhat-b, while simultaneously computing |bhat-b|^2 and |b|^2
167  auto nrms = binary_pre_transform_reduce(*bhat, *b,
168  reductionGlob<ZeroOp<pair<S,S>>>(
169  [](S bhati, S bi){ return bi-bhati;}, // bhati = bi-bhat
170  [](S bhati, S bi){ return make_pair(bhati*bhati, bi*bi); },
171  make_pair_op<S,S>(plus<S>())) );
172  const S enrm = Teuchos::ScalarTraits<S>::squareroot(nrms.first),
173  bnrm = Teuchos::ScalarTraits<S>::squareroot(nrms.second);
174  // check that residual is as requested
175  *out << "|b - A*x|/|b|: " << enrm / bnrm << endl;
176  const double tolerance = db->get<double>("tolerance");
177  if (MPStack::bottom) {
178  // give a little slack
179  if (enrm / bnrm > 5*tolerance) testPassed = false;
180  }
181  else {
182  if (enrm / bnrm > tolerance) testPassed = false;
183  }
184  }
185 
186 
187  // print timings
188  Teuchos::TimeMonitor::summarize( *out );
189  }
190 };
191 
192 #endif // MULTIPREC_DRIVER_HPP
Initialization for a database stack for the recursiveFPCG() algorithm, to be used with Tpetra::Ext::i...
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
Glob::RedOP::result_type binary_pre_transform_reduce(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, Glob glob)
Transforms values of vec_inout while simultaneously performing a parallel reduction.
Definition: Tpetra_RTI.hpp:253
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
Describes a parallel distribution of objects over processes.
A distributed dense vector.
A static identity functor, providing a static method identity() that returns zero.
Definition: Tpetra_RTI.hpp:67