| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef MUELU_SETUPREGIONSMOOTHERS_DEF_HPP |
| 11 |
#define MUELU_SETUPREGIONSMOOTHERS_DEF_HPP |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include <Tpetra_KokkosCompat_DefaultNode.hpp> |
| 16 |
|
| 17 |
#include <Teuchos_RCP.hpp> |
| 18 |
|
| 19 |
#include <Xpetra_ConfigDefs.hpp> |
| 20 |
#include <Xpetra_Export.hpp> |
| 21 |
#include <Xpetra_Import.hpp> |
| 22 |
#include <Xpetra_Map.hpp> |
| 23 |
#include <Xpetra_MultiVector.hpp> |
| 24 |
#include <Xpetra_Vector.hpp> |
| 25 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 26 |
|
| 27 |
#include "SetupRegionMatrix_def.hpp" |
| 28 |
#include "SetupRegionVector_def.hpp" |
| 29 |
|
| 30 |
using Teuchos::Array; |
| 31 |
using Teuchos::ArrayRCP; |
| 32 |
using Teuchos::ArrayView; |
| 33 |
using Teuchos::ParameterList; |
| 34 |
using Teuchos::RCP; |
| 35 |
|
| 36 |
/*! \brief Create list of valid smoother types |
| 37 |
* |
| 38 |
* Create this list here in a routine, such that everyone can use exactly the same list. |
| 39 |
* This avoids copy-and-paste errors. |
| 40 |
* |
| 41 |
* ToDo: replace this list by an enum when we migrate to actual code. |
| 42 |
*/ |
| 43 |
std::map<std::string, int> getListOfValidSmootherTypes() { |
| 44 |
std::map<std::string, int> smootherTypes; |
| 45 |
smootherTypes.insert(std::pair<std::string, int>("None", 0)); |
| 46 |
smootherTypes.insert(std::pair<std::string, int>("Jacobi", 1)); |
| 47 |
smootherTypes.insert(std::pair<std::string, int>("Gauss", 2)); |
| 48 |
smootherTypes.insert(std::pair<std::string, int>("SymmetricGauss", 3)); |
| 49 |
smootherTypes.insert(std::pair<std::string, int>("Chebyshev", 4)); |
| 50 |
|
| 51 |
return smootherTypes; |
| 52 |
} |
| 53 |
|
| 54 |
/*! \brief Compute inverse of diagonal of the operator |
| 55 |
* |
| 56 |
* Computes the inverse of the diagonal in region format and with interface scaling |
| 57 |
*/ |
| 58 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 59 |
void computeInverseDiagonal(RCP<Teuchos::ParameterList> params, |
| 60 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, |
| 61 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, |
| 62 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport) ///< row importer in region layout [in] |
| 63 |
{ |
| 64 |
#include "Xpetra_UseShortNames.hpp" |
| 65 |
using Teuchos::TimeMonitor; |
| 66 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Relaxation Setup"))); |
| 67 |
|
| 68 |
RCP<Vector> diagReg = VectorFactory::Build(revisedRowMap, true); |
| 69 |
|
| 70 |
// extract inverse of diagonal from matrix |
| 71 |
diagReg = VectorFactory::Build(regionMats->getRowMap(), true); |
| 72 |
regionMats->getLocalDiagCopy(*diagReg); |
| 73 |
|
| 74 |
sumInterfaceValues(diagReg, revisedRowMap, rowImport); |
| 75 |
|
| 76 |
diagReg->reciprocal(*diagReg); |
| 77 |
|
| 78 |
params->set<RCP<Vector> >("smoothers: inverse diagonal", diagReg); |
| 79 |
} |
| 80 |
|
| 81 |
/*! \brief Do Jacobi smoothing |
| 82 |
* |
| 83 |
* Perform Jacobi smoothing in the region layout using the true diagonal value |
| 84 |
* recovered from the splitted matrix. |
| 85 |
*/ |
| 86 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 87 |
void jacobiIterate(RCP<Teuchos::ParameterList> smootherParams, |
| 88 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regX, // left-hand side (or solution) |
| 89 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regB, // right-hand side (or residual) |
| 90 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, // matrices in true region layout |
| 91 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] (actually extracted from regionMats) |
| 92 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport, ///< row importer in region layout [in] |
| 93 |
bool& zeroInitGuess) { |
| 94 |
#include "Xpetra_UseShortNames.hpp" |
| 95 |
using Teuchos::TimeMonitor; |
| 96 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Jacobi Iterate"))); |
| 97 |
|
| 98 |
// const Scalar SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 99 |
const Scalar SC_ONE = Teuchos::ScalarTraits<Scalar>::one(); |
| 100 |
|
| 101 |
const int maxIter = smootherParams->get<int>("smoother: sweeps"); |
| 102 |
const double damping = smootherParams->get<double>("smoother: damping"); |
| 103 |
RCP<Vector> diag_inv = smootherParams->get<RCP<Vector> >("smoothers: inverse diagonal"); |
| 104 |
|
| 105 |
RCP<Vector> regRes = VectorFactory::Build(revisedRowMap, true); |
| 106 |
|
| 107 |
for (int iter = 0; iter < maxIter; ++iter) { |
| 108 |
// Update the residual vector |
| 109 |
if (zeroInitGuess) { |
| 110 |
regX->elementWiseMultiply(damping, *diag_inv, *regB, SC_ONE); |
| 111 |
} else { |
| 112 |
computeResidual(regRes, regX, regB, regionMats, *smootherParams); |
| 113 |
|
| 114 |
// update solution according to Jacobi's method |
| 115 |
regX->elementWiseMultiply(damping, *diag_inv, *regRes, SC_ONE); |
| 116 |
} |
| 117 |
zeroInitGuess = false; |
| 118 |
} |
| 119 |
|
| 120 |
return; |
| 121 |
} // jacobiIterate |
| 122 |
|
| 123 |
/*! \brief Do Gauss-Seidel smoothing |
| 124 |
* |
| 125 |
* Perform Gauss-Seidel smoothing in the region layout using the true diagonal value |
| 126 |
* recovered from the splitted matrix. Off-diagonal values are just taken as they are |
| 127 |
* in region format. |
| 128 |
*/ |
| 129 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 130 |
void GSIterate(RCP<Teuchos::ParameterList> smootherParams, |
| 131 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regX, // left-hand side (or solution) |
| 132 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regB, // right-hand side (or residual) |
| 133 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, // matrices in true region layout |
| 134 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] (actually extracted from regionMats) |
| 135 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport, ///< row importer in region layout [in] |
| 136 |
bool& zeroInitGuess, |
| 137 |
bool sgs = false) { |
| 138 |
#include "Xpetra_UseShortNames.hpp" |
| 139 |
using Teuchos::TimeMonitor; |
| 140 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Gauss-Seidel Iterate"))); |
| 141 |
|
| 142 |
// Extract user-given and pre-computed data from paremter list |
| 143 |
const int maxIter = smootherParams->get<int>("smoother: sweeps"); |
| 144 |
const double damping = smootherParams->get<double>("smoother: damping"); |
| 145 |
RCP<Vector> diag_inv = smootherParams->get<RCP<Vector> >("smoothers: inverse diagonal"); |
| 146 |
|
| 147 |
RCP<Vector> regRes = VectorFactory::Build(revisedRowMap, true); |
| 148 |
|
| 149 |
const size_t numRows = regionMats->getLocalNumRows(); |
| 150 |
|
| 151 |
// GS iteration loop |
| 152 |
for (int iter = 0; iter < maxIter; ++iter) { |
| 153 |
// Update the residual vector |
| 154 |
if (!zeroInitGuess) { |
| 155 |
computeResidual(regRes, regX, regB, regionMats, *smootherParams); |
| 156 |
} |
| 157 |
|
| 158 |
// update the solution and the residual |
| 159 |
|
| 160 |
using MT = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 161 |
RCP<Vector> delta = VectorFactory::Build(regionMats->getRowMap(), true); |
| 162 |
ArrayRCP<SC> ldelta = delta->getDataNonConst(0); |
| 163 |
ArrayRCP<SC> OneregX = regX->getDataNonConst(0); |
| 164 |
ArrayRCP<SC> OneregRes = regRes->getDataNonConst(0); |
| 165 |
if (zeroInitGuess) { // copy regB to regRes |
| 166 |
ArrayRCP<SC> rhs = regB->getDataNonConst(0); |
| 167 |
for (size_t k = 0; k < numRows; ++k) OneregRes[k] = rhs[k]; |
| 168 |
} |
| 169 |
ArrayRCP<const SC> Onediag = diag_inv->getData(0); |
| 170 |
|
| 171 |
// Loop over all rows in the region matrix |
| 172 |
for (size_t k = 0; k < numRows; ++k) { |
| 173 |
// Extract a single row |
| 174 |
ArrayView<const LO> AAcols; |
| 175 |
ArrayView<const SC> AAvals; |
| 176 |
regionMats->getLocalRowView(k, AAcols, AAvals); |
| 177 |
const int* Acols = AAcols.getRawPtr(); |
| 178 |
const SC* Avals = AAvals.getRawPtr(); |
| 179 |
const LO RowLeng = AAvals.size(); |
| 180 |
|
| 181 |
// Loop over entries in row k and perform GS iteration |
| 182 |
for (LO kk = 0; kk < RowLeng; kk++) { |
| 183 |
OneregRes[k] -= Avals[kk] * ldelta[Acols[kk]]; |
| 184 |
} |
| 185 |
ldelta[k] = damping * Onediag[k] * OneregRes[k]; |
| 186 |
OneregX[k] += ldelta[k]; |
| 187 |
} |
| 188 |
zeroInitGuess = false; |
| 189 |
|
| 190 |
if (sgs) { |
| 191 |
for (size_t k = numRows; k--;) { |
| 192 |
// Extract a single row |
| 193 |
ArrayView<const LO> AAcols; |
| 194 |
ArrayView<const SC> AAvals; |
| 195 |
regionMats->getLocalRowView(k, AAcols, AAvals); |
| 196 |
const int* Acols = AAcols.getRawPtr(); |
| 197 |
const SC* Avals = AAvals.getRawPtr(); |
| 198 |
const LO RowLeng = AAvals.size(); |
| 199 |
|
| 200 |
// Loop over entries in row k and perform GS iteration |
| 201 |
for (LO kk = 0; kk < RowLeng; kk++) { |
| 202 |
OneregRes[k] -= Avals[kk] * ldelta[Acols[kk]]; |
| 203 |
} |
| 204 |
ldelta[k] = damping * Onediag[k] * OneregRes[k]; |
| 205 |
OneregX[k] += ldelta[k]; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return; |
| 211 |
} // GS |
| 212 |
|
| 213 |
//! Transfer region vector to composite format and compute its 2-norm |
| 214 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 215 |
typename Teuchos::ScalarTraits<Scalar>::magnitudeType |
| 216 |
calcNorm2(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, |
| 217 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport) { |
| 218 |
#include "Xpetra_UseShortNames.hpp" |
| 219 |
const RCP<const Map> mapComp = rowImport->getSourceMap(); |
| 220 |
RCP<Vector> compVec = VectorFactory::Build(mapComp, true); |
| 221 |
regionalToComposite(regVec, compVec, rowImport); |
| 222 |
typename Teuchos::ScalarTraits<Scalar>::magnitudeType norm = compVec->norm2(); |
| 223 |
|
| 224 |
return norm; |
| 225 |
} // calcNorm2 |
| 226 |
|
| 227 |
/*! Compute inner product of two region vectors |
| 228 |
* |
| 229 |
* First, we transform the region vectors to the composite layout. Then, we utilize |
| 230 |
* the Xpetra::Vector::dot() capability to compute the inner product. |
| 231 |
* |
| 232 |
* @param[in] regX First region vector |
| 233 |
* @param[in] regY Second region vector |
| 234 |
* @param[in] rowImport Importer to transfer region vectors to composite layout |
| 235 |
* |
| 236 |
* @return Inner product of regX and regY |
| 237 |
*/ |
| 238 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 239 |
Scalar |
| 240 |
dotProd(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regX, |
| 241 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regY, |
| 242 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport) { |
| 243 |
#include "Xpetra_UseShortNames.hpp" |
| 244 |
const RCP<const Map> mapComp = rowImport->getSourceMap(); |
| 245 |
RCP<Vector> compX = VectorFactory::Build(mapComp, true); |
| 246 |
RCP<Vector> compY = VectorFactory::Build(mapComp, true); |
| 247 |
regionalToComposite(regX, compX, rowImport); |
| 248 |
regionalToComposite(regY, compY, rowImport); |
| 249 |
SC dotVal = compX->dot(*compY); |
| 250 |
|
| 251 |
return dotVal; |
| 252 |
} // dotProd |
| 253 |
|
| 254 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 255 |
Scalar |
| 256 |
powerMethod(RCP<Teuchos::ParameterList> params, |
| 257 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, |
| 258 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, |
| 259 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport, |
| 260 |
const int numIters) { |
| 261 |
#include "Xpetra_UseShortNames.hpp" |
| 262 |
|
| 263 |
RCP<Vector> diag_inv = params->get<RCP<Vector> >("smoothers: inverse diagonal"); |
| 264 |
const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 265 |
const SC SC_ONE = Teuchos::ScalarTraits<Scalar>::one(); |
| 266 |
SC lambdaMax = SC_ZERO; |
| 267 |
SC RQ_top, RQ_bottom, norm; |
| 268 |
|
| 269 |
RCP<Vector> regX = VectorFactory::Build(revisedRowMap, true); |
| 270 |
RCP<Vector> regY = VectorFactory::Build(revisedRowMap, true); |
| 271 |
|
| 272 |
regX->randomize(); |
| 273 |
|
| 274 |
norm = calcNorm2(regX, rowImport); |
| 275 |
regX->scale(SC_ONE / norm); |
| 276 |
|
| 277 |
for (int iter = 0; iter < numIters; ++iter) { |
| 278 |
regionMats->apply(*regX, *regY); // A.apply (x, y); |
| 279 |
sumInterfaceValues(regY, revisedRowMap, rowImport); // step 2 |
| 280 |
|
| 281 |
// Scale by inverse of diagonal |
| 282 |
regY->elementWiseMultiply(SC_ONE, *diag_inv, *regY, SC_ZERO); |
| 283 |
|
| 284 |
RQ_top = dotProd(regY, regX, rowImport); |
| 285 |
RQ_bottom = dotProd(regX, regX, rowImport); |
| 286 |
lambdaMax = RQ_top / RQ_bottom; |
| 287 |
|
| 288 |
norm = calcNorm2(regY, rowImport); |
| 289 |
|
| 290 |
if (norm == SC_ZERO) { // Return something reasonable. |
| 291 |
return SC_ZERO; |
| 292 |
} |
| 293 |
regX->update(SC_ONE / norm, *regY, SC_ZERO); |
| 294 |
} |
| 295 |
|
| 296 |
return lambdaMax; |
| 297 |
} // powerMethod |
| 298 |
|
| 299 |
/*! \brief Performs Chebyshev specific setup |
| 300 |
* |
| 301 |
* Use power method to estimate lambdaMx |
| 302 |
*/ |
| 303 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 304 |
void chebyshevSetup(RCP<Teuchos::ParameterList> params, |
| 305 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, |
| 306 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionInterfaceScaling, |
| 307 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, |
| 308 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport) { |
| 309 |
#include "Xpetra_UseShortNames.hpp" |
| 310 |
using Teuchos::TimeMonitor; |
| 311 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Chebyshev Setup"))); |
| 312 |
|
| 313 |
// Calculate lambdaMax |
| 314 |
Scalar lambdaMax = 1; |
| 315 |
lambdaMax = powerMethod(params, |
| 316 |
regionMats, |
| 317 |
revisedRowMap, |
| 318 |
rowImport, |
| 319 |
10); |
| 320 |
params->set<Scalar>("chebyshev: lambda max", lambdaMax); |
| 321 |
|
| 322 |
} // chebyshevSetup |
| 323 |
|
| 324 |
/*! \brief The textbook Chebyshev algorithm from Ifpack2 translated into the region format |
| 325 |
*/ |
| 326 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 327 |
void chebyshevIterate(RCP<Teuchos::ParameterList> smootherParams, |
| 328 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regX, ///< left-hand side (or solution) |
| 329 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regB, ///< right-hand side (or residual) |
| 330 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, ///< matrices in true region layout |
| 331 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] (actually extracted from regionMats) |
| 332 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport, ///< row importer in region layout [in] |
| 333 |
bool& zeroInitGuess ///< Use a zero vector as initial guess? |
| 334 |
) { |
| 335 |
#include "Xpetra_UseShortNames.hpp" |
| 336 |
using Teuchos::TimeMonitor; |
| 337 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Chebyshev Iterate"))); |
| 338 |
|
| 339 |
// Extract input data from parameter list |
| 340 |
const int maxIter = smootherParams->get<int>("smoother: sweeps"); |
| 341 |
const Scalar eigRatio = smootherParams->get<double>("smoother: Chebyshev eigRatio"); |
| 342 |
const Scalar lambdaMax = smootherParams->get<Scalar>("chebyshev: lambda max"); |
| 343 |
const Scalar boostFactor = smootherParams->get<double>("smoother: Chebyshev boost factor"); |
| 344 |
RCP<Vector> diag_inv = smootherParams->get<RCP<Vector> >("smoothers: inverse diagonal"); |
| 345 |
|
| 346 |
// Define some constants for convenience |
| 347 |
const Scalar SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 348 |
const Scalar SC_ONE = Teuchos::ScalarTraits<Scalar>::one(); |
| 349 |
const Scalar SC_TWO = Teuchos::as<Scalar>(2); |
| 350 |
|
| 351 |
// Estimation of eigenvalue interval of interest: [alpha, beta] |
| 352 |
const Scalar alpha = lambdaMax / eigRatio; // lower bound (estimate via given max-to-min ratio) |
| 353 |
const Scalar beta = boostFactor * lambdaMax; // upper bound (estimated via boost factor) |
| 354 |
|
| 355 |
// Algorithmic constants |
| 356 |
const Scalar delta = SC_TWO / (beta - alpha); |
| 357 |
const Scalar theta = (beta + alpha) / SC_TWO; |
| 358 |
const Scalar s1 = theta * delta; |
| 359 |
|
| 360 |
// Algorithmic parameters |
| 361 |
Scalar dtemp1 = SC_ZERO; |
| 362 |
Scalar dtemp2 = SC_ZERO; |
| 363 |
Scalar rhokp1 = SC_ZERO; |
| 364 |
Scalar rhok = SC_ONE / s1; |
| 365 |
|
| 366 |
RCP<Vector> regRes = VectorFactory::Build(revisedRowMap, true); |
| 367 |
|
| 368 |
RCP<Vector> regP = VectorFactory::Build(revisedRowMap, true); |
| 369 |
RCP<Vector> regZ = VectorFactory::Build(revisedRowMap, true); |
| 370 |
|
| 371 |
// First Iteration |
| 372 |
if (zeroInitGuess) { |
| 373 |
regZ->elementWiseMultiply(SC_ONE, *diag_inv, *regB, SC_ZERO); // Z = D_inv * b |
| 374 |
regP->update(SC_ONE / theta, *regZ, SC_ZERO); // P = 1/theta Z |
| 375 |
regX->update(SC_ONE, *regP, SC_ZERO); // X = 0 + P |
| 376 |
} else { |
| 377 |
// Compute residual vector |
| 378 |
computeResidual(regRes, regX, regB, regionMats, *smootherParams); |
| 379 |
|
| 380 |
regZ->elementWiseMultiply(SC_ONE, *diag_inv, *regRes, SC_ZERO); // z = D_inv * R, that is, D \ R. |
| 381 |
regP->update(SC_ONE / theta, *regZ, SC_ZERO); // P = 1/theta Z |
| 382 |
regX->update(SC_ONE, *regP, SC_ONE); // X = X + P |
| 383 |
} |
| 384 |
|
| 385 |
// The rest of the iterations |
| 386 |
for (int i = 1; i < maxIter; ++i) { |
| 387 |
// Compute residual vector |
| 388 |
computeResidual(regRes, regX, regB, regionMats, *smootherParams); |
| 389 |
|
| 390 |
// z = D_inv * R, that is, D \ R. |
| 391 |
regZ->elementWiseMultiply(SC_ONE, *diag_inv, *regRes, SC_ZERO); |
| 392 |
|
| 393 |
rhokp1 = SC_ONE / (SC_TWO * s1 - rhok); |
| 394 |
dtemp1 = rhokp1 * rhok; |
| 395 |
dtemp2 = SC_TWO * rhokp1 * delta; |
| 396 |
rhok = rhokp1; |
| 397 |
regP->update(dtemp2, *regZ, dtemp1); // P = dtemp2*Z + dtemp1*P |
| 398 |
regX->update(SC_ONE, *regP, SC_ONE); // X = X + P |
| 399 |
|
| 400 |
// If we compute the residual here, we could either do R = B - |
| 401 |
// A*X, or R = R - alpha*A*P. Since we choose the former, we |
| 402 |
// can move the computeResidual call to the top of the loop. |
| 403 |
} |
| 404 |
|
| 405 |
zeroInitGuess = false; |
| 406 |
} // chebyshevIterate |
| 407 |
|
| 408 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 409 |
void smootherSetup(RCP<Teuchos::ParameterList> params, |
| 410 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, |
| 411 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, |
| 412 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionInterfaceScaling, |
| 413 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport) ///< row importer in region layout [in] |
| 414 |
{ |
| 415 |
using Teuchos::TimeMonitor; |
| 416 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Smoother: 1 - Setup"))); |
| 417 |
|
| 418 |
const std::string type = params->get<std::string>("smoother: type"); |
| 419 |
|
| 420 |
std::map<std::string, int> smootherTypes = getListOfValidSmootherTypes(); |
| 421 |
|
| 422 |
switch (smootherTypes[type]) { |
| 423 |
case 0: // None |
| 424 |
{ |
| 425 |
break; |
| 426 |
} |
| 427 |
case 1: // Jacobi |
| 428 |
case 2: // Gauss-Seidel |
| 429 |
case 3: // Symmetric Gauss-Seidel |
| 430 |
{ |
| 431 |
computeInverseDiagonal(params, revisedRowMap, regionMats, rowImport); |
| 432 |
break; |
| 433 |
} |
| 434 |
case 4: // Chebyshev |
| 435 |
{ |
| 436 |
computeInverseDiagonal(params, revisedRowMap, regionMats, rowImport); |
| 437 |
chebyshevSetup(params, regionMats, regionInterfaceScaling, revisedRowMap, rowImport); |
| 438 |
break; |
| 439 |
} |
| 440 |
default: { |
| 441 |
std::cout << "Unknown smoother: " << type << "!" << std::endl; |
| 442 |
throw; |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 448 |
void smootherApply(RCP<Teuchos::ParameterList> params, |
| 449 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regX, |
| 450 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regB, |
| 451 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionMats, |
| 452 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, |
| 453 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport, |
| 454 |
bool& zeroInitGuess) { |
| 455 |
using Teuchos::TimeMonitor; |
| 456 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Region Smoother: 2 - Apply"))); |
| 457 |
|
| 458 |
const std::string type = params->get<std::string>("smoother: type"); |
| 459 |
|
| 460 |
std::map<std::string, int> smootherTypes = getListOfValidSmootherTypes(); |
| 461 |
|
| 462 |
switch (smootherTypes[type]) { |
| 463 |
case 0: // None |
| 464 |
{ |
| 465 |
break; |
| 466 |
} |
| 467 |
case 1: // Jacobi |
| 468 |
{ |
| 469 |
jacobiIterate(params, regX, regB, regionMats, revisedRowMap, rowImport, zeroInitGuess); |
| 470 |
break; |
| 471 |
} |
| 472 |
case 2: // Gauss-Seidel |
| 473 |
{ |
| 474 |
GSIterate(params, regX, regB, regionMats, revisedRowMap, rowImport, zeroInitGuess); |
| 475 |
break; |
| 476 |
} |
| 477 |
case 3: // Symmetric Gauss-Seidel |
| 478 |
{ |
| 479 |
GSIterate(params, regX, regB, regionMats, revisedRowMap, rowImport, zeroInitGuess, true); |
| 480 |
break; |
| 481 |
} |
| 482 |
case 4: // Chebyshev |
| 483 |
chebyshevIterate(params, regX, regB, regionMats, revisedRowMap, rowImport, zeroInitGuess); |
| 484 |
{ |
| 485 |
break; |
| 486 |
} |
| 487 |
default: { |
| 488 |
std::cout << "Unknown smoother: " << type << "!" << std::endl; |
| 489 |
throw; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
} // smootherApply |
| 494 |
|
| 495 |
#endif // MUELU_SETUPREGIONSMOOTHERS_DEF_HPP |