10 #ifndef __Belos_SolverFactory_hpp
11 #define __Belos_SolverFactory_hpp
20 #include <Teuchos_Describable.hpp>
21 #include <Teuchos_StandardCatchMacros.hpp>
40 const std::vector<std::string>& array);
62 template<
class Scalar,
class MV,
class OP>
130 getSolver (
const std::string& solverName,
144 create (
const std::string& solverName,
162 virtual bool isSupported (
const std::string& solverName)
const;
197 instance == Teuchos::null,
198 std::invalid_argument,
"Belos::SolverFactoryParent::registerSolver "
199 "was given a null solver to register.");
201 get_solverManagers()[solverName] = instance;
208 return (get_solverManagers().find(solverName) != get_solverManagers().end());
230 static std::vector<Teuchos::RCP<custom_solver_factory_type> > factories_;
232 static std::map<
const std::string,
Teuchos::RCP<
typename
234 get_solverManagers() {
235 static std::map<
const std::string,
Teuchos::RCP<
typename
237 return solverManagers;
241 template<
class Scalar,
class MV,
class OP>
242 std::vector<Teuchos::RCP<typename SolverFactoryParent<Scalar, MV, OP>::custom_solver_factory_type> >
243 SolverFactoryParent<Scalar, MV, OP>::factories_;
245 template<
class SolverClass,
class Scalar,
class MV,
class OP>
261 template<
class SC,
class MV,
class OP>
280 template<
class SC,
class MV,
class OP>
281 using SolverFactory = typename ::Belos::Impl::SolverFactorySelector<SC, MV, OP>::type;
285 template<
class Scalar,
class MV,
class OP>
292 RCP<solver_base_type> solver = this->getSolver (solverName, solverParams);
294 (solver.is_null (), std::invalid_argument,
295 "Invalid or unsupported Belos solver name \"" << solverName <<
"\".");
299 template<
class Scalar,
class MV,
class OP>
308 for (std::size_t k = 0; k < factories_.size (); ++k) {
309 RCP<CustomSolverFactory<Scalar, MV, OP> > factory = factories_[k];
310 if (! factory.is_null ()) {
311 RCP<SolverManager<Scalar, MV, OP> > solver =
312 factory->getSolver (solverName, solverParams);
313 if (! solver.is_null ()) {
323 std::pair<std::string, bool> aliasResult =
325 const std::string candidateCanonicalName = aliasResult.first;
326 const bool isAnAlias = aliasResult.second;
329 std::string standardized_name = isAnAlias ?
330 candidateCanonicalName :
338 solverParams.
is_null() ? Teuchos::parameterList() : solverParams;
347 it = get_solverManagers().find (standardized_name);
350 it == get_solverManagers().end(),
351 std::invalid_argument,
"Belos solver manager " << solverNameUC <<
352 " with standardized name " << standardized_name <<
" has not been"
356 it->second == Teuchos::null,
357 std::logic_error,
"Belos::SolverFactoryParent: The registered "
358 "clone source for " << solverNameUC <<
" with standardized name "
359 << standardized_name <<
" is null which should never happen."
360 ". Please report this bug to the Belos developers.");
363 RCP<solver_base_type> solver = (it->second)->clone ();
366 solver == Teuchos::null,
367 std::logic_error,
"Belos::SolverFactoryParent: Failed "
368 "to clone SolverManager with name " << solverNameUC <<
" with standardized"
369 " name" << standardized_name <<
"."
370 ". Please report this bug to the Belos developers.");
377 pl = Teuchos::parameterList (solver->getValidParameters ()->name ());
381 pl.
is_null(), std::logic_error,
382 "Belos::SolverFactory: ParameterList to pass to solver is null. This "
383 "should never happen. Please report this bug to the Belos developers.");
384 solver->setParameters (pl);
389 template<
class Scalar,
class MV,
class OP>
394 factories_.push_back (factory);
398 template<
class Scalar,
class MV,
class OP>
407 template<
class Scalar,
class MV,
class OP>
414 std::ostringstream out;
415 out <<
"\"Belos::SolverFactory\": {";
416 if (this->getObjectLabel () !=
"") {
417 out <<
"Label: " << this->getObjectLabel () <<
", ";
419 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name ()
420 <<
"\", MV: \"" << TypeNameTraits<MV>::name ()
421 <<
"\", OP: \"" << TypeNameTraits<OP>::name ()
427 template<
class Scalar,
class MV,
class OP>
449 out <<
"\"Belos::SolverFactory\":" << endl;
450 if (this->getObjectLabel () !=
"") {
451 out <<
"Label: " << this->getObjectLabel () << endl;
454 out <<
"Template parameters:" << endl;
456 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name () <<
"\"" << endl
457 <<
"MV: \"" << TypeNameTraits<MV>::name () <<
"\"" << endl
458 <<
"OP: \"" << TypeNameTraits<OP>::name () <<
"\"" << endl;
466 out <<
"Canonical solver names: ";
470 out <<
"Aliases to canonical names: ";
476 template<
class Scalar,
class MV,
class OP>
481 int numSupported = 0;
484 for (std::size_t k = 0; k < factories_.size (); ++k) {
486 RCP<custom_solver_factory_type> factory = factories_[k];
487 if (! factory.is_null ()) {
488 numSupported += factory->numSupportedSolvers ();
496 template<
class Scalar,
class MV,
class OP>
501 typedef std::vector<std::string>::const_iterator iter_type;
505 const std::size_t numFactories = factories_.
size ();
506 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
509 std::vector<std::string> supportedSolvers =
510 factory->supportedSolverNames ();
511 const std::size_t numSolvers = supportedSolvers.size ();
512 for (std::size_t solvInd = 0; solvInd < numSolvers; ++solvInd) {
513 names.
push_back (supportedSolvers[solvInd]);
520 for (iter_type iter = aliases.begin (); iter != aliases.end (); ++iter) {
526 for (iter_type iter = canonicalNames.begin ();
527 iter != canonicalNames.end (); ++iter) {
534 template<
class Scalar,
class MV,
class OP>
540 const std::size_t numFactories = factories_.size ();
541 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
543 RCP<custom_solver_factory_type> factory = factories_[factInd];
544 if (! factory.is_null ()) {
545 if (factory->isSupported (solverName)) {
556 std::pair<std::string, bool> aliasResult =
558 const std::string candidateCanonicalName = aliasResult.first;
559 const bool validCanonicalName =
560 (get_solverManagers().find(candidateCanonicalName) != get_solverManagers().end());
561 return validCanonicalName;
574 #endif // __Belos_SolverFactory_hpp
Interface for custom Belos solver factories.
Class which manages the output and verbosity of the Belos solvers.
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. ...
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...
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.