| 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 |
/* Originally convert test here: belos/epetra/test/MINRES/test_minres_diag.cpp */ |
| 11 |
|
| 12 |
// Tpetra |
| 13 |
#include <Tpetra_Core.hpp> |
| 14 |
#include <Tpetra_Map.hpp> |
| 15 |
#include <Tpetra_MultiVector.hpp> |
| 16 |
#include <Tpetra_Vector.hpp> |
| 17 |
|
| 18 |
// Teuchos |
| 19 |
#include <Teuchos_Comm.hpp> |
| 20 |
#include <Teuchos_GlobalMPISession.hpp> |
| 21 |
#include <Teuchos_ParameterList.hpp> |
| 22 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 23 |
#include <Teuchos_Time.hpp> |
| 24 |
|
| 25 |
// Belos |
| 26 |
#include "BelosConfigDefs.hpp" |
| 27 |
#include "BelosLinearProblem.hpp" |
| 28 |
#include "BelosMinresSolMgr.hpp" |
| 29 |
#include "BelosTpetraAdapter.hpp" |
| 30 |
#include "BelosTpetraOperator.hpp" |
| 31 |
|
| 32 |
using Teuchos::RCP; |
| 33 |
using Teuchos::rcp; |
| 34 |
|
| 35 |
//************************************************************************************************ |
| 36 |
|
| 37 |
template <class MV> |
| 38 |
class VectorOperator { |
| 39 |
public: |
| 40 |
VectorOperator(int m_in, int n_in) : m(m_in), n(n_in){}; |
| 41 |
|
| 42 |
virtual ~VectorOperator(){}; |
| 43 |
|
| 44 |
virtual void operator()(const MV &x, MV &y) = 0; |
| 45 |
|
| 46 |
int size(int dim) const { return (dim == 1) ? m : n; }; |
| 47 |
|
| 48 |
protected: |
| 49 |
int m, n; // an (m x n) operator |
| 50 |
|
| 51 |
private: |
| 52 |
// Not allowing copy construction. |
| 53 |
VectorOperator(const VectorOperator &) : m(0), n(0){}; |
| 54 |
VectorOperator *operator=(const VectorOperator &) { return NULL; }; |
| 55 |
}; |
| 56 |
|
| 57 |
//************************************************************************************************ |
| 58 |
|
| 59 |
template <class ST, class MV> |
| 60 |
class DiagonalOperator : public VectorOperator<MV> { |
| 61 |
public: |
| 62 |
DiagonalOperator(int n_in, ST v_in) : VectorOperator<MV>(n_in, n_in), v(v_in){}; |
| 63 |
|
| 64 |
~DiagonalOperator(){}; |
| 65 |
|
| 66 |
void operator()(const MV &x, MV &y) { y.scale(v, x); }; |
| 67 |
|
| 68 |
private: |
| 69 |
ST v; |
| 70 |
}; |
| 71 |
|
| 72 |
//************************************************************************************************ |
| 73 |
|
| 74 |
template <class ST, class MV> |
| 75 |
class DiagonalOperator2 : public VectorOperator<MV> { |
| 76 |
public: |
| 77 |
DiagonalOperator2<ST, MV>(int n_in, int min_gid_in, ST v_in) |
| 78 |
: VectorOperator<MV>(n_in, n_in), min_gid(min_gid_in), v(v_in) {} |
| 79 |
|
| 80 |
~DiagonalOperator2(){}; |
| 81 |
|
| 82 |
void operator()(const MV &x, MV &y) { |
| 83 |
auto yLocalData = y.getLocalViewHost(Tpetra::Access::ReadWrite); |
| 84 |
auto xLocalData = x.getLocalViewHost(Tpetra::Access::ReadOnly); |
| 85 |
|
| 86 |
for (size_t j = 0; j < x.getNumVectors(); ++j) { |
| 87 |
for (size_t i = 0; i < x.getLocalLength(); ++i) { |
| 88 |
yLocalData(i, j) = (min_gid + i + 1) * v * xLocalData(i, j); // NOTE: square operator! |
| 89 |
} |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
private: |
| 94 |
int min_gid; |
| 95 |
ST v; |
| 96 |
}; |
| 97 |
|
| 98 |
//************************************************************************************************ |
| 99 |
|
| 100 |
template <class MV> |
| 101 |
class ComposedOperator : public VectorOperator<MV> { |
| 102 |
public: |
| 103 |
ComposedOperator(int n, const RCP<VectorOperator<MV>> &pA_in, const RCP<VectorOperator<MV>> &pB_in); |
| 104 |
|
| 105 |
virtual ~ComposedOperator(){}; |
| 106 |
|
| 107 |
virtual void operator()(const MV &x, MV &y); |
| 108 |
|
| 109 |
private: |
| 110 |
RCP<VectorOperator<MV>> pA; |
| 111 |
RCP<VectorOperator<MV>> pB; |
| 112 |
}; |
| 113 |
|
| 114 |
template <class MV> |
| 115 |
ComposedOperator<MV>::ComposedOperator(int n_in, const RCP<VectorOperator<MV>> &pA_in, |
| 116 |
const RCP<VectorOperator<MV>> &pB_in) |
| 117 |
: VectorOperator<MV>(n_in, n_in), pA(pA_in), pB(pB_in) {} |
| 118 |
|
| 119 |
template <class MV> |
| 120 |
void ComposedOperator<MV>::operator()(const MV &x, MV &y) { |
| 121 |
MV ytemp(y.getMap(), y.getNumVectors(), false); |
| 122 |
(*pB)(x, ytemp); |
| 123 |
(*pA)(ytemp, y); |
| 124 |
} |
| 125 |
|
| 126 |
//************************************************************************************************ |
| 127 |
|
| 128 |
template <class OP, class ST, class MP, class MV> |
| 129 |
class TrilinosInterface : public OP { |
| 130 |
public: |
| 131 |
TrilinosInterface(const RCP<VectorOperator<MV>> pA_in, const RCP<const Teuchos::Comm<int>> pComm_in, |
| 132 |
const RCP<const MP> pMap_in) |
| 133 |
: pA(pA_in), pComm(pComm_in), pMap(pMap_in), use_transpose(false) {} |
| 134 |
|
| 135 |
void apply(const MV &X, MV &Y, Teuchos::ETransp mode = Teuchos::NO_TRANS, ST alpha = Teuchos::ScalarTraits<ST>::one(), |
| 136 |
ST beta = Teuchos::ScalarTraits<ST>::zero()) const override; |
| 137 |
|
| 138 |
virtual ~TrilinosInterface(){}; |
| 139 |
|
| 140 |
bool hasTransposeApply() const { return (use_transpose); }; // always set to false (in fact the default) |
| 141 |
|
| 142 |
RCP<const MP> getDomainMap() const override { return pMap; } |
| 143 |
RCP<const MP> getRangeMap() const override { return pMap; } |
| 144 |
|
| 145 |
private: |
| 146 |
RCP<VectorOperator<MV>> pA; |
| 147 |
RCP<const Teuchos::Comm<int>> pComm; |
| 148 |
RCP<const MP> pMap; |
| 149 |
|
| 150 |
bool use_transpose; |
| 151 |
}; |
| 152 |
|
| 153 |
template <class OP, class ST, class MP, class MV> |
| 154 |
void TrilinosInterface<OP, ST, MP, MV>::apply(const MV &X, MV &Y, Teuchos::ETransp mode, ST alpha, ST beta) const { |
| 155 |
(*pA)(X, Y); |
| 156 |
} |
| 157 |
|
| 158 |
//************************************************************************************************ |
| 159 |
|
| 160 |
template <class OP, class ST, class MP, class MV> |
| 161 |
class IterativeInverseOperator : public VectorOperator<MV> { |
| 162 |
public: |
| 163 |
IterativeInverseOperator(int n_in, int blocksize, const RCP<VectorOperator<MV>> &pA_in, |
| 164 |
std::string opString = "Iterative Solver", bool print_in = false); |
| 165 |
|
| 166 |
virtual ~IterativeInverseOperator() {} |
| 167 |
|
| 168 |
virtual void operator()(const MV &b, MV &x); |
| 169 |
|
| 170 |
private: |
| 171 |
RCP<VectorOperator<MV>> pA; // operator which will be inverted |
| 172 |
// supplies a matrix std::vector multiply |
| 173 |
const bool print; |
| 174 |
|
| 175 |
Teuchos::Time timer; |
| 176 |
RCP<const Teuchos::Comm<int>> pComm; |
| 177 |
RCP<MP> pMap; |
| 178 |
|
| 179 |
RCP<OP> pPE; |
| 180 |
RCP<Teuchos::ParameterList> pList; |
| 181 |
RCP<Belos::LinearProblem<ST, MV, OP>> pProb; |
| 182 |
RCP<Belos::MinresSolMgr<ST, MV, OP>> pBelos; |
| 183 |
}; |
| 184 |
|
| 185 |
template <class OP, class ST, class MP, class MV> |
| 186 |
IterativeInverseOperator<OP, ST, MP, MV>::IterativeInverseOperator(int n_in, int blocksize, |
| 187 |
const RCP<VectorOperator<MV>> &pA_in, |
| 188 |
std::string opString, bool print_in) |
| 189 |
: VectorOperator<MV>(n_in, n_in), // square operator |
| 190 |
pA(pA_in), |
| 191 |
print(print_in), |
| 192 |
timer(opString) { |
| 193 |
int n_global; |
| 194 |
pComm = Tpetra::getDefaultComm(); |
| 195 |
Teuchos::reduceAll<int, int>(*pComm, Teuchos::REDUCE_SUM, 1, &n_in, &n_global); |
| 196 |
|
| 197 |
pMap = rcp(new MP(n_global, n_in, 0, pComm)); |
| 198 |
|
| 199 |
pPE = rcp(new TrilinosInterface<OP, ST, MP, MV>(pA, pComm, pMap)); |
| 200 |
|
| 201 |
pProb = rcp(new Belos::LinearProblem<ST, MV, OP>()); |
| 202 |
pProb->setOperator(pPE); |
| 203 |
|
| 204 |
int max_iter = 100; |
| 205 |
const ST tol = sqrt(std::numeric_limits<ST>::epsilon()); |
| 206 |
int verbosity = Belos::Errors + Belos::Warnings; |
| 207 |
if (print) |
| 208 |
verbosity += Belos::TimingDetails + Belos::StatusTestDetails; |
| 209 |
|
| 210 |
pList = rcp(new Teuchos::ParameterList); |
| 211 |
pList->set("Maximum Iterations", max_iter); |
| 212 |
pList->set("Convergence Tolerance", tol); |
| 213 |
pList->set("Verbosity", verbosity); |
| 214 |
|
| 215 |
pBelos = rcp(new Belos::MinresSolMgr<ST, MV, OP>(pProb, pList)); |
| 216 |
} |
| 217 |
|
| 218 |
template <class OP, class ST, class MP, class MV> |
| 219 |
void IterativeInverseOperator<OP, ST, MP, MV>::operator()(const MV &b, MV &x) { |
| 220 |
int pid = pComm->getRank(); |
| 221 |
|
| 222 |
// Initialize the solution to zero |
| 223 |
x.putScalar(0.0); |
| 224 |
|
| 225 |
// Reset the solver, problem, and status test for next solve (HKT) |
| 226 |
pProb->setProblem(rcp(&x, false), rcp(&b, false)); |
| 227 |
|
| 228 |
timer.start(); |
| 229 |
Belos::ReturnType ret = pBelos->solve(); |
| 230 |
timer.stop(); |
| 231 |
|
| 232 |
if (pid == 0 && print) { |
| 233 |
if (ret == Belos::Converged) { |
| 234 |
std::cout << std::endl << "pid[" << pid << "] Minres converged" << std::endl; |
| 235 |
std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl; |
| 236 |
|
| 237 |
} else |
| 238 |
std::cout << std::endl << "pid[" << pid << "] Minres did not converge" << std::endl; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
//************************************************************************************************ |
| 243 |
|
| 244 |
template <class ScalarType> |
| 245 |
int run(int argc, char *argv[]) { |
| 246 |
// Get default Tpetra template types |
| 247 |
using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type; |
| 248 |
using LO = typename Tpetra::Vector<>::local_ordinal_type; |
| 249 |
using GO = typename Tpetra::Vector<>::global_ordinal_type; |
| 250 |
using NT = typename Tpetra::Vector<>::node_type; |
| 251 |
|
| 252 |
// Init Tpetra types |
| 253 |
using OP = typename Tpetra::Operator<ST, LO, GO, NT>; |
| 254 |
using MV = typename Tpetra::MultiVector<ST, LO, GO, NT>; |
| 255 |
using MP = typename Tpetra::Map<LO, GO, NT>; |
| 256 |
|
| 257 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 258 |
|
| 259 |
RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm(); |
| 260 |
const int pid = rank(*comm); |
| 261 |
|
| 262 |
bool verbose = false; |
| 263 |
bool success = true; |
| 264 |
|
| 265 |
ST tol = sqrt(std::numeric_limits<ST>::epsilon()); // relative residual tolerance |
| 266 |
|
| 267 |
try { |
| 268 |
int n(10); |
| 269 |
int numRHS = 1; |
| 270 |
|
| 271 |
RCP<MP> map = RCP(new MP(n, 0, comm)); |
| 272 |
|
| 273 |
MV X(map, numRHS), Y(map, numRHS); |
| 274 |
X.putScalar(1.0); |
| 275 |
|
| 276 |
// Inner computes inv(D2)*y |
| 277 |
RCP<DiagonalOperator2<ST, MV>> D2 = rcp(new DiagonalOperator2<ST, MV>(n, map->getMinGlobalIndex(), 1.0)); |
| 278 |
IterativeInverseOperator<OP, ST, MP, MV> A2(n, 1, D2, "Belos (inv(D2))", true); |
| 279 |
|
| 280 |
// should return x=(1, 1/2, 1/3, ..., 1/10) |
| 281 |
A2(X, Y); |
| 282 |
|
| 283 |
if (pid == 0) { |
| 284 |
std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl; |
| 285 |
} |
| 286 |
Y.print(std::cout); |
| 287 |
|
| 288 |
// Inner computes inv(D)*x |
| 289 |
RCP<DiagonalOperator<ST, MV>> D = rcp(new DiagonalOperator<ST, MV>(n, 4.0)); |
| 290 |
RCP<IterativeInverseOperator<OP, ST, MP, MV>> Inner = |
| 291 |
rcp(new IterativeInverseOperator<OP, ST, MP, MV>(n, 1, D, "Belos (inv(D))", false)); |
| 292 |
|
| 293 |
// ComposedOperator computed inv(D)*B*x |
| 294 |
RCP<DiagonalOperator<ST, MV>> B = rcp(new DiagonalOperator<ST, MV>(n, 4.0)); |
| 295 |
RCP<ComposedOperator<MV>> C = rcp(new ComposedOperator<MV>(n, Inner, B)); |
| 296 |
|
| 297 |
// Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x |
| 298 |
RCP<IterativeInverseOperator<OP, ST, MP, MV>> Outer = |
| 299 |
rcp(new IterativeInverseOperator<OP, ST, MP, MV>(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true)); |
| 300 |
|
| 301 |
// should return x=1/4 |
| 302 |
(*Inner)(X, Y); |
| 303 |
|
| 304 |
if (pid == 0) { |
| 305 |
std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl; |
| 306 |
} |
| 307 |
Y.print(std::cout); |
| 308 |
|
| 309 |
// should return x=1 |
| 310 |
(*Outer)(X, Y); |
| 311 |
|
| 312 |
if (pid == 0) { |
| 313 |
std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl; |
| 314 |
} |
| 315 |
Y.print(std::cout); |
| 316 |
|
| 317 |
// Compute the norm of Y - 1.0 |
| 318 |
std::vector<ST> norm_Y(Y.getNumVectors()); |
| 319 |
Teuchos::ArrayView<ST> normView(norm_Y); |
| 320 |
|
| 321 |
Y.update(-1.0, X, 1.0); |
| 322 |
Y.norm2(norm_Y); |
| 323 |
|
| 324 |
if (pid == 0) |
| 325 |
std::cout << "Two-norm of std::vector (Y-1.0) : " << norm_Y[0] << std::endl; |
| 326 |
|
| 327 |
success = (norm_Y[0] < tol && !Teuchos::ScalarTraits<ST>::isnaninf(norm_Y[0])); |
| 328 |
|
| 329 |
if (success) { |
| 330 |
if (pid == 0) |
| 331 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 332 |
} else { |
| 333 |
if (pid == 0) |
| 334 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 335 |
} |
| 336 |
} |
| 337 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 338 |
|
| 339 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 340 |
} |
| 341 |
|
| 342 |
//************************************************************************************************ |
| 343 |
//************************************************************************************************ |
| 344 |
|
| 345 |
int main(int argc, char *argv[]) { |
| 346 |
return run<double>(argc, argv); |
| 347 |
// return run<float>(argc, argv); // FAILS |
| 348 |
} |