| 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 |
#include <cstddef> |
| 11 |
#include <cstdio> |
| 12 |
#include <iomanip> |
| 13 |
#include <iostream> |
| 14 |
#include <unistd.h> |
| 15 |
#include <vector> |
| 16 |
#include <sys/resource.h> |
| 17 |
|
| 18 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 19 |
#include <Teuchos_YamlParameterListHelpers.hpp> |
| 20 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 21 |
|
| 22 |
// Xpetra |
| 23 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 24 |
#include <Xpetra_ImportFactory.hpp> |
| 25 |
#include <Xpetra_Operator.hpp> |
| 26 |
#include <Xpetra_Map.hpp> |
| 27 |
#include <Xpetra_MultiVector.hpp> |
| 28 |
#include <Xpetra_IO.hpp> |
| 29 |
|
| 30 |
// Galeri |
| 31 |
#include <Galeri_XpetraParameters.hpp> |
| 32 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 33 |
#include <Galeri_XpetraUtils.hpp> |
| 34 |
#include <Galeri_XpetraMaps.hpp> |
| 35 |
|
| 36 |
#include <MueLu.hpp> |
| 37 |
|
| 38 |
#include <MueLu_BaseClass.hpp> |
| 39 |
#include "Xpetra_Access.hpp" |
| 40 |
#ifdef HAVE_MUELU_EXPLICIT_INSTANTIATION |
| 41 |
#include <MueLu_ExplicitInstantiation.hpp> |
| 42 |
#endif |
| 43 |
#include <MueLu_Level.hpp> |
| 44 |
#include <MueLu_MutuallyExclusiveTime.hpp> |
| 45 |
#include <MueLu_ParameterListInterpreter.hpp> |
| 46 |
#include <MueLu_Utilities.hpp> |
| 47 |
#include <MueLu_PerfModelReporter.hpp> |
| 48 |
#include <MatrixLoad.hpp> |
| 49 |
#include <DriverCore.hpp> |
| 50 |
|
| 51 |
#ifdef HAVE_MUELU_BELOS |
| 52 |
#include <BelosConfigDefs.hpp> |
| 53 |
#include <BelosBiCGStabSolMgr.hpp> |
| 54 |
#include <BelosBlockCGSolMgr.hpp> |
| 55 |
#include <BelosBlockGmresSolMgr.hpp> |
| 56 |
#include <BelosLinearProblem.hpp> |
| 57 |
#include <BelosPseudoBlockCGSolMgr.hpp> |
| 58 |
#include <BelosXpetraAdapter.hpp> // => This header defines Belos::XpetraOp |
| 59 |
#include <BelosMueLuAdapter.hpp> // => This header defines Belos::MueLuOp |
| 60 |
#include <BelosTpetraAdapter.hpp> // => This header defines Belos::TpetraOp |
| 61 |
#ifdef HAVE_MUELU_EPETRA |
| 62 |
#include <BelosEpetraAdapter.hpp> // => This header defines Belos::EpetraPrecOp |
| 63 |
#endif |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifdef HAVE_MUELU_CUDA |
| 67 |
#include "cuda_profiler_api.h" |
| 68 |
#endif |
| 69 |
|
| 70 |
#ifdef HAVE_MUELU_AMGX |
| 71 |
#include <MueLu_AMGXOperator.hpp> |
| 72 |
#include <MueLu_AMGX_Setup.hpp> |
| 73 |
#endif |
| 74 |
#include <MueLu_TpetraOperator.hpp> |
| 75 |
#include <MueLu_CreateTpetraPreconditioner.hpp> |
| 76 |
#include <Xpetra_TpetraOperator.hpp> |
| 77 |
#include "Xpetra_TpetraMultiVector.hpp" |
| 78 |
#include <KokkosBlas1_abs.hpp> |
| 79 |
#include <Tpetra_leftAndOrRightScaleCrsMatrix.hpp> |
| 80 |
#include <Tpetra_computeRowAndColumnOneNorms.hpp> |
| 81 |
|
| 82 |
#ifdef HAVE_MUELU_EPETRA |
| 83 |
#include "Xpetra_EpetraMultiVector.hpp" |
| 84 |
#endif |
| 85 |
|
| 86 |
/*********************************************************************/ |
| 87 |
|
| 88 |
#include "KokkosBlas1_abs_impl.hpp" |
| 89 |
template <class RV, class XV, class SizeType> |
| 90 |
void Temporary_Replacement_For_Kokkos_abs(const RV& R, const XV& X) { |
| 91 |
typedef typename XV::execution_space execution_space; |
| 92 |
const SizeType numRows = X.extent(0); |
| 93 |
Kokkos::RangePolicy<execution_space, SizeType> policy(0, numRows); |
| 94 |
KokkosBlas::Impl::V_Abs_Functor<RV, XV, SizeType> op(R, X); |
| 95 |
Kokkos::parallel_for(policy, op); |
| 96 |
} |
| 97 |
|
| 98 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 99 |
void equilibrateMatrix(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& Axpetra, std::string equilibrate) { |
| 100 |
#include <MueLu_UseShortNames.hpp> |
| 101 |
using Tpetra::computeRowAndColumnOneNorms; |
| 102 |
using Tpetra::leftAndOrRightScaleCrsMatrix; |
| 103 |
bool equilibrate_1norm = (equilibrate == "1-norm"); |
| 104 |
bool equilibrate_diag = (equilibrate == "diag"); |
| 105 |
bool equilibrate_no = (equilibrate == "no"); |
| 106 |
bool assumeSymmetric = false; |
| 107 |
typedef typename Tpetra::Details::EquilibrationInfo<typename Kokkos::ArithTraits<Scalar>::val_type, typename Node::device_type> equil_type; |
| 108 |
|
| 109 |
Teuchos::RCP<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A = toTpetra(Axpetra); |
| 110 |
|
| 111 |
if (Axpetra->getRowMap()->lib() == Xpetra::UseTpetra) { |
| 112 |
equil_type equibResult_ = computeRowAndColumnOneNorms(*A, assumeSymmetric); |
| 113 |
if (equilibrate_1norm) { |
| 114 |
using device_type = typename Node::device_type; |
| 115 |
using mag_type = typename Kokkos::ArithTraits<Scalar>::mag_type; |
| 116 |
using mag_view_type = Kokkos::View<mag_type*, device_type>; |
| 117 |
using scalar_view_type = Kokkos::View<typename equil_type::val_type*, device_type>; |
| 118 |
|
| 119 |
mag_view_type rowDiagAbsVals("rowDiagAbsVals", equibResult_.rowDiagonalEntries.extent(0)); |
| 120 |
// KokkosBlas::abs (rowDiagAbsVals, equibResult_.rowDiagonalEntries); |
| 121 |
Temporary_Replacement_For_Kokkos_abs<mag_view_type, scalar_view_type, LocalOrdinal>(rowDiagAbsVals, equibResult_.rowDiagonalEntries); |
| 122 |
|
| 123 |
mag_view_type colDiagAbsVals("colDiagAbsVals", equibResult_.colDiagonalEntries.extent(0)); |
| 124 |
|
| 125 |
// KokkosBlas::abs (colDiagAbsVals, equibResult_.colDiagonalEntries); |
| 126 |
Temporary_Replacement_For_Kokkos_abs<mag_view_type, scalar_view_type, LocalOrdinal>(colDiagAbsVals, equibResult_.colDiagonalEntries); |
| 127 |
|
| 128 |
leftAndOrRightScaleCrsMatrix(*A, rowDiagAbsVals, colDiagAbsVals, |
| 129 |
true, true, equibResult_.assumeSymmetric, |
| 130 |
Tpetra::SCALING_DIVIDE); |
| 131 |
} else if (equilibrate_diag) { |
| 132 |
auto colScalingFactors = equibResult_.assumeSymmetric ? equibResult_.colNorms : equibResult_.rowScaledColNorms; |
| 133 |
leftAndOrRightScaleCrsMatrix(*A, equibResult_.rowNorms, |
| 134 |
colScalingFactors, true, true, |
| 135 |
equibResult_.assumeSymmetric, |
| 136 |
Tpetra::SCALING_DIVIDE); |
| 137 |
} else if (equilibrate_no) { |
| 138 |
// no-op |
| 139 |
} else |
| 140 |
throw std::runtime_error("Invalid 'equilibrate' option '" + equilibrate + "'"); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/*********************************************************************/ |
| 145 |
// Gets current memory usage in kilobytes |
| 146 |
size_t get_current_memory_usage() { |
| 147 |
size_t memory = 0; |
| 148 |
|
| 149 |
// darwin reports rusage.ru_maxrss in bytes |
| 150 |
#if defined(__APPLE__) || defined(__MACH__) |
| 151 |
const size_t RU_MAXRSS_UNITS = 1024; |
| 152 |
#else |
| 153 |
const size_t RU_MAXRSS_UNITS = 1; |
| 154 |
#endif |
| 155 |
|
| 156 |
struct rusage sys_resources; |
| 157 |
getrusage(RUSAGE_SELF, &sys_resources); |
| 158 |
memory = (unsigned long)sys_resources.ru_maxrss / RU_MAXRSS_UNITS; |
| 159 |
|
| 160 |
/* Success */ |
| 161 |
return memory; |
| 162 |
} |
| 163 |
/*********************************************************************/ |
| 164 |
|
| 165 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 166 |
int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int argc, char* argv[]) { |
| 167 |
#include <MueLu_UseShortNames.hpp> |
| 168 |
using Teuchos::ArrayRCP; |
| 169 |
using Teuchos::ParameterList; |
| 170 |
using Teuchos::RCP; |
| 171 |
using Teuchos::rcp; |
| 172 |
using Teuchos::TimeMonitor; |
| 173 |
|
| 174 |
// ========================================================================= |
| 175 |
// MPI initialization using Teuchos |
| 176 |
// ========================================================================= |
| 177 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 178 |
|
| 179 |
// ========================================================================= |
| 180 |
// Convenient definitions |
| 181 |
// ========================================================================= |
| 182 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 183 |
typedef typename STS::coordinateType real_type; |
| 184 |
typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector; |
| 185 |
|
| 186 |
// ========================================================================= |
| 187 |
// Parameters initialization |
| 188 |
// ========================================================================= |
| 189 |
GO nx = 100, ny = 100, nz = 100; |
| 190 |
Galeri::Xpetra::Parameters<GO> galeriParameters(clp, nx, ny, nz, "Laplace2D"); // manage parameters of the test case |
| 191 |
Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra |
| 192 |
|
| 193 |
std::string xmlFileName = ""; |
| 194 |
clp.setOption("xml", &xmlFileName, "read parameters from an xml file"); |
| 195 |
std::string yamlFileName = ""; |
| 196 |
clp.setOption("yaml", &yamlFileName, "read parameters from a yaml file"); |
| 197 |
bool printTimings = true; |
| 198 |
clp.setOption("timings", "notimings", &printTimings, "print timings to screen"); |
| 199 |
std::string timingsFormat = "table-fixed"; |
| 200 |
clp.setOption("time-format", &timingsFormat, "timings format (table-fixed | table-scientific | yaml)"); |
| 201 |
int writeMatricesOPT = -2; |
| 202 |
clp.setOption("write", &writeMatricesOPT, "write matrices to file (-1 means all; i>=0 means level i)"); |
| 203 |
std::string dsolveType = "belos", solveType; |
| 204 |
clp.setOption("solver", &dsolveType, "solve type: (none | belos | standalone | matvec | simple_cg )"); |
| 205 |
std::string belosType = "cg"; |
| 206 |
clp.setOption("belosType", &belosType, "belos solver type: (Pseudoblock CG | Block CG | Pseudoblock GMRES | Block GMRES | ...) see BelosSolverFactory.hpp for exhaustive list of solvers"); |
| 207 |
bool computeCondEst = false; |
| 208 |
clp.setOption("condEst", "noCondEst", &computeCondEst, "compute condition number estimate (currently only available for Pseudoblock CG)"); |
| 209 |
bool enforceBoundaryConditionsOnInitialGuess = true; |
| 210 |
clp.setOption("enforceBCs", "noEnforceBCs", &enforceBoundaryConditionsOnInitialGuess, "enforce Dirichlet boundary condition on initial guess"); |
| 211 |
double dtol = 1e-12, tol; |
| 212 |
clp.setOption("tol", &dtol, "solver convergence tolerance"); |
| 213 |
bool binaryFormat = false; |
| 214 |
clp.setOption("binary", "ascii", &binaryFormat, "print timings to screen"); |
| 215 |
|
| 216 |
std::string rowMapFile; |
| 217 |
clp.setOption("rowmap", &rowMapFile, "map data file"); |
| 218 |
std::string colMapFile; |
| 219 |
clp.setOption("colmap", &colMapFile, "colmap data file"); |
| 220 |
std::string domainMapFile; |
| 221 |
clp.setOption("domainmap", &domainMapFile, "domainmap data file"); |
| 222 |
std::string rangeMapFile; |
| 223 |
clp.setOption("rangemap", &rangeMapFile, "rangemap data file"); |
| 224 |
std::string matrixFile; |
| 225 |
clp.setOption("matrix", &matrixFile, "matrix data file"); |
| 226 |
std::string rhsFile; |
| 227 |
clp.setOption("rhs", &rhsFile, "rhs data file"); |
| 228 |
std::string solFile; |
| 229 |
clp.setOption("sol", &solFile, "write the solution to this file"); |
| 230 |
std::string coordFile; |
| 231 |
clp.setOption("coords", &coordFile, "coordinates data file"); |
| 232 |
std::string coordMapFile; |
| 233 |
clp.setOption("coordsmap", &coordMapFile, "coordinates map data file"); |
| 234 |
std::string nullFile; |
| 235 |
clp.setOption("nullspace", &nullFile, "nullspace data file"); |
| 236 |
std::string materialFile; |
| 237 |
clp.setOption("material", &materialFile, "material data file"); |
| 238 |
bool tensorMaterialCoefficient = true; |
| 239 |
clp.setOption("tensorCoefficient", "scalarCoefficient", &tensorMaterialCoefficient, "Generate a tensor or scalar material coefficient if none is passed in from file"); |
| 240 |
bool setNullSpace = true; |
| 241 |
clp.setOption("driver-nullspace", "muelu-computed-nullspace", &setNullSpace, "driver sets nullspace"); |
| 242 |
int numRebuilds = 0; |
| 243 |
clp.setOption("rebuild", &numRebuilds, "#times to rebuild hierarchy"); |
| 244 |
int numResolves = 0; |
| 245 |
clp.setOption("resolve", &numResolves, "#times to redo solve"); |
| 246 |
int maxIts = 200; |
| 247 |
clp.setOption("its", &maxIts, "maximum number of solver iterations"); |
| 248 |
int numVectors = 1; |
| 249 |
clp.setOption("multivector", &numVectors, "number of rhs to solve simultaneously"); |
| 250 |
bool scaleResidualHist = true; |
| 251 |
clp.setOption("scale", "noscale", &scaleResidualHist, "scaled Krylov residual history"); |
| 252 |
bool solvePreconditioned = true; |
| 253 |
clp.setOption("solve-preconditioned", "no-solve-preconditioned", &solvePreconditioned, "use MueLu preconditioner in solve"); |
| 254 |
bool useStackedTimer = false; |
| 255 |
clp.setOption("stacked-timer", "no-stacked-timer", &useStackedTimer, "use stacked timer"); |
| 256 |
std::string watchrProblemName = std::string("MueLu Setup-Solve ") + std::to_string(comm->getSize()) + " ranks"; |
| 257 |
clp.setOption("watchr-problem-name", &watchrProblemName, "Problem name for Watchr plot headers"); |
| 258 |
|
| 259 |
std::string equilibrate = "no"; |
| 260 |
clp.setOption("equilibrate", &equilibrate, "equilibrate the system (no | diag | 1-norm)"); |
| 261 |
#ifdef HAVE_MUELU_CUDA |
| 262 |
bool profileSetup = false; |
| 263 |
clp.setOption("cuda-profile-setup", "no-cuda-profile-setup", &profileSetup, "enable CUDA profiling for setup"); |
| 264 |
bool profileSolve = false; |
| 265 |
clp.setOption("cuda-profile-solve", "no-cuda-profile-solve", &profileSolve, "enable CUDA profiling for solve"); |
| 266 |
#else |
| 267 |
bool profileSetup = false; |
| 268 |
bool profileSolve = false; |
| 269 |
#endif |
| 270 |
int cacheSize = 0; |
| 271 |
clp.setOption("cachesize", &cacheSize, "cache size (in KB)"); |
| 272 |
#ifdef HAVE_MPI |
| 273 |
int provideNodeComm = 0; |
| 274 |
clp.setOption("nodecomm", &provideNodeComm, "make the nodal communicator available w/ reduction factor X"); |
| 275 |
#endif |
| 276 |
std::string userBlkFileName = ""; |
| 277 |
clp.setOption("userBlks", &userBlkFileName, "read user smoother blocks from MatrixMarket matrix file. nnz (i,j) ==> jth dof in ith block"); |
| 278 |
int numReruns = 1; |
| 279 |
clp.setOption("reruns", &numReruns, "number of reruns"); |
| 280 |
std::string rerunFilePrefix; |
| 281 |
clp.setOption("fileprefix", &rerunFilePrefix, "if doing reruns, optional prefix to prepend to output files"); |
| 282 |
std::string rerunFileSuffix; |
| 283 |
clp.setOption("filesuffix", &rerunFileSuffix, "if doing reruns, optional suffix to append to output files"); |
| 284 |
std::string levelPerformanceModel = "no"; |
| 285 |
clp.setOption("performance-model", &levelPerformanceModel, "runs the level-by-level performance mode options- 'no', 'yes' or 'verbose'"); |
| 286 |
clp.recogniseAllOptions(true); |
| 287 |
|
| 288 |
switch (clp.parse(argc, argv)) { |
| 289 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 290 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 291 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 292 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 293 |
} |
| 294 |
|
| 295 |
TEUCHOS_TEST_FOR_EXCEPTION(xmlFileName != "" && yamlFileName != "", std::runtime_error, |
| 296 |
"Cannot provide both xml and yaml input files"); |
| 297 |
|
| 298 |
// Instead of checking each time for rank, create a rank 0 stream |
| 299 |
RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 300 |
Teuchos::FancyOStream& out = *fancy; |
| 301 |
out.setOutputToRootOnly(0); |
| 302 |
|
| 303 |
ParameterList paramList; |
| 304 |
auto inst = xpetraParameters.GetInstantiation(); |
| 305 |
|
| 306 |
if (yamlFileName != "") { |
| 307 |
Teuchos::updateParametersFromYamlFileAndBroadcast(yamlFileName, Teuchos::Ptr<ParameterList>(¶mList), *comm); |
| 308 |
} else { |
| 309 |
if (inst == Xpetra::COMPLEX_INT_INT) |
| 310 |
xmlFileName = (xmlFileName != "" ? xmlFileName : "scaling-complex.xml"); |
| 311 |
else |
| 312 |
xmlFileName = (xmlFileName != "" ? xmlFileName : "scaling.xml"); |
| 313 |
Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<ParameterList>(¶mList), *comm); |
| 314 |
} |
| 315 |
|
| 316 |
if (inst == Xpetra::COMPLEX_INT_INT && dsolveType == "belos") { |
| 317 |
belosType = "gmres"; |
| 318 |
out << "WARNING: CG will not work with COMPLEX scalars, switching to GMRES" << std::endl; |
| 319 |
} |
| 320 |
|
| 321 |
#ifdef HAVE_MUELU_AMGX |
| 322 |
// Initialize AMGX |
| 323 |
MueLu::MueLu_AMGX_initialize(); |
| 324 |
MueLu::MueLu_AMGX_initialize_plugins(); |
| 325 |
#endif |
| 326 |
|
| 327 |
bool isDriver = paramList.isSublist("Run1"); |
| 328 |
if (isDriver) { |
| 329 |
// update galeriParameters with the values from the XML file |
| 330 |
ParameterList& realParams = galeriParameters.GetParameterList(); |
| 331 |
|
| 332 |
for (ParameterList::ConstIterator it = realParams.begin(); it != realParams.end(); it++) { |
| 333 |
const std::string& name = realParams.name(it); |
| 334 |
if (paramList.isParameter(name)) |
| 335 |
realParams.setEntry(name, paramList.getEntry(name)); |
| 336 |
} |
| 337 |
|
| 338 |
// Galeri updates (only works with Run1) |
| 339 |
if (paramList.sublist("Run1").isSublist("Galeri")) { |
| 340 |
ParameterList& moreParams = paramList.sublist("Run1").sublist("Galeri"); |
| 341 |
for (ParameterList::ConstIterator it = moreParams.begin(); it != moreParams.end(); it++) { |
| 342 |
const std::string& name = moreParams.name(it); |
| 343 |
if (moreParams.isParameter(name)) |
| 344 |
realParams.setEntry(name, moreParams.getEntry(name)); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
#ifdef HAVE_MPI |
| 350 |
// Generate the node-level communicator, if we want one |
| 351 |
Teuchos::RCP<const Teuchos::Comm<int> > nodeComm; |
| 352 |
int NodeId = comm->getRank(); |
| 353 |
if (provideNodeComm) { |
| 354 |
nodeComm = MueLu::GenerateNodeComm(comm, NodeId, provideNodeComm); |
| 355 |
// printf("DEBUG: Base rank %d => New, node %d, rank %d\n",comm->getRank(),NodeId,nodeComm->getRank()); |
| 356 |
} |
| 357 |
#endif |
| 358 |
|
| 359 |
// Retrieve matrix parameters (they may have been changed on the command line) |
| 360 |
// [for instance, if we changed matrix type from 2D to 3D we need to update nz] |
| 361 |
ParameterList galeriList = galeriParameters.GetParameterList(); |
| 362 |
|
| 363 |
// ========================================================================= |
| 364 |
// Problem construction |
| 365 |
// ========================================================================= |
| 366 |
std::ostringstream galeriStream; |
| 367 |
#ifdef HAVE_MUELU_OPENMP |
| 368 |
std::string node_name = Node::name(); |
| 369 |
if (!comm->getRank() && !node_name.compare("OpenMP/Wrapper")) |
| 370 |
galeriStream << "OpenMP Max Threads = " << omp_get_max_threads() << std::endl; |
| 371 |
#endif |
| 372 |
|
| 373 |
comm->barrier(); |
| 374 |
Teuchos::RCP<Teuchos::StackedTimer> stacked_timer; |
| 375 |
// Constructing the globalTimeMonitor should only be done if not using StackedTimer. |
| 376 |
// This is because if a StackedTimer is already active, globalTimer will be become a sub-timer of the root. |
| 377 |
RCP<TimeMonitor> globalTimeMonitor = Teuchos::null; |
| 378 |
if (useStackedTimer) { |
| 379 |
stacked_timer = rcp(new Teuchos::StackedTimer("MueLu_Driver")); |
| 380 |
Teuchos::TimeMonitor::setStackedTimer(stacked_timer); |
| 381 |
} else |
| 382 |
globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: S - Global Time"))); |
| 383 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 1 - Matrix Build"))); |
| 384 |
|
| 385 |
RCP<Matrix> A; |
| 386 |
RCP<const Map> map; |
| 387 |
RCP<RealValuedMultiVector> coordinates; |
| 388 |
RCP<Xpetra::MultiVector<SC, LO, GO, NO> > nullspace; |
| 389 |
RCP<Xpetra::MultiVector<SC, LO, GO, NO> > material; |
| 390 |
RCP<MultiVector> X; |
| 391 |
RCP<MultiVector> B; |
| 392 |
|
| 393 |
// Load the matrix off disk (or generate it via Galeri) |
| 394 |
MatrixLoad<SC, LO, GO, NO>(comm, lib, binaryFormat, matrixFile, rhsFile, rowMapFile, colMapFile, domainMapFile, rangeMapFile, coordFile, coordMapFile, nullFile, materialFile, map, A, coordinates, nullspace, material, X, B, numVectors, galeriParameters, xpetraParameters, galeriStream); |
| 395 |
comm->barrier(); |
| 396 |
tm = Teuchos::null; |
| 397 |
|
| 398 |
// Do equilibration if requested |
| 399 |
if (lib == Xpetra::UseTpetra) { |
| 400 |
equilibrateMatrix(A, equilibrate); |
| 401 |
} |
| 402 |
|
| 403 |
bool resetStackedTimer = false; |
| 404 |
if (paramList.isParameter("number of reruns")) |
| 405 |
numReruns = paramList.get<int>("number of reruns"); |
| 406 |
|
| 407 |
for (int rerunCount = 1; rerunCount <= numReruns; rerunCount++) { |
| 408 |
bool stop = false; |
| 409 |
ParameterList mueluList, runList; |
| 410 |
const bool mustAlreadyExist = true; |
| 411 |
if (isDriver) { |
| 412 |
runList = paramList.sublist("Run1", mustAlreadyExist); |
| 413 |
mueluList = runList.sublist("MueLu", mustAlreadyExist); |
| 414 |
} else { |
| 415 |
mueluList = paramList; |
| 416 |
stop = true; |
| 417 |
} |
| 418 |
|
| 419 |
if (nullspace.is_null()) { |
| 420 |
int blkSize = 1; |
| 421 |
if (mueluList.isSublist("Matrix")) { |
| 422 |
// Factory style parameter list |
| 423 |
const Teuchos::ParameterList& operatorList = paramList.sublist("Matrix"); |
| 424 |
if (operatorList.isParameter("PDE equations")) |
| 425 |
blkSize = operatorList.get<int>("PDE equations"); |
| 426 |
|
| 427 |
} else if (paramList.isParameter("number of equations")) { |
| 428 |
// Easy style parameter list |
| 429 |
blkSize = paramList.get<int>("number of equations"); |
| 430 |
} |
| 431 |
|
| 432 |
nullspace = MultiVectorFactory::Build(map, blkSize); |
| 433 |
for (int i = 0; i < blkSize; i++) { |
| 434 |
RCP<const Map> domainMap = A->getDomainMap(); |
| 435 |
GO indexBase = domainMap->getIndexBase(); |
| 436 |
|
| 437 |
ArrayRCP<SC> nsData = nullspace->getDataNonConst(i); |
| 438 |
for (int j = 0; j < nsData.size(); j++) { |
| 439 |
GO GID = domainMap->getGlobalElement(j) - indexBase; |
| 440 |
|
| 441 |
if ((GID - i) % blkSize == 0) |
| 442 |
nsData[j] = Teuchos::ScalarTraits<SC>::one(); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (material.is_null() && !coordinates.is_null()) { |
| 448 |
size_t dim = coordinates->getNumVectors(); |
| 449 |
if (tensorMaterialCoefficient) { |
| 450 |
material = MultiVectorFactory::Build(map, dim * dim); |
| 451 |
int k = 0; |
| 452 |
for (size_t i = 0; i < dim; ++i) |
| 453 |
for (size_t j = 0; j < dim; ++j) { |
| 454 |
if (i == j) |
| 455 |
material->getVectorNonConst(k)->putScalar(2.0 * Teuchos::ScalarTraits<SC>::one()); |
| 456 |
else |
| 457 |
material->getVectorNonConst(k)->putScalar(Teuchos::ScalarTraits<SC>::zero()); |
| 458 |
++k; |
| 459 |
} |
| 460 |
} else { |
| 461 |
material = MultiVectorFactory::Build(map, 1); |
| 462 |
material->putScalar(2.0 * Teuchos::ScalarTraits<SC>::one()); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// If doing user based block smoothing, read block information from file. |
| 467 |
// Must do this for both smoothers and coarse solvers |
| 468 |
|
| 469 |
readUserBlks<SC, LO, GO, NO>(userBlkFileName, "coarse", mueluList, A); |
| 470 |
readUserBlks<SC, LO, GO, NO>(userBlkFileName, "smoother", mueluList, A); |
| 471 |
|
| 472 |
int runCount = 1; |
| 473 |
int savedOut = -1; |
| 474 |
FILE* openedOut = NULL; |
| 475 |
do { |
| 476 |
solveType = dsolveType; |
| 477 |
tol = dtol; |
| 478 |
|
| 479 |
if (isDriver) { |
| 480 |
if (runList.isParameter("filename")) { |
| 481 |
// Redirect all output into a filename We have to redirect all output, |
| 482 |
// including printf's, therefore we cannot simply replace C++ cout |
| 483 |
// buffers, and have to use heavy machinary (dup2) |
| 484 |
std::string filename = runList.get<std::string>("filename"); |
| 485 |
if (rerunFilePrefix != "") |
| 486 |
filename = rerunFilePrefix + "_" + filename; |
| 487 |
if (rerunFileSuffix != "") |
| 488 |
filename += "_" + rerunFileSuffix; |
| 489 |
if (numReruns > 1) |
| 490 |
filename += "_run" + MueLu::toString(rerunCount); |
| 491 |
filename += (lib == Xpetra::UseEpetra ? ".epetra" : ".tpetra"); |
| 492 |
|
| 493 |
savedOut = dup(STDOUT_FILENO); |
| 494 |
openedOut = fopen(filename.c_str(), "w"); |
| 495 |
dup2(fileno(openedOut), STDOUT_FILENO); |
| 496 |
} |
| 497 |
if (runList.isParameter("solver")) solveType = runList.get<std::string>("solver"); |
| 498 |
if (runList.isParameter("tol")) tol = runList.get<double>("tol"); |
| 499 |
|
| 500 |
if (resetStackedTimer) { |
| 501 |
stacked_timer = rcp(new Teuchos::StackedTimer("MueLu_Driver")); |
| 502 |
Teuchos::TimeMonitor::setStackedTimer(stacked_timer); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
RCP<Teuchos::FancyOStream> fancy2 = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 507 |
Teuchos::FancyOStream& out2 = *fancy2; |
| 508 |
out2.setOutputToRootOnly(0); |
| 509 |
out2 << galeriStream.str(); |
| 510 |
|
| 511 |
// ========================================================================= |
| 512 |
// Preconditioner construction |
| 513 |
// ========================================================================= |
| 514 |
bool useAMGX = mueluList.isParameter("use external multigrid package") && (mueluList.get<std::string>("use external multigrid package") == "amgx"); |
| 515 |
bool useML = mueluList.isParameter("use external multigrid package") && (mueluList.get<std::string>("use external multigrid package") == "ml"); |
| 516 |
#ifdef HAVE_MPI |
| 517 |
if (provideNodeComm && !useAMGX && !useML) { |
| 518 |
Teuchos::ParameterList& userParamList = mueluList.sublist("user data"); |
| 519 |
userParamList.set("Node Comm", nodeComm); |
| 520 |
} |
| 521 |
#endif |
| 522 |
RCP<Hierarchy> H; |
| 523 |
RCP<Operator> Prec; |
| 524 |
// Build the preconditioner numRebuilds+1 times |
| 525 |
if (solvePreconditioned) { |
| 526 |
out2 << "*********** MueLu ParameterList ***********" << std::endl; |
| 527 |
out2 << mueluList; |
| 528 |
out2 << "*******************************************" << std::endl; |
| 529 |
|
| 530 |
MUELU_SWITCH_TIME_MONITOR(tm, "Driver: 2 - MueLu Setup"); |
| 531 |
PreconditionerSetup(A, coordinates, nullspace, material, mueluList, profileSetup, useAMGX, useML, setNullSpace, numRebuilds, H, Prec); |
| 532 |
} |
| 533 |
comm->barrier(); |
| 534 |
tm = Teuchos::null; |
| 535 |
|
| 536 |
size_t mem = get_current_memory_usage(); |
| 537 |
out2 << "Memory use after preconditioner setup (GB): " << (mem / 1024.0 / 1024.0) << std::endl; |
| 538 |
|
| 539 |
// ========================================================================= |
| 540 |
// System solution (Ax = b) |
| 541 |
// ========================================================================= |
| 542 |
try { |
| 543 |
comm->barrier(); |
| 544 |
if (writeMatricesOPT > -2) { |
| 545 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 3.5 - Matrix output"))); |
| 546 |
H->Write(writeMatricesOPT, writeMatricesOPT); |
| 547 |
if (writeMatricesOPT == 0 || writeMatricesOPT == -1) { |
| 548 |
Xpetra::IO<SC, LO, GO, NO>::Write("b_0.m", *B); |
| 549 |
} |
| 550 |
tm = Teuchos::null; |
| 551 |
} |
| 552 |
|
| 553 |
// Solve the system numResolves+1 times |
| 554 |
SystemSolve(A, X, B, H, Prec, out2, solveType, belosType, profileSolve, useAMGX, useML, cacheSize, numResolves, scaleResidualHist, solvePreconditioned, maxIts, tol, computeCondEst, enforceBoundaryConditionsOnInitialGuess); |
| 555 |
|
| 556 |
comm->barrier(); |
| 557 |
} catch (const std::exception& e) { |
| 558 |
if (isDriver) |
| 559 |
out2 << "MueLu_Driver: solver crashed w/ message:" << e.what() << std::endl; |
| 560 |
else |
| 561 |
throw; |
| 562 |
} |
| 563 |
|
| 564 |
tm = Teuchos::null; |
| 565 |
|
| 566 |
// If we want Level-specific performance model diagnostics, now is the time! |
| 567 |
if ((levelPerformanceModel == "yes" || levelPerformanceModel == "verbose") && !H.is_null()) { |
| 568 |
for (int i = 0; i < H->GetNumLevels(); i++) { |
| 569 |
RCP<Level> level = H->GetLevel(i); |
| 570 |
try { |
| 571 |
RCP<Matrix> A_level = level->Get<RCP<Matrix> >("A"); |
| 572 |
std::string level_name = std::string("Level-") + std::to_string(i) + std::string(": "); |
| 573 |
std::vector<const char*> timers; // MueLu: Laplace2D: Hierarchy: Solve (level=0) |
| 574 |
MueLu::report_spmv_performance_models<Matrix>(A_level, 100, timers, globalTimeMonitor, level_name, levelPerformanceModel == "verbose"); |
| 575 |
} catch (...) { |
| 576 |
; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
globalTimeMonitor = Teuchos::null; |
| 582 |
if (useStackedTimer) |
| 583 |
resetStackedTimer = true; |
| 584 |
|
| 585 |
if (printTimings) { |
| 586 |
RCP<ParameterList> reportParams = rcp(new ParameterList); |
| 587 |
if (timingsFormat == "yaml") { |
| 588 |
reportParams->set("Report format", "YAML"); // "Table" or "YAML" |
| 589 |
reportParams->set("YAML style", "compact"); // "spacious" or "compact" |
| 590 |
} |
| 591 |
reportParams->set("How to merge timer sets", "Union"); |
| 592 |
reportParams->set("alwaysWriteLocal", false); |
| 593 |
reportParams->set("writeGlobalStats", true); |
| 594 |
reportParams->set("writeZeroTimers", false); |
| 595 |
// FIXME: no "ignoreZeroTimers" |
| 596 |
|
| 597 |
const std::string filter = ""; |
| 598 |
|
| 599 |
if (useStackedTimer) { |
| 600 |
stacked_timer->stopBaseTimer(); |
| 601 |
Teuchos::StackedTimer::OutputOptions options; |
| 602 |
options.output_fraction = options.output_histogram = options.output_minmax = true; |
| 603 |
stacked_timer->report(out2, comm, options); |
| 604 |
auto xmlOut = stacked_timer->reportWatchrXML(watchrProblemName, comm); |
| 605 |
if (xmlOut.length()) |
| 606 |
std::cout << "\nAlso created Watchr performance report " << xmlOut << '\n'; |
| 607 |
} else { |
| 608 |
std::ios_base::fmtflags ff(out2.flags()); |
| 609 |
if (timingsFormat == "table-fixed") |
| 610 |
out2 << std::fixed; |
| 611 |
else |
| 612 |
out2 << std::scientific; |
| 613 |
TimeMonitor::report(comm.ptr(), out, filter, reportParams); |
| 614 |
out2 << std::setiosflags(ff); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
TimeMonitor::clearCounters(); |
| 619 |
out2 << std::endl; |
| 620 |
|
| 621 |
if (isDriver) { |
| 622 |
try { |
| 623 |
runList = paramList.sublist("Run" + MueLu::toString(++runCount), mustAlreadyExist); |
| 624 |
mueluList = runList.sublist("MueLu", mustAlreadyExist); |
| 625 |
} catch (Teuchos::Exceptions::InvalidParameterName&) { |
| 626 |
stop = true; |
| 627 |
} |
| 628 |
} |
| 629 |
fflush(NULL); |
| 630 |
comm->barrier(); |
| 631 |
} while (!stop); |
| 632 |
|
| 633 |
// Cleanup Output |
| 634 |
if (openedOut != NULL) { |
| 635 |
TEUCHOS_ASSERT(savedOut >= 0); |
| 636 |
dup2(savedOut, STDOUT_FILENO); |
| 637 |
fclose(openedOut); |
| 638 |
openedOut = NULL; |
| 639 |
} |
| 640 |
|
| 641 |
} // end reruns |
| 642 |
|
| 643 |
if (solFile != "") |
| 644 |
Xpetra::IO<SC, LO, GO, Node>::Write(solFile, *X); |
| 645 |
|
| 646 |
#ifdef HAVE_MUELU_AMGX |
| 647 |
// Finalize AMGX |
| 648 |
MueLu::MueLu_AMGX_finalize_plugins(); |
| 649 |
MueLu::MueLu_AMGX_finalize(); |
| 650 |
#endif |
| 651 |
|
| 652 |
return EXIT_SUCCESS; |
| 653 |
} |
| 654 |
|
| 655 |
//- -- -------------------------------------------------------- |
| 656 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 657 |
#include "MueLu_Test_ETI.hpp" |
| 658 |
|
| 659 |
int main(int argc, char* argv[]) { |
| 660 |
return Automatic_Test_ETI(argc, argv); |
| 661 |
} |