| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
// Matrix-free example 01: Xpetra operator that generates |
| 11 |
// a simple tridiagonal finite difference poisson matrix |
| 12 |
// |
| 13 |
// This solves the problem f''(x) = h^2*(-pi^2)*sin(pi*x) |
| 14 |
// on the domain [0,1] with a finite difference method. |
| 15 |
// This supplies n global DOFs, and breaks it into |
| 16 |
// contiguous segments depending on the number of MPI ranks. |
| 17 |
|
| 18 |
// STL includes |
| 19 |
#include <iostream> |
| 20 |
#include <vector> |
| 21 |
#include <set> |
| 22 |
#include <stdio.h> |
| 23 |
#include <random> |
| 24 |
#include <unistd.h> |
| 25 |
#include <cmath> |
| 26 |
|
| 27 |
// Teuchos includes |
| 28 |
#include "Teuchos_GlobalMPISession.hpp" |
| 29 |
#include "Teuchos_RCP.hpp" |
| 30 |
#include "Teuchos_ParameterList.hpp" |
| 31 |
#include "Teuchos_Time.hpp" |
| 32 |
#include "Teuchos_TimeMonitor.hpp" |
| 33 |
|
| 34 |
// Kokkos include |
| 35 |
#include "Kokkos_Core.hpp" |
| 36 |
//#include "kokkosTools.hpp" |
| 37 |
|
| 38 |
// Tpetra includes |
| 39 |
#include "Tpetra_Map.hpp" |
| 40 |
#include "Tpetra_MultiVector.hpp" |
| 41 |
#include "Tpetra_CrsMatrix.hpp" |
| 42 |
#include "Tpetra_Import.hpp" |
| 43 |
#include "Tpetra_Export.hpp" |
| 44 |
#include "MatrixMarket_Tpetra.hpp" |
| 45 |
|
| 46 |
// Xpetra includes |
| 47 |
#include "Xpetra_TpetraMultiVector.hpp" |
| 48 |
#include "Xpetra_Map.hpp" |
| 49 |
#include "Xpetra_MultiVector.hpp" |
| 50 |
#include "Xpetra_Operator.hpp" |
| 51 |
#include "Xpetra_Import.hpp" |
| 52 |
#include "Xpetra_Export.hpp" |
| 53 |
#include "Xpetra_Utils.hpp" |
| 54 |
#include "Xpetra_Parameters.hpp" |
| 55 |
|
| 56 |
// MueLu headers |
| 57 |
#include "MueLu_Level.hpp" |
| 58 |
#include "MueLu_Hierarchy.hpp" |
| 59 |
#include "MueLu_HierarchyManager.hpp" |
| 60 |
#include "MueLu_SmootherPrototype.hpp" |
| 61 |
#include "MueLu_SmootherFactory.hpp" |
| 62 |
#include "MueLu_ParameterListInterpreter.hpp" |
| 63 |
#include "MueLu_AmalgamationFactory.hpp" |
| 64 |
#include "MueLu_CoordinatesTransferFactory.hpp" |
| 65 |
#include "MueLu_UncoupledAggregationFactory.hpp" |
| 66 |
#include "MueLu_AggregationExportFactory.hpp" |
| 67 |
#include "MueLu_Factory.hpp" |
| 68 |
|
| 69 |
// Belos includes (this is the only package which may be disabled in some configurations) |
| 70 |
#ifdef HAVE_MUELU_BELOS |
| 71 |
#include "BelosConfigDefs.hpp" |
| 72 |
#include "BelosLinearProblem.hpp" |
| 73 |
#include "BelosBlockCGSolMgr.hpp" |
| 74 |
#include "BelosBlockGmresSolMgr.hpp" |
| 75 |
#include "BelosXpetraAdapter.hpp" // this header defines Belos::XpetraOp() |
| 76 |
#include "BelosMueLuAdapter.hpp" // this header defines Belos::MueLuOp() |
| 77 |
#include "BelosTpetraAdapter.hpp" |
| 78 |
#include "BelosTpetraOperator.hpp" |
| 79 |
#include "BelosBlockGmresSolMgr.hpp" |
| 80 |
#include "BelosBlockCGSolMgr.hpp" |
| 81 |
#include "BelosBiCGStabSolMgr.hpp" |
| 82 |
#include "BelosGCRODRSolMgr.hpp" |
| 83 |
#include "BelosPCPGSolMgr.hpp" |
| 84 |
#include "BelosPseudoBlockCGSolMgr.hpp" |
| 85 |
#include "BelosPseudoBlockGmresSolMgr.hpp" |
| 86 |
#include "BelosPseudoBlockStochasticCGSolMgr.hpp" |
| 87 |
#include "BelosPseudoBlockTFQMRSolMgr.hpp" |
| 88 |
#include "BelosRCGSolMgr.hpp" |
| 89 |
#include "BelosTFQMRSolMgr.hpp" |
| 90 |
#endif |
| 91 |
|
| 92 |
// some things like multivectors are "2D views" but only appear as 1D in practice, so we macro a print statement |
| 93 |
#define PRINT_VIEW2_LINEAR(view) \ |
| 94 |
std::cout << #view << " (" << view.extent(0) << "," << view.extent(1) << ") = [" << std::endl; \ |
| 95 |
for (unsigned int i = 0; i < view.extent(0); ++i) \ |
| 96 |
for (unsigned int j = 0; j < view.extent(1); ++j) \ |
| 97 |
std::cout << view(i, j) << " "; \ |
| 98 |
std::cout << "]" << std::endl; |
| 99 |
|
| 100 |
/** |
| 101 |
* This class defines an operator corresponding to the |
| 102 |
* [-1 2 -1] tridiagonal matrix usually obtained from |
| 103 |
* a 1D finite difference discretization. |
| 104 |
*/ |
| 105 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 106 |
class TridiagonalOperator : public Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> { |
| 107 |
public: |
| 108 |
// Xpetra::Operator subclasses should always define typedefs according to Xpetra |
| 109 |
typedef typename Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>::node_type node_type; |
| 110 |
typedef typename Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> MV; |
| 111 |
typedef typename Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type; |
| 112 |
typedef typename Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> import_type; |
| 113 |
typedef typename Xpetra::Export<LocalOrdinal, GlobalOrdinal, Node> export_type; |
| 114 |
|
| 115 |
public: |
| 116 |
/** Constructor |
| 117 |
* \param[in] n The number of global DOFs |
| 118 |
* \param[in] comm The Teuchos::Comm for the object |
| 119 |
*/ |
| 120 |
TridiagonalOperator(const GlobalOrdinal n, |
| 121 |
const Teuchos::RCP<const Teuchos::Comm<int>> comm) { |
| 122 |
TEUCHOS_TEST_FOR_EXCEPTION(comm.is_null(), std::invalid_argument, "TridiagonalOperator constructor: The input Comm object must be nonnull."); |
| 123 |
|
| 124 |
const int my_rank = comm->getRank(); |
| 125 |
const int num_procs = comm->getSize(); |
| 126 |
|
| 127 |
// Construct a default map and let it choose how DOFs are divided |
| 128 |
// Note: This assumes the map constructor is generating contiguous local DOFs |
| 129 |
const GlobalOrdinal index_base = 0; |
| 130 |
opMap_ = Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(Xpetra::UseTpetra, n, index_base, comm); |
| 131 |
LocalOrdinal nlocal = opMap_->getLocalNumElements(); |
| 132 |
|
| 133 |
// Ghosting: procs 0,1,...,n_p-1 are ordered left to right on [0,1] |
| 134 |
if (my_rank > 0) |
| 135 |
++nlocal; |
| 136 |
if (my_rank < num_procs - 1) |
| 137 |
++nlocal; |
| 138 |
|
| 139 |
// Construct a list of columns where this process has nonzero elements |
| 140 |
// For this tridiagonal matrix, this is firstRowItOwns-1:lastRowItOwns+1 |
| 141 |
std::vector<GlobalOrdinal> indices; |
| 142 |
indices.reserve(nlocal); |
| 143 |
if (my_rank > 0) |
| 144 |
indices.push_back(opMap_->getMinGlobalIndex() - 1); |
| 145 |
for (GlobalOrdinal i = opMap_->getMinGlobalIndex(); i <= opMap_->getMaxGlobalIndex(); ++i) |
| 146 |
indices.push_back(i); |
| 147 |
if (my_rank < num_procs - 1) |
| 148 |
indices.push_back(opMap_->getMaxGlobalIndex() + 1); |
| 149 |
Teuchos::ArrayView<const GlobalOrdinal> element_list(indices); |
| 150 |
|
| 151 |
// column Map for handling the redistribution |
| 152 |
const GlobalOrdinal num_global_elements = n + 2 * (num_procs - 1); |
| 153 |
redistMap_ = Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(Xpetra::UseTpetra, num_global_elements, element_list, index_base, comm); |
| 154 |
|
| 155 |
// import object that describes how data will be redistributed |
| 156 |
importer_ = Xpetra::ImportFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(opMap_, redistMap_); |
| 157 |
}; |
| 158 |
|
| 159 |
// Required since we inherit from Xpetra::Operator |
| 160 |
// Destructor |
| 161 |
virtual ~TridiagonalOperator() {} |
| 162 |
|
| 163 |
/** |
| 164 |
* \brief Compute Y := alpha Op X + beta Y. |
| 165 |
* \param[in] X Vector to apply the operator to |
| 166 |
* \param[in] Y Vector to update |
| 167 |
* \param[in] mode Transpose mode (unused since this is symmetric) |
| 168 |
* \param[in] alpha Magnitude of Op*X term |
| 169 |
* \param[in] beta Magnitude of Y term |
| 170 |
*/ |
| 171 |
void |
| 172 |
apply(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X, |
| 173 |
Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y, |
| 174 |
Teuchos::ETransp mode = Teuchos::NO_TRANS, |
| 175 |
Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(), |
| 176 |
Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const { |
| 177 |
// Setup: get comms, ranks, procs |
| 178 |
Teuchos::RCP<const Teuchos::Comm<int>> comm = opMap_->getComm(); |
| 179 |
const int my_rank = comm->getRank(); |
| 180 |
const int num_procs = comm->getSize(); |
| 181 |
const size_t num_vecs = X.getNumVectors(); |
| 182 |
const LocalOrdinal numlocrows = static_cast<LocalOrdinal>(X.getLocalLength()); |
| 183 |
|
| 184 |
// Make a temporary multivector for holding the redistributed data and then redistribute |
| 185 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> redistDataX = Xpetra::MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(redistMap_, num_vecs); |
| 186 |
redistDataX->doImport(X, *importer_, Xpetra::INSERT); |
| 187 |
|
| 188 |
// Get a view of the multivector |
| 189 |
auto KokkosViewX = redistDataX->getDeviceLocalView(Xpetra::Access::ReadOnly); |
| 190 |
auto KokkosViewY = Y.getDeviceLocalView(Xpetra::Access::ReadWrite); |
| 191 |
|
| 192 |
// Perform the matvec with the locally owned data |
| 193 |
// For each column |
| 194 |
for (size_t c = 0; c < num_vecs; ++c) { |
| 195 |
LocalOrdinal offset; |
| 196 |
// On ranks greater than 0, we need the ghosted X values for the computation |
| 197 |
// Y[0,c] = beta*Y[0,c] + alpha*(-colViewX[0] + 2*colViewX[1] - colViewX[2]) |
| 198 |
if (my_rank > 0) { |
| 199 |
KokkosViewY(0, c) = beta * KokkosViewY(0, c) + alpha * (-KokkosViewX(0, c) + 2 * KokkosViewX(1, c) - KokkosViewX(2, c)); |
| 200 |
offset = 0; |
| 201 |
} |
| 202 |
// On rank 0, we only have two entries in the first row |
| 203 |
// Y[0,c] = beta*Y[0,c] + alpha*(2*colViewX[1] - colViewX[2]) |
| 204 |
else { |
| 205 |
KokkosViewY(0, c) = beta * KokkosViewY(0, c) + alpha * (2 * KokkosViewX(0, c) - KokkosViewX(1, c)); |
| 206 |
offset = 1; |
| 207 |
} |
| 208 |
// For all other rows, we need the full stencil |
| 209 |
// Y[r,c] = beta*Y[r,c] + alpha*(-colViewX[r-offset] + 2*colViewX[r+1-offset] - colViewX[r+2-offset]) |
| 210 |
for (LocalOrdinal r = 1; r < numlocrows - 1; ++r) { |
| 211 |
const Scalar newVal = beta * KokkosViewY(r, c) + |
| 212 |
alpha * (-KokkosViewX(r - offset, c) + 2 * KokkosViewX(r + 1 - offset, c) - KokkosViewX(r + 2 - offset, c)); |
| 213 |
KokkosViewY(r, c) = newVal; |
| 214 |
} |
| 215 |
// On ranks other than the last rank, we need the ghosted X values for the computation |
| 216 |
// Y[numlocrows-1,c] = beta*Y[numlocrows-1,c] + alpha*(-colViewX[numlocrows-1-offset] + 2*colViewX[numlocrows-offset] |
| 217 |
// - colViewX[numlocrows+1-offset]) |
| 218 |
if (my_rank < num_procs - 1) { |
| 219 |
const Scalar newVal = beta * KokkosViewY(numlocrows - 1, c) + |
| 220 |
alpha * (-KokkosViewX(numlocrows - 1 - offset, c) + 2 * KokkosViewX(numlocrows - offset, c) - KokkosViewX(numlocrows + 1 - offset, c)); |
| 221 |
KokkosViewY(numlocrows - 1, c) = newVal; |
| 222 |
} |
| 223 |
// On the last rank, we only have two entries in the last row |
| 224 |
// Y[numlocrows-1,c] = beta*Y[numlocrows-1,c] + alpha*(-colViewX[numlocrows-1-offset] + 2*colViewX[numlocrows-offset]) |
| 225 |
else { |
| 226 |
const Scalar newVal = beta * KokkosViewY(numlocrows - 1, c) + |
| 227 |
alpha * (-KokkosViewX(numlocrows - 1 - offset, c) + 2 * KokkosViewX(numlocrows - offset, c)); |
| 228 |
KokkosViewY(numlocrows - 1, c) = newVal; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
//! Returns the Xpetra::Map object associated with the domain of this operator. |
| 234 |
const Teuchos::RCP<const map_type> getDomainMap() const { return opMap_; } |
| 235 |
|
| 236 |
//! Returns the Xpetra::Map object associated with the range of this operator. |
| 237 |
const Teuchos::RCP<const map_type> getRangeMap() const { return opMap_; } |
| 238 |
|
| 239 |
//! Indicates whether this operator supports applying the adjoint operator. |
| 240 |
bool hasTransposeApply() const { return true; } |
| 241 |
|
| 242 |
//! Compute a residual R = B - (*this) * X |
| 243 |
void residual(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X, |
| 244 |
const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& B, |
| 245 |
Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& R) const { |
| 246 |
typedef Teuchos::ScalarTraits<Scalar> STS; |
| 247 |
R.update(STS::one(), B, STS::zero()); // R = 1*B + 0*R |
| 248 |
this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one()); // R = R - Op*X |
| 249 |
} |
| 250 |
|
| 251 |
private: |
| 252 |
Teuchos::RCP<const map_type> opMap_, redistMap_; |
| 253 |
Teuchos::RCP<const import_type> importer_; |
| 254 |
}; |
| 255 |
|
| 256 |
/** |
| 257 |
* This class defines an operator corresponding to the |
| 258 |
* [1 2 1] interpolation stencil. For interpolation to |
| 259 |
* behave correctly, each MPI rank should have a |
| 260 |
* number of DOFs that is divisible by 3, as the |
| 261 |
* coarsening ratio is 3->1. Communication is ignored. |
| 262 |
*/ |
| 263 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 264 |
class MFProlongatorOperator : public Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> { |
| 265 |
public: |
| 266 |
// Xpetra::Operator subclasses should always define typedefs according to Xpetra |
| 267 |
typedef typename Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>::node_type node_type; |
| 268 |
typedef typename Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> MV; |
| 269 |
typedef typename Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type; |
| 270 |
typedef typename Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> import_type; |
| 271 |
typedef typename Xpetra::Export<LocalOrdinal, GlobalOrdinal, Node> export_type; |
| 272 |
|
| 273 |
public: |
| 274 |
/** Constructor |
| 275 |
* \param[in] n The number of global DOFs |
| 276 |
* \param[in] comm The Teuchos::Comm for the object |
| 277 |
*/ |
| 278 |
MFProlongatorOperator(const Teuchos::RCP<const map_type> fine_map) { |
| 279 |
const GlobalOrdinal n = fine_map->getGlobalNumElements(); |
| 280 |
const LocalOrdinal n_local = fine_map->getLocalNumElements(); |
| 281 |
const Teuchos::RCP<const Teuchos::Comm<int>> comm = fine_map->getComm(); |
| 282 |
|
| 283 |
TEUCHOS_TEST_FOR_EXCEPTION(comm.is_null(), std::invalid_argument, "MFProlongatorOperator constructor: The input Comm object must be nonnull."); |
| 284 |
TEUCHOS_TEST_FOR_EXCEPTION(n_local % 3 != 0, std::invalid_argument, "MFProlongatorOperator constructor: The number of local DOFs is not divisible by 3."); |
| 285 |
|
| 286 |
// Construct a default map and let it choose how DOFs are divided |
| 287 |
// Note: This assumes the map constructor is generating contiguous local DOFs |
| 288 |
const GlobalOrdinal index_base = 0; |
| 289 |
rangeMap_ = fine_map; |
| 290 |
domainMap_ = Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(Xpetra::UseTpetra, n / 3, index_base, comm); |
| 291 |
|
| 292 |
TEUCHOS_TEST_FOR_EXCEPTION(domainMap_->getLocalNumElements() != ((size_t)n_local) / 3, std::invalid_argument, "MFProlongatorOperator constructor: The operator is not coarsening by 3."); |
| 293 |
}; |
| 294 |
|
| 295 |
// Required since we inherit from Xpetra::Operator |
| 296 |
// Destructor |
| 297 |
virtual ~MFProlongatorOperator() {} |
| 298 |
|
| 299 |
/** |
| 300 |
* \brief Compute Y := alpha Op X + beta Y. |
| 301 |
* \param[in] X Vector to apply the operator to |
| 302 |
* \param[in] Y Vector to update |
| 303 |
* \param[in] mode Transpose mode (unused since this is symmetric) |
| 304 |
* \param[in] alpha Magnitude of Op*X term |
| 305 |
* \param[in] beta Magnitude of Y term |
| 306 |
*/ |
| 307 |
void |
| 308 |
apply(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X, |
| 309 |
Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y, |
| 310 |
Teuchos::ETransp mode = Teuchos::NO_TRANS, |
| 311 |
Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(), |
| 312 |
Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const { |
| 313 |
// // Setup: get comms, ranks, procs |
| 314 |
// Teuchos::RCP<const Teuchos::Comm<int> > comm = domainMap_->getComm(); |
| 315 |
// const int my_rank = comm->getRank(); |
| 316 |
// const int num_procs = comm->getSize(); |
| 317 |
// const size_t num_vecs = X.getNumVectors(); |
| 318 |
// const LocalOrdinal numlocrows = static_cast<LocalOrdinal>(X.getLocalLength()); |
| 319 |
|
| 320 |
// // Make a temporary multivector for holding the redistributed data and then redistribute |
| 321 |
// Teuchos::RCP<MV> redistDataX = Teuchos::rcp(new MV(redistMap_, num_vecs)); |
| 322 |
// redistDataX->doImport(X, *importer_, Xpetra::INSERT); |
| 323 |
|
| 324 |
// // Get a view of the multivector |
| 325 |
// auto KokkosViewX = redistDataX->getLocalView<HostDevice>(Xpetra::Access::ReadOnly); |
| 326 |
// auto KokkosViewY = Y.getLocalView<HostDevice>(Xpetra::Access::ReadWrite); |
| 327 |
|
| 328 |
// // Perform the matvec with the locally owned data |
| 329 |
// // For each column |
| 330 |
// for(size_t c = 0; c < num_vecs; ++c) { |
| 331 |
// LocalOrdinal offset; |
| 332 |
// // On ranks greater than 0, we need the ghosted X values for the computation |
| 333 |
// // Y[0,c] = beta*Y[0,c] + alpha*(-colViewX[0] + 2*colViewX[1] - colViewX[2]) |
| 334 |
// if(my_rank > 0) { |
| 335 |
// KokkosViewY(0,c) = beta*KokkosViewY(0, c) + alpha*(-KokkosViewX(0, c) + 2*KokkosViewX(1, c) - KokkosViewX(2, c)); |
| 336 |
// offset = 0; |
| 337 |
// } |
| 338 |
// // On rank 0, we only have two entries in the first row |
| 339 |
// // Y[0,c] = beta*Y[0,c] + alpha*(2*colViewX[1] - colViewX[2]) |
| 340 |
// else { |
| 341 |
// KokkosViewY(0,c) = beta*KokkosViewY(0, c) + alpha*(2*KokkosViewX(0, c) - KokkosViewX(1, c)); |
| 342 |
// offset = 1; |
| 343 |
// } |
| 344 |
// // For all other rows, we need the full stencil |
| 345 |
// // Y[r,c] = beta*Y[r,c] + alpha*(-colViewX[r-offset] + 2*colViewX[r+1-offset] - colViewX[r+2-offset]) |
| 346 |
// for(LocalOrdinal r = 1; r < numlocrows - 1; ++r) { |
| 347 |
// const Scalar newVal = beta*KokkosViewY(r, c) + |
| 348 |
// alpha*(-KokkosViewX(r-offset, c) + 2*KokkosViewX(r+1-offset, c) - KokkosViewX(r+2-offset, c)); |
| 349 |
// KokkosViewY(r,c) = newVal; |
| 350 |
// } |
| 351 |
// // On ranks other than the last rank, we need the ghosted X values for the computation |
| 352 |
// // Y[numlocrows-1,c] = beta*Y[numlocrows-1,c] + alpha*(-colViewX[numlocrows-1-offset] + 2*colViewX[numlocrows-offset] |
| 353 |
// // - colViewX[numlocrows+1-offset]) |
| 354 |
// if(my_rank < num_procs - 1) { |
| 355 |
// const Scalar newVal = beta*KokkosViewY(numlocrows-1, c) + |
| 356 |
// alpha*(-KokkosViewX(numlocrows-1-offset, c) + 2*KokkosViewX(numlocrows-offset, c) |
| 357 |
// - KokkosViewX(numlocrows+1-offset, c)); |
| 358 |
// KokkosViewY(numlocrows-1,c) = newVal; |
| 359 |
// } |
| 360 |
// // On the last rank, we only have two entries in the last row |
| 361 |
// // Y[numlocrows-1,c] = beta*Y[numlocrows-1,c] + alpha*(-colViewX[numlocrows-1-offset] + 2*colViewX[numlocrows-offset]) |
| 362 |
// else { |
| 363 |
// const Scalar newVal = beta*KokkosViewY(numlocrows-1, c) + |
| 364 |
// alpha*(-KokkosViewX(numlocrows-1-offset, c) + 2*KokkosViewX(numlocrows-offset, c)); |
| 365 |
// KokkosViewY(numlocrows-1,c) = newVal; |
| 366 |
// } |
| 367 |
// } |
| 368 |
} |
| 369 |
|
| 370 |
//! Returns the Xpetra::Map object associated with the domain of this operator. |
| 371 |
const Teuchos::RCP<const map_type> getDomainMap() const { return domainMap_; } |
| 372 |
|
| 373 |
//! Returns the Xpetra::Map object associated with the range of this operator. |
| 374 |
const Teuchos::RCP<const map_type> getRangeMap() const { return rangeMap_; } |
| 375 |
|
| 376 |
//! Indicates whether this operator supports applying the adjoint operator. |
| 377 |
bool hasTransposeApply() const { return true; } |
| 378 |
|
| 379 |
//! Compute a residual R = B - (*this) * X |
| 380 |
void residual(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X, |
| 381 |
const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& B, |
| 382 |
Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& R) const { |
| 383 |
// throw Exceptions::RuntimeError("Interface not supported"); |
| 384 |
} |
| 385 |
|
| 386 |
private: |
| 387 |
Teuchos::RCP<const map_type> domainMap_, rangeMap_, redistMap_; |
| 388 |
Teuchos::RCP<const import_type> importer_; |
| 389 |
}; |
| 390 |
|
| 391 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 392 |
int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib lib, int argc, char* argv[]) { |
| 393 |
Teuchos::RCP<const Teuchos::Comm<int>> comm = Teuchos::DefaultComm<int>::getComm(); |
| 394 |
const int my_rank = comm->getRank(); |
| 395 |
const int num_procs = comm->getSize(); |
| 396 |
{ |
| 397 |
// Necessary typedefs |
| 398 |
using SC = Scalar; |
| 399 |
using LO = LocalOrdinal; |
| 400 |
using GO = GlobalOrdinal; |
| 401 |
using NO = Node; |
| 402 |
// using map_type = Xpetra::Map<>; // unused |
| 403 |
using MV = Xpetra::MultiVector<SC, LO, GO, NO>; |
| 404 |
|
| 405 |
// Set a command line processor and parse it |
| 406 |
int n = 300; |
| 407 |
int max_iterations = 1000; |
| 408 |
double tol = 1e-10; |
| 409 |
bool do_multigrid = true; |
| 410 |
bool show_timer_summary = false; |
| 411 |
bool belos_verbose = false; |
| 412 |
bool show_kokkos = false; |
| 413 |
bool print_RHS_and_solution = false; |
| 414 |
bool dump_matrix_market = false; |
| 415 |
// problem config |
| 416 |
clp.setOption("n", &n, "Size of the n-by-n operator (default: 300)"); |
| 417 |
clp.setOption("verbose", "no-verbose", &belos_verbose, "Use Belos verbose output (default: false)"); |
| 418 |
// linear solver config |
| 419 |
clp.setOption("max-its", &max_iterations, "Maximum number of Belos iterations (default: 1000)"); |
| 420 |
clp.setOption("tol", &tol, "Belos convergence tolerance (default: 1e-10)"); |
| 421 |
// muelu-specific config |
| 422 |
clp.setOption("multigrid", "no-multigrid", &do_multigrid, "Use MueLu to solve this via multigrid (default: true)"); |
| 423 |
// other print statements and dumps |
| 424 |
clp.setOption("show-timer-summary", "no-timer-summary", &show_timer_summary, "Show a summary of the timer information (default: false)"); |
| 425 |
clp.setOption("show-kokkos", "no-kokkos", &show_kokkos, "Show the Kokkos configuration (default: false) [currently does nothing]"); |
| 426 |
clp.setOption("print-RHS-and-solution", "no-print-RHS-and-solution", &print_RHS_and_solution, "Print the RHS vector and the solution vector to verify the answer to the problem (default: false)"); |
| 427 |
clp.setOption("dump-matrix-market", "no-dump-matrix-market", &dump_matrix_market, "Dump the solution and right-hand side to MatrixMarket format (default: false)"); |
| 428 |
|
| 429 |
clp.recogniseAllOptions(true); |
| 430 |
switch (clp.parse(argc, argv)) { |
| 431 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: |
| 432 |
return EXIT_SUCCESS; |
| 433 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 434 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: |
| 435 |
return EXIT_FAILURE; |
| 436 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: |
| 437 |
break; |
| 438 |
} |
| 439 |
if (my_rank == 0) |
| 440 |
std::cout << "Running example-01 with n=" << n << " verbose=" << belos_verbose << " config=" << show_kokkos << "..." << std::endl; |
| 441 |
|
| 442 |
// print configuration details if needed |
| 443 |
Kokkos::Serial().print_configuration(std::cout, true /*details*/); |
| 444 |
// Kokkos::OpenMP().print_configuration(std::cout, true/*details*/); |
| 445 |
// std::cout << "OpenMP Max Threads = " << omp_get_max_threads() << std::endl; |
| 446 |
// Kokkos::Cuda().print_configuration(std::cout, true/*details*/); |
| 447 |
// Kokkos::HIP().print_configuration(std::cout, true/*details*/); |
| 448 |
|
| 449 |
// Create the operator |
| 450 |
Teuchos::RCP<TridiagonalOperator<SC, LO, GO, NO>> matrix = Teuchos::rcp(new TridiagonalOperator<SC, LO, GO, NO>(n, comm)); |
| 451 |
|
| 452 |
// A useful class name means excruciating template arguments... but I want to avoid headaches |
| 453 |
std::cout << "Creating the vectors..." << std::endl; |
| 454 |
|
| 455 |
// Construct the right-hand side |
| 456 |
Teuchos::RCP<MV> rhs = Xpetra::MultiVectorFactory<SC, LO, GO, NO>::Build(matrix->getRangeMap(), 1); |
| 457 |
rhs->putScalar(0.0); |
| 458 |
|
| 459 |
// Construct the initial guess (seedrandom is not always reproducible across machines, see Github) |
| 460 |
Teuchos::RCP<MV> solution = Xpetra::MultiVectorFactory<SC, LO, GO, NO>::Build(matrix->getDomainMap(), 1); |
| 461 |
Teuchos::ScalarTraits<SC>::seedrandom(314159); |
| 462 |
solution->randomize(); |
| 463 |
// solution->putScalar(1.0) is an alternative if the random seed isn't reproducible |
| 464 |
|
| 465 |
// some computations to make the RHS match the example |
| 466 |
// f''(x) = h^2*(-pi^2)*sin(pi*x) |
| 467 |
{ |
| 468 |
// divide domain up according to MPI ranks |
| 469 |
const SC dx = 1.0 / num_procs; |
| 470 |
const SC x_left = dx * my_rank; |
| 471 |
const SC x_right = dx * (my_rank + 1); |
| 472 |
const size_t n_local = matrix->getRangeMap()->getLocalNumElements(); |
| 473 |
const SC h = (x_right - x_left) / (n_local - 1); |
| 474 |
|
| 475 |
// fill the RHS appropriately |
| 476 |
auto rhs_2d = rhs->getHostLocalView(Xpetra::Access::OverwriteAll); |
| 477 |
auto rhs_1d = Kokkos::subview(rhs_2d, Kokkos::ALL(), 0); |
| 478 |
SC x = x_left; |
| 479 |
for (size_t i = 0; i < n_local; ++i) { |
| 480 |
rhs_1d(i) = 4 * h * h * M_PI * M_PI * sin(2 * M_PI * x); |
| 481 |
x += h; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
if (!do_multigrid) { |
| 486 |
std::cout << "Creating Belos solver..." << std::endl; |
| 487 |
|
| 488 |
// Create solver settings |
| 489 |
Teuchos::RCP<Teuchos::ParameterList> belos_settings = Teuchos::parameterList("Belos"); |
| 490 |
belos_settings->set("Verbosity", belos_verbose ? 1 : 0); |
| 491 |
belos_settings->set("Output Style", 1); |
| 492 |
belos_settings->set("Maximum Iterations", max_iterations); |
| 493 |
// belos_settings->set("Convergence Tolerance", tol); // Relative convergence tolerance requested |
| 494 |
// belos_settings->set("Output Frequency", 1); |
| 495 |
// belos_settings->set("Output Style", Belos::Brief); |
| 496 |
|
| 497 |
// Define and set the linear problem |
| 498 |
Teuchos::RCP<Belos::OperatorT<MV>> belos_operator = Teuchos::rcp(new Belos::XpetraOp<SC, LO, GO, NO>(matrix)); // Turns a Xpetra::Operator object into a Belos operator |
| 499 |
Teuchos::RCP<Belos::LinearProblem<SC, MV, Belos::OperatorT<MV>>> belos_problem = Teuchos::rcp(new Belos::LinearProblem<SC, MV, Belos::OperatorT<MV>>(belos_operator, solution, rhs)); |
| 500 |
bool set = belos_problem->setProblem(); |
| 501 |
if (set == false) { |
| 502 |
if (comm->getRank() == 0) |
| 503 |
std::cout << std::endl |
| 504 |
<< "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 505 |
return EXIT_FAILURE; |
| 506 |
} |
| 507 |
|
| 508 |
// Solve the problem |
| 509 |
std::cout << "Solving the problem..." << std::endl; |
| 510 |
Teuchos::RCP<Belos::SolverManager<SC, MV, Belos::OperatorT<MV>>> solver = Teuchos::rcp(new Belos::BlockCGSolMgr<SC, MV, Belos::OperatorT<MV>>(belos_problem, belos_settings)); |
| 511 |
const Belos::ReturnType belos_result = solver->solve(); |
| 512 |
|
| 513 |
if (my_rank == 0) { |
| 514 |
std::cout << "Belos solver wrapper results: " |
| 515 |
<< (belos_result == Belos::Converged ? "Converged" : "Unconverged") |
| 516 |
<< std::endl |
| 517 |
<< "Number of iterations: " << solver->getNumIters() |
| 518 |
<< std::endl; |
| 519 |
} |
| 520 |
} else { |
| 521 |
std::cout << "Generating multigrid objects..." << std::endl; |
| 522 |
|
| 523 |
Teuchos::ParameterList params; |
| 524 |
params.set("coarse: max size", 1); |
| 525 |
params.set("max levels", 2); |
| 526 |
params.set("transpose: use implicit", true); |
| 527 |
|
| 528 |
// generate coarse matrix-free operator |
| 529 |
Teuchos::RCP<TridiagonalOperator<SC, LO, GO, NO>> coarse_matrix = Teuchos::rcp(new TridiagonalOperator<SC, LO, GO, NO>(n / 3, comm)); |
| 530 |
Teuchos::RCP<MFProlongatorOperator<SC, LO, GO, NO>> P = Teuchos::rcp(new MFProlongatorOperator<SC, LO, GO, NO>(matrix->getDomainMap())); |
| 531 |
|
| 532 |
// create MueLu hierarchy and levels |
| 533 |
Teuchos::RCP<MueLu::Hierarchy<SC, LO, GO, NO>> hierarchy = Teuchos::rcp(new typename MueLu::Hierarchy<SC, LO, GO, NO>()); |
| 534 |
hierarchy->SetProcRankVerbose(matrix->getDomainMap()->getComm()->getRank()); |
| 535 |
Teuchos::RCP<MueLu::HierarchyManager<SC, LO, GO, NO>> hierarchyManager = Teuchos::rcp(new MueLu::ParameterListInterpreter<SC, LO, GO, NO>(params, matrix->getDomainMap()->getComm())); |
| 536 |
Teuchos::RCP<MueLu::FactoryManager<SC, LO, GO, NO>> factoryManager = Teuchos::rcp(new MueLu::FactoryManager<SC, LO, GO, NO>()); |
| 537 |
|
| 538 |
// set A on fine level |
| 539 |
Teuchos::RCP<MueLu::Level> fineLevel = hierarchy->GetLevel(0); |
| 540 |
fineLevel->SetFactoryManager(factoryManager); |
| 541 |
Teuchos::RCP<Xpetra::Operator<SC, LO, GO, NO>> matrix_op = matrix; |
| 542 |
fineLevel->Set("A", matrix_op); |
| 543 |
|
| 544 |
// handle the smoother |
| 545 |
std::string ifpackType = "RELAXATION"; |
| 546 |
Teuchos::ParameterList ifpackList; |
| 547 |
ifpackList.set("relaxation: sweeps", (LO)1); |
| 548 |
ifpackList.set("relaxation: damping factor", (SC)1.0); |
| 549 |
Teuchos::RCP<MueLu::SmootherPrototype<SC, LO, GO, NO>> smootherPrototype = Teuchos::rcp(new MueLu::TrilinosSmoother<SC, LO, GO, NO>(ifpackType, ifpackList)); |
| 550 |
Teuchos::RCP<MueLu::SmootherFactory<SC, LO, GO, NO>> smootherFact = Teuchos::rcp(new MueLu::SmootherFactory<SC, LO, GO, NO>(smootherPrototype)); |
| 551 |
factoryManager->SetFactory("Smoother", smootherFact); |
| 552 |
|
| 553 |
// work on the next level |
| 554 |
// hierarchy->AddNewLevel(); |
| 555 |
// Teuchos::RCP<MueLu::Level> coarseLevel = hierarchy->GetLevel(1); |
| 556 |
// coarseLevel->Set("P", P); |
| 557 |
// coarseLevel->Set("A", coarse_matrix); |
| 558 |
|
| 559 |
// hierarchyManager->SetupHierarchy(*hierarchy); |
| 560 |
// hierarchy->Setup(factoryManager,fineLevel,coarseLevel); |
| 561 |
// hierarchy->setlib(matrix->getDomainMap()->lib()); |
| 562 |
// hierarchyManager->SetupHierarchy(*hierarchy); |
| 563 |
|
| 564 |
std::cout << "Finished hierarchy!" << std::endl; |
| 565 |
|
| 566 |
Teuchos::ParameterList status; |
| 567 |
// status = hierarchy->FullPopulate(PRfact,Acfact,SmooFact,0,maxLevels); |
| 568 |
if (comm->getRank() == 0) { |
| 569 |
std::cout << "======================\n Multigrid statistics \n======================" << std::endl; |
| 570 |
status.print(std::cout, Teuchos::ParameterList::PrintOptions().indent(2)); |
| 571 |
} |
| 572 |
|
| 573 |
// hierarchy->Iterate(*rhs, *solution, max_iterations); |
| 574 |
} |
| 575 |
|
| 576 |
// output the RHS and solution for validation |
| 577 |
// (sleep my_rank is very hacky here, but keeps prints contiguous) |
| 578 |
if (print_RHS_and_solution) { |
| 579 |
sleep(my_rank); |
| 580 |
auto rhs_2d = rhs->getHostLocalView(Xpetra::Access::ReadOnly); |
| 581 |
PRINT_VIEW2_LINEAR(rhs_2d) |
| 582 |
auto solution_2d = solution->getHostLocalView(Xpetra::Access::ReadOnly); |
| 583 |
PRINT_VIEW2_LINEAR(solution_2d) |
| 584 |
} |
| 585 |
|
| 586 |
// can't dump MatrixMarket if it's not a matrix |
| 587 |
if (dump_matrix_market) { |
| 588 |
// Xpetra::MatrixMarket::Writer<vector_type>::writeDenseFile("example_01_solution.mm", *solution); |
| 589 |
// Xpetra::MatrixMarket::Writer<vector_type>::writeDenseFile("example_01_rhs.mm", *rhs); |
| 590 |
} |
| 591 |
} |
| 592 |
Kokkos::finalize(); |
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
//- -- -------------------------------------------------------- |
| 597 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 598 |
#include "MueLu_Test_ETI.hpp" |
| 599 |
|
| 600 |
int main(int argc, char* argv[]) { |
| 601 |
return Automatic_Test_ETI(argc, argv); |
| 602 |
} |