Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_thyra_tpetra_ifpack2_issue_535.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_DefaultComm.hpp"
12 #include "Tpetra_CrsMatrix.hpp"
13 #include "Thyra_Ifpack2PreconditionerFactory.hpp"
15 #include <cstdlib>
16 
17 using Teuchos::RCP;
18 using Teuchos::rcp;
19 typedef Tpetra::Map<int, int> map_type;
20 typedef map_type::local_ordinal_type LO;
21 typedef map_type::global_ordinal_type GO;
22 typedef double ST;
23 typedef Tpetra::CrsMatrix<ST, LO, GO> crs_matrix_type;
24 typedef Tpetra::Operator<ST, LO, GO> op_type;
25 typedef Tpetra::Vector<ST, LO, GO> vec_type;
26 
29 {
30  const Tpetra::global_size_t numGlobalElements = 100;
31 
32  const GO indexBase = 0;
33  auto map = rcp (new map_type (numGlobalElements, indexBase, comm));
34 
35  const LO numMyElements = map->getLocalNumElements ();
36  auto myGlobalElements = map->getLocalElementList ();
37  auto A = rcp (new crs_matrix_type (map, 1));
38 
39  for (LO lclRow = 0; lclRow < numMyElements; ++lclRow) {
40  const GO gblInd = map->getGlobalElement (lclRow);
41  const ST val = static_cast<ST> (1.0) / static_cast<ST> (gblInd + 1);
42  A->insertGlobalValues (gblInd,
43  Teuchos::tuple (gblInd),
44  Teuchos::tuple (val));
45  }
46  A->fillComplete ();
47  return A;
48 }
49 
50 int
51 main (int argc, char *argv[])
52 {
53  using Teuchos::outArg;
54  using Teuchos::REDUCE_MIN;
55  using Teuchos::reduceAll;
56  using std::cerr;
57  using std::endl;
58  int lclSuccess = 1;
59  int gblSuccess = 0;
60 
61  Teuchos::GlobalMPISession session (&argc, &argv, NULL);
62  auto out = Teuchos::VerboseObjectBase::getDefaultOStream ();
63  const auto comm = Teuchos::DefaultComm<int>::getComm ();
64  const int myRank = comm->getRank ();
65 
66  *out << "Creating matrix" << endl;
68  try {
69  A = create_matrix (comm);
70  }
71  catch (std::exception& e) {
72  lclSuccess = 0;
73  std::ostringstream os;
74  os << "Proc " << myRank << ": create_matrix(comm) threw an "
75  "exception: " << e.what () << endl;
76  cerr << os.str ();
77  }
78  catch (...) {
79  lclSuccess = 0;
80  std::ostringstream os;
81  os << "Proc " << myRank << ": create_matrix(comm) threw an "
82  "exception not a subclass of std::exception." << endl;
83  cerr << os.str ();
84  }
85  gblSuccess = 0;
86  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
87  if (gblSuccess != 1) {
88  if (myRank == 0) {
89  *out << "create_matrix(comm) threw an exception on some process."
90  << endl;
91  }
92  *out << "End Result: TEST FAILED" << endl;
93  return EXIT_FAILURE;
94  }
95 
96  // Make sure that A is nonnull on all processes.
97  if (A.is_null ()) {
98  lclSuccess = 0;
99  }
100  gblSuccess = 0;
101  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
102  if (gblSuccess != 1) {
103  if (myRank == 0) {
104  *out << "The result of create_matrix(comm) is null on at least one "
105  "process." << endl;
106  }
107  *out << "End Result: TEST FAILED" << endl;
108  return EXIT_FAILURE;
109  }
110 
111  *out << "Creating vectors" << endl;
112  vec_type b (A->getRangeMap ());
113  b.putScalar (1.0);
114  vec_type x (A->getDomainMap ());
115  x.putScalar (0.0);
116 
117  *out << "Creating Stratimikos linear solver builder" << endl;
119  auto p = Teuchos::rcp (new Teuchos::ParameterList ());
120  try {
121  builder.setParameterList (p);
122  }
123  catch (std::exception& e) {
124  lclSuccess = 0;
125  std::ostringstream os;
126  os << "Proc " << myRank << ": builder.setParameterList(p) threw an "
127  "exception: " << e.what () << endl;
128  cerr << os.str ();
129  }
130  catch (...) {
131  lclSuccess = 0;
132  std::ostringstream os;
133  os << "Proc " << myRank << ": builder.setParameterList(p) threw an "
134  "exception not a subclass of std::exception." << endl;
135  cerr << os.str ();
136  }
137  gblSuccess = 0;
138  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
139  if (gblSuccess != 1) {
140  if (myRank == 0) {
141  *out << "builder.setParameterList(p) threw an exception on some process."
142  << endl;
143  }
144  *out << "End Result: TEST FAILED" << endl;
145  return EXIT_FAILURE;
146  }
147 
148  *out << "Calling builder.createLinearSolveStrategy" << endl;
149  auto lowsFactory = builder.createLinearSolveStrategy ("");
150  lowsFactory->setVerbLevel (Teuchos::VERB_LOW);
151 
152  *out << "Calling Thyra::createConstLinearOp" << endl;
153  const op_type& opA = *A;
154  auto thyraA = Thyra::createConstLinearOp (Teuchos::rcpFromRef (opA));
155 
156  // using Teuchos::rcp_implicit_cast;
157  // auto thyraA = Thyra::createConstLinearOp (rcp_implicit_cast<const op_type> (A));
158 
159  // Make sure that thyraA is nonnull on all processes.
160  if (thyraA.is_null ()) {
161  lclSuccess = 0;
162  }
163  gblSuccess = 0;
164  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
165  if (gblSuccess != 1) {
166  if (myRank == 0) {
167  *out << "The result of Thyra::createConstLinearOp is null on at least "
168  "one process." << endl;
169  }
170  *out << "End Result: TEST FAILED" << endl;
171  return EXIT_FAILURE;
172  }
173 
174  *out << "Creating Thyra Ifpack2 factory" << endl;
176  rcp (new Thyra::Ifpack2PreconditionerFactory<crs_matrix_type> ());
177 
178  *out << "Creating Ifpack2 preconditioner using factory" << endl;
179  typedef Thyra::PreconditionerBase<ST> prec_type;
180  RCP<prec_type> prec;
181  try {
182  prec = factory->createPrec ();
183  }
184  catch (std::exception& e) {
185  lclSuccess = 0;
186  std::ostringstream os;
187  os << "Proc " << myRank << ": factory->createPrec() threw an "
188  "exception: " << e.what () << endl;
189  cerr << os.str ();
190  }
191  catch (...) {
192  lclSuccess = 0;
193  std::ostringstream os;
194  os << "Proc " << myRank << ": factory->createPrec() threw an "
195  "exception not a subclass of std::exception." << endl;
196  cerr << os.str ();
197  }
198  gblSuccess = 0;
199  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
200  if (gblSuccess != 1) {
201  if (myRank == 0) {
202  *out << "factory->createPrec() threw an exception on some process."
203  << endl;
204  }
205  *out << "End Result: TEST FAILED" << endl;
206  return EXIT_FAILURE;
207  }
208 
209  // Make sure that prec is nonnull on all processes.
210  if (prec.is_null ()) {
211  lclSuccess = 0;
212  }
213  gblSuccess = 0;
214  reduceAll<int, int> (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
215  if (gblSuccess != 1) {
216  if (myRank == 0) {
217  *out << "The result of factory->createPrec() is null on at least one "
218  "process." << endl;
219  }
220  *out << "End Result: TEST FAILED" << endl;
221  return EXIT_FAILURE;
222  }
223 
224  Thyra::initializePrec (*factory, thyraA, prec.ptr ()); // segfault!
225 
226  // If this test starts failing, please check ifpack2/adapters/thyra/Thyra_Ifpack2PreconditionerFactory_def.hpp
227  // and corresponding spot in stratimikos/adapters/belos/tpetra/Thyra_BelosTpetraPreconditionerFactory_def.hpp
228  // See PR #11222 for most recent adjustments to these files. -JAL
229 
230  *out << "End Result: TEST PASSED" << endl;
231  return EXIT_SUCCESS;
232 }
int main(int argc, char *argv[])
Tpetra::Map< int, int > map_type
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
RCP< const LinearOpBase< Scalar > > createConstLinearOp(const RCP< const Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraOperator, const RCP< const VectorSpaceBase< Scalar > > rangeSpace=Teuchos::null, const RCP< const VectorSpaceBase< Scalar > > domainSpace=Teuchos::null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(RCP< ParameterList > const &paramList)
bool is_null(const RCP< T > &p)
Ptr< T > ptr() const
Tpetra::Vector< ST, LO, GO > vec_type
Teuchos::RCP< const crs_matrix_type > create_matrix(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Tpetra::CrsMatrix< ST, LO, GO > crs_matrix_type
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...
Tpetra::Operator< ST, LO, GO > op_type
map_type::local_ordinal_type LO
map_type::global_ordinal_type GO