| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef __TrilinosCouplings_TpetraIntrepidStructuredPoissonExample_hpp |
| 11 |
#define __TrilinosCouplings_TpetraIntrepidStructuredPoissonExample_hpp |
| 12 |
|
| 13 |
#include "Tpetra_CrsMatrix.hpp" |
| 14 |
#include "Tpetra_Vector.hpp" |
| 15 |
#include "Teuchos_FancyOStream.hpp" |
| 16 |
#include "Teuchos_ScalarTraits.hpp" |
| 17 |
#include "TrilinosCouplings_config.h" |
| 18 |
|
| 19 |
#ifdef HAVE_TRILINOSCOUPLINGS_MUELU |
| 20 |
# include "MueLu_TpetraOperator.hpp" |
| 21 |
#endif // HAVE_TRILINOSCOUPLINGS_MUELU |
| 22 |
|
| 23 |
#include "TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos.hpp" |
| 24 |
|
| 25 |
namespace TrilinosCouplings { |
| 26 |
/// \namespace TpetraIntrepidPoissonExample |
| 27 |
/// \brief Tpetra version of the Intrepid Poisson test problem example. |
| 28 |
/// |
| 29 |
/// The Intrepid Poisson test problem uses Pamgen to construct a 3-D |
| 30 |
/// mesh (a simple rectangular prism with hex elements) in parallel, |
| 31 |
/// Sacado automatic differentiation to construct a right-hand side of |
| 32 |
/// the PDE corresponding to a given exact solution, and Intrepid to |
| 33 |
/// build a discretization. |
| 34 |
/// |
| 35 |
/// We provide two variants of the Intrepid Poisson test: one that |
| 36 |
/// fills Epetra objects, and one that fills Tpetra objects. The two |
| 37 |
/// variants do exactly the same things otherwise, so you can use them |
| 38 |
/// to compare the performance of Epetra and Tpetra fill. |
| 39 |
/// |
| 40 |
/// This namespace contains the Tpetra variant. It defines typedefs |
| 41 |
/// which you can use when writing a main() driver to run the test. |
| 42 |
/// The makeMatrixAndRightHandSide() function does all the work. You |
| 43 |
/// can use the exactResidualNorm() function to test correctness of |
| 44 |
/// the discretization. In particular, if the continuous exact |
| 45 |
/// solution was chosen from the space of finite element polynomials, |
| 46 |
/// the exact solution of the discrete linear system AX=B should match |
| 47 |
/// the continuous exact solution exactly, modulo rounding error when |
| 48 |
/// assembling the discretization. |
| 49 |
/// |
| 50 |
/// The solveWithBelos() function solves the given linear system using |
| 51 |
/// a Belos iterative solver. You can provide a left and/or right |
| 52 |
/// preconditioner if you want. |
| 53 |
namespace TpetraIntrepidPoissonExample { |
| 54 |
|
| 55 |
// |
| 56 |
// mfh 19 Apr 2012: Leave these typedefs up here for use by main() and |
| 57 |
// the other functions. This example probably only works for ST = |
| 58 |
// double and LO,GO = int, but it probably works for other Node types |
| 59 |
// besides the default. |
| 60 |
// |
| 61 |
typedef double ST; |
| 62 |
typedef Tpetra::Map<>::local_ordinal_type LO; |
| 63 |
typedef Tpetra::Map<>::global_ordinal_type GO; |
| 64 |
typedef Tpetra::Map<>::node_type Node; |
| 65 |
typedef Teuchos::ScalarTraits<ST> STS; |
| 66 |
typedef STS::magnitudeType MT; |
| 67 |
|
| 68 |
// |
| 69 |
// mfh 19 Apr 2012: If you want to change the template parameters of |
| 70 |
// these typedefs, modify the typedefs (ST, LO, GO, Node) above. |
| 71 |
// |
| 72 |
typedef Tpetra::CrsMatrix<ST, LO, GO, Node> sparse_matrix_type; |
| 73 |
typedef Tpetra::Operator<ST, LO, GO, Node> operator_type; |
| 74 |
typedef Tpetra::MultiVector<ST, LO, GO, Node> multivector_type; |
| 75 |
typedef Tpetra::MultiVector<MT, LO, GO, Node> realmultivector_type; |
| 76 |
typedef Tpetra::Vector<ST, LO, GO, Node> vector_type; |
| 77 |
|
| 78 |
/// \brief Create the mesh and build the linear system to solve. |
| 79 |
/// |
| 80 |
/// \param A [out] The sparse matrix. |
| 81 |
/// \param B [out] The right-hand side(s). |
| 82 |
/// \param X_exact [out] The exact solution of the PDE, projected onto |
| 83 |
/// the discrete mesh. This may not necessarily be the same as the |
| 84 |
/// exact solution of the discrete linear system. |
| 85 |
/// \param X [out] The approximate solution(s). |
| 86 |
/// \param err [out] Output stream for errors. |
| 87 |
/// \param out [out] Output stream for verbose output. |
| 88 |
/// \param comm [in] Communicator. |
| 89 |
/// \param node [in/out] Kokkos Node instance. |
| 90 |
/// \param meshInput [in] Pamgen mesh specification string. |
| 91 |
/// |
| 92 |
/// Pamgen is a parallel mesh generation library (that nevertheless |
| 93 |
/// performs no communication, since it limits itself to generate |
| 94 |
/// simple meshes). Here is its technical report: |
| 95 |
/// |
| 96 |
/// @techreport{hensinger2008pamgen, |
| 97 |
/// author = "David M. Hensinger and Richard R. Drake and James G. Foucar and Thomas A. Gardiner", |
| 98 |
/// title = "Pamgen, a Library for Parallel Generation of Simple Finite Element Meshes", |
| 99 |
/// institution = "Sandia National Laboratories", |
| 100 |
/// number = "SAND2008-1933", |
| 101 |
/// month = "April", |
| 102 |
/// year = "2008" |
| 103 |
/// } |
| 104 |
/// |
| 105 |
/// Quote from its abstract: "\textsc{Pamgen} is a parallel mesh |
| 106 |
/// generation library that allows on-the-fly scalable generation of |
| 107 |
/// hexahedral and quadrilateral finite element meshes for several |
| 108 |
/// simple geometries. It has been used to generate more than 1.1 |
| 109 |
/// billion elements on 17,576 processors." |
| 110 |
/// |
| 111 |
/// Pamgen takes a string of commands as input. Read the |
| 112 |
/// "Poisson.xml" file in this directory for an example. The example |
| 113 |
/// uses a "rectilinear" mesh, which has the following fields: |
| 114 |
/// |
| 115 |
/// nx: Number of cells in X direction |
| 116 |
/// ny: Number of cells in Y direction |
| 117 |
/// nz: Number of cells in Z direction |
| 118 |
/// bx: Number of blocks in X direction |
| 119 |
/// by: Number of blocks in Y direction |
| 120 |
/// bz: Number of blocks in Z direction |
| 121 |
/// gmin: Minimum domain coordinates x y z |
| 122 |
/// gmax: Maximum domain coordinates x y z |
| 123 |
/// |
| 124 |
/// Poisson.xml specifies a cube that lives in the unit positive |
| 125 |
/// octant (that is, (x,y,z) with 0 <= x,y,z <= 1), with 20 cells |
| 126 |
/// along each dimension (thus, 20*20*20 = 8000 cells total). If you |
| 127 |
/// want to make the problem bigger, you can scale up nx, ny, and/or |
| 128 |
/// nz. It's probably better for communication balance to scale them |
| 129 |
/// equally. |
| 130 |
/// |
| 131 |
/// The "set assign ... end" statement specifies boundaries |
| 132 |
/// ("nodesets" and "sidesets") of the domain. The Poisson.xml |
| 133 |
/// example names the exterior faces of the cube with IDs. |
| 134 |
/// |
| 135 |
/// The Poisson.xml example does not include a (parallel) |
| 136 |
/// "decomposition strategy ... end" statement. The default strategy |
| 137 |
/// is "bisection," which attempts to assign an equal number of cells |
| 138 |
/// to each parallel process. |
| 139 |
void |
| 140 |
makeMatrixAndRightHandSide (Teuchos::RCP<sparse_matrix_type>& A, |
| 141 |
Teuchos::RCP<vector_type>& B, |
| 142 |
Teuchos::RCP<vector_type>& X_exact, |
| 143 |
Teuchos::RCP<vector_type>& X, |
| 144 |
Teuchos::Array<Teuchos::Array<MT> >& coordArray, |
| 145 |
Teuchos::Array<LO>& lNodesPerDim, |
| 146 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 147 |
const std::string& meshInput, |
| 148 |
const Teuchos::RCP<Teuchos::FancyOStream>& out, |
| 149 |
const Teuchos::RCP<Teuchos::FancyOStream>& err, |
| 150 |
const bool verbose = false, |
| 151 |
const bool debug = false); |
| 152 |
|
| 153 |
//! Just like above, but with multivector_type output arguments. |
| 154 |
void |
| 155 |
makeMatrixAndRightHandSide (Teuchos::RCP<sparse_matrix_type>& A, |
| 156 |
Teuchos::RCP<multivector_type>& B, |
| 157 |
Teuchos::RCP<multivector_type>& X_exact, |
| 158 |
Teuchos::RCP<multivector_type>& X, |
| 159 |
Teuchos::Array<Teuchos::Array<MT> >& coordArray, |
| 160 |
Teuchos::Array<LO>& lNodesPerDim, |
| 161 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 162 |
const std::string& meshInput, |
| 163 |
const Teuchos::RCP<Teuchos::FancyOStream>& out, |
| 164 |
const Teuchos::RCP<Teuchos::FancyOStream>& err, |
| 165 |
const bool verbose = false, |
| 166 |
const bool debug = false); |
| 167 |
|
| 168 |
//! Return \f$\|B - A X_{\text{exact}}\|_2\f$, \f$\|B\|\f$, and \f$\|A\|_F\f$. |
| 169 |
std::vector<Teuchos::ScalarTraits<ST>::magnitudeType> |
| 170 |
exactResidualNorm (const Teuchos::RCP<const sparse_matrix_type>& A, |
| 171 |
const Teuchos::RCP<const vector_type>& B, |
| 172 |
const Teuchos::RCP<const vector_type>& X_exact); |
| 173 |
|
| 174 |
/// \brief Solve the linear system(s) AX=B with Belos. |
| 175 |
/// |
| 176 |
/// X and B are both multivectors, meaning that you may ask Belos to |
| 177 |
/// solve more than one linear system at a time. X and B must have |
| 178 |
/// the same number of columns. |
| 179 |
/// |
| 180 |
/// This interface will change in the future to accept the name of the |
| 181 |
/// Belos solver to use. For now, the solver is hard-coded to |
| 182 |
/// Pseudoblock CG (implemented by Belos::PseudoBlockCGSolMgr). |
| 183 |
/// |
| 184 |
/// \param converged [out] Whether Belos reported that the iterative |
| 185 |
/// method converged (solved the linear system to the desired |
| 186 |
/// tolerance). |
| 187 |
/// |
| 188 |
/// \param numItersPerformed [out] Number of iterations that the Belos |
| 189 |
/// solver performed. |
| 190 |
/// |
| 191 |
/// \param solverName [in] Which iterative linear solver to use. |
| 192 |
/// Any name that Belos::SolverFactory knows will work here. |
| 193 |
/// |
| 194 |
/// \param tol [in] Convergence tolerance for the iterative method. |
| 195 |
/// The meaning of this depends on the particular iterative method. |
| 196 |
/// |
| 197 |
/// \param maxNumIters [in] Maximum number of iterations that the |
| 198 |
/// iterative method should perform, regardless of whether it |
| 199 |
/// converged. |
| 200 |
/// |
| 201 |
/// \param num_steps [in] Number of "time steps", i.e., the number of |
| 202 |
// times the solver is called in a fake time-step loop. |
| 203 |
/// |
| 204 |
/// \param X [in/out] On input: the initial guess(es) for the iterative |
| 205 |
/// method. On output: the computed approximate solution. |
| 206 |
/// |
| 207 |
/// \param A [in] The matrix in the linear system(s) AX=B to solve. |
| 208 |
/// |
| 209 |
/// \param B [in] The right-hand side(s) in the linear system AX=B to solve. |
| 210 |
/// |
| 211 |
/// \param M_left [in] If nonnull, a left preconditioner that the |
| 212 |
/// iterative method may use. If null, the iterative method will |
| 213 |
/// not use a left preconditioner. |
| 214 |
/// |
| 215 |
/// \param M_right [in] If nonnull, a right preconditioner that the |
| 216 |
/// iterative method may use. If null, the iterative method will |
| 217 |
/// not use a right preconditioner. |
| 218 |
void |
| 219 |
solveWithBelos (bool& converged, |
| 220 |
int& numItersPerformed, |
| 221 |
const std::string& solverName, |
| 222 |
const Teuchos::ScalarTraits<ST>::magnitudeType& tol, |
| 223 |
const int maxNumIters, |
| 224 |
const int num_steps, |
| 225 |
const Teuchos::RCP<multivector_type>& X, |
| 226 |
const Teuchos::RCP<const sparse_matrix_type>& A, |
| 227 |
const Teuchos::RCP<const multivector_type>& B, |
| 228 |
const Teuchos::RCP<const operator_type>& M_left=Teuchos::null, |
| 229 |
const Teuchos::RCP<const operator_type>& M_right=Teuchos::null); |
| 230 |
|
| 231 |
/// \brief Solve the linear system(s) AX=B with Belos on the GPU. |
| 232 |
/// |
| 233 |
/// In addition to the parameters taken by solveWithBelos(), this function |
| 234 |
/// additionally takes the following parameters to determine which GPU |
| 235 |
/// device should be attached to which MPI rank: |
| 236 |
/// |
| 237 |
/// \param ranks_per_node [in] Number of MPI ranks per node. For OpenMPI and |
| 238 |
/// MVAPICH MPI libraries, this is determined automatically and thus needn't |
| 239 |
/// be specified. For other MPI libraries it must be supplied by the user. |
| 240 |
/// |
| 241 |
/// \param gpu_ranks_per_node [in] Number of MPI ranks per node associated |
| 242 |
/// with GPUs. This must be less than or equal to the number of devices |
| 243 |
/// per node. |
| 244 |
/// |
| 245 |
/// \param device_offset [in] Offset for computing the CUDA device ID for |
| 246 |
/// each MPI rank associated with a GPU. If there are n GPU's per node, |
| 247 |
/// the first n MPI ranks for each node will be associated with a GPU starting |
| 248 |
/// at device number device_offset. |
| 249 |
/// |
| 250 |
/// \param prec_type [in] The preconditioner type (e.g., "MueLu"). |
| 251 |
void |
| 252 |
solveWithBelosGPU ( |
| 253 |
bool& converged, |
| 254 |
int& numItersPerformed, |
| 255 |
const Teuchos::ScalarTraits<ST>::magnitudeType& tol, |
| 256 |
const int maxNumIters, |
| 257 |
const int num_steps, |
| 258 |
const int ranks_per_node, |
| 259 |
const int gpu_ranks_per_node, |
| 260 |
const int device_offset, |
| 261 |
const std::string& prec_type, |
| 262 |
const Teuchos::RCP<multivector_type>& X, |
| 263 |
const Teuchos::RCP<const sparse_matrix_type>& A, |
| 264 |
const Teuchos::RCP<const multivector_type>& B, |
| 265 |
const Teuchos::RCP<const operator_type>& M_left=Teuchos::null, |
| 266 |
const Teuchos::RCP<const operator_type>& M_right=Teuchos::null); |
| 267 |
|
| 268 |
} // namespace TpetraIntrepidStructuredPoissonExample |
| 269 |
} // namespace TrilinosCouplings |
| 270 |
|
| 271 |
#endif // __TrilinosCouplings_TpetraIntrepidStructuredPoissonExample_hpp |