Debian Codegraph @

packages/belos/kokkos/example/BlockCGKokkosExFile.cpp

raw
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 "BelosBlockCGSolMgr.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 bool expresidual = false; // use explicit residual
63 std::string filename("bcsstk12.mtx"); // example matrix
64 MT tol = 1.0e-5; // relative residual tolerance
65
66 Teuchos::CommandLineProcessor cmdp(false,true);
67 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
68 cmdp.setOption("expres","impres",&expresidual,"Use explicit residual throughout.");
69 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
70 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
71 cmdp.setOption("tol",&tol,"Relative residual tolerance used by Gmres solver.");
72 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
73 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
74
75 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
76 return -1;
77 }
78 if (!verbose)
79 frequency = -1; // reset frequency if test is not verbose
80
81 // Read in a matrix Market file and use it to test the Kokkos Operator.
82 KokkosSparse::CrsMatrix<ST, OT, EXSP> crsMat =
83 KokkosSparse::Impl::read_kokkos_crst_matrix<KokkosSparse::CrsMatrix<ST, OT, EXSP>>(filename.c_str());
84 RCP<Belos::KokkosCrsOperator<ST, OT, EXSP>> A =
85 rcp(new Belos::KokkosCrsOperator<ST,OT,EXSP>(crsMat));
86 OT numRows = crsMat.numRows();
87
88 Teuchos::RCP<MV> X = Teuchos::rcp( new MV(numRows, numrhs) );
89 X->MvRandom();
90 Teuchos::RCP<MV> B = Teuchos::rcp( new MV(numRows, numrhs) );
91 OPT::Apply(*A,*X,*B);
92 X->MvInit(0.0);
93
94 //
95 // ********Other information used by block solver***********
96 // *****************(can be user specified)******************
97 //
98 const int NumGlobalElements = B->GetGlobalLength();
99 if (maxiters == -1)
100 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
101
102 ParameterList belosList;
103 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
104 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
105 belosList.set( "Explicit Residual Test", expresidual); // use explicit residual
106
107 if (verbose) {
108 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
109 Belos::StatusTestDetails + Belos::FinalSummary + Belos::TimingDetails);
110 if (frequency > 0)
111 belosList.set( "Output Frequency", frequency );
112 }
113 else
114 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
115
116 //
117 // Construct an unpreconditioned linear problem instance.
118 //
119 Belos::LinearProblem<ST,KMV,KOP> problem( A, X, B );
120 bool set = problem.setProblem();
121 if (set == false) {
122 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
123 return -1;
124 }
125 //
126 // *******************************************************************
127 // **************Start the block CG iteration*************************
128 // *******************************************************************
129 //
130 // Create an iterative solver manager.
131 RCP< Belos::SolverManager<ST,KMV,KOP> > newSolver
132 = rcp( new Belos::BlockCGSolMgr<ST,KMV,KOP>(rcpFromRef(problem), rcpFromRef(belosList)) );
133
134 //
135 // **********Print out information about problem*******************
136 //
137 std::cout << std::endl << std::endl;
138 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
139 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
140 std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
141 std::cout << "Relative residual tolerance: " << tol << std::endl;
142 std::cout << std::endl;
143 //
144 // Perform solve
145 //
146 Belos::ReturnType ret;
147 ret = newSolver->solve();
148
149 //
150 // Compute actual residuals.
151 //
152 bool badRes = false;
153 std::vector<ST> actual_resids( numrhs );
154 std::vector<ST> rhs_norm( numrhs );
155 MV resid(numRows, numrhs);
156 OPT::Apply( *A, *X, resid );
157 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
158 MVT::MvNorm( resid, actual_resids );
159 MVT::MvNorm( *B, rhs_norm );
160 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
161 for ( int i=0; i<numrhs; i++) {
162 ST actRes = actual_resids[i]/rhs_norm[i];
163 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
164 if (actRes > tol) badRes = true;
165 }
166
167 if (ret!=Belos::Converged || badRes) {
168 success = false;
169 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
170 } else {
171 success = true;
172 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
173 }
174
175 }
176 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
177 }
178 Kokkos::finalize();
179 #ifdef HAVE_MPI
180 MPI_Finalize();
181 #endif
182 return success ? EXIT_SUCCESS : EXIT_FAILURE;
183 }