| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
// |
| 10 |
// This driver reads a problem from a file, which must be in Matrix Market (*.mtx). |
| 11 |
// The problem right-hand side will be generated randomly. |
| 12 |
// |
| 13 |
// NOTE: No preconditioner is used in this example. |
| 14 |
// |
| 15 |
#include "BelosConfigDefs.hpp" |
| 16 |
#include "BelosLinearProblem.hpp" |
| 17 |
#include "BelosGCRODRSolMgr.hpp" |
| 18 |
#include "BelosOutputManager.hpp" |
| 19 |
|
| 20 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 21 |
#include "Teuchos_ParameterList.hpp" |
| 22 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 23 |
|
| 24 |
#include "BelosKokkosAdapter.hpp" |
| 25 |
#include "KokkosKernels_IOUtils.hpp" |
| 26 |
#include "KokkosSparse_IOUtils.hpp" |
| 27 |
#ifdef HAVE_MPI |
| 28 |
#include <mpi.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
int main(int argc, char *argv[]) { |
| 32 |
|
| 33 |
#ifdef HAVE_MPI |
| 34 |
MPI_Init(&argc,&argv); |
| 35 |
#endif |
| 36 |
|
| 37 |
bool success = true; |
| 38 |
Kokkos::initialize(); |
| 39 |
{ |
| 40 |
|
| 41 |
typedef double ST; |
| 42 |
typedef int OT; |
| 43 |
typedef Kokkos::DefaultExecutionSpace EXSP; |
| 44 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 45 |
typedef SCT::magnitudeType MT; |
| 46 |
typedef Belos::KokkosMultiVec<ST, EXSP> MV; |
| 47 |
typedef Belos::MultiVec<ST> KMV; |
| 48 |
typedef Belos::Operator<ST> KOP; |
| 49 |
typedef Belos::MultiVecTraits<ST,KMV> MVT; |
| 50 |
typedef Belos::OperatorTraits<ST,KMV,KOP> OPT; |
| 51 |
|
| 52 |
using Teuchos::ParameterList; |
| 53 |
using Teuchos::RCP; |
| 54 |
using Teuchos::rcp; |
| 55 |
using Teuchos::rcpFromRef; |
| 56 |
|
| 57 |
bool verbose = true; |
| 58 |
try { |
| 59 |
int frequency = 25; // frequency of status test output. |
| 60 |
int numrhs = 1; // number of right-hand sides to solve for |
| 61 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 62 |
int maxsubspace = 300; // maximum number of blocks the solver can use for the subspace |
| 63 |
int maxrestarts = 5; // number of restarts allowed |
| 64 |
bool expresidual = false; // use explicit residual |
| 65 |
std::string filename("bcsstk12.mtx"); // example matrix |
| 66 |
std::string rhsfile(""); |
| 67 |
MT tol = 1.0e-5; // relative residual tolerance |
| 68 |
|
| 69 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 70 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 71 |
cmdp.setOption("expres","impres",&expresidual,"Use explicit residual throughout."); |
| 72 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 73 |
cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS"); |
| 74 |
cmdp.setOption("rhsfile",&rhsfile,"Filename for right-hand side. (*.mtx file) "); |
| 75 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by GCRO-DR solver."); |
| 76 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 77 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 78 |
cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace."); |
| 79 |
cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GCRO-DR solver."); |
| 80 |
|
| 81 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 82 |
return -1; |
| 83 |
} |
| 84 |
if (!verbose) |
| 85 |
frequency = -1; // reset frequency if test is not verbose |
| 86 |
|
| 87 |
// Read in a matrix Market file and use it to test the Kokkos Operator. |
| 88 |
KokkosSparse::CrsMatrix<ST, OT, EXSP> crsMat = |
| 89 |
KokkosSparse::Impl::read_kokkos_crst_matrix<KokkosSparse::CrsMatrix<ST, OT, EXSP>>(filename.c_str()); |
| 90 |
RCP<Belos::KokkosCrsOperator<ST, OT, EXSP>> A = |
| 91 |
rcp(new Belos::KokkosCrsOperator<ST,OT,EXSP>(crsMat)); |
| 92 |
OT numRows = crsMat.numRows(); |
| 93 |
|
| 94 |
Teuchos::RCP<MV> X = Teuchos::rcp( new MV(numRows, numrhs) ); |
| 95 |
X->MvRandom(); |
| 96 |
Teuchos::RCP<MV> B = Teuchos::rcp( new MV(numRows, numrhs) ); |
| 97 |
OPT::Apply(*A,*X,*B); |
| 98 |
X->MvInit(0.0); |
| 99 |
|
| 100 |
// |
| 101 |
// ********Other information used by block solver*********** |
| 102 |
// *****************(can be user specified)****************** |
| 103 |
// |
| 104 |
const int NumGlobalElements = B->GetGlobalLength(); |
| 105 |
if (maxiters == -1) |
| 106 |
maxiters = NumGlobalElements - 1; // maximum number of iterations to run |
| 107 |
|
| 108 |
ParameterList belosList; |
| 109 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 110 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 111 |
belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization |
| 112 |
belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed |
| 113 |
belosList.set( "Explicit Residual Test", expresidual); // Use explicit residual |
| 114 |
|
| 115 |
if (verbose) { |
| 116 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 117 |
Belos::StatusTestDetails + Belos::FinalSummary + Belos::TimingDetails); |
| 118 |
if (frequency > 0) |
| 119 |
belosList.set( "Output Frequency", frequency ); |
| 120 |
} |
| 121 |
else |
| 122 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 123 |
|
| 124 |
// |
| 125 |
// Construct an unpreconditioned linear problem instance. |
| 126 |
// |
| 127 |
Belos::LinearProblem<ST,KMV,KOP> problem( A, X, B ); |
| 128 |
bool set = problem.setProblem(); |
| 129 |
if (set == false) { |
| 130 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 131 |
return -1; |
| 132 |
} |
| 133 |
// |
| 134 |
// ******************************************************************* |
| 135 |
// ***************Start the GCRO-DR iteration************************* |
| 136 |
// ******************************************************************* |
| 137 |
// |
| 138 |
// Create an iterative solver manager. |
| 139 |
RCP< Belos::SolverManager<ST,KMV,KOP> > newSolver |
| 140 |
= rcp( new Belos::GCRODRSolMgr<ST,KMV,KOP,true>(rcpFromRef(problem), rcpFromRef(belosList)) ); |
| 141 |
|
| 142 |
// |
| 143 |
// **********Print out information about problem******************* |
| 144 |
// |
| 145 |
std::cout << std::endl << std::endl; |
| 146 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 147 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 148 |
std::cout << "Max number of GCRO-DR iterations: " << maxiters << std::endl; |
| 149 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 150 |
std::cout << std::endl; |
| 151 |
// |
| 152 |
// Perform solve |
| 153 |
// |
| 154 |
Belos::ReturnType ret; |
| 155 |
ret = newSolver->solve(); |
| 156 |
|
| 157 |
// |
| 158 |
// Compute actual residuals. |
| 159 |
// |
| 160 |
bool badRes = false; |
| 161 |
std::vector<ST> actual_resids( numrhs ); |
| 162 |
std::vector<ST> rhs_norm( numrhs ); |
| 163 |
MV resid(numRows, numrhs); |
| 164 |
OPT::Apply( *A, *X, resid ); |
| 165 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 166 |
MVT::MvNorm( resid, actual_resids ); |
| 167 |
MVT::MvNorm( *B, rhs_norm ); |
| 168 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 169 |
for ( int i=0; i<numrhs; i++) { |
| 170 |
ST actRes = actual_resids[i]/rhs_norm[i]; |
| 171 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 172 |
if (actRes > tol) badRes = true; |
| 173 |
} |
| 174 |
|
| 175 |
if (ret!=Belos::Converged || badRes) { |
| 176 |
success = false; |
| 177 |
std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl; |
| 178 |
} else { |
| 179 |
success = true; |
| 180 |
std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl; |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 185 |
} |
| 186 |
Kokkos::finalize(); |
| 187 |
#ifdef HAVE_MPI |
| 188 |
MPI_Finalize(); |
| 189 |
#endif |
| 190 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 191 |
} |