| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package |
| 4 |
// |
| 5 |
// Copyright 2009 NTESS and the Ifpack2 contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#include <Tpetra_Core.hpp> |
| 10 |
#include <Tpetra_Version.hpp> |
| 11 |
|
| 12 |
#include "Solve.hpp" |
| 13 |
|
| 14 |
int |
| 15 |
main (int argc, char **argv) |
| 16 |
{ |
| 17 |
typedef Ifpack2::Test::ST ST; |
| 18 |
typedef Ifpack2::Test::GO GO; |
| 19 |
typedef Ifpack2::Test::STS STS; |
| 20 |
typedef Ifpack2::Test::map_type map_type; |
| 21 |
typedef Ifpack2::Test::multivector_type multivector_type; |
| 22 |
typedef Ifpack2::Test::sparse_mat_type sparse_mat_type; |
| 23 |
|
| 24 |
// global_size_t: Tpetra defines this unsigned integer type big |
| 25 |
// enough to hold any global dimension or amount of data. |
| 26 |
using Tpetra::global_size_t; |
| 27 |
using Teuchos::Array; |
| 28 |
using Teuchos::ArrayView; |
| 29 |
using Teuchos::ArrayRCP; |
| 30 |
using Teuchos::arcp; |
| 31 |
using Teuchos::RCP; |
| 32 |
using Teuchos::rcp; |
| 33 |
using Teuchos::tuple; |
| 34 |
using std::cerr; |
| 35 |
using std::cout; |
| 36 |
using std::endl; |
| 37 |
|
| 38 |
Tpetra::ScopeGuard tpetraScope (&argc, &argv); |
| 39 |
RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm (); |
| 40 |
const int myRank = comm->getRank (); |
| 41 |
const int numProcs = comm->getSize (); |
| 42 |
|
| 43 |
// The number of rows and columns in the matrix. |
| 44 |
const global_size_t numGlobalElements = 50; |
| 45 |
|
| 46 |
// Create the matrix's row Map. |
| 47 |
RCP<const map_type> map (new map_type (numGlobalElements, 0, comm)); |
| 48 |
const size_t numMyElements = map->getLocalNumElements (); |
| 49 |
ArrayView<const GO> myGlobalElements = map->getLocalElementList (); |
| 50 |
|
| 51 |
// Create a Tpetra sparse matrix with the given row Map. |
| 52 |
sparse_mat_type A (map, 3); |
| 53 |
|
| 54 |
// Fill the sparse matrix, one row at a time. |
| 55 |
const ST two = static_cast<ST> ( 2.0); |
| 56 |
const ST negOne = static_cast<ST> (-1.0); |
| 57 |
|
| 58 |
for (size_t i = 0; i < numMyElements; ++i) { |
| 59 |
// A(0, 0:1) = [2, -1] |
| 60 |
if (myGlobalElements[i] == 0) { |
| 61 |
A.insertGlobalValues (myGlobalElements[i], |
| 62 |
tuple<GO> (myGlobalElements[i], myGlobalElements[i]+1), |
| 63 |
tuple<ST> (two, negOne)); |
| 64 |
} |
| 65 |
// A(N-1, N-2:N-1) = [-1, 2] |
| 66 |
else if (static_cast<global_size_t> (myGlobalElements[i]) == numGlobalElements - 1) { |
| 67 |
A.insertGlobalValues (myGlobalElements[i], |
| 68 |
tuple<GO> (myGlobalElements[i]-1, myGlobalElements[i]), |
| 69 |
tuple<ST> (negOne, two)); |
| 70 |
} |
| 71 |
// A(i, i-1:i+1) = [-1, 2, -1] |
| 72 |
else { |
| 73 |
A.insertGlobalValues (myGlobalElements[i], |
| 74 |
tuple<GO> (myGlobalElements[i]-1, myGlobalElements[i], myGlobalElements[i]+1), |
| 75 |
tuple<ST> (negOne, two, negOne)); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Tell the sparse matrix that we are done adding entries to it. |
| 80 |
A.fillComplete (); |
| 81 |
|
| 82 |
// Create the right-hand side and initial guess of the linear system to solve. |
| 83 |
multivector_type b (map, 1); |
| 84 |
b.putScalar (STS::one ()); |
| 85 |
multivector_type x (map, 1, true); |
| 86 |
|
| 87 |
// Attempt to solve the linear system, with the following parameters: |
| 88 |
// |
| 89 |
// - ILU(k) with k = 1 |
| 90 |
// - Additive Schwarz overlap level: 1 |
| 91 |
// - GMRES restart length: 100 |
| 92 |
// - Maximum number of GMRES iterations (over all restarts): 1000 |
| 93 |
// - GMRES relative residual tolerance: 1.0e-8 |
| 94 |
// - Do not reorder |
| 95 |
|
| 96 |
using Teuchos::reduceAll; |
| 97 |
using Teuchos::REDUCE_MIN; |
| 98 |
using Teuchos::outArg; |
| 99 |
int lclSuccess = 1; |
| 100 |
int gblSuccess = 1; |
| 101 |
std::ostringstream errStrm; |
| 102 |
try { |
| 103 |
Ifpack2::Test::solve (A, b, x, 1, 1, 100, 1000, 1.0e-8, false, "RILUK"); |
| 104 |
} |
| 105 |
catch (std::exception& e) { |
| 106 |
lclSuccess = 0; |
| 107 |
errStrm << e.what (); |
| 108 |
} |
| 109 |
reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess)); |
| 110 |
if (gblSuccess != 1) { |
| 111 |
// We assume that it's OK for MPI processes other than Proc 0 in |
| 112 |
// MPI_COMM_WORLD to print to stderr. That's generally true when |
| 113 |
// we run tests. |
| 114 |
cerr << "Belos solve with Ifpack2 preconditioner threw an exception " |
| 115 |
"on one or more processes!" << endl; |
| 116 |
for (int r = 0; r < numProcs; ++r) { |
| 117 |
if (r == myRank) { |
| 118 |
std::ostringstream os; |
| 119 |
os << "Process " << myRank << ": " << errStrm.str () << endl; |
| 120 |
cerr << os.str (); |
| 121 |
} |
| 122 |
comm->barrier (); // wait for output to finish |
| 123 |
comm->barrier (); |
| 124 |
comm->barrier (); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (comm->getRank () == 0) { |
| 129 |
if (gblSuccess == 1) { |
| 130 |
cout << "End Result: TEST PASSED" << endl; |
| 131 |
} |
| 132 |
else { |
| 133 |
cout << "End Result: TEST FAILED" << endl; |
| 134 |
} |
| 135 |
} |
| 136 |
return EXIT_SUCCESS; |
| 137 |
} |