| 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 DRIVERCORE_HPP |
| 11 |
#include <MueLu.hpp> |
| 12 |
|
| 13 |
// Teuchos |
| 14 |
#include <Teuchos_ScalarTraits.hpp> |
| 15 |
|
| 16 |
// Xpetra |
| 17 |
#include <Xpetra_MultiVector.hpp> |
| 18 |
#include <Xpetra_Vector.hpp> |
| 19 |
#include <Xpetra_Matrix.hpp> |
| 20 |
|
| 21 |
// Belos |
| 22 |
#ifdef HAVE_MUELU_BELOS |
| 23 |
#include <BelosConfigDefs.hpp> |
| 24 |
#include <BelosBiCGStabSolMgr.hpp> |
| 25 |
#include <BelosBlockCGSolMgr.hpp> |
| 26 |
#include <BelosBlockGmresSolMgr.hpp> |
| 27 |
#include <BelosLinearProblem.hpp> |
| 28 |
#include <BelosPseudoBlockCGSolMgr.hpp> |
| 29 |
#include <BelosXpetraAdapter.hpp> // => This header defines Belos::XpetraOp |
| 30 |
#include <BelosMueLuAdapter.hpp> // => This header defines Belos::MueLuOp |
| 31 |
|
| 32 |
#include <BelosTpetraAdapter.hpp> // => This header defines Belos::TpetraOp |
| 33 |
#include <Xpetra_TpetraOperator.hpp> |
| 34 |
#include <MueLu_TpetraOperator.hpp> |
| 35 |
#include <MueLu_CreateTpetraPreconditioner.hpp> |
| 36 |
namespace BelosTpetra { |
| 37 |
namespace Impl { |
| 38 |
extern void register_Cg(const bool verbose); |
| 39 |
extern void register_CgPipeline(const bool verbose); |
| 40 |
extern void register_CgSingleReduce(const bool verbose); |
| 41 |
extern void register_Gmres(const bool verbose); |
| 42 |
extern void register_GmresS(const bool verbose); |
| 43 |
extern void register_GmresSstep(const bool verbose); |
| 44 |
extern void register_GmresSingleReduce(const bool verbose); |
| 45 |
} // namespace Impl |
| 46 |
} // namespace BelosTpetra |
| 47 |
|
| 48 |
#ifdef HAVE_MUELU_EPETRA |
| 49 |
#include <BelosEpetraAdapter.hpp> // => This header defines Belos::EpetraPrecOp |
| 50 |
#endif |
| 51 |
#endif |
| 52 |
|
| 53 |
// Cuda |
| 54 |
#ifdef HAVE_MUELU_CUDA |
| 55 |
#include "cuda_profiler_api.h" |
| 56 |
#endif |
| 57 |
|
| 58 |
// AMGX |
| 59 |
#ifdef HAVE_MUELU_AMGX |
| 60 |
#include <MueLu_AMGXOperator.hpp> |
| 61 |
#endif |
| 62 |
|
| 63 |
#include <MueLu_CreateXpetraPreconditioner.hpp> |
| 64 |
|
| 65 |
//************************************************************************************* |
| 66 |
//************************************************************************************* |
| 67 |
//************************************************************************************* |
| 68 |
// A handy macro to switch time monitors in a StackedTimer-compatible way |
| 69 |
#define MUELU_SWITCH_TIME_MONITOR(tm, timername) \ |
| 70 |
{ \ |
| 71 |
tm = Teuchos::null; \ |
| 72 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(timername))); \ |
| 73 |
} |
| 74 |
|
| 75 |
//************************************************************************************* |
| 76 |
//************************************************************************************* |
| 77 |
//************************************************************************************* |
| 78 |
// Support for ML interface |
| 79 |
#if defined(HAVE_MUELU_ML) and defined(HAVE_MUELU_EPETRA) |
| 80 |
#include <Xpetra_EpetraOperator.hpp> |
| 81 |
#include <ml_MultiLevelPreconditioner.h> |
| 82 |
|
| 83 |
// Helper functions for compilation purposes |
| 84 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 85 |
struct ML_Wrapper { |
| 86 |
static void Generate_ML_MultiLevelPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A, Teuchos::ParameterList& mueluList, |
| 87 |
Teuchos::RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& mlopX) { |
| 88 |
throw std::runtime_error("Template parameter mismatch"); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
template <class GlobalOrdinal> |
| 93 |
struct ML_Wrapper<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode> { |
| 94 |
static void Generate_ML_MultiLevelPreconditioner(Teuchos::RCP<Xpetra::Matrix<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode>>& A, Teuchos::ParameterList& mueluList, |
| 95 |
Teuchos::RCP<Xpetra::Operator<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode>>& mlopX) { |
| 96 |
typedef double SC; |
| 97 |
typedef int LO; |
| 98 |
typedef GlobalOrdinal GO; |
| 99 |
typedef Tpetra::KokkosCompat::KokkosSerialWrapperNode NO; |
| 100 |
Teuchos::RCP<const Epetra_CrsMatrix> Aep = Xpetra::Helpers<SC, LO, GO, NO>::Op2EpetraCrs(A); |
| 101 |
Teuchos::RCP<Epetra_Operator> mlop = Teuchos::rcp<Epetra_Operator>(new ML_Epetra::MultiLevelPreconditioner(*Aep, mueluList)); |
| 102 |
#if defined(HAVE_MUELU_BELOS) |
| 103 |
// NOTE: Belos needs the Apply() and AppleInverse() routines of ML swapped. So... |
| 104 |
mlop = Teuchos::rcp<Belos::EpetraPrecOp>(new Belos::EpetraPrecOp(mlop)); |
| 105 |
#endif |
| 106 |
|
| 107 |
mlopX = Teuchos::rcp(new Xpetra::EpetraOperator<GO, NO>(mlop)); |
| 108 |
} |
| 109 |
}; |
| 110 |
#endif |
| 111 |
|
| 112 |
//************************************************************************************* |
| 113 |
//************************************************************************************* |
| 114 |
//************************************************************************************* |
| 115 |
// This is a standard setup routine |
| 116 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 117 |
void PreconditionerSetup(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A, |
| 118 |
Teuchos::RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>>& coordinates, |
| 119 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& nullspace, |
| 120 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& material, |
| 121 |
Teuchos::ParameterList& mueluList, |
| 122 |
bool profileSetup, |
| 123 |
bool useAMGX, |
| 124 |
bool useML, |
| 125 |
bool setNullSpace, |
| 126 |
int numRebuilds, |
| 127 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& H, |
| 128 |
Teuchos::RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& Prec) { |
| 129 |
#include <MueLu_UseShortNames.hpp> |
| 130 |
using Teuchos::RCP; |
| 131 |
Xpetra::UnderlyingLib lib = A->getRowMap()->lib(); |
| 132 |
typedef typename Teuchos::ScalarTraits<SC>::coordinateType coordinate_type; |
| 133 |
typedef Xpetra::MultiVector<coordinate_type, LO, GO, NO> CoordinateMultiVector; |
| 134 |
// ========================================================================= |
| 135 |
// Preconditioner construction |
| 136 |
// ========================================================================= |
| 137 |
#ifdef HAVE_MUELU_CUDA |
| 138 |
if (profileSetup) cudaProfilerStart(); |
| 139 |
#endif |
| 140 |
|
| 141 |
if (useML && lib != Xpetra::UseEpetra) throw std::runtime_error("Error: Cannot use ML on non-epetra matrices"); |
| 142 |
|
| 143 |
for (int i = 0; i <= numRebuilds; i++) { |
| 144 |
A->SetMaxEigenvalueEstimate(-Teuchos::ScalarTraits<SC>::one()); |
| 145 |
if (useAMGX) { |
| 146 |
#if defined(HAVE_MUELU_AMGX) |
| 147 |
RCP<Tpetra::CrsMatrix<SC, LO, GO, NO>> Ac = toTpetra(A); |
| 148 |
RCP<Tpetra::Operator<SC, LO, GO, NO>> At = Teuchos::rcp_dynamic_cast<Tpetra::Operator<SC, LO, GO, NO>>(Ac); |
| 149 |
RCP<MueLu::TpetraOperator<SC, LO, GO, NO>> Top = MueLu::CreateTpetraPreconditioner(At, mueluList); |
| 150 |
Prec = Teuchos::rcp(new Xpetra::TpetraOperator<SC, LO, GO, NO>(Top)); |
| 151 |
#endif |
| 152 |
} else if (useML) { |
| 153 |
#if defined(HAVE_MUELU_ML) and defined(HAVE_MUELU_EPETRA) |
| 154 |
mueluList.remove("use external multigrid package"); |
| 155 |
if (!coordinates.is_null()) { |
| 156 |
RCP<const Epetra_MultiVector> epetraCoord = MueLu::Utilities<coordinate_type, LO, GO, NO>::MV2EpetraMV(coordinates); |
| 157 |
if (epetraCoord->NumVectors() > 0) mueluList.set("x-coordinates", (*epetraCoord)[0]); |
| 158 |
if (epetraCoord->NumVectors() > 1) mueluList.set("y-coordinates", (*epetraCoord)[1]); |
| 159 |
if (epetraCoord->NumVectors() > 2) mueluList.set("z-coordinates", (*epetraCoord)[2]); |
| 160 |
} |
| 161 |
if (!material.is_null()) { |
| 162 |
RCP<const Epetra_MultiVector> epetraMat = MueLu::Utilities<SC, LO, GO, NO>::MV2EpetraMV(material); |
| 163 |
mueluList.set("material coordinates", (*epetraMat)[0]); |
| 164 |
} |
| 165 |
ML_Wrapper<SC, LO, GO, NO>::Generate_ML_MultiLevelPreconditioner(A, mueluList, Prec); |
| 166 |
#endif |
| 167 |
} else { |
| 168 |
Teuchos::Array<LO> lNodesPerDim(3, 10); |
| 169 |
Teuchos::ParameterList& userParamList = mueluList.sublist("user data"); |
| 170 |
if (!coordinates.is_null()) |
| 171 |
userParamList.set<RCP<CoordinateMultiVector>>("Coordinates", coordinates); |
| 172 |
if (!material.is_null()) |
| 173 |
userParamList.set<RCP<Xpetra::MultiVector<SC, LO, GO, NO>>>("Material", material); |
| 174 |
if (!nullspace.is_null() && setNullSpace) |
| 175 |
userParamList.set<RCP<Xpetra::MultiVector<SC, LO, GO, NO>>>("Nullspace", nullspace); |
| 176 |
userParamList.set<Teuchos::Array<LO>>("Array<LO> lNodesPerDim", lNodesPerDim); |
| 177 |
H = MueLu::CreateXpetraPreconditioner(A, mueluList); |
| 178 |
} |
| 179 |
} |
| 180 |
#ifdef HAVE_MUELU_CUDA |
| 181 |
if (profileSetup) cudaProfilerStop(); |
| 182 |
#endif |
| 183 |
} |
| 184 |
|
| 185 |
#if defined(HAVE_MUELU_EPETRA) |
| 186 |
|
| 187 |
// Helper functions for compilation purposes |
| 188 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 189 |
struct Matvec_Wrapper { |
| 190 |
static void UnwrapEpetra(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A, |
| 191 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& X, |
| 192 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& B, |
| 193 |
Teuchos::RCP<const Epetra_CrsMatrix>& Aepetra, |
| 194 |
Teuchos::RCP<Epetra_MultiVector>& Xepetra, |
| 195 |
Teuchos::RCP<Epetra_MultiVector>& Bepetra) { |
| 196 |
throw std::runtime_error("Template parameter mismatch"); |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
template <class GlobalOrdinal> |
| 201 |
struct Matvec_Wrapper<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode> { |
| 202 |
static void UnwrapEpetra(Teuchos::RCP<Xpetra::Matrix<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode>>& A, |
| 203 |
Teuchos::RCP<Xpetra::MultiVector<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode>>& X, |
| 204 |
Teuchos::RCP<Xpetra::MultiVector<double, int, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSerialWrapperNode>>& B, |
| 205 |
Teuchos::RCP<const Epetra_CrsMatrix>& Aepetra, |
| 206 |
Teuchos::RCP<Epetra_MultiVector>& Xepetra, |
| 207 |
Teuchos::RCP<Epetra_MultiVector>& Bepetra) { |
| 208 |
typedef double SC; |
| 209 |
typedef int LO; |
| 210 |
typedef GlobalOrdinal GO; |
| 211 |
typedef Tpetra::KokkosCompat::KokkosSerialWrapperNode NO; |
| 212 |
Aepetra = Xpetra::Helpers<SC, LO, GO, NO>::Op2EpetraCrs(A); |
| 213 |
Xepetra = Teuchos::rcp(&Xpetra::toEpetra(*X), false); |
| 214 |
Bepetra = Teuchos::rcp(&Xpetra::toEpetra(*B), false); |
| 215 |
} |
| 216 |
}; |
| 217 |
#endif |
| 218 |
|
| 219 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 220 |
bool cg_solve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A, |
| 221 |
Teuchos::RCP<const Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> b, |
| 222 |
Teuchos::RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> x, |
| 223 |
const double tolerance, const int max_iter, const bool barriers) { |
| 224 |
using Teuchos::TimeMonitor; |
| 225 |
using Vector = Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 226 |
using STS = typename Teuchos::ScalarTraits<Scalar>; |
| 227 |
using magnitude_type = typename STS::magnitudeType; |
| 228 |
|
| 229 |
std::string cgTimerName = "CG: total"; |
| 230 |
std::string cgBarrierTimerName = "CG: total barrier"; |
| 231 |
std::string addTimerName = "CG: axpby"; |
| 232 |
std::string matvecTimerName = "CG: spmv"; |
| 233 |
std::string dotTimerName = "CG: dot"; |
| 234 |
std::string matvecBarrierTimerName = "CG: barrier spmv"; |
| 235 |
std::string dotBarrierTimerName = "CG: barrier dot"; |
| 236 |
|
| 237 |
auto comm = A->getRowMap()->getComm(); |
| 238 |
int myproc = comm->getRank(); |
| 239 |
|
| 240 |
typedef typename Vector::scalar_type ScalarType; |
| 241 |
typedef typename Vector::local_ordinal_type LO; |
| 242 |
Teuchos::RCP<Vector> r, p, Ap; |
| 243 |
// int max_iter=200; |
| 244 |
// double tolerance = 1e-8; |
| 245 |
r = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(A->getRangeMap()); |
| 246 |
p = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(A->getRangeMap()); |
| 247 |
Ap = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(A->getRangeMap()); |
| 248 |
|
| 249 |
magnitude_type normr = 0; |
| 250 |
magnitude_type rtrans = 0; |
| 251 |
magnitude_type oldrtrans = 0; |
| 252 |
|
| 253 |
LO print_freq = max_iter / 10; |
| 254 |
print_freq = std::min(print_freq, 50); |
| 255 |
print_freq = std::max(print_freq, 1); |
| 256 |
|
| 257 |
if (barriers) { |
| 258 |
TimeMonitor t(*TimeMonitor::getNewTimer(cgBarrierTimerName)); |
| 259 |
comm->barrier(); |
| 260 |
} |
| 261 |
TimeMonitor timer(*TimeMonitor::getNewTimer(cgTimerName)); |
| 262 |
|
| 263 |
{ |
| 264 |
TimeMonitor t(*TimeMonitor::getNewTimer(addTimerName)); |
| 265 |
p->update(1.0, *x, 0.0, *x, 0.0); |
| 266 |
} |
| 267 |
{ |
| 268 |
if (barriers) { |
| 269 |
TimeMonitor t(*TimeMonitor::getNewTimer(matvecBarrierTimerName)); |
| 270 |
comm->barrier(); |
| 271 |
} |
| 272 |
TimeMonitor t(*TimeMonitor::getNewTimer(matvecTimerName)); |
| 273 |
A->apply(*p, *Ap); |
| 274 |
} |
| 275 |
{ |
| 276 |
TimeMonitor t(*TimeMonitor::getNewTimer(addTimerName)); |
| 277 |
r->update(1.0, *b, -1.0, *Ap, 0.0); |
| 278 |
} |
| 279 |
{ |
| 280 |
if (barriers) { |
| 281 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotBarrierTimerName)); |
| 282 |
comm->barrier(); |
| 283 |
} |
| 284 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotTimerName)); |
| 285 |
rtrans = STS::magnitude(r->dot(*r)); |
| 286 |
} |
| 287 |
|
| 288 |
normr = std::sqrt(rtrans); |
| 289 |
|
| 290 |
if (myproc == 0) { |
| 291 |
std::cout << "Initial Residual = " << normr << std::endl; |
| 292 |
} |
| 293 |
|
| 294 |
magnitude_type brkdown_tol = std::numeric_limits<magnitude_type>::epsilon(); |
| 295 |
LO k; |
| 296 |
for (k = 1; k <= max_iter && normr > tolerance; ++k) { |
| 297 |
if (k == 1) { |
| 298 |
TimeMonitor t(*TimeMonitor::getNewTimer(addTimerName)); |
| 299 |
p->update(1.0, *r, 0.0); |
| 300 |
} else { |
| 301 |
oldrtrans = rtrans; |
| 302 |
{ |
| 303 |
if (barriers) { |
| 304 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotBarrierTimerName)); |
| 305 |
comm->barrier(); |
| 306 |
} |
| 307 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotTimerName)); |
| 308 |
rtrans = STS::magnitude(r->dot(*r)); |
| 309 |
} |
| 310 |
{ |
| 311 |
TimeMonitor t(*TimeMonitor::getNewTimer(addTimerName)); |
| 312 |
magnitude_type beta = rtrans / oldrtrans; |
| 313 |
p->update(1.0, *r, beta); |
| 314 |
} |
| 315 |
} |
| 316 |
normr = std::sqrt(rtrans); |
| 317 |
if (myproc == 0 && (k % print_freq == 0 || k == max_iter)) { |
| 318 |
std::cout << "Iteration = " << k << " Residual = " << normr << std::endl; |
| 319 |
} |
| 320 |
|
| 321 |
magnitude_type alpha = 0; |
| 322 |
magnitude_type p_ap_dot = 0; |
| 323 |
{ |
| 324 |
if (barriers) { |
| 325 |
TimeMonitor t(*TimeMonitor::getNewTimer(matvecBarrierTimerName)); |
| 326 |
comm->barrier(); |
| 327 |
} |
| 328 |
TimeMonitor t(*TimeMonitor::getNewTimer(matvecTimerName)); |
| 329 |
A->apply(*p, *Ap); |
| 330 |
} |
| 331 |
{ |
| 332 |
if (barriers) { |
| 333 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotBarrierTimerName)); |
| 334 |
comm->barrier(); |
| 335 |
} |
| 336 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotTimerName)); |
| 337 |
p_ap_dot = STS::magnitude(Ap->dot(*p)); |
| 338 |
} |
| 339 |
{ |
| 340 |
TimeMonitor t(*TimeMonitor::getNewTimer(addTimerName)); |
| 341 |
if (p_ap_dot < brkdown_tol) { |
| 342 |
if (p_ap_dot < 0) { |
| 343 |
std::cerr << "miniFE::cg_solve ERROR, numerical breakdown!" << std::endl; |
| 344 |
return false; |
| 345 |
} else |
| 346 |
brkdown_tol = 0.1 * p_ap_dot; |
| 347 |
} |
| 348 |
alpha = rtrans / p_ap_dot; |
| 349 |
x->update(alpha, *p, 1.0); |
| 350 |
r->update(-alpha, *Ap, 1.0); |
| 351 |
} |
| 352 |
} |
| 353 |
{ |
| 354 |
if (barriers) { |
| 355 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotBarrierTimerName)); |
| 356 |
comm->barrier(); |
| 357 |
} |
| 358 |
TimeMonitor t(*TimeMonitor::getNewTimer(dotTimerName)); |
| 359 |
rtrans = STS::magnitude(r->dot(*r)); |
| 360 |
} |
| 361 |
|
| 362 |
normr = std::sqrt(rtrans); |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
//************************************************************************************* |
| 367 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 368 |
void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A, |
| 369 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& X, |
| 370 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& B, |
| 371 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& H, |
| 372 |
Teuchos::RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& Prec, |
| 373 |
Teuchos::FancyOStream& out, |
| 374 |
std::string solveType, |
| 375 |
std::string belosType, |
| 376 |
bool profileSolve, |
| 377 |
bool useAMGX, |
| 378 |
bool useML, |
| 379 |
int cacheSize, |
| 380 |
int numResolves, |
| 381 |
bool scaleResidualHist, |
| 382 |
bool solvePreconditioned, |
| 383 |
int maxIts, |
| 384 |
double tol, |
| 385 |
bool computeCondEst, |
| 386 |
bool enforceBoundaryConditionsOnInitialGuess) { |
| 387 |
#include <MueLu_UseShortNames.hpp> |
| 388 |
using Teuchos::RCP; |
| 389 |
using Teuchos::rcp; |
| 390 |
using Teuchos::TimeMonitor; |
| 391 |
Xpetra::UnderlyingLib lib = A->getRowMap()->lib(); |
| 392 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 393 |
SC zero = STS::zero(); |
| 394 |
|
| 395 |
// Cache clearing |
| 396 |
std::vector<int> tempVector; |
| 397 |
int min = 0, max = 10; |
| 398 |
int numInts = 0; |
| 399 |
if (cacheSize > 0) { |
| 400 |
cacheSize *= 1024; // convert to bytes |
| 401 |
numInts = cacheSize / sizeof(int) + 1; |
| 402 |
tempVector.resize(numInts); |
| 403 |
} |
| 404 |
|
| 405 |
// Get the raw matrices for matvec testing |
| 406 |
Teuchos::RCP<Tpetra::CrsMatrix<SC, LO, GO, NO>> Atpetra; |
| 407 |
Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NO>> Xtpetra, Btpetra; |
| 408 |
if (lib == Xpetra::UseTpetra) { |
| 409 |
Atpetra = toTpetra(A); |
| 410 |
Xtpetra = rcp(&Xpetra::toTpetra(*X), false); |
| 411 |
Btpetra = rcp(&Xpetra::toTpetra(*B), false); |
| 412 |
} |
| 413 |
|
| 414 |
#if defined(HAVE_MUELU_EPETRA) |
| 415 |
Teuchos::RCP<const Epetra_CrsMatrix> Aepetra; |
| 416 |
Teuchos::RCP<Epetra_MultiVector> Xepetra, Bepetra; |
| 417 |
if (lib == Xpetra::UseEpetra) { |
| 418 |
Matvec_Wrapper<SC, LO, GO, NO>::UnwrapEpetra(A, X, B, Aepetra, Xepetra, Bepetra); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
for (int solveno = 0; solveno <= numResolves; solveno++) { |
| 423 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 3 - LHS and RHS initialization"))); |
| 424 |
X->putScalar(zero); |
| 425 |
if (enforceBoundaryConditionsOnInitialGuess) { |
| 426 |
out << "Enforcing boundary conditions on initial guess\n"; |
| 427 |
Utilities::EnforceInitialCondition(*A, *B, *X); |
| 428 |
} |
| 429 |
tm = Teuchos::null; |
| 430 |
|
| 431 |
if (solveType == "none") { |
| 432 |
// Do not perform a solve |
| 433 |
} else if (solveType == "matvec") { |
| 434 |
// Just do matvecs |
| 435 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 6 - Matvec"))); |
| 436 |
if (lib == Xpetra::UseTpetra) |
| 437 |
Atpetra->apply(*Btpetra, *Xtpetra); |
| 438 |
|
| 439 |
#if defined(HAVE_MUELU_EPETRA) && !defined(HAVE_MUELU_INST_COMPLEX_INT_INT) && !defined(HAVE_MUELU_INST_FLOAT_INT_INT) |
| 440 |
if (lib == Xpetra::UseEpetra) Aepetra->Apply(*Bepetra, *Xepetra); |
| 441 |
#endif |
| 442 |
// clear the cache (and don't time it) |
| 443 |
tm = Teuchos::null; |
| 444 |
int ttt = rand(); |
| 445 |
for (int i = 0; i < numInts; ++i) |
| 446 |
tempVector[i] += (min + (ttt % static_cast<int>(max - min + 1))); |
| 447 |
} else if (solveType == "standalone") { |
| 448 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 4 - Fixed Point Solve"))); |
| 449 |
#ifdef HAVE_MUELU_CUDA |
| 450 |
if (profileSolve) cudaProfilerStart(); |
| 451 |
#endif |
| 452 |
if (useAMGX) { |
| 453 |
#if defined(HAVE_MUELU_AMGX) |
| 454 |
// Do a fixed-point iteraiton without convergence checks |
| 455 |
RCP<MultiVector> R = MultiVectorFactory::Build(X->getMap(), X->getNumVectors()); |
| 456 |
for (int i = 0; i < maxIts; i++) { |
| 457 |
Utilities::Residual(*A, *X, *B, *R); |
| 458 |
Prec->apply(*R, *X); |
| 459 |
} |
| 460 |
#endif |
| 461 |
} else { |
| 462 |
H->IsPreconditioner(false); |
| 463 |
std::pair<LocalOrdinal, typename STS::magnitudeType> maxItsTol(maxIts, tol); |
| 464 |
H->Iterate(*B, *X, maxItsTol); |
| 465 |
} |
| 466 |
#ifdef HAVE_MUELU_CUDA |
| 467 |
if (profileSolve) cudaProfilerStop(); |
| 468 |
#endif |
| 469 |
} else if (solveType == "belos") { |
| 470 |
#ifdef HAVE_MUELU_BELOS |
| 471 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 5 - Belos Solve"))); |
| 472 |
#ifdef HAVE_MUELU_CUDA |
| 473 |
if (profileSolve) cudaProfilerStart(); |
| 474 |
#endif |
| 475 |
// Operator and Multivector type that will be used with Belos |
| 476 |
typedef MultiVector MV; |
| 477 |
typedef Belos::OperatorT<MV> OP; |
| 478 |
|
| 479 |
// Define Operator and Preconditioner |
| 480 |
Teuchos::RCP<OP> belosOp = rcp(new Belos::XpetraOp<SC, LO, GO, NO>(A)); // Turns a Xpetra::Matrix object into a Belos operator |
| 481 |
Teuchos::RCP<OP> belosPrec; // Turns a MueLu::Hierarchy object into a Belos operator |
| 482 |
if (useAMGX) { |
| 483 |
#if defined(HAVE_MUELU_AMGX) |
| 484 |
belosPrec = rcp(new Belos::XpetraOp<SC, LO, GO, NO>(Prec)); // Turns an Xpetra::Operator object into a Belos operator |
| 485 |
#endif |
| 486 |
} else if (useML) { |
| 487 |
#if defined(HAVE_MUELU_ML) and defined(HAVE_MUELU_EPETRA) |
| 488 |
belosPrec = rcp(new Belos::XpetraOp<SC, LO, GO, NO>(Prec)); // Turns an Xpetra::Operator object into a Belos operator |
| 489 |
#endif |
| 490 |
} else { |
| 491 |
H->IsPreconditioner(true); |
| 492 |
belosPrec = rcp(new Belos::MueLuOp<SC, LO, GO, NO>(H)); // Turns a MueLu::Hierarchy object into a Belos operator |
| 493 |
} |
| 494 |
|
| 495 |
std::string belosTypeUpper(belosType); |
| 496 |
std::transform(belosTypeUpper.begin(), belosTypeUpper.end(), belosTypeUpper.begin(), ::toupper); |
| 497 |
|
| 498 |
// Belos parameter list |
| 499 |
RCP<Teuchos::ParameterList> belosList = Teuchos::parameterList(); |
| 500 |
belosList->set("Maximum Iterations", maxIts); // Maximum number of iterations allowed |
| 501 |
belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested |
| 502 |
belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); |
| 503 |
belosList->set("Output Frequency", 1); |
| 504 |
belosList->set("Output Style", Belos::Brief); |
| 505 |
if (!scaleResidualHist) |
| 506 |
belosList->set("Implicit Residual Scaling", "None"); |
| 507 |
if (computeCondEst && (belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG")) |
| 508 |
belosList->set("Estimate Condition Number", true); |
| 509 |
|
| 510 |
int numIts; |
| 511 |
Scalar conditionNumberEstimate = zero; |
| 512 |
Teuchos::ArrayRCP<typename STS::magnitudeType> eigenvalueEstimates; |
| 513 |
Belos::ReturnType ret = Belos::Unconverged; |
| 514 |
|
| 515 |
constexpr bool verbose = false; |
| 516 |
BelosTpetra::Impl::register_Cg(verbose); |
| 517 |
BelosTpetra::Impl::register_CgPipeline(verbose); |
| 518 |
BelosTpetra::Impl::register_CgSingleReduce(verbose); |
| 519 |
BelosTpetra::Impl::register_Gmres(verbose); |
| 520 |
BelosTpetra::Impl::register_GmresS(verbose); |
| 521 |
BelosTpetra::Impl::register_GmresSstep(verbose); |
| 522 |
BelosTpetra::Impl::register_GmresSingleReduce(verbose); |
| 523 |
|
| 524 |
try { |
| 525 |
TEUCHOS_TEST_FOR_EXCEPTION(lib != Xpetra::UseTpetra, std::invalid_argument, "Need to use Tpetra backend in order to call a Belos Tpetra solver."); |
| 526 |
|
| 527 |
using tMV = Tpetra::MultiVector<SC, LO, GO, NO>; |
| 528 |
using tOP = Tpetra::Operator<SC, LO, GO, NO>; |
| 529 |
|
| 530 |
Teuchos::RCP<tOP> belosPrecTpetra; |
| 531 |
if (useAMGX) { |
| 532 |
#if defined(HAVE_MUELU_AMGX) |
| 533 |
RCP<Xpetra::TpetraOperator<SC, LO, GO, NO>> xto = Teuchos::rcp_dynamic_cast<Xpetra::TpetraOperator<SC, LO, GO, NO>>(Prec); |
| 534 |
belosPrecTpetra = xto->getOperator(); |
| 535 |
#endif |
| 536 |
} else { |
| 537 |
belosPrecTpetra = rcp(new MueLu::TpetraOperator<SC, LO, GO, NO>(H)); |
| 538 |
} |
| 539 |
|
| 540 |
// Construct a Belos LinearProblem object |
| 541 |
RCP<Belos::LinearProblem<SC, tMV, tOP>> belosProblem = rcp(new Belos::LinearProblem<SC, tMV, tOP>(Atpetra, Xtpetra, Btpetra)); |
| 542 |
if (solvePreconditioned) belosProblem->setRightPrec(belosPrecTpetra); |
| 543 |
|
| 544 |
bool set = belosProblem->setProblem(); |
| 545 |
if (set == false) { |
| 546 |
throw MueLu::Exceptions::RuntimeError("ERROR: Belos::LinearProblem failed to set up correctly!"); |
| 547 |
} |
| 548 |
|
| 549 |
// Create an iterative solver manager |
| 550 |
Belos::SolverFactory<SC, tMV, tOP> solverFactory; |
| 551 |
RCP<Belos::SolverManager<SC, tMV, tOP>> solver = solverFactory.create(belosType, belosList); |
| 552 |
solver->setProblem(belosProblem); |
| 553 |
|
| 554 |
// Perform solve |
| 555 |
ret = solver->solve(); |
| 556 |
numIts = solver->getNumIters(); |
| 557 |
|
| 558 |
if ((belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG") && |
| 559 |
belosList->isParameter("Estimate Condition Number") && |
| 560 |
belosList->get<bool>("Estimate Condition Number")) { |
| 561 |
conditionNumberEstimate = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, tMV, tOP>>(solver)->getConditionEstimate(); |
| 562 |
eigenvalueEstimates = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, tMV, tOP>>(solver)->getEigenEstimates(); |
| 563 |
} |
| 564 |
|
| 565 |
} catch (std::invalid_argument&) { |
| 566 |
// Construct a Belos LinearProblem object |
| 567 |
RCP<Belos::LinearProblem<SC, MV, OP>> belosProblem = rcp(new Belos::LinearProblem<SC, MV, OP>(belosOp, X, B)); |
| 568 |
if (solvePreconditioned) belosProblem->setRightPrec(belosPrec); |
| 569 |
|
| 570 |
bool set = belosProblem->setProblem(); |
| 571 |
if (set == false) { |
| 572 |
throw MueLu::Exceptions::RuntimeError("ERROR: Belos::LinearProblem failed to set up correctly!"); |
| 573 |
} |
| 574 |
|
| 575 |
// Create an iterative solver manager |
| 576 |
Belos::SolverFactory<SC, MV, OP> solverFactory; |
| 577 |
RCP<Belos::SolverManager<SC, MV, OP>> solver = solverFactory.create(belosType, belosList); |
| 578 |
solver->setProblem(belosProblem); |
| 579 |
|
| 580 |
// Perform solve |
| 581 |
ret = solver->solve(); |
| 582 |
numIts = solver->getNumIters(); |
| 583 |
|
| 584 |
if ((belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG") && |
| 585 |
belosList->isParameter("Estimate Condition Number") && |
| 586 |
belosList->get<bool>("Estimate Condition Number")) { |
| 587 |
conditionNumberEstimate = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, MV, OP>>(solver)->getConditionEstimate(); |
| 588 |
eigenvalueEstimates = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, MV, OP>>(solver)->getEigenEstimates(); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// Get the number of iterations for this solve. |
| 593 |
out << "Number of iterations performed for this solve: " << numIts << std::endl; |
| 594 |
|
| 595 |
if (conditionNumberEstimate != zero) { |
| 596 |
out << "Condition number estimate: " << conditionNumberEstimate << std::endl; |
| 597 |
out << "Eigenvalue estimates: " << eigenvalueEstimates().toString() << std::endl; |
| 598 |
} |
| 599 |
|
| 600 |
// Check convergence |
| 601 |
if (ret != Belos::Converged) |
| 602 |
out << std::endl |
| 603 |
<< "ERROR: Belos did not converge! " << std::endl; |
| 604 |
else |
| 605 |
out << std::endl |
| 606 |
<< "SUCCESS: Belos converged!" << std::endl; |
| 607 |
#ifdef HAVE_MUELU_CUDA |
| 608 |
if (profileSolve) cudaProfilerStop(); |
| 609 |
#endif |
| 610 |
#endif // ifdef HAVE_MUELU_BELOS |
| 611 |
} else if (solveType == "simple_cg") { |
| 612 |
cg_solve(A, B->getVector(0), X->getVectorNonConst(0), tol, maxIts, true); |
| 613 |
} else { |
| 614 |
throw MueLu::Exceptions::RuntimeError("Unknown solver type: \"" + solveType + "\""); |
| 615 |
} |
| 616 |
} // end resolves |
| 617 |
} |
| 618 |
|
| 619 |
#endif |