// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // To compile and run this example, Trilinos must be configured with // Tpetra, Amesos2, MueLu, Ifpack2, and Belos. // // This example will only work with Tpetra, not Epetra. // // Commonly used options are // --matrixType // --nx // --ny // --xmlFile // // "./MueLu_Repartition_ADR.exe --help" shows all supported command line options. // #include // Belos provides Krylov solvers #include #include #include #include #include #include #include // Galeri #include #include #include #include // ADR subdirectory #include "CreateADRMatrix.hpp" #include #include #include #include "Smooth_Prolongation.cpp" int main(int argc, char *argv[]) { // Define default types typedef double scalar_type; typedef int local_ordinal_type; typedef int global_ordinal_type; typedef Tpetra::KokkosClassic::DefaultNode::DefaultNodeType node_type; // Convenient typedef's typedef Tpetra::Operator operator_type; typedef Tpetra::CrsMatrix crs_matrix_type; typedef Tpetra::RowMatrix row_matrix_type; typedef Tpetra::Vector vector_type; typedef Tpetra::MultiVector multivector_type; typedef Tpetra::Map driver_map_type; typedef MueLu::TpetraOperator muelu_tpetra_operator_type; typedef MueLu::Utilities MueLuUtilities; typedef Belos::LinearProblem linear_problem_type; typedef Belos::SolverManager belos_solver_manager_type; typedef Belos::PseudoBlockCGSolMgr belos_pseudocg_manager_type; typedef Belos::BlockGmresSolMgr belos_gmres_manager_type; typedef Belos::BiCGStabSolMgr belos_bicgstab_manager_type; typedef Ifpack2::Preconditioner precond_type; // MueLu_UseShortNames.hpp wants these typedefs. typedef scalar_type Scalar; typedef local_ordinal_type LocalOrdinal; typedef global_ordinal_type GlobalOrdinal; typedef node_type Node; #include typedef Galeri::Xpetra::Problem GaleriXpetraProblem; typedef ADR::Xpetra::Problem ADRXpetraProblem; using Teuchos::RCP; // reference count pointers using Teuchos::rcp; // reference count pointers // // MPI initialization using Teuchos // Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); RCP > comm = Teuchos::DefaultComm::getComm(); int mypid = comm->getRank(); /* int subCommRank[3]={0,1,2}; Teuchos::ArrayView arraySubCommRank(subCommRank, 3); auto subComm = comm->createSubcommunicator(arraySubCommRank); */ Teuchos::CommandLineProcessor clp(false); global_ordinal_type maxIts = 10000; scalar_type tol = 1e-10; std::string solverOptionsFile = "final_parser.xml"; std::string krylovSolverType = "bicgstab"; clp.setOption("xmlFile", &solverOptionsFile, "XML file containing MueLu solver parameters"); clp.setOption("maxits", &maxIts, "maximum number of Krylov iterations"); clp.setOption("tol", &tol, "tolerance for Krylov solver"); clp.setOption("krylovType", &krylovSolverType, "cg or gmres solver"); 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; } Teuchos::ParameterList xmlParams; Teuchos::ParameterList mueluParams; Teuchos::ParameterList problemParams; Teuchos::updateParametersFromXmlFile(solverOptionsFile, Teuchos::inoutArg(xmlParams)); mueluParams = xmlParams.sublist(static_cast("MueLu")); problemParams = xmlParams.sublist(static_cast("Problem")); // Problem definition std::string problem_type = problemParams.get(static_cast("problem type")); // Parameters Scalar Lx = problemParams.get(static_cast("domain size in x-direction")); Scalar Ly = problemParams.get(static_cast("domain size in y-direction")); Scalar Lz = problemParams.get(static_cast("domain size in z-direction")); global_ordinal_type nx = problemParams.get(static_cast("nodes in x-direction")); global_ordinal_type ny = problemParams.get(static_cast("nodes in y-direction")); global_ordinal_type nz = problemParams.get(static_cast("nodes in z-direction")); global_ordinal_type number_runs = problemParams.get(static_cast("number of runs")); MueLu::DomainPartitioning domain; int keep_boundary = 0; Scalar stretchx = (Scalar)Lx / nx; Scalar stretchy = (Scalar)Ly / ny; Scalar stretchz = (Scalar)Lz / nz; ADR::Xpetra::Parameters matrixParameters(clp, nx, ny, nz, problem_type, keep_boundary, stretchx, stretchy, stretchz); // manage parameters of the test case Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra // // Construct the problem // global_ordinal_type indexBase = 0; RCP xpetraMap; std::vector ind; // Creation of the map where processor 0 gets nothing at the fine level if (comm->getSize() > 1) { ind.reserve(static_cast(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1) + 1)); if (mypid != 0 && mypid != comm->getSize() - 1) for (int i = 0; i <= (static_cast(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1))) - 1; ++i) ind.emplace_back((mypid - 1) * static_cast(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1)) + i); if (mypid == comm->getSize() - 1) for (int i = (mypid - 1) * static_cast(matrixParameters.GetNumGlobalElements() / (comm->getSize() - 1)); i != matrixParameters.GetNumGlobalElements(); ++i) ind.emplace_back(i); ind.shrink_to_fit(); Teuchos::ArrayView elementList(ind); xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), elementList, indexBase, comm); } else if (comm->getSize() == 1) xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), indexBase, comm); RCP coordinates; if (problem_type == "ADR1D") coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates("1D", xpetraMap, matrixParameters.GetParameterList()); else if (problem_type == "ADR2D") coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates("2D", xpetraMap, matrixParameters.GetParameterList()); else if (problem_type == "ADR3D") coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates("3D", xpetraMap, matrixParameters.GetParameterList()); RCP Pr = ADR::Xpetra::BuildProblem(matrixParameters.GetMatrixType(), xpetraMap, matrixParameters.GetParameterList()); RCP xpetraA = Pr->BuildMatrix(); RCP A = MueLutoTpetra(xpetraA); RCP map = toTpetra(xpetraMap); // Construct a multigrid preconditioner RCP M = MueLu::CreateTpetraPreconditioner((RCP)A, mueluParams, toTpetra(coordinates)); RCP X = rcp(new multivector_type(map, 1)); RCP B = rcp(new multivector_type(map, 1)); for (int iter = 1; iter <= number_runs; ++iter) { X->putScalar((scalar_type)0.0); B->randomize(); // // Set up Krylov solver and iterate. // RCP X_muelu = rcp(new multivector_type(map, 1)); RCP Problem_muelu = rcp(new linear_problem_type(A, X_muelu, B)); Problem_muelu->setRightPrec(M); Problem_muelu->setProblem(); RCP belosList = rcp(new Teuchos::ParameterList()); belosList->set("Maximum Iterations", maxIts); // Maximum number of iterations allowed belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested // belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); belosList->set("Verbosity", Belos::Errors); belosList->set("Output Frequency", 1); belosList->set("Output Style", Belos::Brief); belosList->set("Implicit Residual Scaling", "None"); RCP solver; if (krylovSolverType == "cg") solver = rcp(new belos_pseudocg_manager_type(Problem_muelu, belosList)); else if (krylovSolverType == "gmres") solver = rcp(new belos_gmres_manager_type(Problem_muelu, belosList)); else if (krylovSolverType == "bicgstab") solver = rcp(new belos_bicgstab_manager_type(Problem_muelu, belosList)); else throw std::invalid_argument("bad Krylov solver type"); solver->solve(); int numIterations_muelu = solver->getNumIters(); Teuchos::Array::magnitudeType> normVec_muelu(1); multivector_type residual_muelu(B->getMap(), 1); A->apply(*X_muelu, residual_muelu); residual_muelu.update(1.0, *B, -1.0); residual_muelu.norm2(normVec_muelu); if (mypid == 0) { std::cout << "number of iterations with MueLu preconditioner= " << numIterations_muelu << std::endl; std::cout << "||Residual|| = " << normVec_muelu[0] << std::endl; } } #include Teuchos::TimeMonitor::summarize(); return EXIT_SUCCESS; }