//@HEADER // ************************************************************************ // // Kokkos v. 4.0 // Copyright (2022) National Technology & Engineering // Solutions of Sandia, LLC (NTESS). // // Under the terms of Contract DE-NA0003525 with NTESS, // the U.S. Government retains certain rights in this software. // // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. // See https://kokkos.org/LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //@HEADER #ifndef KOKKOS_EXAMPLE_BELOS_SOLVE #define KOKKOS_EXAMPLE_BELOS_SOLVE // Tpetra #include "Kokkos_ArithTraits.hpp" #include "Tpetra_MultiVector.hpp" #include "Tpetra_CrsMatrix.hpp" #include "CGSolve.hpp" // for result_struct #include "TrilinosCouplings_config.h" #if defined(HAVE_TRILINOSCOUPLINGS_BELOS) && defined(HAVE_TRILINOSCOUPLINGS_MUELU) // Belos #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosPseudoBlockCGSolMgr.hpp" #include "BelosPseudoBlockGmresSolMgr.hpp" #include "BelosTpetraAdapter.hpp" #include "Teuchos_TimeMonitor.hpp" #include "SGPreconditioner.hpp" #include "MeanBasedPreconditioner.hpp" #include "MueLuPreconditioner.hpp" namespace Kokkos { namespace Example { // Functor to copy the nodal coordinates out of the mesh fixture into // a multi-vector. This functor is necessary because the respective views // have potentially different layouts. template struct FillCoords { typedef typename coords_type::execution_space execution_space; typedef typename coords_type::size_type size_type; const coords_type m_coords; const coords_vec_type m_coords_vec; const size_type m_dim; FillCoords( const coords_type& coords, const coords_vec_type& coords_vec ) : m_coords(coords), m_coords_vec(coords_vec), m_dim(coords.extent(1)) { // Note: coords contains off-processor halo nodes and thus is longer // than coords_vec, which is the same length as the solution vector. // These extra halo nodes are stored at the end. Kokkos::parallel_for( m_coords_vec.extent(0), *this ); } KOKKOS_INLINE_FUNCTION void operator() (const size_type i) const { for (size_type j=0; j void fill_coords( const coords_type& coords, const coords_vec_type& coords_vec ) { FillCoords(coords, coords_vec); } template struct ExtractEnsembleIts { static std::vector apply(const Belos::SolverManager& solver) { return std::vector(); } }; template result_struct belos_solve( Tpetra::CrsMatrix& A, const Tpetra::MultiVector& b, Tpetra::MultiVector& x, Teuchos::RCP< Tpetra::Operator >& precOp, const Mesh& mesh, const int use_muelu, const int use_mean_based, const Teuchos::RCP& fenlParams, const unsigned max_iter = 200, const typename Kokkos::ArithTraits::mag_type tolerance = Kokkos::ArithTraits::epsilon()) { typedef Tpetra::Operator OperatorType; typedef Tpetra::MultiVector VectorType; typedef typename VectorType::dot_type BelosScalarType; typedef Belos::LinearProblem ProblemType; typedef Belos::PseudoBlockCGSolMgr CGSolverType; typedef Belos::PseudoBlockGmresSolMgr GmresSolverType; typedef Belos::SolverManager SolverType; typedef SGPreconditioner PreconditionerType; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcpFromRef; using Teuchos::ParameterList; // Create some timers used by Belos so we can access them Teuchos::RCP time_mat_vec = Teuchos::TimeMonitor::getNewTimer("Belos: Operation Op*x"); Teuchos::RCP time_prec_apply = Teuchos::TimeMonitor::getNewTimer("Belos: Operation Prec*x"); Teuchos::RCP time_total; Teuchos::RCP time_prec_setup = Teuchos::TimeMonitor::getNewTimer("Total MueLu setup time"); //-------------------------------- // Create preconditioner if requested and we weren't given one if (use_muelu && precOp == Teuchos::null) { Teuchos::TimeMonitor timeMon(*time_prec_setup); RCP preconditioner; // Create tpetra-vector storing coordinates for repartitioning typename Mesh::node_coord_type node_coords = mesh.node_coord(); //Teuchos::RCP coords = Teuchos::RCP > coords = Teuchos::rcp(new Tpetra::MultiVector(x.getMap(), node_coords.extent(1))); fill_coords(node_coords, coords->getLocalViewDevice(Tpetra::Access::ReadWrite)); RCP mueluParams = Teuchos::sublist(fenlParams, "MueLu"); if (use_mean_based) { precOp = build_mean_based_muelu_preconditioner(rcpFromRef(A), mueluParams, coords); } else { precOp = build_muelu_preconditioner(rcpFromRef(A), mueluParams, coords); } } //-------------------------------- // Set up linear solver RCP belosParams = Teuchos::sublist(fenlParams, "Belos"); if (!(belosParams->isParameter("Convergence Tolerance"))) belosParams->set("Convergence Tolerance", tolerance); if (!(belosParams->isParameter("Maximum Iterations"))) belosParams->set("Maximum Iterations", Teuchos::as(max_iter)); if (!(belosParams->isParameter("Output Frequency"))) belosParams->set("Output Frequency", 1); RCP problem = rcp(new ProblemType(rcpFromRef(A), rcpFromRef(x), rcpFromRef(b))); std::string belos_solver = belosParams->get("Belos Solver", "CG"); RCP solver; if (belos_solver == "CG" || belos_solver == "cg") { time_total = Teuchos::TimeMonitor::getNewTimer("Belos: PseudoBlockCGSolMgr total solve time"); solver = rcp(new CGSolverType(problem, belosParams)); } else if (belos_solver == "GMRES" || belos_solver == "Gmres" || belos_solver == "gmres") { time_total = Teuchos::TimeMonitor::getNewTimer("Belos: PseudoBlockGmresSolMgr total solve time"); solver = rcp(new GmresSolverType(problem, belosParams)); } else TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "Invalid solver " << belos_solver); if (use_muelu || use_mean_based){ problem->setRightPrec(precOp); } const bool isSet = problem->setProblem(); TEUCHOS_TEST_FOR_EXCEPTION(!isSet, std::runtime_error, "Belos failed to set problem correctly."); //-------------------------------- // Solve for nonlinear update Belos::ReturnType result = solver->solve(); bool converged = (result == Belos::Converged); TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error, "Belos solver did not converge!"); result_struct cgsolve; cgsolve.iteration = solver->getNumIters(); //cgsolve.norm_res = solver->achievedTol(); cgsolve.iter_time = time_total->totalElapsedTime(); cgsolve.total_time = time_total->totalElapsedTime(); cgsolve.matvec_time = time_mat_vec->totalElapsedTime(); cgsolve.prec_apply_time = time_prec_apply->totalElapsedTime(); cgsolve.prec_setup_time = time_prec_setup->totalElapsedTime(); // Extract the number of iterations for ensembles cgsolve.ensemble_its = ExtractEnsembleIts::apply(*solver); // if (cgsolve.ensemble_its.size() > 0) { // std::cout << std::endl << "ensemble iterations = "; // for (std::size_t i=0; i result_struct belos_solve( Tpetra::CrsMatrix& A, const Tpetra::MultiVector& b, Tpetra::MultiVector& x, Teuchos::RCP< Tpetra::Operator >& precOp, const Mesh& mesh, const int use_muelu, const int use_mean_based, const Teuchos::RCP& fenlParams, const unsigned max_iter = 200, const typename Kokkos::ArithTraits::mag_type tolerance = Kokkos::ArithTraits::epsilon()) { TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "Belos solver requested but not compiled!"); } } // namespace Example } // namespace Kokkos #endif //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- #endif /* #ifndef KOKKOS_EXAMPLE_BELOS_SOLVE */