// @HEADER // ***************************************************************************** // Belos: Block Linear Solvers Package // // Copyright 2004-2016 NTESS and the Belos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // This test generates diagonal matrices for TFQMR to solve. // // NOTE: No preconditioner is used in this case. // // Teuchos #include #include // Tpetra #include #include #include #include #include // Belos #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosTpetraAdapter.hpp" #include "BelosTpetraOperator.hpp" #include "BelosTFQMRSolMgr.hpp" using namespace Belos; //************************************************************************************************ 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 Teuchos::RCP>& pA_in, const Teuchos::RCP>& pB_in); virtual ~ComposedOperator() {}; virtual void operator () (const MV &x, MV &y); private: Teuchos::RCP> pA; Teuchos::RCP> pB; }; template ComposedOperator::ComposedOperator(int n_in, const Teuchos::RCP>& pA_in, const Teuchos::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 Teuchos::RCP> pA_in, const Teuchos::RCP> pComm_in, const Teuchos::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);}; Teuchos::RCP getDomainMap() const override {return pMap; } Teuchos::RCP getRangeMap() const override {return pMap; } private: Teuchos::RCP> pA; Teuchos::RCP> pComm; Teuchos::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 Teuchos::RCP>& pA_in, std::string opString="Iterative Solver", bool print_in=false); virtual ~IterativeInverseOperator() {} virtual void operator () (const MV &b, MV &x); private: Teuchos::RCP> pA; // operator which will be inverted // supplies a matrix std::vector multiply const bool print; Teuchos::Time timer; Teuchos::RCP> pComm; Teuchos::RCP pMap; Teuchos::RCP pPE; Teuchos::RCP pList; Teuchos::RCP > pProb; Teuchos::RCP > pBelos; }; template IterativeInverseOperator::IterativeInverseOperator(int n_in, int blocksize, const Teuchos::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 nGlobal; pComm = Tpetra::getDefaultComm(); Teuchos::reduceAll(*pComm, Teuchos::REDUCE_SUM, 1, &n_in, &nGlobal); pMap = Teuchos::rcp( new MP(nGlobal, n_in, 0, pComm) ); pPE = Teuchos::rcp( new TrilinosInterface(pA, pComm, pMap ) ); pProb = Teuchos::rcp( new LinearProblem() ); pProb->setOperator( pPE ); int max_iter = 100; ST tol = sqrt(std::numeric_limits::epsilon()); int verbosity = Belos::Errors + Belos::Warnings; if (print) verbosity += Belos::TimingDetails + Belos::StatusTestDetails; pList = Teuchos::rcp( new Teuchos::ParameterList ); pList->set( "Maximum Iterations", max_iter ); pList->set( "Convergence Tolerance", tol ); pList->set( "Verbosity", verbosity ); pBelos = Teuchos::rcp( new TFQMRSolMgr(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( Teuchos::rcp(&x, false), Teuchos::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 << "] TFQMR converged" << std::endl; std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl; } else std::cout << std::endl << "pid[" << pid << "] TFQMR 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::MultiVector<>::local_ordinal_type; using GO = typename Tpetra::MultiVector<>::global_ordinal_type; using NT = typename Tpetra::MultiVector<>::node_type; // Init Tpetra types using OP = typename Tpetra::Operator; using MV = typename Tpetra::MultiVector; using MP = typename Tpetra::Map; using Teuchos::RCP; using Teuchos::rcp; Teuchos::GlobalMPISession session(&argc, &argv, NULL); RCP > comm = Tpetra::getDefaultComm(); int pid = rank(*comm); bool verbose = false; bool success = true; ST tol = sqrt(std::numeric_limits::epsilon()); 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)); // Composed_Operator 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()); 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); // wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON // run(argc, argv); }