| 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 |
#ifndef BELOS_TPETRA_GMRES_SSTEP_HPP |
| 11 |
#define BELOS_TPETRA_GMRES_SSTEP_HPP |
| 12 |
|
| 13 |
#include "Belos_Tpetra_Gmres.hpp" |
| 14 |
#include "Belos_Tpetra_UpdateNewton.hpp" |
| 15 |
|
| 16 |
#include "KokkosBlas3_trsm.hpp" |
| 17 |
|
| 18 |
namespace BelosTpetra { |
| 19 |
namespace Impl { |
| 20 |
|
| 21 |
template<class SC = Tpetra::Operator<>::scalar_type, |
| 22 |
class MV = Tpetra::MultiVector<>, |
| 23 |
class OP = Tpetra::Operator<> > |
| 24 |
class CholQR { |
| 25 |
private: |
| 26 |
using LO = typename MV::local_ordinal_type; |
| 27 |
using blas_type = Teuchos::BLAS<LO, SC>; |
| 28 |
using lapack_type = Teuchos::LAPACK<LO, SC>; |
| 29 |
|
| 30 |
public: |
| 31 |
/// \typedef FactorOutput |
| 32 |
/// \brief Return value of \c factor(). |
| 33 |
/// |
| 34 |
/// Here, FactorOutput is just a minimal object whose value is |
| 35 |
/// irrelevant, so that this class' interface looks like that of |
| 36 |
/// \c CholQR. |
| 37 |
using FactorOutput = int; |
| 38 |
using STS = Teuchos::ScalarTraits<SC>; |
| 39 |
using MVT = Belos::MultiVecTraits<SC, MV>; |
| 40 |
using mag_type = typename STS::magnitudeType; |
| 41 |
using STM = Teuchos::ScalarTraits<mag_type>; |
| 42 |
using dense_matrix_type = Teuchos::SerialDenseMatrix<LO, SC>; |
| 43 |
|
| 44 |
/// \brief Constructor |
| 45 |
/// |
| 46 |
/// \param theCacheSizeHint [in] Cache size hint in bytes. If 0, |
| 47 |
/// the implementation will pick a reasonable size, which may be |
| 48 |
/// queried by calling cache_size_hint(). |
| 49 |
CholQR () = default; |
| 50 |
|
| 51 |
/// \brief Compute the QR factorization of the matrix A. |
| 52 |
/// |
| 53 |
/// Compute the QR factorization of the nrows by ncols matrix A, |
| 54 |
/// with nrows >= ncols, stored either in column-major order (the |
| 55 |
/// default) or as contiguous column-major cache blocks, with |
| 56 |
/// leading dimension lda >= nrows. |
| 57 |
FactorOutput |
| 58 |
factor (Teuchos::FancyOStream* outPtr, MV& A, dense_matrix_type& R) |
| 59 |
{ |
| 60 |
Teuchos::RCP< Teuchos::Time > factorTimer = Teuchos::TimeMonitor::getNewCounter ("CholQR::factor"); |
| 61 |
Teuchos::TimeMonitor LocalTimer (*factorTimer); |
| 62 |
|
| 63 |
blas_type blas; |
| 64 |
lapack_type lapack; |
| 65 |
|
| 66 |
const SC zero = STS::zero (); |
| 67 |
const SC one = STS::one (); |
| 68 |
|
| 69 |
// quick return |
| 70 |
size_t ncols = A.getNumVectors (); |
| 71 |
size_t nrows = A.getLocalLength (); |
| 72 |
if (ncols == 0 || nrows == 0) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
// Compute R := A^T * A, using a single BLAS call. |
| 77 |
// MV with "static" memory (e.g., Tpetra manages the static GPU memory pool) |
| 78 |
MV R_mv = impl::makeStaticLocalMultiVector (A, ncols, ncols); |
| 79 |
//R_mv.putScalar (STS::zero ()); |
| 80 |
|
| 81 |
// compute R := A^T * A |
| 82 |
R_mv.multiply (Teuchos::CONJ_TRANS, Teuchos::NO_TRANS, one, A, A, zero); |
| 83 |
|
| 84 |
// Compute the Cholesky factorization of R in place, so that |
| 85 |
// A^T * A = R^T * R, where R is ncols by ncols upper |
| 86 |
// triangular. |
| 87 |
int info = 0; |
| 88 |
{ |
| 89 |
auto R_h = R_mv.getLocalViewHost (Tpetra::Access::ReadWrite); |
| 90 |
int ldr = int (R_h.extent (0)); |
| 91 |
SC *Rdata = reinterpret_cast<SC*> (R_h.data ()); |
| 92 |
lapack.POTRF ('U', ncols, Rdata, ldr, &info); |
| 93 |
if (info > 0) { |
| 94 |
// FIXME (mfh 17 Sep 2018) Don't throw; report an error code. |
| 95 |
//ncols = info; |
| 96 |
//throw std::runtime_error("Cholesky factorization failed"); |
| 97 |
*outPtr << " > POTRF( " << ncols << " ) failed with info = " << info << std::endl; |
| 98 |
for (size_t i=info-1; i<ncols; i++) { |
| 99 |
R_h(i, i) = one; |
| 100 |
for (size_t j=i+1; j<ncols; j++) { |
| 101 |
R_h(i, j) = zero; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
// Copy to the output R |
| 107 |
Tpetra::deep_copy (R, R_mv); |
| 108 |
// TODO: replace with a routine to zero out lower-triangular |
| 109 |
// : not needed, but done for testing |
| 110 |
for (size_t i=0; i<ncols; i++) { |
| 111 |
for (size_t j=0; j<i; j++) { |
| 112 |
R(i, j) = zero; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Compute A := A * R^{-1}. We do this in place in A, using |
| 117 |
// BLAS' TRSM with the R factor (form POTRF) stored in the upper |
| 118 |
// triangle of R. |
| 119 |
|
| 120 |
// Compute A_cur / R (Matlab notation for A_cur * R^{-1}) in place. |
| 121 |
{ |
| 122 |
auto A_d = A.getLocalViewDevice (Tpetra::Access::ReadWrite); |
| 123 |
auto R_d = R_mv.getLocalViewDevice (Tpetra::Access::ReadOnly); |
| 124 |
KokkosBlas::trsm ("R", "U", "N", "N", |
| 125 |
one, R_d, A_d); |
| 126 |
} |
| 127 |
return (info > 0 ? info-1 : ncols); |
| 128 |
} |
| 129 |
|
| 130 |
// recursive call to factor |
| 131 |
FactorOutput |
| 132 |
reFactor (Teuchos::FancyOStream* outPtr, MV& A, dense_matrix_type& R) |
| 133 |
{ |
| 134 |
int ncols = int (A.getNumVectors ()); |
| 135 |
int rank = 0; |
| 136 |
int old_rank = -1; |
| 137 |
|
| 138 |
// recursively call factor while cols remaining and has made progress |
| 139 |
while (rank < ncols && old_rank != rank) { |
| 140 |
Teuchos::Range1D next_index(rank, ncols-1); |
| 141 |
MV nextA = * (A.subView(next_index)); |
| 142 |
|
| 143 |
dense_matrix_type nextR (Teuchos::View, R, ncols-rank, ncols-rank, rank, rank); |
| 144 |
old_rank = rank; |
| 145 |
auto new_rank = factor (outPtr, nextA, nextR); |
| 146 |
if (outPtr != nullptr) { |
| 147 |
if (rank > 0) { |
| 148 |
*outPtr << " ++ reCholQR("; |
| 149 |
} else { |
| 150 |
*outPtr << " >> CholQR("; |
| 151 |
} |
| 152 |
*outPtr << rank << ":" << ncols-1 << "), new_rank = " << new_rank << std::endl; |
| 153 |
} |
| 154 |
rank += new_rank; |
| 155 |
} |
| 156 |
return rank; |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
template<class SC = Tpetra::Operator<>::scalar_type, |
| 162 |
class MV = Tpetra::MultiVector<SC>, |
| 163 |
class OP = Tpetra::Operator<SC>> |
| 164 |
class GmresSstep : public Gmres<SC, MV, OP> { |
| 165 |
private: |
| 166 |
using base_type = Gmres<SC, MV, OP>; |
| 167 |
using MVT = Belos::MultiVecTraits<SC, MV>; |
| 168 |
using LO = typename MV::local_ordinal_type; |
| 169 |
using STS = Teuchos::ScalarTraits<SC>; |
| 170 |
using mag_type = typename STS::magnitudeType; |
| 171 |
using STM = Teuchos::ScalarTraits<mag_type>; |
| 172 |
using complex_type = std::complex<mag_type>; |
| 173 |
using dense_matrix_type = Teuchos::SerialDenseMatrix<LO, SC>; |
| 174 |
using dense_vector_type = Teuchos::SerialDenseVector<LO, SC>; |
| 175 |
using vec_type = typename Krylov<SC, MV, OP>::vec_type; |
| 176 |
using device_type = typename MV::device_type; |
| 177 |
using dot_type = typename MV::dot_type; |
| 178 |
|
| 179 |
public: |
| 180 |
GmresSstep () : |
| 181 |
base_type::Gmres (), |
| 182 |
useCholQR2_ (false), |
| 183 |
cholqr_ (Teuchos::null) |
| 184 |
{} |
| 185 |
|
| 186 |
GmresSstep (const Teuchos::RCP<const OP>& A) : |
| 187 |
base_type::Gmres (A), |
| 188 |
cholqr_ (Teuchos::null) |
| 189 |
{} |
| 190 |
|
| 191 |
virtual ~GmresSstep () = default; |
| 192 |
|
| 193 |
virtual void |
| 194 |
getParameters (Teuchos::ParameterList& params, |
| 195 |
const bool defaultValues) const |
| 196 |
{ |
| 197 |
base_type::getParameters (params, defaultValues); |
| 198 |
|
| 199 |
const int stepSize = defaultValues ? 5 : this->input_.stepSize; |
| 200 |
params.set ("Step Size", stepSize ); |
| 201 |
} |
| 202 |
|
| 203 |
virtual void |
| 204 |
setParameters (Teuchos::ParameterList& params) { |
| 205 |
base_type::setParameters (params); |
| 206 |
int stepSize = params.get<int> ("Step Size", this->input_.stepSize); |
| 207 |
this->input_.stepSize = stepSize; |
| 208 |
|
| 209 |
bool computeRitzValuesOnFly |
| 210 |
= params.get<bool> ("Compute Ritz Values on Fly", this->input_.computeRitzValuesOnFly); |
| 211 |
this->input_.computeRitzValuesOnFly = computeRitzValuesOnFly; |
| 212 |
|
| 213 |
constexpr bool useCholQR_default = true; |
| 214 |
bool useCholQR = params.get<bool> ("CholeskyQR", useCholQR_default); |
| 215 |
|
| 216 |
bool useCholQR2 = params.get<bool> ("CholeskyQR2", useCholQR2_); |
| 217 |
useCholQR2_ = useCholQR2; |
| 218 |
|
| 219 |
if ((!useCholQR && !useCholQR2) && !cholqr_.is_null ()) { |
| 220 |
cholqr_ = Teuchos::null; |
| 221 |
} else if ((useCholQR || useCholQR2) && cholqr_.is_null ()) { |
| 222 |
cholqr_ = Teuchos::rcp (new CholQR<SC, MV, OP> ()); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private: |
| 227 |
SolverOutput<SC> |
| 228 |
solveOneVec (Teuchos::FancyOStream* outPtr, |
| 229 |
vec_type& X, // in X/out X |
| 230 |
vec_type& B, // in B/out R (not left-preconditioned) |
| 231 |
const OP& A, |
| 232 |
const OP& M, |
| 233 |
const SolverInput<SC>& input) |
| 234 |
{ |
| 235 |
using std::endl; |
| 236 |
int stepSize = input.stepSize; |
| 237 |
int restart = input.resCycle; |
| 238 |
int step = stepSize; |
| 239 |
const SC zero = STS::zero (); |
| 240 |
const SC one = STS::one (); |
| 241 |
|
| 242 |
// timers |
| 243 |
Teuchos::RCP< Teuchos::Time > spmvTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSstep::matrix-apply"); |
| 244 |
Teuchos::RCP< Teuchos::Time > bortTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSstep::BOrtho"); |
| 245 |
Teuchos::RCP< Teuchos::Time > tsqrTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSstep::TSQR"); |
| 246 |
|
| 247 |
// initialize output parameters |
| 248 |
SolverOutput<SC> output {}; |
| 249 |
output.converged = false; |
| 250 |
output.numRests = 0; |
| 251 |
output.numIters = 0; |
| 252 |
|
| 253 |
if (outPtr != nullptr) { |
| 254 |
*outPtr << "GmresSstep" << endl; |
| 255 |
Indent indent1 (outPtr); |
| 256 |
*outPtr << "Solver input:" << endl; |
| 257 |
Indent indentInner (outPtr); |
| 258 |
*outPtr << input; |
| 259 |
} |
| 260 |
|
| 261 |
Teuchos::BLAS<LO, SC> blas; |
| 262 |
Teuchos::LAPACK<LO, SC> lapack; |
| 263 |
dense_matrix_type H (restart+1, restart, true); // Hessenburg matrix |
| 264 |
dense_matrix_type T (restart+1, restart, true); // H reduced to upper-triangular matrix |
| 265 |
dense_matrix_type G (restart+1, step+1, true); // Upper-triangular matrix from ortho process |
| 266 |
dense_matrix_type G2(restart+1, restart, true); // a copy of Hessenburg matrix for computing Ritz values |
| 267 |
dense_vector_type y (restart+1, true); |
| 268 |
dense_matrix_type h (restart+1, 1, true); // used for reorthogonalization |
| 269 |
std::vector<mag_type> cs (restart); |
| 270 |
std::vector<SC> sn (restart); |
| 271 |
|
| 272 |
mag_type b_norm; // initial residual norm |
| 273 |
mag_type b0_norm; // initial residual norm, not left preconditioned |
| 274 |
mag_type r_norm; |
| 275 |
mag_type r_norm_imp; |
| 276 |
mag_type metric; |
| 277 |
|
| 278 |
bool zeroOut = false; // Kokkos::View:init can take a long time on GPU? |
| 279 |
vec_type R (B.getMap (), zeroOut); |
| 280 |
vec_type Y (B.getMap (), zeroOut); |
| 281 |
vec_type MP (B.getMap (), zeroOut); |
| 282 |
MV Q (B.getMap (), restart+1, zeroOut); |
| 283 |
vec_type P0 = * (Q.getVectorNonConst (0)); |
| 284 |
|
| 285 |
// Compute initial residual (making sure R = B - Ax) |
| 286 |
{ |
| 287 |
Teuchos::TimeMonitor LocalTimer (*spmvTimer); |
| 288 |
A.apply (X, R); |
| 289 |
} |
| 290 |
R.update (one, B, -one); |
| 291 |
b0_norm = R.norm2 (); // initial residual norm, not preconditioned |
| 292 |
if (input.precoSide == "left") { |
| 293 |
M.apply (R, P0); |
| 294 |
r_norm = P0.norm2 (); // initial residual norm, left-preconditioned |
| 295 |
} else { |
| 296 |
r_norm = b0_norm; |
| 297 |
} |
| 298 |
b_norm = r_norm; |
| 299 |
|
| 300 |
metric = this->getConvergenceMetric (b0_norm, b0_norm, input); |
| 301 |
if (metric <= input.tol) { |
| 302 |
if (outPtr != nullptr) { |
| 303 |
*outPtr << "Initial guess' residual norm " << b_norm |
| 304 |
<< " meets tolerance " << input.tol << endl; |
| 305 |
} |
| 306 |
output.absResid = r_norm; |
| 307 |
output.relResid = r_norm / b0_norm; |
| 308 |
output.converged = true; |
| 309 |
// Return residual norm as B |
| 310 |
Tpetra::deep_copy (B, P0); |
| 311 |
return output; |
| 312 |
} else if (STM::isnaninf (metric)) { |
| 313 |
if (outPtr != nullptr) { |
| 314 |
*outPtr << "Initial guess' residual norm " << b_norm |
| 315 |
<< " is nan " << endl; |
| 316 |
} |
| 317 |
output.absResid = r_norm; |
| 318 |
output.relResid = r_norm / b0_norm; |
| 319 |
output.converged = false; |
| 320 |
// Return residual norm as B |
| 321 |
Tpetra::deep_copy (B, P0); |
| 322 |
return output; |
| 323 |
} else if (input.computeRitzValues && !input.computeRitzValuesOnFly) { |
| 324 |
// Invoke standard Gmres for the first restart cycle, to compute |
| 325 |
// Ritz values for use as Newton shifts |
| 326 |
if (outPtr != nullptr) { |
| 327 |
*outPtr << "Run standard GMRES for first restart cycle" << endl; |
| 328 |
} |
| 329 |
SolverInput<SC> input_gmres = input; |
| 330 |
input_gmres.maxNumIters = input.resCycle; |
| 331 |
input_gmres.maxNumIters = std::min(input.resCycle, input.maxNumIters); |
| 332 |
input_gmres.computeRitzValues = true; |
| 333 |
|
| 334 |
Tpetra::deep_copy (R, B); |
| 335 |
output = Gmres<SC, MV, OP>::solveOneVec (outPtr, X, R, A, M, |
| 336 |
input_gmres); |
| 337 |
if (output.converged) { |
| 338 |
return output; // standard GMRES converged |
| 339 |
} |
| 340 |
|
| 341 |
if (input.precoSide == "left") { |
| 342 |
M.apply (R, P0); |
| 343 |
r_norm = P0.norm2 (); // residual norm |
| 344 |
} |
| 345 |
else { |
| 346 |
r_norm = output.absResid; |
| 347 |
} |
| 348 |
output.numRests++; |
| 349 |
} |
| 350 |
|
| 351 |
// Initialize starting vector |
| 352 |
if (input.precoSide != "left") { |
| 353 |
Tpetra::deep_copy (P0, R); |
| 354 |
} |
| 355 |
P0.scale (one / r_norm); |
| 356 |
y[0] = r_norm; |
| 357 |
|
| 358 |
// Main loop |
| 359 |
int iter = 0; |
| 360 |
while (output.numIters < input.maxNumIters && ! output.converged) { |
| 361 |
if (outPtr != nullptr) { |
| 362 |
*outPtr << "Restart cycle " << output.numRests << ":" << endl; |
| 363 |
Indent indent2 (outPtr); |
| 364 |
*outPtr << output; |
| 365 |
} |
| 366 |
|
| 367 |
if (input.maxNumIters < output.numIters+restart) { |
| 368 |
restart = input.maxNumIters-output.numIters; |
| 369 |
} |
| 370 |
|
| 371 |
// Restart cycle |
| 372 |
for (; iter < restart && metric > input.tol; iter+=step) { |
| 373 |
if (outPtr != nullptr) { |
| 374 |
*outPtr << "Current s-step iteration: iter=" << iter |
| 375 |
<< ", restart=" << restart |
| 376 |
<< ", step=" << step |
| 377 |
<< ", metric=" << metric << endl; |
| 378 |
Indent indent3 (outPtr); |
| 379 |
} |
| 380 |
|
| 381 |
// Compute matrix powers |
| 382 |
if (input.computeRitzValuesOnFly && output.numIters < input.stepSize) { |
| 383 |
stepSize = 1; |
| 384 |
} else { |
| 385 |
stepSize = input.stepSize; |
| 386 |
} |
| 387 |
for (step=0; step < stepSize && iter+step < restart; step++) { |
| 388 |
// AP = A*P |
| 389 |
vec_type P = * (Q.getVectorNonConst (iter+step)); |
| 390 |
vec_type AP = * (Q.getVectorNonConst (iter+step+1)); |
| 391 |
if (input.precoSide == "none") { |
| 392 |
Teuchos::TimeMonitor LocalTimer (*spmvTimer); |
| 393 |
A.apply (P, AP); |
| 394 |
} |
| 395 |
else if (input.precoSide == "right") { |
| 396 |
M.apply (P, MP); |
| 397 |
{ |
| 398 |
Teuchos::TimeMonitor LocalTimer (*spmvTimer); |
| 399 |
A.apply (MP, AP); |
| 400 |
} |
| 401 |
} |
| 402 |
else { |
| 403 |
{ |
| 404 |
Teuchos::TimeMonitor LocalTimer (*spmvTimer); |
| 405 |
A.apply (P, MP); |
| 406 |
} |
| 407 |
M.apply (MP, AP); |
| 408 |
} |
| 409 |
// Shift for Newton basis |
| 410 |
if ( int (output.ritzValues.size()) > step) { |
| 411 |
//AP.update (-output.ritzValues(step), P, one); |
| 412 |
const complex_type theta = output.ritzValues[step]; |
| 413 |
UpdateNewton<SC, MV>::updateNewtonV(iter+step, Q, theta); |
| 414 |
} |
| 415 |
output.numIters++; |
| 416 |
} |
| 417 |
|
| 418 |
// Orthogonalization |
| 419 |
{ |
| 420 |
Teuchos::TimeMonitor LocalTimer (*bortTimer); |
| 421 |
this->projectBelosOrthoManager (iter, step, Q, G); |
| 422 |
} |
| 423 |
int rank = 0; |
| 424 |
{ |
| 425 |
Teuchos::TimeMonitor LocalTimer (*tsqrTimer); |
| 426 |
rank = recursiveCholQR (outPtr, iter, step, Q, G); |
| 427 |
if (useCholQR2_) { |
| 428 |
rank = recursiveCholQR (outPtr, iter, step, Q, G2); |
| 429 |
// merge R |
| 430 |
dense_matrix_type Rfix (Teuchos::View, G2, step+1, step+1, iter, 0); |
| 431 |
dense_matrix_type Rold (Teuchos::View, G, step+1, step+1, iter, 0); |
| 432 |
blas.TRMM (Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, |
| 433 |
Teuchos::NO_TRANS, Teuchos::NON_UNIT_DIAG, |
| 434 |
step+1, step+1, |
| 435 |
one, Rfix.values(), Rfix.stride(), |
| 436 |
Rold.values(), Rold.stride()); |
| 437 |
} |
| 438 |
if (rank == 0) { |
| 439 |
// FIXME: Don't throw; report an error code. |
| 440 |
throw std::runtime_error("orthogonalization failed with rank = 0"); |
| 441 |
} |
| 442 |
} |
| 443 |
updateHessenburg (iter, step, output.ritzValues, H, G); |
| 444 |
|
| 445 |
// Convergence check |
| 446 |
if (rank == step+1 && H(iter+step, iter+step-1) != zero) { |
| 447 |
// Copy H to T and apply Givens rotations to new columns of T and y |
| 448 |
for (int iiter = 0; iiter < step; iiter++) { |
| 449 |
// Check negative norm |
| 450 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 451 |
(STS::real (H(iter+iiter+1, iter+iiter)) < STM::zero (), |
| 452 |
std::runtime_error, "At iteration " << output.numIters << ", H(" |
| 453 |
<< iter+iiter+1 << ", " << iter+iiter << ") = " |
| 454 |
<< H(iter+iiter+1, iter+iiter) << " < 0."); |
| 455 |
|
| 456 |
for (int i = 0; i <= iter+iiter+1; i++) { |
| 457 |
T(i, iter+iiter) = H(i, iter+iiter); |
| 458 |
} |
| 459 |
this->reduceHessenburgToTriangular(iter+iiter, T, cs, sn, y); |
| 460 |
metric = this->getConvergenceMetric (STS::magnitude (y(iter+iiter+1)), b_norm, input); |
| 461 |
if (outPtr != nullptr) { |
| 462 |
*outPtr << " > implicit residual norm=(" << iter+iiter+1 << ")=" |
| 463 |
<< STS::magnitude (y(iter+iiter+1)) |
| 464 |
<< " metric=" << metric << endl; |
| 465 |
} |
| 466 |
if (STM::isnaninf (metric) || metric <= input.tol) { |
| 467 |
if (outPtr != nullptr) { |
| 468 |
*outPtr << " > break at step = " << iiter+1 << " (" << step << ")" << endl; |
| 469 |
} |
| 470 |
step = iiter+1; |
| 471 |
break; |
| 472 |
} |
| 473 |
} |
| 474 |
if (STM::isnaninf (metric)) { |
| 475 |
// metric is nan |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
else { |
| 480 |
metric = STM::zero (); |
| 481 |
} |
| 482 |
|
| 483 |
// Optionally, compute Ritz values for generating Newton basis |
| 484 |
if (input.computeRitzValuesOnFly && int (output.ritzValues.size()) == 0 |
| 485 |
&& output.numIters >= input.stepSize) { |
| 486 |
for (int i = 0; i < input.stepSize; i++) { |
| 487 |
for (int iiter = 0; iiter < input.stepSize; iiter++) { |
| 488 |
G2(i, iiter) = H(i, iiter); |
| 489 |
} |
| 490 |
} |
| 491 |
computeRitzValues (input.stepSize, G2, output.ritzValues); |
| 492 |
sortRitzValues <LO, SC> (input.stepSize, output.ritzValues); |
| 493 |
if (outPtr != nullptr) { |
| 494 |
*outPtr << " > ComputeRitzValues: " << endl; |
| 495 |
for (int i = 0; i < input.stepSize; i++) { |
| 496 |
*outPtr << " > ritzValues[ " << i << " ] = " << output.ritzValues[i] << endl; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} // End of restart cycle |
| 501 |
|
| 502 |
// Update solution |
| 503 |
if (iter < restart) { |
| 504 |
// save the old solution, just in case explicit residual norm failed the convergence test |
| 505 |
Tpetra::deep_copy (Y, X); |
| 506 |
blas.COPY (1+iter, y.values(), 1, h.values(), 1); |
| 507 |
} |
| 508 |
r_norm_imp = STS::magnitude (y (iter)); // save implicit residual norm |
| 509 |
blas.TRSM (Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, |
| 510 |
Teuchos::NO_TRANS, Teuchos::NON_UNIT_DIAG, |
| 511 |
iter, 1, one, |
| 512 |
T.values(), T.stride(), y.values(), y.stride()); |
| 513 |
Teuchos::Range1D cols(0, iter-1); |
| 514 |
Teuchos::RCP<const MV> Qj = Q.subView(cols); |
| 515 |
dense_vector_type y_iter (Teuchos::View, y.values (), iter); |
| 516 |
if (input.precoSide == "right") { |
| 517 |
MVT::MvTimesMatAddMv (one, *Qj, y_iter, zero, R); |
| 518 |
M.apply (R, MP); |
| 519 |
X.update (one, MP, one); |
| 520 |
} |
| 521 |
else { |
| 522 |
MVT::MvTimesMatAddMv (one, *Qj, y_iter, one, X); |
| 523 |
} |
| 524 |
// Compute real residual (not-preconditioned) |
| 525 |
{ |
| 526 |
Teuchos::TimeMonitor LocalTimer (*spmvTimer); |
| 527 |
A.apply (X, R); |
| 528 |
} |
| 529 |
R.update (one, B, -one); |
| 530 |
r_norm = R.norm2 (); // residual norm |
| 531 |
output.absResid = r_norm; |
| 532 |
output.relResid = r_norm / b0_norm; |
| 533 |
if (outPtr != nullptr) { |
| 534 |
*outPtr << "Implicit and explicit residual norms at restart: " << r_norm_imp << ", " << r_norm << endl; |
| 535 |
} |
| 536 |
|
| 537 |
metric = this->getConvergenceMetric (r_norm, b0_norm, input); |
| 538 |
if (metric <= input.tol) { |
| 539 |
// update solution |
| 540 |
output.converged = true; |
| 541 |
} |
| 542 |
else if (STM::isnaninf (metric)) { |
| 543 |
// failed with nan |
| 544 |
// Return residual norm as B |
| 545 |
Tpetra::deep_copy (B, R); |
| 546 |
return output; |
| 547 |
} |
| 548 |
else if (output.numIters < input.maxNumIters) { |
| 549 |
// Restart, only if max inner-iteration was reached. |
| 550 |
// Otherwise continue the inner-iteration. |
| 551 |
if (iter >= restart) { |
| 552 |
// Restart: Initialize starting vector for restart |
| 553 |
iter = 0; |
| 554 |
P0 = * (Q.getVectorNonConst (0)); |
| 555 |
if (input.precoSide == "left") { // left-precond'd residual norm |
| 556 |
M.apply (R, P0); |
| 557 |
r_norm = P0.norm2 (); |
| 558 |
} |
| 559 |
else { |
| 560 |
// set the starting vector |
| 561 |
Tpetra::deep_copy (P0, R); |
| 562 |
} |
| 563 |
P0.scale (one / r_norm); |
| 564 |
y[0] = SC {r_norm}; |
| 565 |
for (int i=1; i < restart+1; ++i) { |
| 566 |
y[i] = STS::zero (); |
| 567 |
} |
| 568 |
output.numRests++; |
| 569 |
} |
| 570 |
else { |
| 571 |
// reset to the old solution |
| 572 |
Tpetra::deep_copy (X, Y); |
| 573 |
blas.COPY (1+iter, h.values(), 1, y.values(), 1); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
// Return residual norm as B |
| 579 |
Tpetra::deep_copy (B, R); |
| 580 |
|
| 581 |
if (outPtr != nullptr) { |
| 582 |
*outPtr << "At end of solve:" << endl; |
| 583 |
Indent indentInner (outPtr); |
| 584 |
*outPtr << output; |
| 585 |
} |
| 586 |
return output; |
| 587 |
} |
| 588 |
|
| 589 |
protected: |
| 590 |
void |
| 591 |
updateHessenburg (const int n, |
| 592 |
const int s, |
| 593 |
std::vector<complex_type>& S, |
| 594 |
dense_matrix_type& H, |
| 595 |
dense_matrix_type& R) const |
| 596 |
{ |
| 597 |
const SC one = STS::one (); |
| 598 |
const SC zero = STS::zero (); |
| 599 |
|
| 600 |
// 1) multiply H with R(1:n+s+1, 1:n+s+1) from left |
| 601 |
// where R(1:n, 1:n) = I and |
| 602 |
// H(n+1:n+s+1, n+1:n+s) = 0, except h(n+j+1,n+j)=1 for j=1,..,s |
| 603 |
// 1.1) copy: H(j:n-1, j:n-1) = R(j:n-1, j:n-1), i.e., H = R*B |
| 604 |
for (int j = 0; j < s; j++ ) { |
| 605 |
for (int i = 0; i <= n+j+1; i++) { |
| 606 |
H(i, n+j) = R(i, j+1); |
| 607 |
if (int (S.size ()) > j && i <= n+j) { |
| 608 |
//H(i, n+j) += S[j].real * R(i, j); |
| 609 |
H(i, n+j) += UpdateNewton<SC, MV>::updateNewtonH (i, j, R, S[j]); |
| 610 |
} |
| 611 |
} |
| 612 |
for(int i = n+j+2; i <= n+s; i++) { |
| 613 |
H(i, n+j) = zero; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
// submatrices |
| 618 |
dense_matrix_type r_diag (Teuchos::View, R, s, s, n, 0); |
| 619 |
dense_matrix_type h_diag (Teuchos::View, H, s+1, s, n, n); |
| 620 |
Teuchos::BLAS<LO, SC> blas; |
| 621 |
|
| 622 |
if (n == 0) { // >> first matrix-power iteration << |
| 623 |
// 2) multiply H with R(1:s, 1:s)^{-1} from right |
| 624 |
// diagonal block |
| 625 |
blas.TRSM (Teuchos::RIGHT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS, |
| 626 |
Teuchos::NON_UNIT_DIAG, s+1, s, one, |
| 627 |
r_diag.values(), r_diag.stride(), |
| 628 |
h_diag.values(), h_diag.stride()); |
| 629 |
} else { // >> rest of iterations << |
| 630 |
// 1.2) update the starting vector |
| 631 |
for (int i = 0; i < n; i++ ) { |
| 632 |
H(i, n-1) += H(n, n-1) * R(i, 0); |
| 633 |
} |
| 634 |
H(n, n-1) *= R(n, 0); |
| 635 |
|
| 636 |
// 2) multiply H with R(1:n+s, 1:n+s)^{-1} from right, |
| 637 |
// where R(1:n, 1:n) = I |
| 638 |
// 2.1) diagonal block |
| 639 |
for (int j = 0; j < s; j++ ) { |
| 640 |
H(n, n+j) -= H(n, n-1) * R(n-1, j); |
| 641 |
} |
| 642 |
// diagonal block |
| 643 |
blas.TRSM (Teuchos::RIGHT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS, |
| 644 |
Teuchos::NON_UNIT_DIAG, s, s, one, |
| 645 |
r_diag.values(), r_diag.stride(), |
| 646 |
h_diag.values(), h_diag.stride()); |
| 647 |
H(n+s, n+s-1) /= R(n+s-1, s-1); |
| 648 |
|
| 649 |
// 2.2) upper off-diagonal block: H(0:j-1, j:j+n-2) |
| 650 |
dense_matrix_type r_off (Teuchos::View, R, n, s, 0, 0); |
| 651 |
dense_matrix_type h_off (Teuchos::View, H, n, s, 0, n); |
| 652 |
blas.GEMM(Teuchos::NO_TRANS, Teuchos::NO_TRANS, |
| 653 |
n, s, n, |
| 654 |
-one, H.values(), H.stride(), |
| 655 |
r_off.values(), r_off.stride(), |
| 656 |
one, h_off.values(), h_off.stride()); |
| 657 |
|
| 658 |
blas.TRSM(Teuchos::RIGHT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS, |
| 659 |
Teuchos::NON_UNIT_DIAG, n, s, one, |
| 660 |
r_diag.values(), r_diag.stride(), |
| 661 |
h_off.values(), h_off.stride() ); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
//! Apply the orthogonalization using Belos' OrthoManager |
| 666 |
int |
| 667 |
normalizeCholQR (Teuchos::FancyOStream* outPtr, |
| 668 |
const int n, |
| 669 |
const int s, |
| 670 |
MV& Q, |
| 671 |
dense_matrix_type& R) |
| 672 |
{ |
| 673 |
// vector to be orthogonalized |
| 674 |
Teuchos::Range1D index_prev(n, n+s); |
| 675 |
MV Qnew = * (Q.subView(index_prev)); |
| 676 |
|
| 677 |
dense_matrix_type r_new (Teuchos::View, R, s+1, s+1, n, 0); |
| 678 |
|
| 679 |
int rank = 0; |
| 680 |
if (cholqr_ != Teuchos::null) { |
| 681 |
rank = cholqr_->factor (outPtr, Qnew, r_new); |
| 682 |
} |
| 683 |
else { |
| 684 |
rank = this->normalizeBelosOrthoManager (Qnew, r_new); |
| 685 |
} |
| 686 |
return rank; |
| 687 |
} |
| 688 |
|
| 689 |
//! Apply the orthogonalization using Belos' OrthoManager |
| 690 |
int |
| 691 |
recursiveCholQR (Teuchos::FancyOStream* outPtr, |
| 692 |
const int n, |
| 693 |
const int s, |
| 694 |
MV& Q, |
| 695 |
dense_matrix_type& R) |
| 696 |
{ |
| 697 |
// vector to be orthogonalized |
| 698 |
Teuchos::Range1D index_prev(n, n+s); |
| 699 |
MV Qnew = * (Q.subView(index_prev)); |
| 700 |
|
| 701 |
dense_matrix_type r_new (Teuchos::View, R, s+1, s+1, n, 0); |
| 702 |
|
| 703 |
int rank = 0; |
| 704 |
if (cholqr_ != Teuchos::null) { |
| 705 |
rank = cholqr_->reFactor (outPtr, Qnew, r_new); |
| 706 |
} |
| 707 |
else { |
| 708 |
rank = this->normalizeBelosOrthoManager (Qnew, r_new); |
| 709 |
} |
| 710 |
return rank; |
| 711 |
} |
| 712 |
|
| 713 |
private: |
| 714 |
bool useCholQR2_; |
| 715 |
Teuchos::RCP<CholQR<SC, MV, OP> > cholqr_; |
| 716 |
}; |
| 717 |
|
| 718 |
|
| 719 |
template<class SC, class MV, class OP, |
| 720 |
template<class, class, class> class KrylovSubclassType> |
| 721 |
class SolverManager; |
| 722 |
|
| 723 |
// This is the Belos::SolverManager subclass that gets registered with |
| 724 |
// Belos::SolverFactory. |
| 725 |
template<class SC, class MV, class OP> |
| 726 |
using GmresSstepSolverManager = SolverManager<SC, MV, OP, GmresSstep>; |
| 727 |
|
| 728 |
/// \brief Register GmresSstepSolverManager for all enabled Tpetra |
| 729 |
/// template parameter combinations. |
| 730 |
void register_GmresSstep (const bool verbose); |
| 731 |
|
| 732 |
} // namespace Impl |
| 733 |
} // namespace BelosTpetra |
| 734 |
|
| 735 |
#endif // BELOS_TPETRA_GMRES_SSTEP_HPP |