// @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 // Tpetra provides distributed sparse linear algebra //#include //#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 // MueLu main header: include most common header files in one line #include #include #include #include #include #include #include #include #include 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::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; // 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); // Problem definition std::string problem_type = "ADR2D"; // Parameters Scalar Lx = 6.0; Scalar Ly = 6.0; Scalar Lz = 6.0; global_ordinal_type nx = 50; global_ordinal_type ny = 50; global_ordinal_type nz = 50; 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 global_ordinal_type maxIts = 10000; scalar_type tol = 1e-10; std::string solverOptionsFile = "dd.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; } if (xpetraParameters.GetLib() == Xpetra::UseEpetra) { throw std::invalid_argument("This example only supports Tpetra."); } ParameterList mueluParams; Teuchos::updateParametersFromXmlFile(solverOptionsFile, Teuchos::inoutArg(mueluParams)); // // Construct the problem // global_ordinal_type indexBase = 0; RCP xpetraMap; std::vector indices; // Creation of the map where processor 0 gets nothing at the fine level if (comm->getSize() > 1) { indices.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) indices.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) indices.emplace_back(i); indices.shrink_to_fit(); Teuchos::ArrayView elementList(indices); 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; // RCP xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), indexBase, comm); // RCP xpetraMap = Galeri::Xpetra::CreateMap(Xpetra::UseTpetra, "Cartesian2D", comm, matrixParameters.GetParameterList()); std::cout << "Before coordinates" << std::endl; coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates("2D", xpetraMap, matrixParameters.GetParameterList()); std::cout << "After coordinates" << std::endl; RCP Pr = ADR::Xpetra::BuildProblem(matrixParameters.GetMatrixType(), xpetraMap, matrixParameters.GetParameterList()); RCP xpetraA = Pr->BuildMatrix(); RCP A = toTpetra(xpetraA); RCP 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)); X->putScalar((scalar_type)0.0); B->randomize(); RCP Problem = rcp(new linear_problem_type(A, X, B)); Problem->setRightPrec(M); Problem->setProblem(); // // Set up Krylov solver and iterate. // RCP belosList = rcp(new 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("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, belosList)); else if (krylovSolverType == "gmres") solver = rcp(new belos_gmres_manager_type(Problem, belosList)); else if (krylovSolverType == "bicgstab") solver = rcp(new belos_bicgstab_manager_type(Problem, belosList)); else throw std::invalid_argument("bad Krylov solver type"); solver->solve(); int numIterations = solver->getNumIters(); Teuchos::Array::magnitudeType> normVec(1); multivector_type Ax(B->getMap(), 1); multivector_type residual(B->getMap(), 1); A->apply(*X, residual); residual.update(1.0, *B, -1.0); residual.norm2(normVec); if (mypid == 0) { std::cout << "number of iterations = " << numIterations << std::endl; std::cout << "||Residual|| = " << normVec[0] << std::endl; } return EXIT_SUCCESS; }