| 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 |
#include "TrilinosCouplings_config.h" |
| 11 |
#include "TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos.hpp" |
| 12 |
#include "TrilinosCouplings_TpetraIntrepidPoissonExample.hpp" |
| 13 |
#include <BelosTpetraAdapter.hpp> |
| 14 |
|
| 15 |
#include "Teuchos_Comm.hpp" |
| 16 |
|
| 17 |
#include <cstdlib> |
| 18 |
|
| 19 |
namespace TrilinosCouplings { |
| 20 |
namespace TpetraIntrepidPoissonExample { |
| 21 |
|
| 22 |
void |
| 23 |
solveWithBelos (bool& converged, |
| 24 |
int& numItersPerformed, |
| 25 |
const std::string& solverName, |
| 26 |
const Teuchos::ScalarTraits<ST>::magnitudeType& tol, |
| 27 |
const int maxNumIters, |
| 28 |
const int num_steps, |
| 29 |
const Teuchos::RCP<multivector_type>& X, |
| 30 |
const Teuchos::RCP<const sparse_matrix_type>& A, |
| 31 |
const Teuchos::RCP<const multivector_type>& B, |
| 32 |
const Teuchos::RCP<const operator_type>& M_left, |
| 33 |
const Teuchos::RCP<const operator_type>& M_right) |
| 34 |
{ |
| 35 |
typedef multivector_type MV; |
| 36 |
typedef operator_type OP; |
| 37 |
|
| 38 |
// Invoke the generic solve routine. |
| 39 |
IntrepidPoissonExample::solveWithBelos<ST, MV, OP> (converged, numItersPerformed, |
| 40 |
solverName, tol, maxNumIters, |
| 41 |
num_steps, |
| 42 |
X, A, B, M_left, M_right); |
| 43 |
} |
| 44 |
|
| 45 |
/// \brief Solve the linear system(s) AX=B with Belos by cloning to a new |
| 46 |
/// node type. |
| 47 |
/// |
| 48 |
/// This works just like the above solveWithBelos() function except it clones |
| 49 |
/// the matrix, LHS, RHS, and preconditioner(s) to the new node type. This is |
| 50 |
/// useful for solving the linear system on, e.g., a GPU. |
| 51 |
template <typename CloneNode> |
| 52 |
void |
| 53 |
cloneAndSolveWithBelos ( |
| 54 |
bool& converged, |
| 55 |
int& numItersPerformed, |
| 56 |
const Teuchos::ScalarTraits<ST>::magnitudeType& tol, |
| 57 |
const int maxNumIters, |
| 58 |
const int num_steps, |
| 59 |
const Teuchos::RCP<multivector_type>& X, |
| 60 |
const Teuchos::RCP<const sparse_matrix_type>& A, |
| 61 |
const Teuchos::RCP<const multivector_type>& B, |
| 62 |
const std::string& prec_type, |
| 63 |
const Teuchos::RCP<const operator_type>& M_left=Teuchos::null, |
| 64 |
const Teuchos::RCP<const operator_type>& M_right=Teuchos::null) { |
| 65 |
|
| 66 |
using Teuchos::ParameterList; |
| 67 |
using Teuchos::parameterList; |
| 68 |
using Teuchos::RCP; |
| 69 |
using Teuchos::rcp_dynamic_cast; |
| 70 |
|
| 71 |
typedef Tpetra::CrsMatrix<ST, LO, GO, CloneNode> clone_sparse_matrix_type; |
| 72 |
typedef Tpetra::Operator<ST, LO, GO, CloneNode> clone_operator_type; |
| 73 |
typedef Tpetra::MultiVector<ST, LO, GO, CloneNode> clone_multi_vector_type; |
| 74 |
typedef clone_multi_vector_type MV; |
| 75 |
typedef clone_operator_type OP; |
| 76 |
|
| 77 |
// Clone Matrix, RHS, LHS |
| 78 |
RCP<ParameterList> plClone = parameterList(); |
| 79 |
RCP<clone_sparse_matrix_type> A_clone; |
| 80 |
RCP<clone_multi_vector_type> B_clone, X_clone; |
| 81 |
{ |
| 82 |
TEUCHOS_FUNC_TIME_MONITOR_DIFF("Clone System", clone_system); |
| 83 |
A_clone = rcp(new clone_sparse_matrix_type(A, Teuchos::Copy)); |
| 84 |
B_clone = rcp(new clone_multi_vector_type(B, Teuchos::Copy)); |
| 85 |
X_clone = rcp(new clone_multi_vector_type(X, Teuchos::Copy)); |
| 86 |
} |
| 87 |
|
| 88 |
// Clone preconditioner(s) |
| 89 |
RCP<const clone_operator_type> M_left_clone, M_right_clone; |
| 90 |
{ |
| 91 |
TEUCHOS_FUNC_TIME_MONITOR_DIFF("Clone Preconditioner", clone_prec); |
| 92 |
|
| 93 |
#ifdef HAVE_TRILINOSCOUPLINGS_MUELU |
| 94 |
if (M_left != Teuchos::null && prec_type == "MueLu") { |
| 95 |
RCP< const MueLu::TpetraOperator<ST,LO,GO,Node> > M_muelu = |
| 96 |
rcp_dynamic_cast<const MueLu::TpetraOperator<ST,LO,GO,Node> >(M_left); |
| 97 |
M_left_clone = rcp(new clone_operator_type(M_muelu)); |
| 98 |
} |
| 99 |
if (M_right != Teuchos::null && prec_type == "MueLu") { |
| 100 |
RCP< const MueLu::TpetraOperator<ST,LO,GO,Node> > M_muelu = |
| 101 |
rcp_dynamic_cast<const MueLu::TpetraOperator<ST,LO,GO,Node> >(M_right); |
| 102 |
M_right_clone = rcp(new clone_operator_type(M_muelu)); |
| 103 |
} |
| 104 |
#else |
| 105 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 106 |
prec_type == "MueLu", std::runtime_error, "Tpetra scaling example: " |
| 107 |
"In order to precondition with MueLu, you must have built Trilinos " |
| 108 |
"with the MueLu package enabled."); |
| 109 |
#endif // HAVE_TRILINOSCOUPLINGS_MUELU |
| 110 |
} |
| 111 |
|
| 112 |
// Solve |
| 113 |
{ |
| 114 |
TEUCHOS_FUNC_TIME_MONITOR_DIFF("Clone Solve", clone_solve); |
| 115 |
IntrepidPoissonExample::solveWithBelos<ST,MV,OP> ( |
| 116 |
converged, numItersPerformed, tol, maxNumIters, num_steps, |
| 117 |
X_clone, A_clone, B_clone, M_left_clone, M_right_clone); |
| 118 |
} |
| 119 |
|
| 120 |
// Copy X_clone back into X |
| 121 |
{ |
| 122 |
TEUCHOS_FUNC_TIME_MONITOR_DIFF("Clone Solution", clone_sol); |
| 123 |
RCP<multivector_type> X_host = rcp(new multivector_type(X_clone, Teuchos::Copy)); |
| 124 |
X->update(1.0, *X_host, 0.0); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
solveWithBelosGPU ( |
| 130 |
bool& converged, |
| 131 |
int& numItersPerformed, |
| 132 |
const Teuchos::ScalarTraits<ST>::magnitudeType& tol, |
| 133 |
const int maxNumIters, |
| 134 |
const int num_steps, |
| 135 |
const int ranks_per_node, |
| 136 |
const int gpu_ranks_per_node, |
| 137 |
const int device_offset, |
| 138 |
const std::string& prec_type, |
| 139 |
const Teuchos::RCP<multivector_type>& X, |
| 140 |
const Teuchos::RCP<const sparse_matrix_type>& A, |
| 141 |
const Teuchos::RCP<const multivector_type>& B, |
| 142 |
const Teuchos::RCP<const operator_type>& M_left, |
| 143 |
const Teuchos::RCP<const operator_type>& M_right) { |
| 144 |
|
| 145 |
|
| 146 |
// FIXME (mfh 11 Feb 2015) I am removing |
| 147 |
// KokkosClassic::ThrustGPUNode, so this example won't build. We |
| 148 |
// need to test this example, though! We should figure out how to |
| 149 |
// test it in a sensible way without needing ThrustGPUNode. |
| 150 |
#if 0 // ifdef HAVE_KOKKOSCLASSIC_THRUST |
| 151 |
using Teuchos::Comm; |
| 152 |
using Teuchos::outArg; |
| 153 |
using Teuchos::ParameterList; |
| 154 |
using Teuchos::parameterList; |
| 155 |
using Teuchos::RCP; |
| 156 |
using Teuchos::rcp; |
| 157 |
|
| 158 |
// Compute whether we are a CPU or GPU rank, and GPU device ID |
| 159 |
// The first gpu_ranks_per_node ranks are associated with GPUs |
| 160 |
// (may not be the best choice performance-wise) |
| 161 |
RCP<const Comm<int> > comm = X->getMap()->getComm(); |
| 162 |
int num_ranks = comm->getSize(); |
| 163 |
int my_rank = comm->getRank(); |
| 164 |
|
| 165 |
// Compute rank local to node |
| 166 |
int node_rank; |
| 167 |
char *str; |
| 168 |
if ((str = std::getenv("MV2_COMM_WORLD_LOCAL_RANK")) != NULL) { |
| 169 |
node_rank = std::atoi(str); |
| 170 |
} |
| 171 |
else if ((str = std::getenv("OMPI_COMM_WORLD_LOCAL_RANK")) != NULL) { |
| 172 |
node_rank = std::atoi(str); |
| 173 |
} |
| 174 |
else { |
| 175 |
// Default to user telling us the number of ranks_per_node and |
| 176 |
// assuming ranks are laid out consecutively on each node |
| 177 |
int num_node = num_ranks / ranks_per_node; |
| 178 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 179 |
num_node*ranks_per_node != num_ranks, std::logic_error, |
| 180 |
"ranks_per_node does not evenly divide num_ranks"); |
| 181 |
node_rank = num_node == 1 ? my_rank : my_rank % num_node; |
| 182 |
} |
| 183 |
bool gpu_rank = node_rank < gpu_ranks_per_node; |
| 184 |
int num_device; cudaGetDeviceCount(&num_device); |
| 185 |
int device_id = node_rank + device_offset; |
| 186 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 187 |
gpu_ranks_per_node > num_device, std::logic_error, |
| 188 |
"gpu_ranks_per_node cannot exceed number of GPU devices"); |
| 189 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 190 |
gpu_rank && device_id > num_device, std::logic_error, |
| 191 |
"Invalid device ID " << device_id << ". You probably are trying" << |
| 192 |
" to run with too many MPI ranks"); |
| 193 |
|
| 194 |
ParameterList node_params; |
| 195 |
if (gpu_rank) { |
| 196 |
std::cout << "MPI rank " << my_rank |
| 197 |
<< ": Attached to GPU " << device_id |
| 198 |
<< std::endl; |
| 199 |
|
| 200 |
#if TPETRA_USE_KOKKOS_DISTOBJECT && defined(KOKKOS_ENABLE_CUDA) |
| 201 |
if (!Kokkos::HostSpace::execution_space::is_initialized()) |
| 202 |
Kokkos::HostSpace::execution_space::initialize(); |
| 203 |
if (!Kokkos::Cuda::is_initialized()) |
| 204 |
Kokkos::Cuda::initialize( Kokkos::Cuda::SelectDevice(device_id) ); |
| 205 |
#endif |
| 206 |
node_params.set("Verbose", 1); |
| 207 |
node_params.set("Device Number", device_id); |
| 208 |
RCP<KokkosClassic::ThrustGPUNode> gpu_node = |
| 209 |
rcp(new KokkosClassic::ThrustGPUNode(node_params)); |
| 210 |
cloneAndSolveWithBelos( |
| 211 |
converged, numItersPerformed, tol, maxNumIters, num_steps, |
| 212 |
gpu_node, X, A, B, prec_type, M_left, M_right); |
| 213 |
#if TPETRA_USE_KOKKOS_DISTOBJECT && defined(KOKKOS_ENABLE_CUDA) |
| 214 |
Kokkos::Cuda::finalize(); |
| 215 |
Kokkos::HostSpace::execution_space::finalize(); |
| 216 |
#endif |
| 217 |
} |
| 218 |
else { |
| 219 |
// Note for the non-GPU ranks, we still have to clone since new |
| 220 |
// matrices are created which call fillComplete() (which involves |
| 221 |
// communication) |
| 222 |
RCP<Node> node = X->getMap()->getNode(); |
| 223 |
cloneAndSolveWithBelos( |
| 224 |
converged, numItersPerformed, tol, maxNumIters, num_steps, |
| 225 |
node, X, A, B, prec_type, M_left, M_right); |
| 226 |
} |
| 227 |
|
| 228 |
#else // don't have ThrustGPUNode |
| 229 |
|
| 230 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 231 |
true, std::logic_error, "KokkosClassic::ThrustGPUNode has been deprecated " |
| 232 |
"and removed. Please rewrite this function to use " |
| 233 |
"Tpetra::KokkosCompat::KokkosCudaWrapperNode instead."); |
| 234 |
|
| 235 |
#endif // whether ThrustGPUNode exists |
| 236 |
} |
| 237 |
|
| 238 |
} // namespace TpetraIntrepidPoissonExample |
| 239 |
} // namespace TrilinosCouplings |