Debian Codegraph @

packages/trilinoscouplings/examples/fenl/BelosSolve.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_EXAMPLE_BELOS_SOLVE
18 #define KOKKOS_EXAMPLE_BELOS_SOLVE
19
20 // Tpetra
21 #include "Kokkos_ArithTraits.hpp"
22 #include "Tpetra_MultiVector.hpp"
23 #include "Tpetra_CrsMatrix.hpp"
24
25 #include "CGSolve.hpp" // for result_struct
26
27 #include "TrilinosCouplings_config.h"
28 #if defined(HAVE_TRILINOSCOUPLINGS_BELOS) && defined(HAVE_TRILINOSCOUPLINGS_MUELU)
29
30 // Belos
31 #include "BelosConfigDefs.hpp"
32 #include "BelosLinearProblem.hpp"
33 #include "BelosPseudoBlockCGSolMgr.hpp"
34 #include "BelosPseudoBlockGmresSolMgr.hpp"
35 #include "BelosTpetraAdapter.hpp"
36
37 #include "Teuchos_TimeMonitor.hpp"
38 #include "SGPreconditioner.hpp"
39 #include "MeanBasedPreconditioner.hpp"
40 #include "MueLuPreconditioner.hpp"
41
42 namespace Kokkos {
43 namespace Example {
44
45 // Functor to copy the nodal coordinates out of the mesh fixture into
46 // a multi-vector. This functor is necessary because the respective views
47 // have potentially different layouts.
48 template <typename coords_type, typename coords_vec_type>
49 struct FillCoords {
50 typedef typename coords_type::execution_space execution_space;
51 typedef typename coords_type::size_type size_type;
52
53 const coords_type m_coords;
54 const coords_vec_type m_coords_vec;
55 const size_type m_dim;
56
57 FillCoords( const coords_type& coords, const coords_vec_type& coords_vec )
58 : m_coords(coords), m_coords_vec(coords_vec), m_dim(coords.extent(1))
59 {
60 // Note: coords contains off-processor halo nodes and thus is longer
61 // than coords_vec, which is the same length as the solution vector.
62 // These extra halo nodes are stored at the end.
63 Kokkos::parallel_for( m_coords_vec.extent(0), *this );
64 }
65
66 KOKKOS_INLINE_FUNCTION
67 void operator() (const size_type i) const
68 {
69 for (size_type j=0; j<m_dim; ++j)
70 m_coords_vec(i,j) = m_coords(i,j);
71 }
72 };
73
74 template <typename coords_type, typename coords_vec_type>
75 void fill_coords( const coords_type& coords,
76 const coords_vec_type& coords_vec ) {
77 FillCoords<coords_type,coords_vec_type>(coords, coords_vec);
78 }
79
80 template <typename S, typename V, typename O>
81 struct ExtractEnsembleIts {
82 static std::vector<int>
83 apply(const Belos::SolverManager<S,V,O>& solver) {
84 return std::vector<int>();
85 }
86 };
87
88 template <class SM, class SV, class LO, class GO, class N, class Mesh>
89 result_struct
90 belos_solve(
91 Tpetra::CrsMatrix<SM,LO,GO,N>& A,
92 const Tpetra::MultiVector<SV,LO,GO,N>& b,
93 Tpetra::MultiVector<SV,LO,GO,N>& x,
94 Teuchos::RCP< Tpetra::Operator<SM,LO,GO,N> >& precOp,
95 const Mesh& mesh,
96 const int use_muelu,
97 const int use_mean_based,
98 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
99 const unsigned max_iter = 200,
100 const typename Kokkos::ArithTraits<SV>::mag_type tolerance =
101 Kokkos::ArithTraits<SV>::epsilon())
102 {
103 typedef Tpetra::Operator<SM,LO,GO,N> OperatorType;
104 typedef Tpetra::MultiVector<SV,LO,GO,N> VectorType;
105 typedef typename VectorType::dot_type BelosScalarType;
106 typedef Belos::LinearProblem<BelosScalarType, VectorType, OperatorType> ProblemType;
107 typedef Belos::PseudoBlockCGSolMgr<BelosScalarType, VectorType, OperatorType> CGSolverType;
108 typedef Belos::PseudoBlockGmresSolMgr<BelosScalarType, VectorType, OperatorType> GmresSolverType;
109 typedef Belos::SolverManager<BelosScalarType, VectorType, OperatorType> SolverType;
110 typedef SGPreconditioner<SM,LO,GO,N> PreconditionerType;
111 using Teuchos::RCP;
112 using Teuchos::rcp;
113 using Teuchos::rcpFromRef;
114 using Teuchos::ParameterList;
115
116 // Create some timers used by Belos so we can access them
117 Teuchos::RCP<Teuchos::Time> time_mat_vec =
118 Teuchos::TimeMonitor::getNewTimer("Belos: Operation Op*x");
119 Teuchos::RCP<Teuchos::Time> time_prec_apply =
120 Teuchos::TimeMonitor::getNewTimer("Belos: Operation Prec*x");
121 Teuchos::RCP<Teuchos::Time> time_total;
122 Teuchos::RCP<Teuchos::Time> time_prec_setup =
123 Teuchos::TimeMonitor::getNewTimer("Total MueLu setup time");
124
125 //--------------------------------
126 // Create preconditioner if requested and we weren't given one
127 if (use_muelu && precOp == Teuchos::null) {
128 Teuchos::TimeMonitor timeMon(*time_prec_setup);
129 RCP<PreconditionerType> preconditioner;
130
131 // Create tpetra-vector storing coordinates for repartitioning
132 typename Mesh::node_coord_type node_coords = mesh.node_coord();
133 //Teuchos::RCP<VectorType> coords =
134 Teuchos::RCP<Tpetra::MultiVector<double,LO,GO,N> > coords =
135 Teuchos::rcp(new Tpetra::MultiVector<double,LO,GO,N>(x.getMap(), node_coords.extent(1)));
136 fill_coords(node_coords, coords->getLocalViewDevice(Tpetra::Access::ReadWrite));
137
138 RCP<ParameterList> mueluParams = Teuchos::sublist(fenlParams, "MueLu");
139 if (use_mean_based) {
140 precOp = build_mean_based_muelu_preconditioner(rcpFromRef(A), mueluParams,
141 coords);
142 }
143 else {
144 precOp = build_muelu_preconditioner(rcpFromRef(A), mueluParams, coords);
145 }
146 }
147
148 //--------------------------------
149 // Set up linear solver
150 RCP<ParameterList> belosParams = Teuchos::sublist(fenlParams, "Belos");
151
152 if (!(belosParams->isParameter("Convergence Tolerance")))
153 belosParams->set("Convergence Tolerance", tolerance);
154 if (!(belosParams->isParameter("Maximum Iterations")))
155 belosParams->set("Maximum Iterations", Teuchos::as<int>(max_iter));
156 if (!(belosParams->isParameter("Output Frequency")))
157 belosParams->set("Output Frequency", 1);
158
159 RCP<ProblemType> problem =
160 rcp(new ProblemType(rcpFromRef(A), rcpFromRef(x), rcpFromRef(b)));
161
162 std::string belos_solver = belosParams->get("Belos Solver", "CG");
163 RCP<SolverType> solver;
164 if (belos_solver == "CG" ||
165 belos_solver == "cg") {
166 time_total =
167 Teuchos::TimeMonitor::getNewTimer("Belos: PseudoBlockCGSolMgr total solve time");
168 solver = rcp(new CGSolverType(problem, belosParams));
169 }
170 else if (belos_solver == "GMRES" ||
171 belos_solver == "Gmres" ||
172 belos_solver == "gmres") {
173 time_total =
174 Teuchos::TimeMonitor::getNewTimer("Belos: PseudoBlockGmresSolMgr total solve time");
175 solver = rcp(new GmresSolverType(problem, belosParams));
176 }
177 else
178 TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
179 "Invalid solver " << belos_solver);
180
181 if (use_muelu || use_mean_based){
182 problem->setRightPrec(precOp);
183 }
184 const bool isSet = problem->setProblem();
185
186 TEUCHOS_TEST_FOR_EXCEPTION(!isSet, std::runtime_error,
187 "Belos failed to set problem correctly.");
188
189 //--------------------------------
190 // Solve for nonlinear update
191 Belos::ReturnType result = solver->solve();
192 bool converged = (result == Belos::Converged);
193 TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error,
194 "Belos solver did not converge!");
195
196 result_struct cgsolve;
197 cgsolve.iteration = solver->getNumIters();
198 //cgsolve.norm_res = solver->achievedTol();
199
200 cgsolve.iter_time = time_total->totalElapsedTime();
201 cgsolve.total_time = time_total->totalElapsedTime();
202 cgsolve.matvec_time = time_mat_vec->totalElapsedTime();
203 cgsolve.prec_apply_time = time_prec_apply->totalElapsedTime();
204 cgsolve.prec_setup_time = time_prec_setup->totalElapsedTime();
205
206 // Extract the number of iterations for ensembles
207 cgsolve.ensemble_its =
208 ExtractEnsembleIts<BelosScalarType, VectorType, OperatorType>::apply(*solver);
209 // if (cgsolve.ensemble_its.size() > 0) {
210 // std::cout << std::endl << "ensemble iterations = ";
211 // for (std::size_t i=0; i<cgsolve.ensemble_its.size(); ++i)
212 // std::cout << cgsolve.ensemble_its[i] << " ";
213 // std::cout << std::endl;
214 // }
215
216 return cgsolve;
217 }
218
219
220 } // namespace Example
221 } // namespace Kokkos
222
223 //----------------------------------------------------------------------------
224 //----------------------------------------------------------------------------
225
226 #else
227
228 namespace Kokkos {
229 namespace Example {
230
231 template <class SM, class SV, class LO, class GO, class N, class Mesh>
232 result_struct
233 belos_solve(
234 Tpetra::CrsMatrix<SM,LO,GO,N>& A,
235 const Tpetra::MultiVector<SV,LO,GO,N>& b,
236 Tpetra::MultiVector<SV,LO,GO,N>& x,
237 Teuchos::RCP< Tpetra::Operator<SM,LO,GO,N> >& precOp,
238 const Mesh& mesh,
239 const int use_muelu,
240 const int use_mean_based,
241 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
242 const unsigned max_iter = 200,
243 const typename Kokkos::ArithTraits<SV>::mag_type tolerance =
244 Kokkos::ArithTraits<SV>::epsilon())
245 {
246 TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
247 "Belos solver requested but not compiled!");
248 }
249
250 } // namespace Example
251 } // namespace Kokkos
252
253 #endif
254
255 //----------------------------------------------------------------------------
256 //----------------------------------------------------------------------------
257
258 #endif /* #ifndef KOKKOS_EXAMPLE_BELOS_SOLVE */