Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
IRTRDriver.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 IRTR_DRIVER_HPP
45 #define IRTR_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 "IRTR_solvers.hpp"
59 
60 
61 #define XFORM_REDUCE TPETRA_BINARY_PRETRANSFORM_REDUCE
62 
63  namespace IRTRdetails {
64 
65  struct trivial_fpu_fix {
66  void fix() {}
67  void unfix() {}
68  };
69 #ifdef HAVE_TPETRA_QD
70  struct nontrivial_fpu_fix {
71  unsigned int old_cw;
72  void fix() {fpu_fix_start(&old_cw);}
73  void unfix() {fpu_fix_end(&old_cw);}
74  };
75 #endif
76  // implementations
77  template <class T> struct fpu_fix : trivial_fpu_fix {};
78 #ifdef HAVE_TPETRA_QD
79  template <> struct fpu_fix<qd_real> : nontrivial_fpu_fix {};
80  template <> struct fpu_fix<dd_real> : nontrivial_fpu_fix {};
81 #endif
82 
83  }
84 
85 
86 template <class S, class Sinner = S>
87 class IRTR_Driver {
88  public:
89  // input
90  Teuchos::RCP<Teuchos::FancyOStream> out;
91  Teuchos::RCP<Teuchos::ParameterList> params;
92  std::string matrixFile;
93  // output
94  bool testPassed;
95 
96  template <class Node>
97  void run(Teuchos::ParameterList &myMachPL, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
98  {
99  using std::plus;
100  using std::endl;
101  using Teuchos::null;
102  using Teuchos::RCP;
103  using Teuchos::ParameterList;
104  using Teuchos::parameterList;
105  using Tpetra::RTI::ZeroOp;
106 
107  // Static types
108  typedef int LO;
109  typedef int GO;
110  typedef Tpetra::Map<LO,GO,Node> Map;
111  typedef Tpetra::CrsMatrix<S,LO,GO,Node> CrsMatrix;
112  typedef Tpetra::Vector<S,LO,GO,Node> Vector;
113  typedef Teuchos::ScalarTraits<S> ST;
114 
115  IRTRdetails::fpu_fix<S> ff; ff.fix();
116 
117  *out << "Running test with Node==" << Teuchos::typeName(*node) << " on rank " << comm->getRank() << "/" << comm->getSize() << std::endl;
118 
119  // read the matrix
120  RCP<CrsMatrix> A;
121  RCP<const Map> rowMap = null;
122  RCP<ParameterList> fillParams = parameterList();
123  // must preserve the local graph in order to do convert() calls later
124  if (Teuchos::TypeTraits::is_same<S,Sinner>::value) {
125  fillParams->set("Preserve Local Graph",false);
126  }
127  else {
128  fillParams->set("Preserve Local Graph",true);
129  }
130  Tpetra::Utils::readHBMatrix(matrixFile,comm,node,A,rowMap,fillParams);
131  rowMap = A->getRowMap();
132 
133  testPassed = true;
134 
135  // compute an inital vector
136  auto x = Tpetra::createVector<S>(rowMap);
137  x->randomize();
138 
139  // call the solve
140  S lambda = TpetraExamples::IRTR<Sinner,S,LO,GO,Node>(out,*params,A,x);
141 
142  // check that residual is as requested
143  {
144  auto r = Tpetra::createVector<S>(rowMap);
145  A->apply(*x,*r);
146  // compute A*x - x*lambda, while simultaneously computing |A*x - x*lambda|
147  const S r_r = XFORM_REDUCE(r, x, // fused:
148  r - x*lambda, // : r = r - x*lambda = A*x - x*lambda
149  r*r, ZeroOp<S>, plus<S>() ); // : sum r'*r
150  const S rnrm = Teuchos::ScalarTraits<S>::squareroot(r_r);
151  // check that residual is as requested
152  *out << "|A*x - x*lambda|/|lambda|: " << rnrm / ST::magnitude(lambda) << endl;
153  const double tolerance = params->get<double>("tolerance");
154  if (rnrm / lambda > tolerance) testPassed = false;
155  }
156 
157  ff.unfix();
158 
159  // print timings
160  Teuchos::TimeMonitor::summarize( *out );
161  }
162 };
163 
164 #endif // IRTR_DRIVER_HPP
Sparse matrix that presents a compressed sparse row interface.
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