Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosSolverFactory.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef __Belos_SolverFactory_hpp
43 #define __Belos_SolverFactory_hpp
44 
45 #include <BelosConfigDefs.hpp>
46 #include <BelosOutputManager.hpp>
47 #include <BelosSolverManager.hpp>
48 
51 #include <BelosBlockCGSolMgr.hpp>
54 #include <BelosGCRODRSolMgr.hpp>
55 #include <BelosRCGSolMgr.hpp>
56 #include <BelosMinresSolMgr.hpp>
57 #include <BelosLSQRSolMgr.hpp>
58 
59 #include <Teuchos_Array.hpp>
60 #include <Teuchos_Describable.hpp>
61 #include <Teuchos_StandardCatchMacros.hpp>
63 
64 #include <algorithm>
65 #include <map>
66 #include <sstream>
67 #include <stdexcept>
68 #include <vector>
69 
70 namespace Belos {
71 
72 namespace details {
73 
102 };
103 
104 } // namespace details
105 
213 template<class Scalar, class MV, class OP>
215 public:
223 
225  SolverFactory();
226 
257  create (const std::string& solverName,
258  const Teuchos::RCP<Teuchos::ParameterList>& solverParams);
259 
265  int numSupportedSolvers () const;
266 
273 
275  bool isSupported (const std::string& solverName) const;
276 
278 
279 
281  std::string description() const;
282 
288  void describe (Teuchos::FancyOStream& out,
291 
292 private:
305  std::map<std::string, std::string> aliasToCanonicalName_;
306 
320  std::map<std::string, details::EBelosSolverType> canonicalNameToEnum_;
321 
327  void
328  reviseParameterListForAlias (const std::string& aliasName,
329  const Teuchos::RCP<Teuchos::ParameterList>& solverParams);
330 
332  Teuchos::Array<std::string> canonicalSolverNames () const;
333 
335  Teuchos::Array<std::string> solverNameAliases () const;
336 };
337 
338 
339 namespace details {
340 
359 template<class SolverManagerBaseType, class SolverManagerType>
362 
381 template<class Scalar, class MV, class OP>
385 {
386  typedef SolverManager<Scalar, MV, OP> base_type;
387 
388  switch (solverType) {
390  typedef BlockGmresSolMgr<Scalar, MV, OP> impl_type;
391  return makeSolverManagerTmpl<base_type, impl_type> (params);
392  break;
393  }
395  typedef PseudoBlockGmresSolMgr<Scalar, MV, OP> impl_type;
396  return makeSolverManagerTmpl<base_type, impl_type> (params);
397  break;
398  }
399  case SOLVER_TYPE_BLOCK_CG: {
400  typedef BlockCGSolMgr<Scalar, MV, OP> impl_type;
401  return makeSolverManagerTmpl<base_type, impl_type> (params);
402  break;
403  }
405  typedef PseudoBlockCGSolMgr<Scalar, MV, OP> impl_type;
406  return makeSolverManagerTmpl<base_type, impl_type> (params);
407  break;
408  }
409  case SOLVER_TYPE_GCRODR: {
410  typedef GCRODRSolMgr<Scalar, MV, OP> impl_type;
411  return makeSolverManagerTmpl<base_type, impl_type> (params);
412  break;
413  }
414  case SOLVER_TYPE_RCG: {
415  typedef RCGSolMgr<Scalar, MV, OP> impl_type;
416  return makeSolverManagerTmpl<base_type, impl_type> (params);
417  break;
418  }
419  case SOLVER_TYPE_MINRES: {
420  typedef MinresSolMgr<Scalar, MV, OP> impl_type;
421  return makeSolverManagerTmpl<base_type, impl_type> (params);
422  break;
423  }
424  case SOLVER_TYPE_LSQR: {
425  typedef LSQRSolMgr<Scalar, MV, OP> impl_type;
426  return makeSolverManagerTmpl<base_type, impl_type> (params);
427  break;
428  }
431  return makeSolverManagerTmpl<base_type, impl_type> (params);
432  }
433  default: // Fall through; let the code below handle it.
434  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
435  "Invalid EBelosSolverType enum value " << solverType
436  << ". Please report this bug to the Belos developers.");
437  }
438 
439  // Compiler guard. This may result in a warning on some compilers
440  // for an unreachable statement, but it will prevent a warning on
441  // other compilers for a "missing return statement at end of
442  // non-void function."
443  return Teuchos::null;
444 }
445 
446 template<class SolverManagerBaseType, class SolverManagerType>
449 {
451  using Teuchos::parameterList;
452  using Teuchos::RCP;
453 
454  RCP<SolverManagerType> solver = rcp (new SolverManagerType);
455 
456  // Some solvers may not like to get a null ParameterList. If params
457  // is null, replace it with the solver's default parameters.
458  RCP<ParameterList> pl;
459  if (params.is_null()) {
460  pl = parameterList (*solver->getValidParameters());
461  } else {
462  pl = params;
463  }
464  TEUCHOS_TEST_FOR_EXCEPTION(pl.is_null(), std::logic_error,
465  "ParameterList to pass to solver is null. This "
466  "should never happen. Please report this bug to "
467  "the Belos developers.");
468  solver->setParameters (pl);
469  return solver;
470 }
471 
472 } // namespace details
473 
474 
475 template<class Scalar, class MV, class OP>
477 {
478  aliasToCanonicalName_["GMRES"] = "Pseudoblock GMRES";
479  // NOTE (mfh 29 Nov 2011) Accessing the flexible capability requires
480  // setting a parameter in the solver's parameter list. This affects
481  // the SolverFactory's interface, since using the "Flexible GMRES"
482  // alias requires modifying the user's parameter list if necessary.
483  // This is a good idea because users may not know about the
484  // parameter, or may have forgotten.
485  aliasToCanonicalName_["Flexible GMRES"] = "Block GMRES";
486  aliasToCanonicalName_["CG"] = "Pseudoblock CG";
487  aliasToCanonicalName_["Stochastic CG"] = "Pseudoblock Stochastic CG";
488  aliasToCanonicalName_["Recycling CG"] = "RCG";
489  aliasToCanonicalName_["Recycling GMRES"] = "GCRODR";
490  // For compatibility with Stratimikos' Belos adapter.
491  aliasToCanonicalName_["Pseudo Block GMRES"] = "Pseudoblock GMRES";
492  aliasToCanonicalName_["Pseudo Block CG"] = "Pseudoblock CG";
493 
494  // Mapping from canonical solver name (a string) to its
495  // corresponding enum value. This mapping is one-to-one.
496  canonicalNameToEnum_["Block GMRES"] = details::SOLVER_TYPE_BLOCK_GMRES;
497  canonicalNameToEnum_["Pseudoblock GMRES"] = details::SOLVER_TYPE_PSEUDO_BLOCK_GMRES;
498  canonicalNameToEnum_["Block CG"] = details::SOLVER_TYPE_BLOCK_CG;
499  canonicalNameToEnum_["Pseudoblock CG"] = details::SOLVER_TYPE_PSEUDO_BLOCK_CG;
500  canonicalNameToEnum_["Pseudoblock Stochastic CG"] = details::SOLVER_TYPE_STOCHASTIC_CG;
501  canonicalNameToEnum_["GCRODR"] = details::SOLVER_TYPE_GCRODR;
502  canonicalNameToEnum_["RCG"] = details::SOLVER_TYPE_RCG;
503  canonicalNameToEnum_["MINRES"] = details::SOLVER_TYPE_MINRES;
504  canonicalNameToEnum_["LSQR"] = details::SOLVER_TYPE_LSQR;
505 }
506 
507 
508 template<class Scalar, class MV, class OP>
509 void
511 reviseParameterListForAlias (const std::string& aliasName,
512  const Teuchos::RCP<Teuchos::ParameterList>& solverParams)
513 {
514  TEUCHOS_TEST_FOR_EXCEPTION(solverParams.is_null(), std::logic_error,
515  "Belos::SolverFactory::reviseParameterListForAlias: the input "
516  "ParameterList is supposed to be nonnull. Please report this "
517  "bug to the Belos developers.");
518  if (aliasName == "Flexible GMRES") {
519  // "Gmres" uses title case in this solver's parameter list. For
520  // our alias, we prefer the all-capitals "GMRES" that the
521  // algorithm's authors (Saad and Schultz) used.
522  solverParams->set ("Flexible Gmres", true);
523  }
524 }
525 
526 
527 template<class Scalar, class MV, class OP>
530 create (const std::string& solverName,
531  const Teuchos::RCP<Teuchos::ParameterList>& solverParams)
532 {
533  // Check whether the given name is an alias.
534  std::map<std::string, std::string>::const_iterator aliasIter =
535  aliasToCanonicalName_.find (solverName);
536  const bool isAnAlias = (aliasIter != aliasToCanonicalName_.end());
537  const std::string candidateCanonicalName =
538  isAnAlias ? aliasIter->second : solverName;
539 
540  // Get the canonical name.
541  std::map<std::string, details::EBelosSolverType>::const_iterator canonicalIter =
542  canonicalNameToEnum_.find (candidateCanonicalName);
543  const bool validCanonicalName = (canonicalIter != canonicalNameToEnum_.end());
544 
545  // Check whether we found a canonical name. If we didn't and the
546  // input name is a valid alias, that's a bug. Otherwise, the input
547  // name is invalid.
548  TEUCHOS_TEST_FOR_EXCEPTION(! validCanonicalName && isAnAlias, std::logic_error,
549  "Valid alias \"" << solverName << "\" has candidate canonical name \""
550  << candidateCanonicalName << "\", which is not a canonical solver name. "
551  "Please report this bug to the Belos developers.");
552  TEUCHOS_TEST_FOR_EXCEPTION(! validCanonicalName && ! isAnAlias,
553  std::invalid_argument, "Invalid solver name \"" << solverName << "\".");
554 
555  // If the input list is null, we create a new list and use that.
556  // This is OK because the effect of a null parameter list input is
557  // to use default parameter values. Thus, we can always replace a
558  // null list with an empty list.
560  solverParams.is_null() ? Teuchos::parameterList() : solverParams;
561 
562  // Possibly modify the input parameter list as needed.
563  if (isAnAlias) {
564  reviseParameterListForAlias (solverName, pl);
565  }
566 
567  return details::makeSolverManagerFromEnum<Scalar, MV, OP> (canonicalIter->second, pl);
568 }
569 
570 
571 template<class Scalar, class MV, class OP>
572 std::string
574 {
576  std::ostringstream os;
577  os << "Belos::SolverFactory<" << TypeNameTraits<Scalar>::name()
578  << ", " << TypeNameTraits<Scalar>::name()
579  << ", " << TypeNameTraits<MV>::name()
580  << ", " << TypeNameTraits<OP>::name()
581  << ">";
582  return os.str();
583 }
584 
585 template<class Scalar, class MV, class OP>
586 void
589  const Teuchos::EVerbosityLevel verbLevel) const
590 {
591  using std::endl;
593 
594  Teuchos::OSTab tab1 (out);
595  out << this->description();
596 
597  // At higher verbosity levels, print out the list of supported solvers.
598  if (static_cast<int> (verbLevel) > static_cast<int> (Teuchos::VERB_LOW)) {
599  out << ":" << endl;
600  Teuchos::OSTab tab2 (out);
601  out << "Number of supported solvers: " << numSupportedSolvers()
602  << endl;
603  out << "Supported canonical solver names:";
604  {
605  Teuchos::Array<std::string> names = canonicalSolverNames();
606  for (iter_type iter = names.begin(); iter != names.end(); ++iter) {
607  out << *iter;
608  if (iter + 1 != names.end()) {
609  out << ", ";
610  }
611  }
612  }
613  out << "Supported aliases to canonical names:";
614  {
615  Teuchos::Array<std::string> names = solverNameAliases();
616  for (iter_type iter = names.begin(); iter != names.end(); ++iter) {
617  out << *iter;
618  if (iter + 1 != names.end()) {
619  out << ", ";
620  }
621  }
622  }
623  }
624 }
625 
626 template<class Scalar, class MV, class OP>
627 int
629 {
630  return static_cast<int> (canonicalNameToEnum_.size());
631 }
632 
633 template<class Scalar, class MV, class OP>
636 {
637  Teuchos::Array<std::string> canonicalNames;
638  typedef std::map<std::string, details::EBelosSolverType>::const_iterator iter_type;
639  for (iter_type iter = canonicalNameToEnum_.begin();
640  iter != canonicalNameToEnum_.end(); ++iter) {
641  canonicalNames.push_back (iter->first);
642  }
643  return canonicalNames;
644 }
645 
646 template<class Scalar, class MV, class OP>
648 SolverFactory<Scalar, MV, OP>::solverNameAliases () const
649 {
651  {
652  typedef std::map<std::string, std::string>::const_iterator iter_type;
653  for (iter_type iter = aliasToCanonicalName_.begin();
654  iter != aliasToCanonicalName_.end(); ++iter) {
655  names.push_back (iter->first);
656  }
657  }
658  return names;
659 }
660 
661 template<class Scalar, class MV, class OP>
664 {
666  {
667  typedef std::map<std::string, std::string>::const_iterator iter_type;
668  for (iter_type iter = aliasToCanonicalName_.begin();
669  iter != aliasToCanonicalName_.end(); ++iter) {
670  names.push_back (iter->first);
671  }
672  }
673  {
674  typedef std::map<std::string, details::EBelosSolverType>::const_iterator iter_type;
675  for (iter_type iter = canonicalNameToEnum_.begin();
676  iter != canonicalNameToEnum_.end(); ++iter) {
677  names.push_back (iter->first);
678  }
679  }
680  return names;
681 }
682 
683 } // namespace Belos
684 
685 #endif // __Belos_SolverFactory_hpp
686 
Solver manager for the MINRES linear solver.
Class which manages the output and verbosity of the Belos solvers.
The Belos::PseudoBlockCGSolMgr provides a solver manager for the BlockCG linear solver.
bool isSupported(const std::string &solverName) const
Whether the given solver name names a supported solver.
SolverManager< Scalar, MV, OP > solver_base_type
The type returned by create().
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::Array< std::string > supportedSolverNames() const
List of supported solver names.
Teuchos::RCP< SolverManager< Scalar, MV, OP > > makeSolverManagerFromEnum(const EBelosSolverType solverType, const Teuchos::RCP< Teuchos::ParameterList > &params)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
The Belos::RCGSolMgr provides a powerful and fully-featured solver manager over the RCG (Recycling Co...
The Belos::BlockCGSolMgr provides a powerful and fully-featured solver manager over the CG and BlockC...
Interface to Block GMRES and Flexible GMRES.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
int numSupportedSolvers() const
Number of supported solvers.
The Belos::GCRODRSolMgr provides a solver manager for the GCRODR linear solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
The Belos::BlockGmresSolMgr provides a solver manager for the BlockGmres linear solver.
MINRES linear solver solution manager.
Teuchos::RCP< SolverManagerBaseType > makeSolverManagerTmpl(const Teuchos::RCP< Teuchos::ParameterList > &params)
The Belos::GCRODRSolMgr provides a powerful and fully-featured solver manager over the GCRODR linear ...
Interface to standard and &quot;pseudoblock&quot; GMRES.
The Belos::PseudoBlockStochasticCGSolMgr provides a solver manager for the stochastic BlockCG linear ...
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
The Belos::BlockCGSolMgr provides a solver manager for the BlockCG linear solver. ...
Teuchos::RCP< solver_base_type > create(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Create, configure, and return the specified solver.
iterator end()
std::vector< T >::const_iterator const_iterator
static const EVerbosityLevel verbLevel_default
LSQRSolMgr: interface to the LSQR method.
void push_back(const value_type &x)
EBelosSolverType
1-to-1 enumeration of all supported SolverManager subclasses.
The Belos::PseudoBlockStochasticCGSolMgr provides a powerful and fully-featured solver manager over t...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this SolverFactory object.
LSQR method (for linear systems and linear least-squares problems).
The Belos::RCGSolMgr provides a solver manager for the RCG (Recycling Conjugate Gradient) linear solv...
The Belos::PseudoBlockGmresSolMgr provides a solver manager for the BlockGmres linear solver...
Factory for all solvers which Belos supports.
Belos header file which uses auto-configuration information to include necessary C++ headers...
iterator begin()
std::string description() const
A string description of this SolverFactory object.
bool is_null() const