42 #ifndef __Belos_SolverFactory_hpp
43 #define __Belos_SolverFactory_hpp
72 const std::vector<std::string>& array);
94 template<
class Scalar,
class MV,
class OP>
162 getSolver (
const std::string& solverName,
176 create (
const std::string& solverName,
194 virtual bool isSupported (
const std::string& solverName)
const;
230 std::invalid_argument,
"Belos::SolverFactoryParent::registerSolver "
231 "was given a null solver to register.");
262 static std::vector<Teuchos::RCP<custom_solver_factory_type> >
factories_;
264 static std::map<
const std::string,
Teuchos::RCP<
typename
267 static std::map<
const std::string,
Teuchos::RCP<
typename
269 return solverManagers;
273 template<
class Scalar,
class MV,
class OP>
274 std::vector<Teuchos::RCP<typename SolverFactoryParent<Scalar, MV, OP>::custom_solver_factory_type> >
275 SolverFactoryParent<Scalar, MV, OP>::factories_;
277 template<
class SolverClass,
class Scalar,
class MV,
class OP>
293 template<
class SC,
class MV,
class OP>
312 template<
class SC,
class MV,
class OP>
313 using SolverFactory = typename ::Belos::Impl::SolverFactorySelector<SC, MV, OP>::type;
317 template<
class Scalar,
class MV,
class OP>
324 RCP<solver_base_type> solver = this->getSolver (solverName, solverParams);
326 (solver.is_null (), std::invalid_argument,
327 "Invalid or unsupported Belos solver name \"" << solverName <<
"\".");
331 template<
class Scalar,
class MV,
class OP>
340 for (std::size_t k = 0; k < factories_.size (); ++k) {
341 RCP<CustomSolverFactory<Scalar, MV, OP> > factory = factories_[k];
342 if (! factory.is_null ()) {
343 RCP<SolverManager<Scalar, MV, OP> > solver =
344 factory->getSolver (solverName, solverParams);
345 if (! solver.is_null ()) {
355 std::pair<std::string, bool> aliasResult =
357 const std::string candidateCanonicalName = aliasResult.first;
358 const bool isAnAlias = aliasResult.second;
361 std::string standardized_name = isAnAlias ?
362 candidateCanonicalName :
370 solverParams.
is_null() ? Teuchos::parameterList() : solverParams;
379 it = get_solverManagers().find (standardized_name);
382 it == get_solverManagers().
end(),
383 std::invalid_argument,
"Belos solver manager " << solverNameUC <<
384 " with standardized name " << standardized_name <<
" has not been"
389 std::logic_error,
"Belos::SolverFactoryParent: The registered "
390 "clone source for " << solverNameUC <<
" with standardized name "
391 << standardized_name <<
" is null which should never happen."
392 ". Please report this bug to the Belos developers.");
395 RCP<solver_base_type> solver = (it->second)->clone ();
399 std::logic_error,
"Belos::SolverFactoryParent: Failed "
400 "to clone SolverManager with name " << solverNameUC <<
" with standardized"
401 " name" << standardized_name <<
"."
402 ". Please report this bug to the Belos developers.");
409 pl = Teuchos::parameterList (solver->getValidParameters ()->name ());
413 pl.
is_null(), std::logic_error,
414 "Belos::SolverFactory: ParameterList to pass to solver is null. This "
415 "should never happen. Please report this bug to the Belos developers.");
416 solver->setParameters (pl);
421 template<
class Scalar,
class MV,
class OP>
426 factories_.push_back (factory);
430 template<
class Scalar,
class MV,
class OP>
439 template<
class Scalar,
class MV,
class OP>
446 std::ostringstream out;
447 out <<
"\"Belos::SolverFactory\": {";
448 if (this->getObjectLabel () !=
"") {
449 out <<
"Label: " << this->getObjectLabel () <<
", ";
451 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name ()
452 <<
"\", MV: \"" << TypeNameTraits<MV>::name ()
453 <<
"\", OP: \"" << TypeNameTraits<OP>::name ()
459 template<
class Scalar,
class MV,
class OP>
481 out <<
"\"Belos::SolverFactory\":" << endl;
482 if (this->getObjectLabel () !=
"") {
483 out <<
"Label: " << this->getObjectLabel () << endl;
486 out <<
"Template parameters:" << endl;
488 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name () <<
"\"" << endl
489 <<
"MV: \"" << TypeNameTraits<MV>::name () <<
"\"" << endl
490 <<
"OP: \"" << TypeNameTraits<OP>::name () <<
"\"" << endl;
498 out <<
"Canonical solver names: ";
502 out <<
"Aliases to canonical names: ";
508 template<
class Scalar,
class MV,
class OP>
513 int numSupported = 0;
516 for (std::size_t k = 0; k < factories_.size (); ++k) {
518 RCP<custom_solver_factory_type> factory = factories_[k];
519 if (! factory.is_null ()) {
520 numSupported += factory->numSupportedSolvers ();
528 template<
class Scalar,
class MV,
class OP>
533 typedef std::vector<std::string>::const_iterator iter_type;
537 const std::size_t numFactories = factories_.
size ();
538 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
541 std::vector<std::string> supportedSolvers =
542 factory->supportedSolverNames ();
543 const std::size_t numSolvers = supportedSolvers.size ();
544 for (std::size_t solvInd = 0; solvInd < numSolvers; ++solvInd) {
545 names.
push_back (supportedSolvers[solvInd]);
552 for (iter_type
iter = aliases.begin ();
iter != aliases.end (); ++
iter) {
558 for (iter_type
iter = canonicalNames.begin ();
559 iter != canonicalNames.end (); ++
iter) {
566 template<
class Scalar,
class MV,
class OP>
572 const std::size_t numFactories = factories_.size ();
573 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
575 RCP<custom_solver_factory_type> factory = factories_[factInd];
576 if (! factory.is_null ()) {
577 if (factory->isSupported (solverName)) {
588 std::pair<std::string, bool> aliasResult =
590 const std::string candidateCanonicalName = aliasResult.first;
591 const bool validCanonicalName =
592 (get_solverManagers().find(candidateCanonicalName) != get_solverManagers().end());
593 return validCanonicalName;
606 #endif // __Belos_SolverFactory_hpp
Interface for custom Belos solver factories.
Class which manages the output and verbosity of the Belos solvers.
static std::vector< Teuchos::RCP< custom_solver_factory_type > > factories_
The list of solver factories given to addFactory.
SolverFactoryParent< SC, MV, OP > type
static void registerSolver(const std::string &solverName, Teuchos::RCP< SolverFactoryParent< Scalar, MV, OP >::solver_base_type > instance)
register a solver for Inverted Injection (DII).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void clearFactories()
Clear all custom solver factories.
std::string upperCase(const std::string &s)
Return the upper-case version of s.
virtual int numSupportedSolvers() const
Number of supported solvers.
void addFactory(const Teuchos::RCP< custom_solver_factory_type > &factory)
Add a custom solver factory.
Declaration of alias functions for solver names.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object.
Pure virtual base class which describes the basic interface for a solver manager. ...
bool is_null(const RCP< T > &p)
std::vector< std::string > solverNameAliases()
List of supported aliases (to canonical solver names).
void printStringArray(std::ostream &out, const Teuchos::ArrayView< const std::string > &array)
Print the given array of strings, in YAML format, to out.
int numSupportedSolvers()
Number of Belos solvers supported for any linear algebra implementation ("generically").
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
virtual Teuchos::RCP< solver_base_type > getSolver(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Return an instance of the specified solver, or Teuchos::null if this factory does not provide the req...
static const EVerbosityLevel verbLevel_default
virtual Teuchos::Array< std::string > supportedSolverNames() const
List of supported solver names.
void push_back(const value_type &x)
virtual Teuchos::RCP< solver_base_type > create(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Create, configure, and return the specified solver.
Specializations of Belos::SolverFactory may inherit from this class to get basic SolverFactory functi...
static std::map< const std::string, Teuchos::RCP< typename SolverFactoryParent< Scalar, MV, OP >::solver_base_type > > & get_solverManagers()
std::pair< std::string, bool > getCanonicalNameFromAlias(const std::string &candidateAlias)
Get the candidate canonical name for a given candidate alias.
::Belos::SolverManager< Scalar, MV, OP > solver_base_type
The type of the solver returned by create().
void reviseParameterListForAlias(const std::string &aliasName, Teuchos::ParameterList &solverParams)
Modify the input ParameterList appropriately for the given solver alias.
void registerSolverSubclassForTypes(const std::string &solverName)
virtual bool isSupported(const std::string &solverName) const
Whether the given solver name names a supported solver.
Belos header file which uses auto-configuration information to include necessary C++ headers...
typename::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory
virtual std::string description() const
A string description of this object.
static bool isSolverRegistered(const std::string &solverName)
is solver registered for Inverted Injection (DII).
CustomSolverFactory< Scalar, MV, OP > custom_solver_factory_type
The type of a solver factory that users may give to addFactory() (which see below) ...
std::vector< std::string > canonicalSolverNames()
List of canonical solver names.