MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_GMRESSolver_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_GMRESSOLVER_DEF_HPP
47 #define MUELU_GMRESSOLVER_DEF_HPP
48 
49 #include <Teuchos_LAPACK.hpp>
50 
51 #include <Xpetra_MatrixFactory.hpp>
52 #include <Xpetra_MatrixMatrix.hpp>
53 #include <Xpetra_IO.hpp>
54 
56 
57 #include "MueLu_Constraint.hpp"
58 #include "MueLu_Monitor.hpp"
59 #include "MueLu_Utilities.hpp"
60 
61 namespace MueLu {
62 
63 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
65  : nIts_(Its) {}
66 
67 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
69  for (int i = 0; i < k; i++) {
70  SC w1 = c[i] * v[i] - s[i] * v[i + 1];
71  SC w2 = s[i] * v[i] + c[i] * v[i + 1];
72  v[i] = w1;
73  v[i + 1] = w2;
74  }
75 }
76 
77 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
78 void GMRESSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP<Matrix>& finalP) const {
79  PrintMonitor m(*this, "GMRES iterations");
80 
81  finalP = MatrixFactory2::BuildCopy(rcpFromRef(P0));
82  if (nIts_ == 0)
83  return;
84 
86  "For now, solving Hessenberg system works only for a single iteration");
87 
89 
90  RCP<const Matrix> A = rcpFromRef(Aref);
91  // bool useTpetra = (A->getRowMap()->lib() == Xpetra::UseTpetra);
92 
93  // FIXME: Don't know why, but in the MATLAB code we have D = I. Follow that for now.
94 #if 0
96 #else
97  ArrayRCP<const SC> D(A->getLocalNumRows(), one);
98 #endif
99 
100  Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2);
101 
102  // Initial P0 would only be used for multiplication
103  RCP<Matrix> X = rcp_const_cast<Matrix>(rcpFromRef(P0)), tmpAP, newV;
104  std::vector<RCP<Matrix> > V(nIts_ + 1);
105 
106  // T is used only for projecting onto
107  RCP<CrsMatrix> T_ = CrsMatrixFactory::Build(C.GetPattern());
108  T_->fillComplete(P0.getDomainMap(), P0.getRangeMap());
109  RCP<Matrix> T = rcp(new CrsMatrixWrap(T_));
110 
111  SC rho;
112  {
113  // R_0 = -D^{-1}*A*X_0
114  // V_0 = R_0 / ||R_0||_F
115  tmpAP = MatrixMatrix::Multiply(*A, false, *X, false, mmfancy, true /*doFillComplete*/, true /*optimizeStorage*/);
116  C.Apply(*tmpAP, *T);
117 
118  V[0] = MatrixFactory2::BuildCopy(T);
119  Utilities::MyOldScaleMatrix(*V[0], D, true /*doInverse*/, true /*doFillComplete*/, false /*doOptimizeStorage*/);
120 
121  rho = sqrt(Utilities::Frobenius(*V[0], *V[0]));
122 
123  V[0]->scale(-one / rho);
124  }
125 
126  std::vector<SC> h((nIts_ + 1) * (nIts_ + 1));
127  std::vector<SC> c(nIts_ + 1, 0.0);
128  std::vector<SC> s(nIts_ + 1, 0.0);
129  std::vector<SC> g(nIts_ + 1, 0.0);
130  g[0] = rho;
131 
132 #define I(i, j) ((i) + (j) * (nIts_ + 1)) // column ordering
133  for (size_t i = 0; i < nIts_; i++) {
134  // V_{i+1} = D^{-1}*A*V_i
135  tmpAP = MatrixMatrix::Multiply(*A, false, *V[i], false, mmfancy, true /*doFillComplete*/, true /*optimizeStorage*/);
136  C.Apply(*tmpAP, *T);
137 
138  V[i + 1] = MatrixFactory2::BuildCopy(T);
139  Utilities::MyOldScaleMatrix(*V[i + 1], D, true /*doInverse*/, true /*doFillComplete*/, false /*doOptimizeStorage*/);
140 
141  // Update Hessenberg matrix
142  for (size_t j = 0; j <= i; j++) {
143  h[I(j, i)] = Utilities::Frobenius(*V[i + 1], *V[j]);
144 
145  // V_{i+1} = V_{i+1} - h(j,i+1)*V_j
146 #ifndef TWO_ARG_MATRIX_ADD
147  newV = Teuchos::null;
148  MatrixMatrix::TwoMatrixAdd(*V[j], false, -h[I(j, i)], *V[i + 1], false, one, newV, mmfancy);
149  newV->fillComplete(V[i + 1]->getDomainMap(), V[i + 1]->getRangeMap());
150  V[i + 1].swap(newV);
151 #else
152  // FIXME: this does not work now. Fails with the following exception:
153  // what(): ../../packages/tpetra/core/ext/TpetraExt_MatrixMatrix_def.hpp:408:
154  //
155  // Throw number = 1
156  //
157  // Throw test that evaluated to true: B.isLocallyIndexed()
158  //
159  // TpetraExt::MatrixMatrix::Add(): ERROR, input matrix B must not be locally indexed
160  MatrixMatrix::TwoMatrixAdd(*V[j], false, -h[I(j, i)], *V[i + 1], one);
161 #endif
162  }
163  h[I(i + 1, i)] = sqrt(Utilities::Frobenius(*V[i + 1], *V[i + 1]));
164 
165  // NOTE: potentially we'll need some reorthogonalization code here
166  // The matching MATLAB code is
167  // normav = norm(v.num(k+1).matrix, 'fro');
168  // normav2 = h(k+1,k);
169  // if (reorth == -1 && normav + .001*normav2 == normav)
170  // for j = 1:k
171  // hr = v(:,j)'*v(:,k+1); % hr=v(:,k+1)'*v(:,j);
172  // h(j,k) = h(j,k)+hr;
173  // v(:,k+1) = v(:,k+1)-hr*v(:,j);
174  // end
175  // h(k+1,k) = norm(v(:,k+1));
176  // end
177 
178  // Check for nonsymmetric case
179  if (h[I(i + 1, i)] != zero) {
180  // Normalize V_i
181  V[i + 1]->scale(one / h[I(i + 1, i)]);
182  }
183 
184  if (i > 0)
185  givapp(&c[0], &s[0], &h[I(0, i)], i); // Due to column ordering &h[...] is a column
186 
187  SC nu = sqrt(h[I(i, i)] * h[I(i, i)] + h[I(i + 1, i)] * h[I(i + 1, i)]);
188  if (nu != zero) {
189  c[i] = h[I(i, i)] / nu;
190  s[i] = -h[I(i + 1, i)] / nu;
191  h[I(i, i)] = c[i] * h[I(i, i)] - s[i] * h[I(i + 1, i)];
192  h[I(i + 1, i)] = zero;
193 
194  givapp(&c[i], &s[i], &g[i], 1);
195  }
196  }
197 
198  // Solve Hessenberg system
199  // y = solve(H, \rho e_1)
200  std::vector<SC> y(nIts_);
201  if (nIts_ == 1) {
202  y[0] = g[0] / h[I(0, 0)];
203  }
204 #undef I
205 
206  // Compute final
207  for (size_t i = 0; i < nIts_; i++) {
208 #ifndef TWO_ARG_MATRIX_ADD
209  newV = Teuchos::null;
210  MatrixMatrix::TwoMatrixAdd(*V[i], false, y[i], *finalP, false, one, newV, mmfancy);
211  newV->fillComplete(finalP->getDomainMap(), finalP->getRangeMap());
212  finalP.swap(newV);
213 #else
214  MatrixMatrix::TwoMatrixAdd(*V[i], false, y[i], *finalP, one);
215 #endif
216  }
217 }
218 
219 } // namespace MueLu
220 
221 #endif // ifndef MUELU_GMRESSOLVER_DECL_HPP
void givapp(SC *c, SC *s, SC *v, int k) const
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Constraint space information for the potential prolongator.
void swap(RCP< T > &r_ptr)
RCP< const CrsGraph > GetPattern() const
Print even more statistics.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
#define I(i, j)
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal_arcp(const Matrix &A)
Extract Matrix Diagonal.
void Iterate(const Matrix &A, const Constraint &C, const Matrix &P0, RCP< Matrix > &P) const
Iterate.
void Apply(const Matrix &P, Matrix &Projected) const
Apply constraint.
Scalar SC
Exception throws to report errors in the internal logical of the program.
static Scalar Frobenius(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B)
Frobenius inner product of two matrices.