MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_CreateTpetraPreconditioner.hpp
Go to the documentation of this file.
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 #ifndef MUELU_CREATE_TPETRA_PRECONDITIONER_HPP
11 #define MUELU_CREATE_TPETRA_PRECONDITIONER_HPP
12 
15 
17 #include <Tpetra_Operator.hpp>
18 #include <Tpetra_RowMatrix.hpp>
19 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
20 #include <Tpetra_BlockCrsMatrix.hpp>
21 #include <Xpetra_CrsMatrix.hpp>
22 #include <Xpetra_MultiVector.hpp>
23 #include <Xpetra_MultiVectorFactory.hpp>
24 
25 #include <MueLu.hpp>
26 
27 #include <MueLu_Exceptions.hpp>
28 #include <MueLu_Hierarchy.hpp>
29 #include <MueLu_MasterList.hpp>
30 #include <MueLu_ParameterListInterpreter.hpp>
31 #include <MueLu_TpetraOperator.hpp>
33 #include <MueLu_Utilities.hpp>
34 #include <MueLu_HierarchyUtils.hpp>
35 
36 #if defined(HAVE_MUELU_AMGX)
37 #include <MueLu_AMGXOperator.hpp>
38 #include <amgx_c.h>
39 #include "cuda_runtime.h"
40 #endif
41 
42 namespace MueLu {
43 
51 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
54  Teuchos::ParameterList& inParamList) {
55  typedef Scalar SC;
56  typedef LocalOrdinal LO;
57  typedef GlobalOrdinal GO;
58  typedef Node NO;
59 
61 
62  typedef Xpetra::MultiVector<SC, LO, GO, NO> MultiVector;
63  typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
67 
68 #if defined(HAVE_MUELU_AMGX)
69  std::string externalMG = "use external multigrid package";
70  if (inParamList.isParameter(externalMG) && inParamList.get<std::string>(externalMG) == "amgx") {
71  const RCP<crs_matrix_type> constCrsA = rcp_dynamic_cast<crs_matrix_type>(inA);
72  TEUCHOS_TEST_FOR_EXCEPTION(constCrsA == Teuchos::null, Exceptions::RuntimeError, "CreateTpetraPreconditioner: failed to dynamic cast to Tpetra::CrsMatrix, which is required to be able to use AmgX.");
73  return rcp(new AMGXOperator<SC, LO, GO, NO>(constCrsA, inParamList));
74  }
75 #endif
76 
77  // Wrap A
78  RCP<Matrix> A;
79  RCP<block_crs_matrix_type> bcrsA = rcp_dynamic_cast<block_crs_matrix_type>(inA);
80  RCP<crs_matrix_type> crsA = rcp_dynamic_cast<crs_matrix_type>(inA);
81  if (crsA != Teuchos::null)
82  A = TpetraCrs_To_XpetraMatrix<SC, LO, GO, NO>(crsA);
83  else if (bcrsA != Teuchos::null) {
85  TEUCHOS_TEST_FOR_EXCEPTION(temp == Teuchos::null, Exceptions::RuntimeError, "CreateTpetraPreconditioner: cast from Tpetra::BlockCrsMatrix to Xpetra::TpetraBlockCrsMatrix failed.");
87  } else {
88  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "CreateTpetraPreconditioner: only Tpetra CrsMatrix and BlockCrsMatrix types are supported.");
89  }
90 
91  Teuchos::ParameterList& userList = inParamList.sublist("user data");
92  if (userList.isParameter("Coordinates")) {
94  try {
95  coordinates = TpetraMultiVector_To_XpetraMultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LO, GO, NO>(userList.get<RCP<Tpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> > >("Coordinates"));
98  }
99  userList.set<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LO, GO, NO> > >("Coordinates", coordinates);
100  }
101 
102  if (userList.isParameter("Nullspace")) {
103  RCP<MultiVector> nullspace = Teuchos::null;
104  try {
105  nullspace = TpetraMultiVector_To_XpetraMultiVector<SC, LO, GO, NO>(userList.get<RCP<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >("Nullspace"));
107  nullspace = userList.get<RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >("Nullspace");
108  }
109  userList.set<RCP<MultiVector> >("Nullspace", nullspace);
110  }
111 
112  RCP<Hierarchy> H = MueLu::CreateXpetraPreconditioner<SC, LO, GO, NO>(A, inParamList);
113  return rcp(new TpetraOperator<SC, LO, GO, NO>(H));
114 }
115 
125 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
128  const std::string& xmlFileName) {
129  Teuchos::ParameterList paramList;
130  Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *inA->getDomainMap()->getComm());
131  return CreateTpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(inA, paramList);
132 }
133 
142 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
145  Teuchos::ParameterList paramList;
146  return CreateTpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(inA, paramList);
147 }
148 
156 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
159  typedef Scalar SC;
160  typedef LocalOrdinal LO;
161  typedef GlobalOrdinal GO;
162  typedef Node NO;
163 
164  typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
166 
167  RCP<Hierarchy> H = Op.GetHierarchy();
168  RCP<Matrix> A = TpetraCrs_To_XpetraMatrix<SC, LO, GO, NO>(inA);
169 
170  MueLu::ReuseXpetraPreconditioner<SC, LO, GO, NO>(A, H);
171 }
172 
173 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
176  typedef Scalar SC;
177  typedef LocalOrdinal LO;
178  typedef GlobalOrdinal GO;
179  typedef Node NO;
180 
181  typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
183 
184  RCP<Hierarchy> H = Op.GetHierarchy();
186  TEUCHOS_TEST_FOR_EXCEPTION(temp == Teuchos::null, Exceptions::RuntimeError, "ReuseTpetraPreconditioner: cast from Tpetra::BlockCrsMatrix to Xpetra::TpetraBlockCrsMatrix failed.");
188 
189  MueLu::ReuseXpetraPreconditioner<SC, LO, GO, NO>(A, H);
190 }
191 
192 } // namespace MueLu
193 
194 #endif // ifndef MUELU_CREATE_TPETRA_PRECONDITIONER_HPP
Teuchos::RCP< MueLu::TpetraOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > CreateTpetraPreconditioner(const Teuchos::RCP< Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &inA, Teuchos::ParameterList &inParamList)
Helper function to create a MueLu or AMGX preconditioner that can be used by Tpetra.Given a Tpetra::Operator, this function returns a constructed MueLu preconditioner.
Various adapters that will create a MueLu preconditioner that is an Xpetra::Matrix.
MueLu::DefaultLocalOrdinal LocalOrdinal
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
LocalOrdinal LO
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
MueLu::DefaultNode Node
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Scalar SC
RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > GetHierarchy() const
Direct access to the underlying MueLu::Hierarchy.
Wraps an existing MueLu::Hierarchy as a Tpetra::Operator.
Node NO
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Exception throws to report errors in the internal logical of the program.
void ReuseTpetraPreconditioner(const Teuchos::RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &inA, MueLu::TpetraOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Helper function to reuse an existing MueLu preconditioner.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
Adapter for AmgX library from Nvidia.