// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include #include #include #include #include #include #include "MueLu.hpp" #include #include #include #ifdef HAVE_MUELU_STRATIMIKOS #include #include #endif #ifdef HAVE_MUELU_TEKO #include #include #include #include #include #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MueLu_Utilities.hpp" // FIXME #include "MueLu_UseDefaultTypes.hpp" template Teuchos::RCP > ReadBinary(const std::string& fileName, const Teuchos::RCP >& comm) { typedef Scalar SC; typedef LocalOrdinal LO; typedef GlobalOrdinal GO; typedef Node NO; TEUCHOS_TEST_FOR_EXCEPTION(comm->getSize() != 1, MueLu::Exceptions::RuntimeError, "Serial read only"); std::ifstream ifs(fileName.c_str(), std::ios::binary); TEUCHOS_TEST_FOR_EXCEPTION(!ifs.good(), MueLu::Exceptions::RuntimeError, "Can not read \"" << fileName << "\""); int m, n, nnz; ifs.read(reinterpret_cast(&m), sizeof(m)); ifs.read(reinterpret_cast(&n), sizeof(n)); ifs.read(reinterpret_cast(&nnz), sizeof(nnz)); typedef Tpetra::Map tMap; typedef Tpetra::CrsMatrix tCrsMatrix; GO indexBase = 0; Teuchos::RCP rowMap = rcp(new tMap(m, indexBase, comm)), rangeMap = rowMap; Teuchos::RCP colMap = rcp(new tMap(n, indexBase, comm)), domainMap = colMap; ; Teuchos::RCP A = rcp(new tCrsMatrix(rowMap, colMap, 9)); TEUCHOS_TEST_FOR_EXCEPTION(sizeof(int) != sizeof(GO), MueLu::Exceptions::RuntimeError, "Incompatible sizes"); Teuchos::Array inds; Teuchos::Array vals; for (int i = 0; i < m; i++) { int row, rownnz; ifs.read(reinterpret_cast(&row), sizeof(row)); ifs.read(reinterpret_cast(&rownnz), sizeof(rownnz)); inds.resize(rownnz); vals.resize(rownnz); for (int j = 0; j < rownnz; j++) { int index; ifs.read(reinterpret_cast(&index), sizeof(index)); inds[j] = Teuchos::as(index); } for (int j = 0; j < rownnz; j++) { double value; ifs.read(reinterpret_cast(&value), sizeof(value)); vals[j] = Teuchos::as(value); } A->insertGlobalValues(row, inds, vals); } A->fillComplete(domainMap, rangeMap); return A; } int main(int argc, char* argv[]) { #include using Teuchos::Array; using Teuchos::ArrayRCP; using Teuchos::as; using Teuchos::null; using Teuchos::ParameterList; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcp_dynamic_cast; using Teuchos::TimeMonitor; using Thyra::tpetraVectorSpace; using Tpetra::MatrixMarket::Reader; // ========================================================================= // MPI initialization using Teuchos // ========================================================================= Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); bool success = true; bool verbose = true; try { RCP > comm = Teuchos::DefaultComm::getComm(); TEUCHOS_TEST_FOR_EXCEPTION(comm->getSize() > 1, MueLu::Exceptions::RuntimeError, "For now, Q2Q1 only works in serial. We are working on the parallel implementation."); // ========================================================================= // Parameter initialization // ========================================================================= Teuchos::CommandLineProcessor clp(false); Xpetra::Parameters xpetraParameters(clp); // configure problem std::string prefix = "./Q2Q1_9x9_"; clp.setOption("prefix", &prefix, "prefix for data files"); std::string rhs = ""; clp.setOption("rhs", &rhs, "rhs"); // configure run std::string xmlFileName = "driver.xml"; clp.setOption("xml", &xmlFileName, "read parameters from a file [default = 'driver.xml']"); double tol = 1e-8; clp.setOption("tol", &tol, "solver convergence tolerance"); std::string type = "unstructured"; clp.setOption("type", &type, "structured/unstructured"); int use9ptPatA = 1; clp.setOption("use9pt", &use9ptPatA, "use 9-point stencil matrix for velocity prolongator construction"); int useFilters = 1; clp.setOption("usefilters", &useFilters, "use filters on A and BB^T"); double tau_1 = 0.06; clp.setOption("tau_1", &tau_1, "tau_1 parameter from paper (for matrix filtering)"); double tau_2 = sqrt(0.0015); clp.setOption("tau_2", &tau_2, "tau_2 parameter from paper (for mid point detection)"); int binary = 0; clp.setOption("binary", &binary, "read matrix in binary format"); // configure misc int printTimings = 0; clp.setOption("timings", &printTimings, "print timings to screen"); switch (clp.parse(argc, argv)) { case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; case Teuchos::CommandLineProcessor::PARSE_ERROR: case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; } typedef Tpetra::CrsMatrix tCrsMatrix; typedef Tpetra::Operator tOperator; typedef Tpetra::MultiVector tMultiVector; typedef Tpetra::Map tMap; typedef Thyra::TpetraVectorSpace THTP_Vs; // Read data from files RCP A11, A119Pt, A21, A12; RCP Vcoords, Pcoords; ArrayRCP p2vMap; std::string filename; try { if (!binary) { filename = prefix + "A.mm"; A11 = Reader::readSparseFile(filename.c_str(), comm); A119Pt = A11; if (use9ptPatA) { filename = prefix + "AForPat.mm"; A119Pt = Reader::readSparseFile(filename.c_str(), comm); } } else { filename = prefix + "A.dat"; A11 = ReadBinary(filename.c_str(), comm); A119Pt = A11; if (use9ptPatA) { filename = prefix + "AForPat.dat"; A119Pt = ReadBinary(filename.c_str(), comm); } } filename = prefix + "B.mm"; A21 = Reader::readSparseFile(filename.c_str(), comm); filename = prefix + "Bt.mm"; A12 = Reader::readSparseFile(filename.c_str(), comm); RCP cmap1 = A11->getDomainMap(), cmap2 = A12->getDomainMap(); filename = prefix + "VelCoords.mm"; Vcoords = Reader::readDenseFile(filename.c_str(), comm, cmap1); filename = prefix + "PresCoords.mm"; Pcoords = Reader::readDenseFile(filename.c_str(), comm, cmap2); // For now, we assume that p2v maps local pressure DOF to a local x-velocity DOF filename = prefix + "p2vMap.mm"; ArrayRCP slop = Xpetra::IO::ReadMultiVector(filename.c_str(), Xpetra::toXpetra(A21->getRangeMap())) ->getData(0); p2vMap.resize(slop.size()); for (int i = 0; i < slop.size(); i++) p2vMap[i] = as(slop[i]); } catch (...) { throw MueLu::Exceptions::RuntimeError("Error reading file: \"" + filename + "\""); } // Convert matrices to Teko/Thyra operators RCP domain11 = tpetraVectorSpace(A11->getDomainMap()); RCP domain12 = tpetraVectorSpace(A12->getDomainMap()); RCP domain21 = tpetraVectorSpace(A21->getDomainMap()); RCP range11 = tpetraVectorSpace(A11->getRangeMap()); RCP range12 = tpetraVectorSpace(A12->getRangeMap()); RCP range21 = tpetraVectorSpace(A21->getRangeMap()); Teko::LinearOp thA11 = Thyra::tpetraLinearOp(range11, domain11, A11); Teko::LinearOp thA12 = Thyra::tpetraLinearOp(range12, domain12, A12); Teko::LinearOp thA21 = Thyra::tpetraLinearOp(range21, domain21, A21); Teko::LinearOp thA11_9Pt = Thyra::tpetraLinearOp(range11, domain11, A119Pt); // Bang together the parameter list. Right now, all the MueLu details is // hardwired in the MueLu-TpetraQ2Q1 classes. We probably want to switch // things so that several of these hardwired features could be modified // via parameter lists. RCP stratimikosList = rcp(new ParameterList); stratimikosList->set("Linear Solver Type", "Belos"); stratimikosList->set("Preconditioner Type", "MueLu-TpetraQ2Q1"); ParameterList& BelosList = stratimikosList->sublist("Linear Solver Types").sublist("Belos"); BelosList.set("Solver Type", "Block GMRES"); // FIXME: should it be "Pseudo Block GMRES"? BelosList.sublist("VerboseObject").set("Verbosity Level", "low"); // this is needed, as otherwise Stratimikos ignores Belos output ParameterList& GmresDetails = BelosList.sublist("Solver Types").sublist("Block GMRES"); GmresDetails.set("Maximum Iterations", 100); GmresDetails.set("Convergence Tolerance", tol); GmresDetails.set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); GmresDetails.set("Output Frequency", 1); GmresDetails.set("Output Style", Belos::Brief); ParameterList& Q2Q1List = stratimikosList->sublist("Preconditioner Types").sublist("MueLu-TpetraQ2Q1"); Q2Q1List.set("useFilters", useFilters); Q2Q1List.set("tau_1", tau_1); Q2Q1List.set("tau_2", tau_2); Q2Q1List.set("Velcoords", Vcoords); Q2Q1List.set("Prescoords", Pcoords); Q2Q1List.set("p2vMap", p2vMap); Q2Q1List.set("A11", thA11); Q2Q1List.set("A12", thA12); Q2Q1List.set("A21", thA21); Q2Q1List.set("A11_9Pt", thA11_9Pt); Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr(&Q2Q1List), *comm); std::cout << "Input parameters: " << *stratimikosList << std::endl; // Stratimikos vodou typedef Thyra::PreconditionerFactoryBase Base; typedef Thyra::LinearOpWithSolveFactoryBase LOWSFB; typedef Thyra::LinearOpWithSolveBase LOWSB; typedef Thyra::MultiVectorBase TH_Mvb; Stratimikos::LinearSolverBuilder linearSolverBuilder; // Thyra::addMueLuToStratimikosBuilder(linearSolverBuilder); Stratimikos::enableMueLu(linearSolverBuilder); // Epetra Stratimikos::enableMueLuTpetraQ2Q1(linearSolverBuilder, "MueLu-TpetraQ2Q1"); // Tpetra linearSolverBuilder.setParameterList(stratimikosList); RCP lowsFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder); RCP nsA = lowsFactory->createOp(); // I've hacked together a big matrix that does not use strided maps by // simply reading the data again. Normally, this would be supplied by Eric // Cyr and would be Teko operators. int numElem = A12->getRangeMap()->getLocalNumElements() + A21->getRangeMap()->getLocalNumElements(); RCP fullMap = toTpetra(MapFactory::createUniformContigMap(Xpetra::UseTpetra, numElem, comm)); RCP A; if (!binary) A = Reader::readSparseFile((prefix + "BigA.mm").c_str(), fullMap, fullMap, fullMap, fullMap, true, true, false); else A = ReadBinary((prefix + "BigA.dat").c_str(), comm); const RCP > thA = Thyra::createLinearOp(A); Thyra::initializeOp(*lowsFactory, thA, nsA.ptr()); RCP tX = Tpetra::createVector(fullMap), tB; if (rhs == "") { tX->randomize(); tB = Tpetra::createVector(fullMap); A->apply(*tX, *tB); } else { typedef Tpetra::MatrixMarket::Reader reader_type; tB = reader_type::readDenseFile(rhs.c_str(), fullMap->getComm(), fullMap); } tX->putScalar(0.0); // Set the initial guess Dirichlet points to the proper value. // This step is pretty important as the preconditioner may return zero at Dirichlet points ArrayRCP dirBCs = Utilities::DetectDirichletRows(*MueLu::TpetraCrs_To_XpetraMatrix(rcp_dynamic_cast(A))); ArrayRCP tXdata = tX->getDataNonConst(0); ArrayRCP tBdata = tB->getData(0); for (LO i = 0; i < tXdata.size(); i++) if (dirBCs[i]) tXdata[i] = tBdata[i]; RCP sX = Thyra::createMultiVector(tX); RCP sB = Thyra::createMultiVector(tB); Thyra::SolveStatus solveStatus = Thyra::solve(*nsA, Thyra::NOTRANS, *sB, sX.ptr()); if (printTimings) { const bool alwaysWriteLocal = false; const bool writeGlobalStats = true; const bool writeZeroTimers = false; const bool ignoreZeroTimers = true; const std::string filter = ""; TimeMonitor::summarize(comm.ptr(), std::cout, alwaysWriteLocal, writeGlobalStats, writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers); } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return (success ? EXIT_SUCCESS : EXIT_FAILURE); }