// @HEADER // ***************************************************************************** // Belos: Block Linear Solvers Package // // Copyright 2004-2016 NTESS and the Belos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /* Originally convert test here: belos/epetra/test/MINRES/test_minres_diag.cpp */ // Tpetra #include #include #include #include // Teuchos #include #include #include #include #include // Belos #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosMinresSolMgr.hpp" #include "BelosTpetraAdapter.hpp" #include "BelosTpetraOperator.hpp" using Teuchos::RCP; using Teuchos::rcp; //************************************************************************************************ template class VectorOperator { public: VectorOperator(int m_in, int n_in) : m(m_in), n(n_in){}; virtual ~VectorOperator(){}; virtual void operator()(const MV &x, MV &y) = 0; int size(int dim) const { return (dim == 1) ? m : n; }; protected: int m, n; // an (m x n) operator private: // Not allowing copy construction. VectorOperator(const VectorOperator &) : m(0), n(0){}; VectorOperator *operator=(const VectorOperator &) { return NULL; }; }; //************************************************************************************************ template class DiagonalOperator : public VectorOperator { public: DiagonalOperator(int n_in, ST v_in) : VectorOperator(n_in, n_in), v(v_in){}; ~DiagonalOperator(){}; void operator()(const MV &x, MV &y) { y.scale(v, x); }; private: ST v; }; //************************************************************************************************ template class DiagonalOperator2 : public VectorOperator { public: DiagonalOperator2(int n_in, int min_gid_in, ST v_in) : VectorOperator(n_in, n_in), min_gid(min_gid_in), v(v_in) {} ~DiagonalOperator2(){}; void operator()(const MV &x, MV &y) { auto yLocalData = y.getLocalViewHost(Tpetra::Access::ReadWrite); auto xLocalData = x.getLocalViewHost(Tpetra::Access::ReadOnly); for (size_t j = 0; j < x.getNumVectors(); ++j) { for (size_t i = 0; i < x.getLocalLength(); ++i) { yLocalData(i, j) = (min_gid + i + 1) * v * xLocalData(i, j); // NOTE: square operator! } } }; private: int min_gid; ST v; }; //************************************************************************************************ template class ComposedOperator : public VectorOperator { public: ComposedOperator(int n, const RCP> &pA_in, const RCP> &pB_in); virtual ~ComposedOperator(){}; virtual void operator()(const MV &x, MV &y); private: RCP> pA; RCP> pB; }; template ComposedOperator::ComposedOperator(int n_in, const RCP> &pA_in, const RCP> &pB_in) : VectorOperator(n_in, n_in), pA(pA_in), pB(pB_in) {} template void ComposedOperator::operator()(const MV &x, MV &y) { MV ytemp(y.getMap(), y.getNumVectors(), false); (*pB)(x, ytemp); (*pA)(ytemp, y); } //************************************************************************************************ template class TrilinosInterface : public OP { public: TrilinosInterface(const RCP> pA_in, const RCP> pComm_in, const RCP pMap_in) : pA(pA_in), pComm(pComm_in), pMap(pMap_in), use_transpose(false) {} void apply(const MV &X, MV &Y, Teuchos::ETransp mode = Teuchos::NO_TRANS, ST alpha = Teuchos::ScalarTraits::one(), ST beta = Teuchos::ScalarTraits::zero()) const override; virtual ~TrilinosInterface(){}; bool hasTransposeApply() const { return (use_transpose); }; // always set to false (in fact the default) RCP getDomainMap() const override { return pMap; } RCP getRangeMap() const override { return pMap; } private: RCP> pA; RCP> pComm; RCP pMap; bool use_transpose; }; template void TrilinosInterface::apply(const MV &X, MV &Y, Teuchos::ETransp mode, ST alpha, ST beta) const { (*pA)(X, Y); } //************************************************************************************************ template class IterativeInverseOperator : public VectorOperator { public: IterativeInverseOperator(int n_in, int blocksize, const RCP> &pA_in, std::string opString = "Iterative Solver", bool print_in = false); virtual ~IterativeInverseOperator() {} virtual void operator()(const MV &b, MV &x); private: RCP> pA; // operator which will be inverted // supplies a matrix std::vector multiply const bool print; Teuchos::Time timer; RCP> pComm; RCP pMap; RCP pPE; RCP pList; RCP> pProb; RCP> pBelos; }; template IterativeInverseOperator::IterativeInverseOperator(int n_in, int blocksize, const RCP> &pA_in, std::string opString, bool print_in) : VectorOperator(n_in, n_in), // square operator pA(pA_in), print(print_in), timer(opString) { int n_global; pComm = Tpetra::getDefaultComm(); Teuchos::reduceAll(*pComm, Teuchos::REDUCE_SUM, 1, &n_in, &n_global); pMap = rcp(new MP(n_global, n_in, 0, pComm)); pPE = rcp(new TrilinosInterface(pA, pComm, pMap)); pProb = rcp(new Belos::LinearProblem()); pProb->setOperator(pPE); int max_iter = 100; const ST tol = sqrt(std::numeric_limits::epsilon()); int verbosity = Belos::Errors + Belos::Warnings; if (print) verbosity += Belos::TimingDetails + Belos::StatusTestDetails; pList = rcp(new Teuchos::ParameterList); pList->set("Maximum Iterations", max_iter); pList->set("Convergence Tolerance", tol); pList->set("Verbosity", verbosity); pBelos = rcp(new Belos::MinresSolMgr(pProb, pList)); } template void IterativeInverseOperator::operator()(const MV &b, MV &x) { int pid = pComm->getRank(); // Initialize the solution to zero x.putScalar(0.0); // Reset the solver, problem, and status test for next solve (HKT) pProb->setProblem(rcp(&x, false), rcp(&b, false)); timer.start(); Belos::ReturnType ret = pBelos->solve(); timer.stop(); if (pid == 0 && print) { if (ret == Belos::Converged) { std::cout << std::endl << "pid[" << pid << "] Minres converged" << std::endl; std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl; } else std::cout << std::endl << "pid[" << pid << "] Minres did not converge" << std::endl; } } //************************************************************************************************ template int run(int argc, char *argv[]) { // Get default Tpetra template types using ST = typename Tpetra::MultiVector::scalar_type; using LO = typename Tpetra::Vector<>::local_ordinal_type; using GO = typename Tpetra::Vector<>::global_ordinal_type; using NT = typename Tpetra::Vector<>::node_type; // Init Tpetra types using OP = typename Tpetra::Operator; using MV = typename Tpetra::MultiVector; using MP = typename Tpetra::Map; Teuchos::GlobalMPISession session(&argc, &argv, NULL); RCP> comm = Tpetra::getDefaultComm(); const int pid = rank(*comm); bool verbose = false; bool success = true; ST tol = sqrt(std::numeric_limits::epsilon()); // relative residual tolerance try { int n(10); int numRHS = 1; RCP map = RCP(new MP(n, 0, comm)); MV X(map, numRHS), Y(map, numRHS); X.putScalar(1.0); // Inner computes inv(D2)*y RCP> D2 = rcp(new DiagonalOperator2(n, map->getMinGlobalIndex(), 1.0)); IterativeInverseOperator A2(n, 1, D2, "Belos (inv(D2))", true); // should return x=(1, 1/2, 1/3, ..., 1/10) A2(X, Y); if (pid == 0) { std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl; } Y.print(std::cout); // Inner computes inv(D)*x RCP> D = rcp(new DiagonalOperator(n, 4.0)); RCP> Inner = rcp(new IterativeInverseOperator(n, 1, D, "Belos (inv(D))", false)); // ComposedOperator computed inv(D)*B*x RCP> B = rcp(new DiagonalOperator(n, 4.0)); RCP> C = rcp(new ComposedOperator(n, Inner, B)); // Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x RCP> Outer = rcp(new IterativeInverseOperator(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true)); // should return x=1/4 (*Inner)(X, Y); if (pid == 0) { std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl; } Y.print(std::cout); // should return x=1 (*Outer)(X, Y); if (pid == 0) { std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl; } Y.print(std::cout); // Compute the norm of Y - 1.0 std::vector norm_Y(Y.getNumVectors()); Teuchos::ArrayView normView(norm_Y); Y.update(-1.0, X, 1.0); Y.norm2(norm_Y); if (pid == 0) std::cout << "Two-norm of std::vector (Y-1.0) : " << norm_Y[0] << std::endl; success = (norm_Y[0] < tol && !Teuchos::ScalarTraits::isnaninf(norm_Y[0])); if (success) { if (pid == 0) std::cout << "End Result: TEST PASSED" << std::endl; } else { if (pid == 0) std::cout << "End Result: TEST FAILED" << std::endl; } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return (success ? EXIT_SUCCESS : EXIT_FAILURE); } //************************************************************************************************ //************************************************************************************************ int main(int argc, char *argv[]) { return run(argc, argv); // return run(argc, argv); // FAILS }