48 #ifndef AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP 49 #define AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP 51 #include "Amesos2_config.h" 53 #include "Amesos2_Solver.hpp" 54 #include "Trilinos_Details_LinearSolver.hpp" 55 #include "Trilinos_Details_LinearSolverFactory.hpp" 56 #include <type_traits> 58 #ifdef HAVE_AMESOS2_EPETRA 59 # include "Epetra_CrsMatrix.h" 60 #endif // HAVE_AMESOS2_EPETRA 63 #ifndef HAVE_AMESOS2_TPETRA 64 # define HAVE_AMESOS2_TPETRA 65 #endif // HAVE_AMESOS2_TPETRA 67 #ifdef HAVE_AMESOS2_TPETRA 68 # include "Tpetra_CrsMatrix.hpp" 69 #endif // HAVE_AMESOS2_TPETRA 80 struct GetMatrixType {
83 #ifdef HAVE_AMESOS2_EPETRA 84 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
85 "Amesos2::Details::GetMatrixType: OP = Epetra_MultiVector. " 86 "This probably means that you mixed up MV and OP.");
87 #endif // HAVE_AMESOS2_EPETRA 89 #ifdef HAVE_AMESOS2_TPETRA 90 static_assert(! std::is_same<OP, Tpetra::MultiVector<
typename OP::scalar_type,
91 typename OP::local_ordinal_type,
typename OP::global_ordinal_type,
92 typename OP::node_type> >::value,
93 "Amesos2::Details::GetMatrixType: OP = Tpetra::MultiVector. " 94 "This probably means that you mixed up MV and OP.");
95 #endif // HAVE_AMESOS2_TPETRA 98 #ifdef HAVE_AMESOS2_EPETRA 100 struct GetMatrixType<Epetra_Operator> {
101 typedef Epetra_CrsMatrix type;
103 #endif // HAVE_AMESOS2_EPETRA 105 #ifdef HAVE_AMESOS2_TPETRA 106 template<
class S,
class LO,
class GO,
class NT>
107 struct GetMatrixType<Tpetra::Operator<S, LO, GO, NT> > {
108 typedef Tpetra::CrsMatrix<S, LO, GO, NT> type;
110 #endif // HAVE_AMESOS2_TPETRA 112 template<
class MV,
class OP,
class NormType>
114 public Trilinos::Details::LinearSolver<MV, OP, NormType>,
115 virtual public Teuchos::Describable
117 #ifdef HAVE_AMESOS2_EPETRA 118 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
119 "Amesos2::Details::LinearSolver: OP = Epetra_MultiVector. " 120 "This probably means that you mixed up MV and OP.");
121 static_assert(! std::is_same<MV, Epetra_Operator>::value,
122 "Amesos2::Details::LinearSolver: MV = Epetra_Operator. " 123 "This probably means that you mixed up MV and OP.");
124 #endif // HAVE_AMESOS2_EPETRA 133 typedef typename GetMatrixType<OP>::type crs_matrix_type;
134 static_assert(! std::is_same<crs_matrix_type, int>::value,
135 "Amesos2::Details::LinearSolver: The given OP type is not " 154 LinearSolver (
const std::string& solverName) :
155 solverName_ (solverName)
163 if (solverName ==
"") {
165 if (Amesos2::query (
"klu2")) {
166 solverName_ =
"klu2";
168 else if (Amesos2::query (
"superlu")) {
169 solverName_ =
"superlu";
171 else if (Amesos2::query (
"superludist")) {
172 solverName_ =
"superludist";
174 else if (Amesos2::query (
"cholmod")) {
175 solverName_ =
"cholmod";
177 else if (Amesos2::query (
"basker")) {
178 solverName_ =
"basker";
180 else if (Amesos2::query (
"superlumt")) {
181 solverName_ =
"superlumt";
183 else if (Amesos2::query (
"pardiso_mkl")) {
184 solverName_ =
"pardiso_mkl";
186 else if (Amesos2::query (
"mumps")) {
187 solverName_ =
"mumps";
189 else if (Amesos2::query (
"lapack")) {
190 solverName_ =
"lapack";
198 virtual ~LinearSolver () {}
204 void setMatrix (
const Teuchos::RCP<const OP>& A) {
207 using Teuchos::TypeNameTraits;
208 typedef crs_matrix_type MAT;
209 const char prefix[] =
"Amesos2::Details::LinearSolver::setMatrix: ";
212 solver_ = Teuchos::null;
220 RCP<const MAT> A_mat = Teuchos::rcp_dynamic_cast<
const MAT> (A);
221 TEUCHOS_TEST_FOR_EXCEPTION
222 (A_mat.is_null (), std::invalid_argument,
223 "Amesos2::Details::LinearSolver::setMatrix: " 224 "The input matrix A must be a CrsMatrix.");
225 if (solver_.is_null ()) {
232 RCP<solver_type> solver;
234 solver = Amesos2::create<MAT, MV> (solverName_, A_mat, null, null);
236 catch (std::exception& e) {
237 TEUCHOS_TEST_FOR_EXCEPTION
238 (
true, std::invalid_argument, prefix <<
"Failed to create Amesos2 " 239 "solver named \"" << solverName_ <<
"\". " 240 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
241 <<
", MV = " << TypeNameTraits<MV>::name ()
242 <<
" threw an exception: " << e.what ());
244 TEUCHOS_TEST_FOR_EXCEPTION
245 (solver.is_null (), std::invalid_argument, prefix <<
"Failed to " 246 "create Amesos2 solver named \"" << solverName_ <<
"\". " 247 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
248 <<
", MV = " << TypeNameTraits<MV>::name ()
249 <<
" returned null.");
252 if (! params_.is_null ()) {
253 solver->setParameters (params_);
256 }
else if (A_ != A) {
257 solver_->setA (A_mat);
266 Teuchos::RCP<const OP> getMatrix ()
const {
271 void solve (MV& X,
const MV& B) {
272 const char prefix[] =
"Amesos2::Details::LinearSolver::solve: ";
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_->solve (&X, &B);
285 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params) {
286 if (! solver_.is_null ()) {
287 solver_->setParameters (params);
297 const char prefix[] =
"Amesos2::Details::LinearSolver::symbolic: ";
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_->symbolicFactorization ();
312 const char prefix[] =
"Amesos2::Details::LinearSolver::numeric: ";
313 TEUCHOS_TEST_FOR_EXCEPTION
314 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not " 315 "exist yet. You must call setMatrix() with a nonnull matrix before you " 316 "may call this method.");
317 TEUCHOS_TEST_FOR_EXCEPTION
318 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been " 319 "set yet. You must call setMatrix() with a nonnull matrix before you " 320 "may call this method.");
321 solver_->numericFactorization ();
325 std::string description ()
const {
326 using Teuchos::TypeNameTraits;
327 if (solver_.is_null ()) {
328 std::ostringstream os;
329 os <<
"\"Amesos2::Details::LinearSolver\": {" 330 <<
"MV: " << TypeNameTraits<MV>::name ()
331 <<
", OP: " << TypeNameTraits<OP>::name ()
332 <<
", NormType: " << TypeNameTraits<NormType>::name ()
336 return solver_->description ();
342 describe (Teuchos::FancyOStream& out,
343 const Teuchos::EVerbosityLevel verbLevel =
344 Teuchos::Describable::verbLevel_default)
const 346 using Teuchos::TypeNameTraits;
348 if (solver_.is_null ()) {
349 if (verbLevel > Teuchos::VERB_NONE) {
350 Teuchos::OSTab tab0 (out);
351 out <<
"\"Amesos2::Details::LinearSolver\":" << endl;
352 Teuchos::OSTab tab1 (out);
353 out <<
"MV: " << TypeNameTraits<MV>::name () << endl
354 <<
"OP: " << TypeNameTraits<OP>::name () << endl
355 <<
"NormType: " << TypeNameTraits<NormType>::name () << endl;
358 if (! solver_.is_null ()) {
359 solver_->describe (out, verbLevel);
364 std::string solverName_;
365 Teuchos::RCP<solver_type> solver_;
366 Teuchos::RCP<const OP> A_;
367 Teuchos::RCP<Teuchos::ParameterList> params_;
370 template<
class MV,
class OP,
class NormType>
371 Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
376 return rcp (
new Amesos2::Details::LinearSolver<MV, OP, NormType> (solverName));
379 template<
class MV,
class OP,
class NormType>
384 #ifdef HAVE_TEUCHOSCORE_CXX11 385 typedef std::shared_ptr<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
388 typedef Teuchos::RCP<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
390 #endif // HAVE_TEUCHOSCORE_CXX11 393 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType> (
"Amesos2", factory);
406 #define AMESOS2_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \ 407 template class Amesos2::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \ 408 Tpetra::Operator<SC, LO, GO, NT>, \ 409 typename Tpetra::MultiVector<SC, LO, GO, NT>::mag_type>; 411 #endif // AMESOS2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP Interface for a "factory" that creates Amesos2 solvers.
Definition: Amesos2_Details_LinearSolverFactory_decl.hpp:77
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
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:373
Interface to Amesos2 solver objects.
Definition: Amesos2_Solver_decl.hpp:78
static void registerLinearSolverFactory()
Register this LinearSolverFactory with the central registry.
Definition: Amesos2_Details_LinearSolverFactory_def.hpp:382
Contains declarations for Amesos2::create and Amesos2::query.