Belos  Version of the Day
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 
49 #include <BelosBlockCGSolMgr.hpp>
51 #include <BelosGCRODRSolMgr.hpp>
55 #include <BelosLSQRSolMgr.hpp>
56 #include <BelosMinresSolMgr.hpp>
57 #include <BelosGmresPolySolMgr.hpp>
58 #include <BelosPCPGSolMgr.hpp>
59 #include <BelosRCGSolMgr.hpp>
60 #include <BelosTFQMRSolMgr.hpp>
63 #include <BelosBiCGStabSolMgr.hpp>
64 
66 
67 #include <Teuchos_Describable.hpp>
68 #include <Teuchos_StandardCatchMacros.hpp>
69 #include <Teuchos_TypeNameTraits.hpp>
70 
71 #include <algorithm>
72 #include <locale>
73 #include <map>
74 #include <sstream>
75 #include <stdexcept>
76 #include <vector>
77 
78 namespace Belos {
79 
208 template<class Scalar, class MV, class OP>
209 class SolverFactory : public Teuchos::Describable {
210 public:
217 
219  SolverFactory ();
220 
244  Teuchos::RCP<solver_base_type>
245  create (const std::string& solverName,
246  const Teuchos::RCP<Teuchos::ParameterList>& solverParams);
247 
253  int numSupportedSolvers () const;
254 
260  Teuchos::Array<std::string> supportedSolverNames () const;
261 
263  bool isSupported (const std::string& solverName) const;
264 
266 
267 
269  std::string description() const;
270 
276  void describe (Teuchos::FancyOStream& out,
277  const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const;
279 
280 private:
282  static void
283  printStringArray (std::ostream& out,
284  const Teuchos::ArrayView<const std::string>& array)
285  {
286  typedef Teuchos::ArrayView<std::string>::const_iterator iter_type;
287 
288  out << "[";
289  for (iter_type iter = array.begin(); iter != array.end(); ++iter) {
290  out << "\"" << *iter << "\"";
291  if (iter + 1 != array.end()) {
292  out << ", ";
293  }
294  }
295  out << "]";
296  }
297 
299  static void
300  printStringArray (std::ostream& out,
301  const std::vector<std::string>& array)
302  {
303  typedef std::vector<std::string>::const_iterator iter_type;
304 
305  out << "[";
306  for (iter_type iter = array.begin(); iter != array.end(); ++iter) {
307  out << "\"" << *iter << "\"";
308  if (iter + 1 != array.end()) {
309  out << ", ";
310  }
311  }
312  out << "]";
313  }
314 };
315 
316 
317 namespace Details {
318 
337 template<class SolverManagerBaseType, class SolverManagerType>
338 Teuchos::RCP<SolverManagerBaseType>
339 makeSolverManagerTmpl (const Teuchos::RCP<Teuchos::ParameterList>& params);
340 
359 template<class Scalar, class MV, class OP>
360 Teuchos::RCP<SolverManager<Scalar, MV, OP> >
362  const Teuchos::RCP<Teuchos::ParameterList>& params)
363 {
364  typedef SolverManager<Scalar, MV, OP> base_type;
365 
366  switch (solverType) {
368  typedef BlockGmresSolMgr<Scalar, MV, OP> impl_type;
369  return makeSolverManagerTmpl<base_type, impl_type> (params);
370  }
372  typedef PseudoBlockGmresSolMgr<Scalar, MV, OP> impl_type;
373  return makeSolverManagerTmpl<base_type, impl_type> (params);
374  }
375  case SOLVER_TYPE_BLOCK_CG: {
376  typedef BlockCGSolMgr<Scalar, MV, OP> impl_type;
377  return makeSolverManagerTmpl<base_type, impl_type> (params);
378  }
380  typedef PseudoBlockCGSolMgr<Scalar, MV, OP> impl_type;
381  return makeSolverManagerTmpl<base_type, impl_type> (params);
382  }
383  case SOLVER_TYPE_GCRODR: {
384  typedef GCRODRSolMgr<Scalar, MV, OP> impl_type;
385  return makeSolverManagerTmpl<base_type, impl_type> (params);
386  }
387  case SOLVER_TYPE_RCG: {
388  typedef RCGSolMgr<Scalar, MV, OP> impl_type;
389  return makeSolverManagerTmpl<base_type, impl_type> (params);
390  }
391  case SOLVER_TYPE_MINRES: {
392  typedef MinresSolMgr<Scalar, MV, OP> impl_type;
393  return makeSolverManagerTmpl<base_type, impl_type> (params);
394  }
395  case SOLVER_TYPE_LSQR: {
396  typedef LSQRSolMgr<Scalar, MV, OP> impl_type;
397  return makeSolverManagerTmpl<base_type, impl_type> (params);
398  }
401  return makeSolverManagerTmpl<base_type, impl_type> (params);
402  }
403  case SOLVER_TYPE_TFQMR: {
404  typedef TFQMRSolMgr<Scalar, MV, OP> impl_type;
405  return makeSolverManagerTmpl<base_type, impl_type> (params);
406  }
408  typedef PseudoBlockTFQMRSolMgr<Scalar, MV, OP> impl_type;
409  return makeSolverManagerTmpl<base_type, impl_type> (params);
410  }
411  case SOLVER_TYPE_GMRES_POLY: {
412  typedef GmresPolySolMgr<Scalar, MV, OP> impl_type;
413  return makeSolverManagerTmpl<base_type, impl_type> (params);
414  }
415  case SOLVER_TYPE_PCPG: {
416  typedef PCPGSolMgr<Scalar, MV, OP> impl_type;
417  return makeSolverManagerTmpl<base_type, impl_type> (params);
418  }
420  typedef FixedPointSolMgr<Scalar, MV, OP> impl_type;
421  return makeSolverManagerTmpl<base_type, impl_type> (params);
422  }
423  case SOLVER_TYPE_BICGSTAB: {
424  typedef BiCGStabSolMgr<Scalar, MV, OP> impl_type;
425  return makeSolverManagerTmpl<base_type, impl_type> (params);
426  }
427  default: // Fall through; let the code below handle it.
428  TEUCHOS_TEST_FOR_EXCEPTION(
429  true, std::logic_error, "Belos::SolverFactory: Invalid EBelosSolverType "
430  "enum value " << solverType << ". Please report this bug to the Belos "
431  "developers.");
432  }
433 
434  // Compiler guard. This may result in a warning on some compilers
435  // for an unreachable statement, but it will prevent a warning on
436  // other compilers for a "missing return statement at end of
437  // non-void function."
438  return Teuchos::null;
439 }
440 
441 template<class SolverManagerBaseType, class SolverManagerType>
442 Teuchos::RCP<SolverManagerBaseType>
443 makeSolverManagerTmpl (const Teuchos::RCP<Teuchos::ParameterList>& params)
444 {
445  using Teuchos::ParameterList;
446  using Teuchos::parameterList;
447  using Teuchos::RCP;
448 
449  RCP<SolverManagerType> solver = rcp (new SolverManagerType);
450 
451  // Some solvers may not like to get a null ParameterList. If params
452  // is null, replace it with an empty parameter list. The solver
453  // will fill in default parameters for that case. Use the name of
454  // the solver's default parameters to name the new empty list.
455  RCP<ParameterList> pl;
456  if (params.is_null()) {
457  pl = parameterList (solver->getValidParameters ()->name ());
458  } else {
459  pl = params;
460  }
461  TEUCHOS_TEST_FOR_EXCEPTION(
462  pl.is_null(), std::logic_error,
463  "Belos::SolverFactory: ParameterList to pass to solver is null. This "
464  "should never happen. Please report this bug to the Belos developers.");
465  solver->setParameters (pl);
466  return solver;
467 }
468 
469 } // namespace Details
470 
471 
472 template<class Scalar, class MV, class OP>
474 {}
475 
476 template<class Scalar, class MV, class OP>
477 Teuchos::RCP<typename SolverFactory<Scalar, MV, OP>::solver_base_type>
479 create (const std::string& solverName,
480  const Teuchos::RCP<Teuchos::ParameterList>& solverParams)
481 {
482  const char prefix[] = "Belos::SolverFactory: ";
483 
484  // Upper-case version of the input solver name.
485  std::string solverNameUC (solverName);
486  {
487  typedef std::string::value_type char_t;
488  typedef std::ctype<char_t> facet_type;
489  const facet_type& facet = std::use_facet<facet_type> (std::locale ());
490 
491  const std::string::size_type len = solverName.size ();
492  for (std::string::size_type k = 0; k < len; ++k) {
493  solverNameUC[k] = facet.toupper (solverName[k]);
494  }
495  }
496 
497  // Check whether the given name is an alias.
498  std::pair<std::string, bool> aliasResult =
499  Details::getCanonicalNameFromAlias (solverNameUC);
500  const std::string candidateCanonicalName = aliasResult.first;
501  const bool isAnAlias = aliasResult.second;
502 
503  // Get the canonical name.
504  const Details::EBelosSolverType solverEnum =
506  candidateCanonicalName :
507  solverNameUC);
508  const bool validCanonicalName =
509  (solverEnum != Details::SOLVER_TYPE_UPPER_BOUND);
510 
511  // Check whether we found a canonical name. If we didn't and the
512  // input name is a valid alias, that's a bug. Otherwise, the input
513  // name is invalid.
514  TEUCHOS_TEST_FOR_EXCEPTION
515  (! validCanonicalName && isAnAlias, std::logic_error,
516  prefix << "Valid alias \"" << solverName << "\" has candidate canonical "
517  "name \"" << candidateCanonicalName << "\", which is not a canonical "
518  "solver name. Please report this bug to the Belos developers.");
519  TEUCHOS_TEST_FOR_EXCEPTION
520  (! validCanonicalName && ! isAnAlias, std::invalid_argument,
521  prefix << "Invalid solver name \"" << solverName << "\".");
522 
523  // If the input list is null, we create a new list and use that.
524  // This is OK because the effect of a null parameter list input is
525  // to use default parameter values. Thus, we can always replace a
526  // null list with an empty list.
527  Teuchos::RCP<Teuchos::ParameterList> pl =
528  solverParams.is_null() ? Teuchos::parameterList() : solverParams;
529 
530  // Possibly modify the input parameter list as needed.
531  if (isAnAlias) {
532  Details::reviseParameterListForAlias (solverNameUC, *pl);
533  }
534 
535  return Details::makeSolverManagerFromEnum<Scalar, MV, OP> (solverEnum, pl);
536 }
537 
538 
539 template<class Scalar, class MV, class OP>
540 std::string
542 {
543  using Teuchos::TypeNameTraits;
544 
545  std::ostringstream out;
546  out << "\"Belos::SolverFactory\": {";
547  if (this->getObjectLabel () != "") {
548  out << "Label: " << this->getObjectLabel () << ", ";
549  }
550  out << "Scalar: " << TypeNameTraits<Scalar>::name ()
551  << ", MV: " << TypeNameTraits<MV>::name ()
552  << ", OP: " << TypeNameTraits<OP>::name ()
553  << "}";
554  return out.str ();
555 }
556 
557 
558 template<class Scalar, class MV, class OP>
559 void
561 describe (Teuchos::FancyOStream& out,
562  const Teuchos::EVerbosityLevel verbLevel) const
563 {
564  using Teuchos::TypeNameTraits;
565  using std::endl;
566 
567  const Teuchos::EVerbosityLevel vl =
568  (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
569 
570  if (vl == Teuchos::VERB_NONE) {
571  return;
572  }
573 
574  // By convention, describe() always begins with a tab.
575  Teuchos::OSTab tab0 (out);
576  // The description prints in YAML format. The class name needs to
577  // be protected with quotes, so that YAML doesn't get confused
578  // between the colons in the class name and the colon separating
579  // (key,value) pairs.
580  out << "\"Belos::SolverFactory\":" << endl;
581  if (this->getObjectLabel () != "") {
582  out << "Label: " << this->getObjectLabel () << endl;
583  }
584  {
585  out << "Template parameters:" << endl;
586  Teuchos::OSTab tab1 (out);
587  out << "Scalar: " << TypeNameTraits<Scalar>::name () << endl
588  << "MV: " << TypeNameTraits<MV>::name () << endl
589  << "OP: " << TypeNameTraits<OP>::name () << endl;
590  }
591 
592  // At higher verbosity levels, print out the list of supported solvers.
593  if (vl > Teuchos::VERB_LOW) {
594  Teuchos::OSTab tab1 (out);
595  out << "Number of solvers: " << numSupportedSolvers ()
596  << endl;
597  out << "Canonical solver names: ";
598  printStringArray (out, Details::canonicalSolverNames ());
599  out << endl;
600 
601  out << "Aliases to canonical names: ";
602  printStringArray (out, Details::solverNameAliases ());
603  out << endl;
604  }
605 }
606 
607 template<class Scalar, class MV, class OP>
608 int
610 {
612 }
613 
614 template<class Scalar, class MV, class OP>
615 Teuchos::Array<std::string>
617 {
618  typedef std::vector<std::string>::const_iterator iter_type;
619  Teuchos::Array<std::string> names;
620 
621  {
622  std::vector<std::string> aliases = Details::solverNameAliases ();
623  for (iter_type iter = aliases.begin (); iter != aliases.end (); ++iter) {
624  names.push_back (*iter);
625  }
626  }
627  {
628  std::vector<std::string> canonicalNames = Details::canonicalSolverNames ();
629  for (iter_type iter = canonicalNames.begin (); iter != canonicalNames.end (); ++iter) {
630  names.push_back (*iter);
631  }
632  }
633  return names;
634 }
635 
636 } // namespace Belos
637 
638 #endif // __Belos_SolverFactory_hpp
639 
Solver manager for the MINRES linear solver.
std::string description() const
A string description of this object.
The Belos::FixedPointSolMgr provides a powerful and fully-featured solver manager over the FixedPoint...
Class which manages the output and verbosity of the Belos solvers.
The Belos::PseudoBlockCGSolMgr provides a solver manager for the BlockCG linear solver.
Teuchos::RCP< SolverManagerBaseType > makeSolverManagerTmpl(const Teuchos::RCP< Teuchos::ParameterList > &params)
The Belos::PseudoBlockTFQMRSolMgr provides a solver manager for the pseudo-block TFQMR linear solver...
Implementation of the RCG (Recycling Conjugate Gradient) iterative linear solver. ...
EBelosSolverType getEnumFromCanonicalName(const std::string &canonicalName)
Map from canonical solver name to solver enum value.
The Belos::BlockCGSolMgr provides a powerful and fully-featured solver manager over the CG and BlockC...
The Belos::FixedPointSolMgr provides a solver manager for the FixedPoint linear solver.
Declaration and definition of Belos::PCPGSolMgr (PCPG iterative linear solver).
Interface to Block GMRES and Flexible GMRES.
Declaration of Belos::Details::EBelosSolverType enum, and associated functions.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
Teuchos::RCP< SolverManager< Scalar, MV, OP > > makeSolverManagerFromEnum(const EBelosSolverType solverType, const Teuchos::RCP< Teuchos::ParameterList > &params)
Declaration and definition of Belos::GCRODRSolMgr, which implements the GCRODR (recycling GMRES) solv...
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.
std::vector< std::string > solverNameAliases()
List of supported aliases (to canonical solver names).
MINRES linear solver solution manager.
Declaration and definition of Belos::GmresPolySolMgr (hybrid block GMRES linear solver).
The Belos::BiCGStabSolMgr provides a solver manager for the BiCGStab linear solver.
EBelosSolverType
1-to-1 enumeration of all supported SolverManager subclasses.
Implementation of the GCRODR (Recycling GMRES) iterative linear solver.
int numSupportedSolvers()
Number of Belos solvers supported for any linear algebra implementation ("generically").
The Belos::PseudoBlockTFQMRSolMgr provides a powerful and fully-featured solver manager over the pseu...
Interface to standard and "pseudoblock" GMRES.
The Belos::PseudoBlockStochasticCGSolMgr provides a solver manager for the stochastic BlockCG linear ...
Hybrid block GMRES iterative linear solver.
int numSupportedSolvers() const
Number of supported solvers.
Teuchos::Array< std::string > supportedSolverNames() const
List of supported solver names.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
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.
bool isSupported(const std::string &solverName) const
Whether the given solver name names a supported solver.
LSQRSolMgr: interface to the LSQR method.
The Belos::BiCGStabSolMgr provides a powerful and fully-featured solver manager over the pseudo-block...
std::pair< std::string, bool > getCanonicalNameFromAlias(const std::string &candidateAlias)
Get the candidate canonical name for a given candidate alias.
The Belos::PseudoBlockStochasticCGSolMgr provides a powerful and fully-featured solver manager over t...
PCPG iterative linear solver.
SolverManager< Scalar, MV, OP > solver_base_type
The type of the solver returned by create().
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...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object.
The Belos::PseudoBlockGmresSolMgr provides a solver manager for the BlockGmres linear solver...
SolverFactory()
Default constructor.
void reviseParameterListForAlias(const std::string &aliasName, Teuchos::ParameterList &solverParams)
Modify the input ParameterList appropriately for the given solver alias.
Factory for all solvers which Belos supports.
Belos header file which uses auto-configuration information to include necessary C++ headers...
The Belos::TFQMRSolMgr provides a solver manager for the TFQMR linear solver.
std::vector< std::string > canonicalSolverNames()
List of canonical solver names.

Generated on Mon Feb 5 2018 15:01:51 for Belos by doxygen 1.8.13