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> 59 #ifdef HAVE_TPETRA_INST_INT_INT 60 #ifdef HAVE_AMESOS2_EPETRA 61 # include "Epetra_CrsMatrix.h" 62 #endif // HAVE_AMESOS2_EPETRA 63 #endif // HAVE_TPETRA_INST_INT_INT 66 #ifndef HAVE_AMESOS2_TPETRA 67 # define HAVE_AMESOS2_TPETRA 68 #endif // HAVE_AMESOS2_TPETRA 70 #ifdef HAVE_AMESOS2_TPETRA 71 # include "Tpetra_CrsMatrix.hpp" 72 #endif // HAVE_AMESOS2_TPETRA 83 struct GetMatrixType {
87 #ifdef HAVE_TPETRA_INST_INT_INT 88 #ifdef HAVE_AMESOS2_EPETRA 89 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
90 "Amesos2::Details::GetMatrixType: OP = Epetra_MultiVector. " 91 "This probably means that you mixed up MV and OP.");
92 #endif // HAVE_AMESOS2_EPETRA 93 #endif // HAVE_TPETRA_INST_INT_INT 95 #ifdef HAVE_AMESOS2_TPETRA 96 static_assert(! std::is_same<OP, Tpetra::MultiVector<
typename OP::scalar_type,
97 typename OP::local_ordinal_type,
typename OP::global_ordinal_type,
98 typename OP::node_type> >::value,
99 "Amesos2::Details::GetMatrixType: OP = Tpetra::MultiVector. " 100 "This probably means that you mixed up MV and OP.");
101 #endif // HAVE_AMESOS2_TPETRA 104 #ifdef HAVE_TPETRA_INST_INT_INT 105 #ifdef HAVE_AMESOS2_EPETRA 107 struct GetMatrixType<Epetra_Operator> {
108 typedef Epetra_CrsMatrix type;
110 #endif // HAVE_AMESOS2_EPETRA 111 #endif // HAVE_TPETRA_INST_INT_INT 114 #ifdef HAVE_AMESOS2_TPETRA 115 template<
class S,
class LO,
class GO,
class NT>
116 struct GetMatrixType<Tpetra::Operator<S, LO, GO, NT> > {
117 typedef Tpetra::CrsMatrix<S, LO, GO, NT> type;
119 #endif // HAVE_AMESOS2_TPETRA 121 template<
class MV,
class OP,
class NormType>
123 public Trilinos::Details::LinearSolver<MV, OP, NormType>,
124 virtual public Teuchos::Describable
127 #ifdef HAVE_TPETRA_INST_INT_INT 128 #ifdef HAVE_AMESOS2_EPETRA 129 static_assert(! std::is_same<OP, Epetra_MultiVector>::value,
130 "Amesos2::Details::LinearSolver: OP = Epetra_MultiVector. " 131 "This probably means that you mixed up MV and OP.");
132 static_assert(! std::is_same<MV, Epetra_Operator>::value,
133 "Amesos2::Details::LinearSolver: MV = Epetra_Operator. " 134 "This probably means that you mixed up MV and OP.");
135 #endif // HAVE_AMESOS2_EPETRA 136 #endif // HAVE_TPETRA_INST_INT_INT 145 typedef typename GetMatrixType<OP>::type crs_matrix_type;
146 static_assert(! std::is_same<crs_matrix_type, int>::value,
147 "Amesos2::Details::LinearSolver: The given OP type is not " 166 LinearSolver (
const std::string& solverName) :
167 solverName_ (solverName)
175 if (solverName ==
"") {
177 if (Amesos2::query (
"klu2")) {
178 solverName_ =
"klu2";
180 else if (Amesos2::query (
"superlu")) {
181 solverName_ =
"superlu";
183 else if (Amesos2::query (
"superludist")) {
184 solverName_ =
"superludist";
186 else if (Amesos2::query (
"cholmod")) {
187 solverName_ =
"cholmod";
189 else if (Amesos2::query (
"basker")) {
190 solverName_ =
"basker";
192 else if (Amesos2::query (
"superlumt")) {
193 solverName_ =
"superlumt";
195 else if (Amesos2::query (
"pardiso_mkl")) {
196 solverName_ =
"pardiso_mkl";
198 else if (Amesos2::query (
"mumps")) {
199 solverName_ =
"mumps";
201 else if (Amesos2::query (
"lapack")) {
202 solverName_ =
"lapack";
210 virtual ~LinearSolver () {}
216 void setMatrix (
const Teuchos::RCP<const OP>& A) {
219 using Teuchos::TypeNameTraits;
220 typedef crs_matrix_type MAT;
221 const char prefix[] =
"Amesos2::Details::LinearSolver::setMatrix: ";
224 solver_ = Teuchos::null;
232 RCP<const MAT> A_mat = Teuchos::rcp_dynamic_cast<
const MAT> (A);
233 TEUCHOS_TEST_FOR_EXCEPTION
234 (A_mat.is_null (), std::invalid_argument,
235 "Amesos2::Details::LinearSolver::setMatrix: " 236 "The input matrix A must be a CrsMatrix.");
237 if (solver_.is_null ()) {
244 RCP<solver_type> solver;
246 solver = Amesos2::create<MAT, MV> (solverName_, A_mat, null, null);
248 catch (std::exception& e) {
249 TEUCHOS_TEST_FOR_EXCEPTION
250 (
true, std::invalid_argument, prefix <<
"Failed to create Amesos2 " 251 "solver named \"" << solverName_ <<
"\". " 252 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
253 <<
", MV = " << TypeNameTraits<MV>::name ()
254 <<
" threw an exception: " << e.what ());
256 TEUCHOS_TEST_FOR_EXCEPTION
257 (solver.is_null (), std::invalid_argument, prefix <<
"Failed to " 258 "create Amesos2 solver named \"" << solverName_ <<
"\". " 259 "Amesos2::create<MAT = " << TypeNameTraits<MAT>::name ()
260 <<
", MV = " << TypeNameTraits<MV>::name ()
261 <<
" returned null.");
264 if (! params_.is_null ()) {
265 solver->setParameters (params_);
268 }
else if (A_ != A) {
269 solver_->setA (A_mat);
278 Teuchos::RCP<const OP> getMatrix ()
const {
283 void solve (MV& X,
const MV& B) {
284 const char prefix[] =
"Amesos2::Details::LinearSolver::solve: ";
285 TEUCHOS_TEST_FOR_EXCEPTION
286 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not " 287 "exist yet. You must call setMatrix() with a nonnull matrix before you " 288 "may call this method.");
289 TEUCHOS_TEST_FOR_EXCEPTION
290 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been " 291 "set yet. You must call setMatrix() with a nonnull matrix before you " 292 "may call this method.");
293 solver_->solve (&X, &B);
297 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params) {
298 if (! solver_.is_null ()) {
299 solver_->setParameters (params);
309 const char prefix[] =
"Amesos2::Details::LinearSolver::symbolic: ";
310 TEUCHOS_TEST_FOR_EXCEPTION
311 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not " 312 "exist yet. You must call setMatrix() with a nonnull matrix before you " 313 "may call this method.");
314 TEUCHOS_TEST_FOR_EXCEPTION
315 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been " 316 "set yet. You must call setMatrix() with a nonnull matrix before you " 317 "may call this method.");
318 solver_->symbolicFactorization ();
324 const char prefix[] =
"Amesos2::Details::LinearSolver::numeric: ";
325 TEUCHOS_TEST_FOR_EXCEPTION
326 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not " 327 "exist yet. You must call setMatrix() with a nonnull matrix before you " 328 "may call this method.");
329 TEUCHOS_TEST_FOR_EXCEPTION
330 (A_.is_null (), std::runtime_error, prefix <<
"The matrix has not been " 331 "set yet. You must call setMatrix() with a nonnull matrix before you " 332 "may call this method.");
333 solver_->numericFactorization ();
337 std::string description ()
const {
338 using Teuchos::TypeNameTraits;
339 if (solver_.is_null ()) {
340 std::ostringstream os;
341 os <<
"\"Amesos2::Details::LinearSolver\": {" 342 <<
"MV: " << TypeNameTraits<MV>::name ()
343 <<
", OP: " << TypeNameTraits<OP>::name ()
344 <<
", NormType: " << TypeNameTraits<NormType>::name ()
348 return solver_->description ();
354 describe (Teuchos::FancyOStream& out,
355 const Teuchos::EVerbosityLevel verbLevel =
356 Teuchos::Describable::verbLevel_default)
const 358 using Teuchos::TypeNameTraits;
360 if (solver_.is_null ()) {
361 if (verbLevel > Teuchos::VERB_NONE) {
362 Teuchos::OSTab tab0 (out);
363 out <<
"\"Amesos2::Details::LinearSolver\":" << endl;
364 Teuchos::OSTab tab1 (out);
365 out <<
"MV: " << TypeNameTraits<MV>::name () << endl
366 <<
"OP: " << TypeNameTraits<OP>::name () << endl
367 <<
"NormType: " << TypeNameTraits<NormType>::name () << endl;
370 if (! solver_.is_null ()) {
371 solver_->describe (out, verbLevel);
376 std::string solverName_;
377 Teuchos::RCP<solver_type> solver_;
378 Teuchos::RCP<const OP> A_;
379 Teuchos::RCP<Teuchos::ParameterList> params_;
382 template<
class MV,
class OP,
class NormType>
383 Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
388 return rcp (
new Amesos2::Details::LinearSolver<MV, OP, NormType> (solverName));
391 template<
class MV,
class OP,
class NormType>
396 #ifdef HAVE_TEUCHOSCORE_CXX11 397 typedef std::shared_ptr<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
400 typedef Teuchos::RCP<Amesos2::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
402 #endif // HAVE_TEUCHOSCORE_CXX11 405 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType> (
"Amesos2", factory);
418 #define AMESOS2_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \ 419 template class Amesos2::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \ 420 Tpetra::Operator<SC, LO, GO, NT>, \ 421 typename Tpetra::MultiVector<SC, LO, GO, NT>::mag_type>; 423 #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:385
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:394
Contains declarations for Amesos2::create and Amesos2::query.