10#ifndef __Belos_SolverFactory_hpp
11#define __Belos_SolverFactory_hpp
20#include <Teuchos_Describable.hpp>
21#include <Teuchos_StandardCatchMacros.hpp>
22#include <Teuchos_TypeNameTraits.hpp>
35 const Teuchos::ArrayView<const std::string>& array);
40 const std::vector<std::string>& array);
64template<
class Scalar,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,Scalar>>
66 public Teuchos::Describable
131 virtual Teuchos::RCP<solver_base_type>
133 const Teuchos::RCP<Teuchos::ParameterList>& solverParams);
145 virtual Teuchos::RCP<solver_base_type>
147 const Teuchos::RCP<Teuchos::ParameterList>& solverParams);
181 addFactory (
const Teuchos::RCP<custom_solver_factory_type>& factory);
198 TEUCHOS_TEST_FOR_EXCEPTION(
199 instance == Teuchos::null,
200 std::invalid_argument,
"Belos::SolverFactoryParent::registerSolver "
201 "was given a null solver to register.");
203 get_solverManagers()[solverName] = instance;
210 return (get_solverManagers().find(solverName) != get_solverManagers().end());
226 const Teuchos::EVerbosityLevel verbLevel =
227 Teuchos::Describable::verbLevel_default)
const;
232 static std::vector<Teuchos::RCP<custom_solver_factory_type> > factories_;
234 static std::map<
const std::string, Teuchos::RCP<
typename
236 get_solverManagers() {
237 static std::map<
const std::string, Teuchos::RCP<
typename
239 return solverManagers;
243template<
class Scalar,
class MV,
class OP,
class DM>
244std::vector<Teuchos::RCP<typename SolverFactoryParent<Scalar, MV, OP, DM>::custom_solver_factory_type> >
245SolverFactoryParent<Scalar, MV, OP, DM>::factories_;
247template<
class SolverClass,
class Scalar,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,Scalar>>
250 Teuchos::RCP<SolverClass> solver (
new SolverClass);
263template<
class SC,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,SC>>
282template<
class SC,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,SC>>
283using SolverFactory = typename ::Belos::Impl::SolverFactorySelector<SC, MV, OP, DM>::type;
287template<
class Scalar,
class MV,
class OP,
class DM>
288Teuchos::RCP<typename SolverFactoryParent<Scalar, MV, OP, DM>::solver_base_type>
290create (
const std::string& solverName,
291 const Teuchos::RCP<Teuchos::ParameterList>& solverParams)
294 RCP<solver_base_type> solver = this->
getSolver (solverName, solverParams);
295 TEUCHOS_TEST_FOR_EXCEPTION
296 (solver.is_null (), std::invalid_argument,
297 "Invalid or unsupported Belos solver name \"" << solverName <<
"\".");
301template<
class Scalar,
class MV,
class OP,
class DM>
302Teuchos::RCP<typename SolverFactoryParent<Scalar, MV, OP, DM>::solver_base_type>
305 const Teuchos::RCP<Teuchos::ParameterList>& solverParams)
310 for (std::size_t k = 0; k < factories_.size (); ++k) {
311 RCP<CustomSolverFactory<Scalar, MV, OP, DM> > factory = factories_[k];
312 if (! factory.is_null ()) {
313 RCP<SolverManager<Scalar, MV, OP, DM> > solver =
314 factory->getSolver (solverName, solverParams);
315 if (! solver.is_null ()) {
325 std::pair<std::string, bool> aliasResult =
327 const std::string candidateCanonicalName = aliasResult.first;
328 const bool isAnAlias = aliasResult.second;
331 std::string standardized_name = isAnAlias ?
332 candidateCanonicalName :
339 Teuchos::RCP<Teuchos::ParameterList> pl =
340 solverParams.is_null() ? Teuchos::parameterList() : solverParams;
347 typename std::map<
const std::string, Teuchos::RCP<
349 it = get_solverManagers().find (standardized_name);
351 TEUCHOS_TEST_FOR_EXCEPTION(
352 it == get_solverManagers().end(),
353 std::invalid_argument,
"Belos solver manager " << solverNameUC <<
354 " with standardized name " << standardized_name <<
" has not been"
357 TEUCHOS_TEST_FOR_EXCEPTION(
358 it->second == Teuchos::null,
359 std::logic_error,
"Belos::SolverFactoryParent: The registered "
360 "clone source for " << solverNameUC <<
" with standardized name "
361 << standardized_name <<
" is null which should never happen."
362 ". Please report this bug to the Belos developers.");
365 RCP<solver_base_type> solver = (it->second)->clone ();
367 TEUCHOS_TEST_FOR_EXCEPTION(
368 solver == Teuchos::null,
369 std::logic_error,
"Belos::SolverFactoryParent: Failed "
370 "to clone SolverManager with name " << solverNameUC <<
" with standardized"
371 " name" << standardized_name <<
"."
372 ". Please report this bug to the Belos developers.");
379 pl = Teuchos::parameterList (solver->getValidParameters ()->name ());
382 TEUCHOS_TEST_FOR_EXCEPTION(
383 pl.is_null(), std::logic_error,
384 "Belos::SolverFactory: ParameterList to pass to solver is null. This "
385 "should never happen. Please report this bug to the Belos developers.");
386 solver->setParameters (pl);
391template<
class Scalar,
class MV,
class OP,
class DM>
396 factories_.push_back (factory);
400template<
class Scalar,
class MV,
class OP,
class DM>
409template<
class Scalar,
class MV,
class OP,
class DM>
414 using Teuchos::TypeNameTraits;
416 std::ostringstream out;
417 out <<
"\"Belos::SolverFactory\": {";
418 if (this->getObjectLabel () !=
"") {
419 out <<
"Label: " << this->getObjectLabel () <<
", ";
421 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name ()
422 <<
"\", MV: \"" << TypeNameTraits<MV>::name ()
423 <<
"\", OP: \"" << TypeNameTraits<OP>::name ()
424 <<
"\", DM: \"" << TypeNameTraits<DM>::name ()
430template<
class Scalar,
class MV,
class OP,
class DM>
433describe (Teuchos::FancyOStream& out,
434 const Teuchos::EVerbosityLevel verbLevel)
const
436 using Teuchos::TypeNameTraits;
439 const Teuchos::EVerbosityLevel vl =
440 (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
442 if (vl == Teuchos::VERB_NONE) {
447 Teuchos::OSTab tab0 (out);
452 out <<
"\"Belos::SolverFactory\":" << endl;
453 if (this->getObjectLabel () !=
"") {
454 out <<
"Label: " << this->getObjectLabel () << endl;
457 out <<
"Template parameters:" << endl;
458 Teuchos::OSTab tab1 (out);
459 out <<
"Scalar: \"" << TypeNameTraits<Scalar>::name () <<
"\"" << endl
460 <<
"MV: \"" << TypeNameTraits<MV>::name () <<
"\"" << endl
461 <<
"OP: \"" << TypeNameTraits<OP>::name () <<
"\"" << endl
462 <<
"DM: \"" << TypeNameTraits<DM>::name () <<
"\"" << endl;
466 if (vl > Teuchos::VERB_LOW) {
467 Teuchos::OSTab tab1 (out);
470 out <<
"Canonical solver names: ";
474 out <<
"Aliases to canonical names: ";
480template<
class Scalar,
class MV,
class OP,
class DM>
485 int numSupported = 0;
488 for (std::size_t k = 0; k < factories_.size (); ++k) {
490 RCP<custom_solver_factory_type> factory = factories_[k];
491 if (! factory.is_null ()) {
492 numSupported += factory->numSupportedSolvers ();
500template<
class Scalar,
class MV,
class OP,
class DM>
501Teuchos::Array<std::string>
505 typedef std::vector<std::string>::const_iterator iter_type;
506 Teuchos::Array<std::string> names;
509 const std::size_t numFactories = factories_.size ();
510 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
511 Teuchos::RCP<custom_solver_factory_type> factory = factories_[factInd];
512 if (! factory.is_null ()) {
513 std::vector<std::string> supportedSolvers =
514 factory->supportedSolverNames ();
515 const std::size_t numSolvers = supportedSolvers.size ();
516 for (std::size_t solvInd = 0; solvInd < numSolvers; ++solvInd) {
517 names.push_back (supportedSolvers[solvInd]);
524 for (iter_type iter = aliases.begin (); iter != aliases.end (); ++iter) {
525 names.push_back (*iter);
530 for (iter_type iter = canonicalNames.begin ();
531 iter != canonicalNames.end (); ++iter) {
532 names.push_back (*iter);
538template<
class Scalar,
class MV,
class OP,
class DM>
544 const std::size_t numFactories = factories_.size ();
545 for (std::size_t factInd = 0; factInd < numFactories; ++factInd) {
547 RCP<custom_solver_factory_type> factory = factories_[factInd];
548 if (! factory.is_null ()) {
549 if (factory->isSupported (solverName)) {
560 std::pair<std::string, bool> aliasResult =
562 const std::string candidateCanonicalName = aliasResult.first;
563 const bool validCanonicalName =
564 (get_solverManagers().find(candidateCanonicalName) != get_solverManagers().end());
565 return validCanonicalName;
Declaration of alias functions for solver names.
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class which describes the basic interface for a solver manager.
Interface for custom Belos solver factories.
Specializations of Belos::SolverFactory may inherit from this class to get basic SolverFactory functi...
virtual int numSupportedSolvers() const
Number of supported solvers.
void clearFactories()
Clear all custom solver factories.
void addFactory(const Teuchos::RCP< custom_solver_factory_type > &factory)
Add a custom solver factory.
static bool isSolverRegistered(const std::string &solverName)
is solver registered for Inverted Injection (DII).
virtual Teuchos::RCP< solver_base_type > create(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Create, configure, and return the specified solver.
virtual Teuchos::Array< std::string > supportedSolverNames() const
List of supported solver names.
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...
virtual bool isSupported(const std::string &solverName) const
Whether the given solver name names a supported solver.
CustomSolverFactory< Scalar, MV, OP, DM > custom_solver_factory_type
The type of a solver factory that users may give to addFactory() (which see below).
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object.
static void registerSolver(const std::string &solverName, Teuchos::RCP< SolverFactoryParent< Scalar, MV, OP, DM >::solver_base_type > instance)
register a solver for Inverted Injection (DII).
::Belos::SolverManager< Scalar, MV, OP, DM > solver_base_type
The type of the solver returned by create().
virtual std::string description() const
A string description of this object.
SolverFactoryParent< SC, MV, OP, DM > type
std::pair< std::string, bool > getCanonicalNameFromAlias(const std::string &candidateAlias)
Get the candidate canonical name for a given candidate alias.
void reviseParameterListForAlias(const std::string &aliasName, Teuchos::ParameterList &solverParams)
Modify the input ParameterList appropriately for the given solver alias.
std::vector< std::string > solverNameAliases()
List of supported aliases (to canonical solver names).
int numSupportedSolvers()
Number of Belos solvers supported for any linear algebra implementation ("generically").
std::vector< std::string > canonicalSolverNames()
List of canonical solver names.
void registerSolverSubclassForTypes(const std::string &solverName)
std::string upperCase(const std::string &s)
Return the upper-case version of s.
void printStringArray(std::ostream &out, const Teuchos::ArrayView< const std::string > &array)
Print the given array of strings, in YAML format, to out.
typename ::Belos::Impl::SolverFactorySelector< SC, MV, OP, DM >::type SolverFactory