10 #ifndef TRILINOS_DETAILS_LINEARSOLVER_FACTORY_HPP
11 #define TRILINOS_DETAILS_LINEARSOLVER_FACTORY_HPP
51 #include "TeuchosRemainder_config.h"
53 #ifdef HAVE_TEUCHOSCORE_CXX11
55 #endif // HAVE_TEUCHOSCORE_CXX11
73 #if ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) && defined(HAVE_TEUCHOS_CXX_ATTRIBUTE_WEAK)
89 #endif // ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) && defined(HAVE_TEUCHOS_CXX_ATTRIBUTE_WEAK)
104 template<
class MV,
class OP,
class NormType>
133 template<
class MV,
class OP,
class NormType>
135 getLinearSolver (
const std::string& packageName,
const std::string& solverName);
202 template<
class MV,
class OP,
class NormType>
294 template<
class MV,
class OP,
class NormType>
296 registerLinearSolverFactory (
const std::string& packageName,
297 #ifdef HAVE_TEUCHOSCORE_CXX11
301 #endif // HAVE_TEUCHOSCORE_CXX11
367 template<
class MV,
class OP,
class NormType>
375 #ifdef HAVE_TEUCHOSCORE_CXX11
379 #endif // HAVE_TEUCHOSCORE_CXX11
388 typedef std::map<std::string, factory_pointer_type>
map_type;
407 typedef typename map_type::iterator iter_type;
408 iter_type it = factories_->find (packageName);
409 if (it == factories_->end ()) {
435 (factory.
get () == NULL, std::invalid_argument,
"Trilinos::Details::"
436 "LinearSolverFactoryRepository::registerLinearSolverFactory: Input "
437 "'factory' is NULL!");
439 if (factories_->find (packageName) == factories_->end ()) {
440 factories_->insert (std::make_pair (packageName, factory));
460 static void createFactories () {
461 if (factories_ == NULL) {
467 (void) atexit (freeFactories);
470 (factories_ == NULL, std::logic_error,
"Trilinos::Details::"
471 "LinearSolverFactoryRepository::createFactories: "
472 "Should never get here! factories_ is NULL.");
483 static void freeFactories () {
484 if (factories_ != NULL) {
494 template<
class MV,
class OP,
class NormType>
495 typename LinearSolverFactoryRepository<MV, OP, NormType>::map_type*
496 LinearSolverFactoryRepository<MV, OP, NormType>::factories_ = NULL;
504 template<
class MV,
class OP,
class NormType>
506 registerLinearSolverFactory (
const std::string& packageName,
507 #ifdef HAVE_TEUCHOSCORE_CXX11
508 const std::shared_ptr<LinearSolverFactory<MV, OP, NormType> >& factory)
517 template<
class MV,
class OP,
class NormType>
519 getLinearSolver (
const std::string& packageName,
const std::string& solverName)
523 typedef Impl::LinearSolverFactoryRepository<MV, OP, NormType> repo_type;
524 typedef typename repo_type::factory_pointer_type factory_pointer_type;
525 typedef LinearSolver<MV, OP, NormType> solver_type;
526 const char prefix[] =
"Trilinos::Details::getLinearSolver: ";
530 #if ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) && defined(HAVE_TEUCHOS_CXX_ATTRIBUTE_WEAK)
541 #endif // ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) && defined(HAVE_TEUCHOS_CXX_ATTRIBUTE_WEAK)
547 const bool haveRunTimeReg =
552 (! pkgExists, std::invalid_argument, prefix <<
"Package \"" << packageName
553 <<
"\" never registered a LinearSolverFactory for _any_ combination of "
554 "template parameters MV, OP, and NormType. This means either that the "
555 "package name is invalid, or that the package is not enabled. "
556 "Trilinos_ENABLE_LINEAR_SOLVER_FACTORY_REGISTRATION = "
557 << (haveRunTimeReg ?
"ON" :
"OFF") <<
".");
559 factory_pointer_type factory = repo_type::getFactory (packageName);
561 (factory.get () == NULL, std::invalid_argument, prefix <<
"Package \"" <<
562 packageName <<
"\" is valid, but it never registered a LinearSolverFactory"
563 " for template parameters "
564 "MV = " << TypeNameTraits<MV>::name () <<
", "
565 "OP = " << TypeNameTraits<OP>::name () <<
", "
566 "NormType = " << TypeNameTraits<NormType>::name () <<
". "
567 "Trilinos_ENABLE_LINEAR_SOLVER_FACTORY_REGISTRATION = "
568 << (haveRunTimeReg ?
"ON" :
"OFF") <<
".");
570 RCP<solver_type> solver = factory->getLinearSolver (solverName);
572 (solver.is_null (), std::invalid_argument, prefix <<
"Invalid solver name "
573 "\"" << solverName <<
"\". However, package \"" << packageName <<
"\" is "
574 "valid, and it did register a LinearSolverFactory for template parameters "
575 "MV = " << TypeNameTraits<MV>::name () <<
", "
576 "OP = " << TypeNameTraits<OP>::name () <<
", "
577 "NormType = " << TypeNameTraits<NormType>::name () <<
". "
578 "Trilinos_ENABLE_LINEAR_SOLVER_FACTORY_REGISTRATION = "
579 << (haveRunTimeReg ?
"ON" :
"OFF") <<
".");
587 #endif // TRILINOS_DETAILS_LINEARSOLVER_FACTORY_HPP
static factory_pointer_type getFactory(const std::string &packageName)
Get a LinearSolverFactory from the given package.
Teuchos::RCP< LinearSolverFactory< MV, OP, NormType > > factory_pointer_type
Type of a reference-counted pointer to LinearSolverFactory.
Repository of solver factories.
bool rememberRegisteredSomeLinearSolverFactory(const std::string &packageName)
Remember which packages registered at least one LinearSolverFactory, with any template parameters...
static void registerLinearSolverFactory(const std::string &packageName, const factory_pointer_type &factory)
Register the given factory from a package.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
T * get() const
Get the raw C++ pointer to the underlying object.
bool registeredSomeLinearSolverFactory(const std::string &packageName)
Did the package with the given name register at least one LinearSolverFactory, with any template para...
virtual Teuchos::RCP< LinearSolver< MV, OP, NormType > > getLinearSolver(const std::string &solverName)=0
Get an instance of a solver from a particular package.
bool haveLinearSolverFactoryRunTimeRegistration()
Whether the CMake run-time registration option is ON.
std::map< std::string, factory_pointer_type > map_type
Type of a data structure that looks up a LinearSolverFactory corresponding to a given package name...
Interface for a "factory" that creates solvers.
Default traits class that just returns typeid(T).name().
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.