| 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 <cstdio> |
| 11 |
#include <unistd.h> |
| 12 |
#include <iostream> |
| 13 |
|
| 14 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 15 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 16 |
#include <Teuchos_ArrayRCP.hpp> |
| 17 |
|
| 18 |
#include "MueLu.hpp" |
| 19 |
|
| 20 |
#include <Tpetra_CrsMatrix.hpp> |
| 21 |
#include <Tpetra_Core.hpp> |
| 22 |
#include <MatrixMarket_Tpetra.hpp> |
| 23 |
|
| 24 |
#ifdef HAVE_MUELU_STRATIMIKOS |
| 25 |
#include <Stratimikos_LinearSolverBuilder.hpp> |
| 26 |
#include <Stratimikos_MueLuHelpers.hpp> |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef HAVE_MUELU_TEKO |
| 30 |
#include <Teko_InverseFactory.hpp> |
| 31 |
#include <Teko_InverseLibrary.hpp> |
| 32 |
#include <Teko_InvLSCStrategy.hpp> |
| 33 |
#include <Teko_LSCPreconditionerFactory.hpp> |
| 34 |
#include <Teko_SIMPLEPreconditionerFactory.hpp> |
| 35 |
#include <Teko_Utilities.hpp> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <Thyra_DefaultPreconditioner.hpp> |
| 39 |
#include <Thyra_DefaultScaledAdjointLinearOp.hpp> |
| 40 |
#include <Thyra_LinearOpWithSolveBase.hpp> |
| 41 |
#include <Thyra_LinearOpWithSolveBase.hpp> |
| 42 |
#include <Thyra_LinearOpWithSolveFactoryBase.hpp> |
| 43 |
#include <Thyra_LinearOpWithSolveFactoryHelpers.hpp> |
| 44 |
#include <Thyra_MueLuPreconditionerFactory.hpp> |
| 45 |
#include <Thyra_MueLuTpetraQ2Q1PreconditionerFactory.hpp> |
| 46 |
#include <Thyra_MultiVectorStdOps.hpp> |
| 47 |
#include <Thyra_PreconditionerFactoryHelpers.hpp> |
| 48 |
#include <Thyra_SolveSupportTypes.hpp> |
| 49 |
#include <Thyra_TpetraLinearOp.hpp> |
| 50 |
#include <Thyra_TpetraVectorSpace.hpp> |
| 51 |
#include <Thyra_VectorBase.hpp> |
| 52 |
#include <Thyra_VectorBase.hpp> |
| 53 |
#include <Thyra_VectorStdOps.hpp> |
| 54 |
|
| 55 |
#include <BelosConfigDefs.hpp> |
| 56 |
#include <BelosLinearProblem.hpp> |
| 57 |
#include <BelosSolverFactory.hpp> |
| 58 |
#include <BelosTpetraAdapter.hpp> |
| 59 |
|
| 60 |
#include <Xpetra_MapFactory.hpp> |
| 61 |
#include <Xpetra_IO.hpp> |
| 62 |
|
| 63 |
#include "MueLu_Utilities.hpp" |
| 64 |
|
| 65 |
// FIXME |
| 66 |
#include "MueLu_UseDefaultTypes.hpp" |
| 67 |
|
| 68 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 69 |
Teuchos::RCP<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > |
| 70 |
ReadBinary(const std::string& fileName, const Teuchos::RCP<const Teuchos::Comm<int> >& comm) { |
| 71 |
typedef Scalar SC; |
| 72 |
typedef LocalOrdinal LO; |
| 73 |
typedef GlobalOrdinal GO; |
| 74 |
typedef Node NO; |
| 75 |
TEUCHOS_TEST_FOR_EXCEPTION(comm->getSize() != 1, MueLu::Exceptions::RuntimeError, "Serial read only"); |
| 76 |
|
| 77 |
std::ifstream ifs(fileName.c_str(), std::ios::binary); |
| 78 |
TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), MueLu::Exceptions::RuntimeError, "Can not read \"" << fileName << "\""); |
| 79 |
|
| 80 |
int m, n, nnz; |
| 81 |
ifs.read(reinterpret_cast<char*>(&m), sizeof(m)); |
| 82 |
ifs.read(reinterpret_cast<char*>(&n), sizeof(n)); |
| 83 |
ifs.read(reinterpret_cast<char*>(&nnz), sizeof(nnz)); |
| 84 |
|
| 85 |
typedef Tpetra::Map<LO, GO, NO> tMap; |
| 86 |
typedef Tpetra::CrsMatrix<SC, LO, GO, NO> tCrsMatrix; |
| 87 |
|
| 88 |
GO indexBase = 0; |
| 89 |
Teuchos::RCP<tMap> rowMap = rcp(new tMap(m, indexBase, comm)), rangeMap = rowMap; |
| 90 |
Teuchos::RCP<tMap> colMap = rcp(new tMap(n, indexBase, comm)), domainMap = colMap; |
| 91 |
; |
| 92 |
Teuchos::RCP<tCrsMatrix> A = rcp(new tCrsMatrix(rowMap, colMap, 9)); |
| 93 |
|
| 94 |
TEUCHOS_TEST_FOR_EXCEPTION(sizeof(int) != sizeof(GO), MueLu::Exceptions::RuntimeError, "Incompatible sizes"); |
| 95 |
|
| 96 |
Teuchos::Array<GO> inds; |
| 97 |
Teuchos::Array<SC> vals; |
| 98 |
for (int i = 0; i < m; i++) { |
| 99 |
int row, rownnz; |
| 100 |
ifs.read(reinterpret_cast<char*>(&row), sizeof(row)); |
| 101 |
ifs.read(reinterpret_cast<char*>(&rownnz), sizeof(rownnz)); |
| 102 |
inds.resize(rownnz); |
| 103 |
vals.resize(rownnz); |
| 104 |
for (int j = 0; j < rownnz; j++) { |
| 105 |
int index; |
| 106 |
ifs.read(reinterpret_cast<char*>(&index), sizeof(index)); |
| 107 |
inds[j] = Teuchos::as<GO>(index); |
| 108 |
} |
| 109 |
for (int j = 0; j < rownnz; j++) { |
| 110 |
double value; |
| 111 |
ifs.read(reinterpret_cast<char*>(&value), sizeof(value)); |
| 112 |
vals[j] = Teuchos::as<SC>(value); |
| 113 |
} |
| 114 |
A->insertGlobalValues(row, inds, vals); |
| 115 |
} |
| 116 |
|
| 117 |
A->fillComplete(domainMap, rangeMap); |
| 118 |
|
| 119 |
return A; |
| 120 |
} |
| 121 |
|
| 122 |
int main(int argc, char* argv[]) { |
| 123 |
#include <MueLu_UseShortNames.hpp> |
| 124 |
|
| 125 |
using Teuchos::Array; |
| 126 |
using Teuchos::ArrayRCP; |
| 127 |
using Teuchos::as; |
| 128 |
using Teuchos::null; |
| 129 |
using Teuchos::ParameterList; |
| 130 |
using Teuchos::RCP; |
| 131 |
using Teuchos::rcp; |
| 132 |
using Teuchos::rcp_dynamic_cast; |
| 133 |
using Teuchos::TimeMonitor; |
| 134 |
using Thyra::tpetraVectorSpace; |
| 135 |
using Tpetra::MatrixMarket::Reader; |
| 136 |
|
| 137 |
// ========================================================================= |
| 138 |
// MPI initialization using Teuchos |
| 139 |
// ========================================================================= |
| 140 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); |
| 141 |
|
| 142 |
bool success = true; |
| 143 |
bool verbose = true; |
| 144 |
try { |
| 145 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 146 |
|
| 147 |
TEUCHOS_TEST_FOR_EXCEPTION(comm->getSize() > 1, MueLu::Exceptions::RuntimeError, |
| 148 |
"For now, Q2Q1 only works in serial. We are working on the parallel implementation."); |
| 149 |
|
| 150 |
// ========================================================================= |
| 151 |
// Parameter initialization |
| 152 |
// ========================================================================= |
| 153 |
Teuchos::CommandLineProcessor clp(false); |
| 154 |
|
| 155 |
Xpetra::Parameters xpetraParameters(clp); |
| 156 |
|
| 157 |
// configure problem |
| 158 |
std::string prefix = "./Q2Q1_9x9_"; |
| 159 |
clp.setOption("prefix", &prefix, "prefix for data files"); |
| 160 |
std::string rhs = ""; |
| 161 |
clp.setOption("rhs", &rhs, "rhs"); |
| 162 |
|
| 163 |
// configure run |
| 164 |
std::string xmlFileName = "driver.xml"; |
| 165 |
clp.setOption("xml", &xmlFileName, "read parameters from a file [default = 'driver.xml']"); |
| 166 |
double tol = 1e-8; |
| 167 |
clp.setOption("tol", &tol, "solver convergence tolerance"); |
| 168 |
std::string type = "unstructured"; |
| 169 |
clp.setOption("type", &type, "structured/unstructured"); |
| 170 |
int use9ptPatA = 1; |
| 171 |
clp.setOption("use9pt", &use9ptPatA, "use 9-point stencil matrix for velocity prolongator construction"); |
| 172 |
int useFilters = 1; |
| 173 |
clp.setOption("usefilters", &useFilters, "use filters on A and BB^T"); |
| 174 |
|
| 175 |
double tau_1 = 0.06; |
| 176 |
clp.setOption("tau_1", &tau_1, "tau_1 parameter from paper (for matrix filtering)"); |
| 177 |
double tau_2 = sqrt(0.0015); |
| 178 |
clp.setOption("tau_2", &tau_2, "tau_2 parameter from paper (for mid point detection)"); |
| 179 |
|
| 180 |
int binary = 0; |
| 181 |
clp.setOption("binary", &binary, "read matrix in binary format"); |
| 182 |
|
| 183 |
// configure misc |
| 184 |
int printTimings = 0; |
| 185 |
clp.setOption("timings", &printTimings, "print timings to screen"); |
| 186 |
|
| 187 |
switch (clp.parse(argc, argv)) { |
| 188 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 189 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 190 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 191 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 192 |
} |
| 193 |
|
| 194 |
typedef Tpetra::CrsMatrix<SC, LO, GO, NO> tCrsMatrix; |
| 195 |
typedef Tpetra::Operator<SC, LO, GO, NO> tOperator; |
| 196 |
typedef Tpetra::MultiVector<SC, LO, GO, NO> tMultiVector; |
| 197 |
typedef Tpetra::Map<LO, GO, NO> tMap; |
| 198 |
typedef Thyra::TpetraVectorSpace<SC, LO, GO, NO> THTP_Vs; |
| 199 |
|
| 200 |
// Read data from files |
| 201 |
RCP<tOperator> A11, A119Pt, A21, A12; |
| 202 |
RCP<tMultiVector> Vcoords, Pcoords; |
| 203 |
ArrayRCP<LO> p2vMap; |
| 204 |
|
| 205 |
std::string filename; |
| 206 |
try { |
| 207 |
if (!binary) { |
| 208 |
filename = prefix + "A.mm"; |
| 209 |
A11 = Reader<tCrsMatrix>::readSparseFile(filename.c_str(), comm); |
| 210 |
A119Pt = A11; |
| 211 |
if (use9ptPatA) { |
| 212 |
filename = prefix + "AForPat.mm"; |
| 213 |
A119Pt = Reader<tCrsMatrix>::readSparseFile(filename.c_str(), comm); |
| 214 |
} |
| 215 |
} else { |
| 216 |
filename = prefix + "A.dat"; |
| 217 |
A11 = ReadBinary<SC, LO, GO, NO>(filename.c_str(), comm); |
| 218 |
A119Pt = A11; |
| 219 |
if (use9ptPatA) { |
| 220 |
filename = prefix + "AForPat.dat"; |
| 221 |
A119Pt = ReadBinary<SC, LO, GO, NO>(filename.c_str(), comm); |
| 222 |
} |
| 223 |
} |
| 224 |
filename = prefix + "B.mm"; |
| 225 |
A21 = Reader<tCrsMatrix>::readSparseFile(filename.c_str(), comm); |
| 226 |
filename = prefix + "Bt.mm"; |
| 227 |
A12 = Reader<tCrsMatrix>::readSparseFile(filename.c_str(), comm); |
| 228 |
|
| 229 |
RCP<const tMap> cmap1 = A11->getDomainMap(), cmap2 = A12->getDomainMap(); |
| 230 |
filename = prefix + "VelCoords.mm"; |
| 231 |
Vcoords = Reader<tCrsMatrix>::readDenseFile(filename.c_str(), comm, cmap1); |
| 232 |
filename = prefix + "PresCoords.mm"; |
| 233 |
Pcoords = Reader<tCrsMatrix>::readDenseFile(filename.c_str(), comm, cmap2); |
| 234 |
|
| 235 |
// For now, we assume that p2v maps local pressure DOF to a local x-velocity DOF |
| 236 |
filename = prefix + "p2vMap.mm"; |
| 237 |
ArrayRCP<const SC> slop = Xpetra::IO<SC, LO, GO, NO>::ReadMultiVector(filename.c_str(), |
| 238 |
Xpetra::toXpetra(A21->getRangeMap())) |
| 239 |
->getData(0); |
| 240 |
p2vMap.resize(slop.size()); |
| 241 |
for (int i = 0; i < slop.size(); i++) |
| 242 |
p2vMap[i] = as<LO>(slop[i]); |
| 243 |
} catch (...) { |
| 244 |
throw MueLu::Exceptions::RuntimeError("Error reading file: \"" + filename + "\""); |
| 245 |
} |
| 246 |
|
| 247 |
// Convert matrices to Teko/Thyra operators |
| 248 |
RCP<const THTP_Vs> domain11 = tpetraVectorSpace<SC>(A11->getDomainMap()); |
| 249 |
RCP<const THTP_Vs> domain12 = tpetraVectorSpace<SC>(A12->getDomainMap()); |
| 250 |
RCP<const THTP_Vs> domain21 = tpetraVectorSpace<SC>(A21->getDomainMap()); |
| 251 |
|
| 252 |
RCP<const THTP_Vs> range11 = tpetraVectorSpace<SC>(A11->getRangeMap()); |
| 253 |
RCP<const THTP_Vs> range12 = tpetraVectorSpace<SC>(A12->getRangeMap()); |
| 254 |
RCP<const THTP_Vs> range21 = tpetraVectorSpace<SC>(A21->getRangeMap()); |
| 255 |
|
| 256 |
Teko::LinearOp thA11 = Thyra::tpetraLinearOp<double>(range11, domain11, A11); |
| 257 |
Teko::LinearOp thA12 = Thyra::tpetraLinearOp<double>(range12, domain12, A12); |
| 258 |
Teko::LinearOp thA21 = Thyra::tpetraLinearOp<double>(range21, domain21, A21); |
| 259 |
Teko::LinearOp thA11_9Pt = Thyra::tpetraLinearOp<double>(range11, domain11, A119Pt); |
| 260 |
|
| 261 |
// Bang together the parameter list. Right now, all the MueLu details is |
| 262 |
// hardwired in the MueLu-TpetraQ2Q1 classes. We probably want to switch |
| 263 |
// things so that several of these hardwired features could be modified |
| 264 |
// via parameter lists. |
| 265 |
|
| 266 |
RCP<ParameterList> stratimikosList = rcp(new ParameterList); |
| 267 |
stratimikosList->set("Linear Solver Type", "Belos"); |
| 268 |
stratimikosList->set("Preconditioner Type", "MueLu-TpetraQ2Q1"); |
| 269 |
|
| 270 |
ParameterList& BelosList = stratimikosList->sublist("Linear Solver Types").sublist("Belos"); |
| 271 |
BelosList.set("Solver Type", "Block GMRES"); // FIXME: should it be "Pseudo Block GMRES"? |
| 272 |
BelosList.sublist("VerboseObject").set("Verbosity Level", "low"); // this is needed, as otherwise Stratimikos ignores Belos output |
| 273 |
|
| 274 |
ParameterList& GmresDetails = BelosList.sublist("Solver Types").sublist("Block GMRES"); |
| 275 |
GmresDetails.set("Maximum Iterations", 100); |
| 276 |
GmresDetails.set("Convergence Tolerance", tol); |
| 277 |
GmresDetails.set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); |
| 278 |
GmresDetails.set("Output Frequency", 1); |
| 279 |
GmresDetails.set("Output Style", Belos::Brief); |
| 280 |
|
| 281 |
ParameterList& Q2Q1List = stratimikosList->sublist("Preconditioner Types").sublist("MueLu-TpetraQ2Q1"); |
| 282 |
Q2Q1List.set("useFilters", useFilters); |
| 283 |
Q2Q1List.set("tau_1", tau_1); |
| 284 |
Q2Q1List.set("tau_2", tau_2); |
| 285 |
Q2Q1List.set("Velcoords", Vcoords); |
| 286 |
Q2Q1List.set("Prescoords", Pcoords); |
| 287 |
Q2Q1List.set("p2vMap", p2vMap); |
| 288 |
Q2Q1List.set("A11", thA11); |
| 289 |
Q2Q1List.set("A12", thA12); |
| 290 |
Q2Q1List.set("A21", thA21); |
| 291 |
Q2Q1List.set("A11_9Pt", thA11_9Pt); |
| 292 |
|
| 293 |
Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<ParameterList>(&Q2Q1List), *comm); |
| 294 |
|
| 295 |
std::cout << "Input parameters: " << *stratimikosList << std::endl; |
| 296 |
|
| 297 |
// Stratimikos vodou |
| 298 |
typedef Thyra::PreconditionerFactoryBase<SC> Base; |
| 299 |
typedef Thyra::LinearOpWithSolveFactoryBase<SC> LOWSFB; |
| 300 |
typedef Thyra::LinearOpWithSolveBase<SC> LOWSB; |
| 301 |
typedef Thyra::MultiVectorBase<SC> TH_Mvb; |
| 302 |
|
| 303 |
Stratimikos::LinearSolverBuilder<SC> linearSolverBuilder; |
| 304 |
|
| 305 |
// Thyra::addMueLuToStratimikosBuilder(linearSolverBuilder); |
| 306 |
|
| 307 |
Stratimikos::enableMueLu<SC, LO, GO, NO>(linearSolverBuilder); // Epetra |
| 308 |
Stratimikos::enableMueLuTpetraQ2Q1<SC, LO, GO, NO>(linearSolverBuilder, "MueLu-TpetraQ2Q1"); // Tpetra |
| 309 |
|
| 310 |
linearSolverBuilder.setParameterList(stratimikosList); |
| 311 |
RCP<const LOWSFB> lowsFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder); |
| 312 |
RCP<LOWSB> nsA = lowsFactory->createOp(); |
| 313 |
|
| 314 |
// I've hacked together a big matrix that does not use strided maps by |
| 315 |
// simply reading the data again. Normally, this would be supplied by Eric |
| 316 |
// Cyr and would be Teko operators. |
| 317 |
|
| 318 |
int numElem = A12->getRangeMap()->getLocalNumElements() + A21->getRangeMap()->getLocalNumElements(); |
| 319 |
RCP<const tMap> fullMap = toTpetra(MapFactory::createUniformContigMap(Xpetra::UseTpetra, numElem, comm)); |
| 320 |
|
| 321 |
RCP<tOperator> A; |
| 322 |
if (!binary) |
| 323 |
A = Reader<tCrsMatrix>::readSparseFile((prefix + "BigA.mm").c_str(), fullMap, fullMap, fullMap, fullMap, true, true, false); |
| 324 |
else |
| 325 |
A = ReadBinary<SC, LO, GO, NO>((prefix + "BigA.dat").c_str(), comm); |
| 326 |
|
| 327 |
const RCP<Thyra::LinearOpBase<SC> > thA = Thyra::createLinearOp(A); |
| 328 |
Thyra::initializeOp<SC>(*lowsFactory, thA, nsA.ptr()); |
| 329 |
|
| 330 |
RCP<tMultiVector> tX = Tpetra::createVector<SC>(fullMap), tB; |
| 331 |
if (rhs == "") { |
| 332 |
tX->randomize(); |
| 333 |
|
| 334 |
tB = Tpetra::createVector<SC>(fullMap); |
| 335 |
A->apply(*tX, *tB); |
| 336 |
} else { |
| 337 |
typedef Tpetra::MatrixMarket::Reader<tCrsMatrix> reader_type; |
| 338 |
tB = reader_type::readDenseFile(rhs.c_str(), fullMap->getComm(), fullMap); |
| 339 |
} |
| 340 |
|
| 341 |
tX->putScalar(0.0); |
| 342 |
|
| 343 |
// Set the initial guess Dirichlet points to the proper value. |
| 344 |
// This step is pretty important as the preconditioner may return zero at Dirichlet points |
| 345 |
ArrayRCP<const bool> dirBCs = Utilities::DetectDirichletRows(*MueLu::TpetraCrs_To_XpetraMatrix(rcp_dynamic_cast<tCrsMatrix>(A))); |
| 346 |
ArrayRCP<SC> tXdata = tX->getDataNonConst(0); |
| 347 |
ArrayRCP<const SC> tBdata = tB->getData(0); |
| 348 |
for (LO i = 0; i < tXdata.size(); i++) |
| 349 |
if (dirBCs[i]) |
| 350 |
tXdata[i] = tBdata[i]; |
| 351 |
|
| 352 |
RCP<TH_Mvb> sX = Thyra::createMultiVector(tX); |
| 353 |
RCP<TH_Mvb> sB = Thyra::createMultiVector(tB); |
| 354 |
|
| 355 |
Thyra::SolveStatus<SC> solveStatus = Thyra::solve(*nsA, Thyra::NOTRANS, *sB, sX.ptr()); |
| 356 |
|
| 357 |
if (printTimings) { |
| 358 |
const bool alwaysWriteLocal = false; |
| 359 |
const bool writeGlobalStats = true; |
| 360 |
const bool writeZeroTimers = false; |
| 361 |
const bool ignoreZeroTimers = true; |
| 362 |
const std::string filter = ""; |
| 363 |
TimeMonitor::summarize(comm.ptr(), std::cout, alwaysWriteLocal, writeGlobalStats, |
| 364 |
writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers); |
| 365 |
} |
| 366 |
} |
| 367 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 368 |
|
| 369 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 370 |
} |