14 #ifndef AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
15 #define AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
17 #include "Amesos2_config.h"
19 #include "Amesos2_Solver.hpp"
20 #include "Trilinos_Details_LinearSolver.hpp"
21 #include "Trilinos_Details_LinearSolverFactory.hpp"
22 #include <type_traits>
25 #ifdef HAVE_AMESOS2_EPETRA
26 # include "Epetra_CrsMatrix.h"
27 #endif // HAVE_AMESOS2_EPETRA
30 #ifndef HAVE_AMESOS2_TPETRA
31 # define HAVE_AMESOS2_TPETRA
32 #endif // HAVE_AMESOS2_TPETRA
34 #ifdef HAVE_AMESOS2_TPETRA
35 # include "Tpetra_CrsMatrix.hpp"
36 #endif // HAVE_AMESOS2_TPETRA
47 struct GetMatrixType {
51 #ifdef HAVE_AMESOS2_EPETRA
52 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
53 "Amesos2::Details::GetMatrixType: OP = Epetra_MultiVector. "
54 "This probably means that you mixed up MV and OP.");
55 #endif // HAVE_AMESOS2_EPETRA
57 #ifdef HAVE_AMESOS2_TPETRA
58 static_assert(! std::is_same<OP, Tpetra::MultiVector<
typename OP::scalar_type,
59 typename OP::local_ordinal_type,
typename OP::global_ordinal_type,
60 typename OP::node_type> >::value,
61 "Amesos2::Details::GetMatrixType: OP = Tpetra::MultiVector. "
62 "This probably means that you mixed up MV and OP.");
63 #endif // HAVE_AMESOS2_TPETRA
66 #ifdef HAVE_AMESOS2_EPETRA
68 struct GetMatrixType<Epetra_Operator> {
69 typedef Epetra_CrsMatrix type;
71 #endif // HAVE_AMESOS2_EPETRA
74 #ifdef HAVE_AMESOS2_TPETRA
75 template<
class S,
class LO,
class GO,
class NT>
76 struct GetMatrixType<Tpetra::Operator<S, LO, GO, NT> > {
77 typedef Tpetra::CrsMatrix<S, LO, GO, NT> type;
79 #endif // HAVE_AMESOS2_TPETRA
81 template<
class MV,
class OP,
class NormType>
83 public Trilinos::Details::LinearSolver<MV, OP, NormType>,
84 virtual public Teuchos::Describable
87 #ifdef HAVE_AMESOS2_EPETRA
88 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
89 "Amesos2::Details::LinearSolver: OP = Epetra_MultiVector. "
90 "This probably means that you mixed up MV and OP.");
91 static_assert(! std::is_same<MV, Epetra_Operator>::value,
92 "Amesos2::Details::LinearSolver: MV = Epetra_Operator. "
93 "This probably means that you mixed up MV and OP.");
94 #endif // HAVE_AMESOS2_EPETRA
103 typedef typename GetMatrixType<OP>::type crs_matrix_type;
104 static_assert(! std::is_same<crs_matrix_type, int>::value,
105 "Amesos2::Details::LinearSolver: The given OP type is not "
124 LinearSolver (
const std::string& solverName) :
125 solverName_ (solverName)
133 if (solverName ==
"") {
135 if (Amesos2::query (
"klu2")) {
136 solverName_ =
"klu2";
138 else if (Amesos2::query (
"superlu")) {
139 solverName_ =
"superlu";
141 else if (Amesos2::query (
"superludist")) {
142 solverName_ =
"superludist";
144 else if (Amesos2::query (
"cholmod")) {
145 solverName_ =
"cholmod";
147 else if (Amesos2::query (
"cusolver")) {
148 solverName_ =
"cusolver";
150 else if (Amesos2::query (
"basker")) {
151 solverName_ =
"basker";
153 else if (Amesos2::query (
"shylubasker")) {
154 solverName_ =
"shylubasker";
156 else if (Amesos2::query (
"ShyLUBasker")) {
157 solverName_ =
"shylubasker";
159 else if (Amesos2::query (
"superlumt")) {
160 solverName_ =
"superlumt";
162 else if (Amesos2::query (
"pardiso_mkl")) {
163 solverName_ =
"pardiso_mkl";
165 else if (Amesos2::query (
"css_mkl")) {
166 solverName_ =
"css_mkl";
168 else if (Amesos2::query (
"mumps")) {
169 solverName_ =
"mumps";
171 else if (Amesos2::query (
"lapack")) {
172 solverName_ =
"lapack";
174 else if (Amesos2::query (
"umfpack")) {
175 solverName_ =
"umfpack";
183 virtual ~LinearSolver () {}
189 void setMatrix (
const Teuchos::RCP<const OP>& A) {
192 using Teuchos::TypeNameTraits;
193 typedef crs_matrix_type MAT;
194 const char prefix[] =
"Amesos2::Details::LinearSolver::setMatrix: ";
197 solver_ = Teuchos::null;
205 RCP<const MAT> A_mat = Teuchos::rcp_dynamic_cast<
const MAT> (A);
206 TEUCHOS_TEST_FOR_EXCEPTION
207 (A_mat.is_null (), std::invalid_argument,
208 "Amesos2::Details::LinearSolver::setMatrix: "
209 "The input matrix A must be a CrsMatrix.");
210 if (solver_.is_null ()) {
217 RCP<solver_type> solver;
219 solver = Amesos2::create<MAT, MV> (solverName_, A_mat, null, null);
221 catch (std::exception& e) {
222 TEUCHOS_TEST_FOR_EXCEPTION
223 (
true, std::invalid_argument, prefix <<
"Failed to create Amesos2 "
224 "solver named \"" << solverName_ <<
"\". "
225 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
226 <<
", MV = " << TypeNameTraits<MV>::name ()
227 <<
" threw an exception: " << e.what ());
229 TEUCHOS_TEST_FOR_EXCEPTION
230 (solver.is_null (), std::invalid_argument, prefix <<
"Failed to "
231 "create Amesos2 solver named \"" << solverName_ <<
"\". "
232 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
233 <<
", MV = " << TypeNameTraits<MV>::name ()
234 <<
" returned null.");
237 if (! params_.is_null ()) {
238 solver->setParameters (params_);
241 }
else if (A_ != A) {
242 solver_->setA (A_mat);
251 Teuchos::RCP<const OP> getMatrix ()
const {
256 void solve (MV& X,
const MV& B) {
257 const char prefix[] =
"Amesos2::Details::LinearSolver::solve: ";
258 TEUCHOS_TEST_FOR_EXCEPTION
259 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
260 "exist yet. You must call setMatrix() with a nonnull matrix before you "
261 "may call this method.");
262 TEUCHOS_TEST_FOR_EXCEPTION
263 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been "
264 "set yet. You must call setMatrix() with a nonnull matrix before you "
265 "may call this method.");
266 solver_->solve (&X, &B);
270 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params) {
271 if (! solver_.is_null ()) {
272 solver_->setParameters (params);
282 const char prefix[] =
"Amesos2::Details::LinearSolver::symbolic: ";
283 TEUCHOS_TEST_FOR_EXCEPTION
284 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
285 "exist yet. You must call setMatrix() with a nonnull matrix before you "
286 "may call this method.");
287 TEUCHOS_TEST_FOR_EXCEPTION
288 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been "
289 "set yet. You must call setMatrix() with a nonnull matrix before you "
290 "may call this method.");
291 solver_->symbolicFactorization ();
297 const char prefix[] =
"Amesos2::Details::LinearSolver::numeric: ";
298 TEUCHOS_TEST_FOR_EXCEPTION
299 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
300 "exist yet. You must call setMatrix() with a nonnull matrix before you "
301 "may call this method.");
302 TEUCHOS_TEST_FOR_EXCEPTION
303 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been "
304 "set yet. You must call setMatrix() with a nonnull matrix before you "
305 "may call this method.");
306 solver_->numericFactorization ();
310 std::string description ()
const {
311 using Teuchos::TypeNameTraits;
312 if (solver_.is_null ()) {
313 std::ostringstream os;
314 os <<
"\"Amesos2::Details::LinearSolver\": {"
315 <<
"MV: " << TypeNameTraits<MV>::name ()
316 <<
", OP: " << TypeNameTraits<OP>::name ()
317 <<
", NormType: " << TypeNameTraits<NormType>::name ()
321 return solver_->description ();
327 describe (Teuchos::FancyOStream& out,
328 const Teuchos::EVerbosityLevel verbLevel =
329 Teuchos::Describable::verbLevel_default)
const
331 using Teuchos::TypeNameTraits;
333 if (solver_.is_null ()) {
334 if (verbLevel > Teuchos::VERB_NONE) {
335 Teuchos::OSTab tab0 (out);
336 out <<
"\"Amesos2::Details::LinearSolver\":" << endl;
337 Teuchos::OSTab tab1 (out);
338 out <<
"MV: " << TypeNameTraits<MV>::name () << endl
339 <<
"OP: " << TypeNameTraits<OP>::name () << endl
340 <<
"NormType: " << TypeNameTraits<NormType>::name () << endl;
343 if (! solver_.is_null ()) {
344 solver_->describe (out, verbLevel);
349 std::string solverName_;
350 Teuchos::RCP<solver_type> solver_;
351 Teuchos::RCP<const OP> A_;
352 Teuchos::RCP<Teuchos::ParameterList> params_;
355 template<
class MV,
class OP,
class NormType>
356 Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
361 return rcp (
new Amesos2::Details::LinearSolver<MV, OP, NormType> (solverName));
364 template<
class MV,
class OP,
class NormType>
369 #ifdef HAVE_TEUCHOSCORE_CXX11
370 typedef std::shared_ptr<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
373 typedef Teuchos::RCP<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
375 #endif // HAVE_TEUCHOSCORE_CXX11
378 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType> (
"Amesos2", factory);
391 #define AMESOS2_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \
392 template class Amesos2::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \
393 Tpetra::Operator<SC, LO, GO, NT>, \
394 typename Tpetra::MultiVector<SC, LO, GO, NT>::mag_type>;
396 #endif // AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
Interface for a "factory" that creates Amesos2 solvers.
Definition: Amesos2_Details_LinearSolverFactory_decl.hpp:43
virtual Teuchos::RCP< Trilinos::Details::LinearSolver< MV, OP, NormType > > getLinearSolver(const std::string &solverName)
Get an instance of a Amesos2 solver.
Definition: Amesos2_Details_LinearSolverFactory_def.hpp:358
Interface to Amesos2 solver objects.
Definition: Amesos2_Solver_decl.hpp:44
static void registerLinearSolverFactory()
Register this LinearSolverFactory with the central registry.
Definition: Amesos2_Details_LinearSolverFactory_def.hpp:367
Contains declarations for Amesos2::create and Amesos2::query.