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"
26#ifndef HAVE_AMESOS2_TPETRA
27# define HAVE_AMESOS2_TPETRA
30#ifdef HAVE_AMESOS2_TPETRA
31# include "Tpetra_CrsMatrix.hpp"
46#ifdef HAVE_AMESOS2_TPETRA
47 static_assert(! std::is_same<OP, Tpetra::MultiVector<
typename OP::scalar_type,
48 typename OP::local_ordinal_type,
typename OP::global_ordinal_type,
49 typename OP::node_type> >::value,
50 "Amesos2::Details::GetMatrixType: OP = Tpetra::MultiVector. "
51 "This probably means that you mixed up MV and OP.");
55#ifdef HAVE_AMESOS2_TPETRA
56template<
class S,
class LO,
class GO,
class NT>
57struct GetMatrixType<Tpetra::Operator<S, LO, GO, NT> > {
58 typedef Tpetra::CrsMatrix<S, LO, GO, NT> type;
62template<
class MV,
class OP,
class NormType>
64 public Trilinos::Details::LinearSolver<MV, OP, NormType>,
65 virtual public Teuchos::Describable
75 typedef typename GetMatrixType<OP>::type crs_matrix_type;
76 static_assert(! std::is_same<crs_matrix_type, int>::value,
77 "Amesos2::Details::LinearSolver: The given OP type is not "
96 LinearSolver (
const std::string& solverName) :
97 solverName_ (solverName)
105 if (solverName ==
"") {
107 if (Amesos2::query (
"klu2")) {
108 solverName_ =
"klu2";
110 else if (Amesos2::query (
"superlu")) {
111 solverName_ =
"superlu";
113 else if (Amesos2::query (
"superludist")) {
114 solverName_ =
"superludist";
116 else if (Amesos2::query (
"cholmod")) {
117 solverName_ =
"cholmod";
119 else if (Amesos2::query (
"cusolver")) {
120 solverName_ =
"cusolver";
122 else if (Amesos2::query (
"basker")) {
123 solverName_ =
"basker";
125 else if (Amesos2::query (
"shylubasker")) {
126 solverName_ =
"shylubasker";
128 else if (Amesos2::query (
"ShyLUBasker")) {
129 solverName_ =
"shylubasker";
131 else if (Amesos2::query (
"superlumt")) {
132 solverName_ =
"superlumt";
134 else if (Amesos2::query (
"pardiso_mkl")) {
135 solverName_ =
"pardiso_mkl";
137 else if (Amesos2::query (
"css_mkl")) {
138 solverName_ =
"css_mkl";
140 else if (Amesos2::query (
"mumps")) {
141 solverName_ =
"mumps";
143 else if (Amesos2::query (
"lapack")) {
144 solverName_ =
"lapack";
146 else if (Amesos2::query (
"umfpack")) {
147 solverName_ =
"umfpack";
149 else if (Amesos2::query (
"tacho")) {
150 solverName_ =
"tacho";
158 virtual ~LinearSolver () {}
164 void setMatrix (
const Teuchos::RCP<const OP>& A) {
167 using Teuchos::TypeNameTraits;
168 typedef crs_matrix_type MAT;
169 const char prefix[] =
"Amesos2::Details::LinearSolver::setMatrix: ";
172 solver_ = Teuchos::null;
180 RCP<const MAT> A_mat = Teuchos::rcp_dynamic_cast<const MAT> (A);
181 TEUCHOS_TEST_FOR_EXCEPTION
182 (A_mat.is_null (), std::invalid_argument,
183 "Amesos2::Details::LinearSolver::setMatrix: "
184 "The input matrix A must be a CrsMatrix.");
185 if (solver_.is_null ()) {
192 RCP<solver_type> solver;
194 solver = Amesos2::create<MAT, MV> (solverName_, A_mat, null, null);
196 catch (std::exception& e) {
197 TEUCHOS_TEST_FOR_EXCEPTION
198 (
true, std::invalid_argument, prefix <<
"Failed to create Amesos2 "
199 "solver named \"" << solverName_ <<
"\". "
200 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
201 <<
", MV = " << TypeNameTraits<MV>::name ()
202 <<
" threw an exception: " << e.what ());
204 TEUCHOS_TEST_FOR_EXCEPTION
205 (solver.is_null (), std::invalid_argument, prefix <<
"Failed to "
206 "create Amesos2 solver named \"" << solverName_ <<
"\". "
207 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
208 <<
", MV = " << TypeNameTraits<MV>::name ()
209 <<
" returned null.");
212 if (! params_.is_null ()) {
213 solver->setParameters (params_);
216 }
else if (A_ != A) {
217 solver_->setA (A_mat);
226 Teuchos::RCP<const OP> getMatrix ()
const {
231 void solve (MV& X,
const MV& B) {
232 const char prefix[] =
"Amesos2::Details::LinearSolver::solve: ";
233 TEUCHOS_TEST_FOR_EXCEPTION
234 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
235 "exist yet. You must call setMatrix() with a nonnull matrix before you "
236 "may call this method.");
237 TEUCHOS_TEST_FOR_EXCEPTION
238 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been "
239 "set yet. You must call setMatrix() with a nonnull matrix before you "
240 "may call this method.");
241 solver_->solve (&X, &B);
245 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params) {
246 if (! solver_.is_null ()) {
247 solver_->setParameters (params);
257 const char prefix[] =
"Amesos2::Details::LinearSolver::symbolic: ";
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_->symbolicFactorization ();
272 const char prefix[] =
"Amesos2::Details::LinearSolver::numeric: ";
273 TEUCHOS_TEST_FOR_EXCEPTION
274 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
275 "exist yet. You must call setMatrix() with a nonnull matrix before you "
276 "may call this method.");
277 TEUCHOS_TEST_FOR_EXCEPTION
278 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been "
279 "set yet. You must call setMatrix() with a nonnull matrix before you "
280 "may call this method.");
281 solver_->numericFactorization ();
285 std::string description ()
const {
286 using Teuchos::TypeNameTraits;
287 if (solver_.is_null ()) {
288 std::ostringstream os;
289 os <<
"\"Amesos2::Details::LinearSolver\": {"
290 <<
"MV: " << TypeNameTraits<MV>::name ()
291 <<
", OP: " << TypeNameTraits<OP>::name ()
292 <<
", NormType: " << TypeNameTraits<NormType>::name ()
296 return solver_->description ();
302 describe (Teuchos::FancyOStream& out,
303 const Teuchos::EVerbosityLevel verbLevel =
304 Teuchos::Describable::verbLevel_default)
const
306 using Teuchos::TypeNameTraits;
308 if (solver_.is_null ()) {
309 if (verbLevel > Teuchos::VERB_NONE) {
310 Teuchos::OSTab tab0 (out);
311 out <<
"\"Amesos2::Details::LinearSolver\":" << endl;
312 Teuchos::OSTab tab1 (out);
313 out <<
"MV: " << TypeNameTraits<MV>::name () << endl
314 <<
"OP: " << TypeNameTraits<OP>::name () << endl
315 <<
"NormType: " << TypeNameTraits<NormType>::name () << endl;
318 if (! solver_.is_null ()) {
319 solver_->describe (out, verbLevel);
324 std::string solverName_;
325 Teuchos::RCP<solver_type> solver_;
326 Teuchos::RCP<const OP> A_;
327 Teuchos::RCP<Teuchos::ParameterList> params_;
330template<
class MV,
class OP,
class NormType>
331Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
336 return rcp (
new Amesos2::Details::LinearSolver<MV, OP, NormType> (solverName));
339template<
class MV,
class OP,
class NormType>
344#ifdef HAVE_TEUCHOSCORE_CXX11
345 typedef std::shared_ptr<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
348 typedef Teuchos::RCP<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
353 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType> (
"Amesos2", factory);
366#define AMESOS2_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \
367 template class Amesos2::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \
368 Tpetra::Operator<SC, LO, GO, NT>, \
369 typename Tpetra::MultiVector<SC, LO, GO, NT>::mag_type>;
Contains declarations for Amesos2::create and Amesos2::query.
Interface for a "factory" that creates Amesos2 solvers.
Definition Amesos2_Details_LinearSolverFactory_decl.hpp:44
static void registerLinearSolverFactory()
Register this LinearSolverFactory with the central registry.
Definition Amesos2_Details_LinearSolverFactory_def.hpp:342
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:333
Interface to Amesos2 solver objects.
Definition Amesos2_Solver_decl.hpp:44