Poster of Linux kernelThe best gift for a Linux geek
PetscPreconditioner

PetscPreconditioner

Section: C Library Functions (3) Updated: Thu Apr 7 2011
Local index Up
 

NAME

PetscPreconditioner -  

SYNOPSIS


#include <petsc_preconditioner.h>

Inherits Preconditioner< T >.  

Public Member Functions


PetscPreconditioner ()

virtual ~PetscPreconditioner ()

virtual void apply (const NumericVector< T > &x, NumericVector< T > &y)

virtual void clear ()

virtual void init ()

PC pc ()

bool initialized () const

void set_matrix (SparseMatrix< Number > &mat)

PreconditionerType type () const

void set_type (const PreconditionerType pct)
 

Static Public Member Functions


static void set_petsc_preconditioner_type (const PreconditionerType &preconditioner_type, PC &pc)

static Preconditioner< T > * build (const SolverPackage solver_package=libMesh::default_solver_package())

static std::string get_info ()

static void print_info ()

static unsigned int n_objects ()
 

Protected Types


typedef std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
 

Protected Member Functions


void increment_constructor_count (const std::string &name)

void increment_destructor_count (const std::string &name)
 

Protected Attributes


PC _pc

Mat _mat

SparseMatrix< T > * _matrix

PreconditionerType _preconditioner_type

bool _is_initialized
 

Static Protected Attributes


static Counts _counts

static Threads::atomic< unsigned int > _n_objects

static Threads::spin_mutex _mutex
 

Detailed Description

 

template<typename T> class PetscPreconditioner< T >

This class provides an interface to the suite of preconditioners available from Petsc.

Author:

Derek Gaston, 2009

Definition at line 56 of file petsc_preconditioner.h.  

Member Typedef Documentation

 

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > ReferenceCounter::Counts [protected, inherited]Data structure to log the information. The log is identified by the class name.

Definition at line 105 of file reference_counter.h.  

Constructor & Destructor Documentation

 

template<typename T > PetscPreconditioner< T >::PetscPreconditioner () [inline]Constructor. Initializes PetscPreconditioner data structures

Definition at line 117 of file petsc_preconditioner.h.

                                             :
  Preconditioner<T>()
{
}
 

template<typename T > PetscPreconditioner< T >::~PetscPreconditioner () [inline, virtual]Destructor.

Definition at line 126 of file petsc_preconditioner.h.

{
  this->clear ();
}
 

Member Function Documentation

 

template<typename T > void PetscPreconditioner< T >::apply (const NumericVector< T > &x, NumericVector< T > &y) [virtual]Computes the preconditioned vector 'y' based on input 'x'. Usually by solving Py=x to get the action of P^-1 x.

Implements Preconditioner< T >.

Definition at line 38 of file petsc_preconditioner.C.

References PetscVector< T >::vec().

{
  PetscVector<T> & x_pvec = libmesh_cast_ref<PetscVector<T>&>(const_cast<NumericVector<T>&>(x));
  PetscVector<T> & y_pvec = libmesh_cast_ref<PetscVector<T>&>(const_cast<NumericVector<T>&>(y));

  Vec x_vec = x_pvec.vec();
  Vec y_vec = y_pvec.vec();

  PCApply(_pc,x_vec,y_vec);
}
 

template<typename T > Preconditioner< T > * Preconditioner< T >::build (const SolverPackagesolver_package = libMesh::default_solver_package()) [static, inherited]Builds a Preconditioner using the linear solver package specified by solver_package

Definition at line 33 of file preconditioner.C.

References libMeshEnums::PETSC_SOLVERS.

{
  // Build the appropriate solver
  switch (solver_package)
    {

/*
#ifdef LIBMESH_HAVE_LASPACK
    case LASPACK_SOLVERS:
      {
        AutoPtr<Preconditioner<T> > ap(new LaspackPreconditioner<T>);
        return ap;
      }
#endif
*/

#ifdef LIBMESH_HAVE_PETSC
    case PETSC_SOLVERS:
      {
        return new PetscPreconditioner<T>();
      }
#endif

/*
#ifdef LIBMESH_HAVE_TRILINOS
    case TRILINOS_SOLVERS:
      {
        AutoPtr<Preconditioner<T> > ap(new AztecPreconditioner<T>);
        return ap;
      }
#endif
*/
    default:
      std::cerr << 'ERROR:  Unrecognized solver package: '
                << solver_package
                << std::endl;
      libmesh_error();
    }
    
  return NULL;    
}
 

template<typename T > virtual void PetscPreconditioner< T >::clear () [inline, virtual]Release all memory and clear data structures.

Reimplemented from Preconditioner< T >.

Definition at line 79 of file petsc_preconditioner.h.

{}
 

std::string ReferenceCounter::get_info () [static, inherited]Gets a string containing the reference information.

Definition at line 45 of file reference_counter.C.

References ReferenceCounter::_counts, and Quality::name().

Referenced by ReferenceCounter::print_info().

{
#if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)

  std::ostringstream out;
  
  out << '
      << ' ---------------------------------------------------------------------------- 
      << '| Reference count information                                                |
      << ' ---------------------------------------------------------------------------- ;
  
  for (Counts::iterator it = _counts.begin();
       it != _counts.end(); ++it)
    {
      const std::string name(it->first);
      const unsigned int creations    = it->second.first;
      const unsigned int destructions = it->second.second;

      out << '| ' << name << ' reference count information:
          << '|  Creations:    ' << creations    << '
          << '|  Destructions: ' << destructions << ';
    }
  
  out << ' ---------------------------------------------------------------------------- ;

  return out.str();

#else

  return '';
  
#endif
}
 

void ReferenceCounter::increment_constructor_count (const std::string &name) [inline, protected, inherited]Increments the construction counter. Should be called in the constructor of any derived class that will be reference counted.

Definition at line 149 of file reference_counter.h.

References ReferenceCounter::_counts, Quality::name(), and Threads::spin_mtx.

Referenced by ReferenceCountedObject< Value >::ReferenceCountedObject().

{
  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
  std::pair<unsigned int, unsigned int>& p = _counts[name];

  p.first++;
}
 

void ReferenceCounter::increment_destructor_count (const std::string &name) [inline, protected, inherited]Increments the destruction counter. Should be called in the destructor of any derived class that will be reference counted.

Definition at line 167 of file reference_counter.h.

References ReferenceCounter::_counts, Quality::name(), and Threads::spin_mtx.

Referenced by ReferenceCountedObject< Value >::~ReferenceCountedObject().

{
  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
  std::pair<unsigned int, unsigned int>& p = _counts[name];

  p.second++;
}
 

template<typename T > void PetscPreconditioner< T >::init () [virtual]Initialize data structures if not done so already.

Reimplemented from Preconditioner< T >.

Definition at line 51 of file petsc_preconditioner.C.

References libMesh::libMeshPrivateData::_is_initialized, libMeshEnums::AMG_PRECOND, libMesh::COMM_WORLD, and PetscMatrix< T >::mat().

{
  if(!this->_matrix)
  {
    std::cerr << 'ERROR: No matrix set for PetscPreconditioner, but init() called' << std::endl;
    libmesh_error();
  }

  //Clear the preconditioner in case it has been created in the past
  if(!this->_is_initialized)
  {
    //Create the preconditioning object
    PCCreate(libMesh::COMM_WORLD,&_pc);

    //Set the PCType
    set_petsc_preconditioner_type(this->_preconditioner_type, _pc);

#ifdef LIBMESH_HAVE_PETSC_HYPRE
    if(this->_preconditioner_type == AMG_PRECOND)
      PCHYPRESetType(this->_pc, 'boomeramg');
#endif

    PetscMatrix<T> * pmatrix = libmesh_cast_ptr<PetscMatrix<T>*, SparseMatrix<T> >(this->_matrix);
    
    _mat = pmatrix->mat();
  }
  
  PCSetOperators(_pc,_mat,_mat,SAME_NONZERO_PATTERN);

  this->_is_initialized = true;
}
 

template<typename T> bool Preconditioner< T >::initialized () const [inline, inherited]Returns:

true if the data structures are initialized, false otherwise.

Definition at line 84 of file preconditioner.h.

{ return _is_initialized; }
 

static unsigned int ReferenceCounter::n_objects () [inline, static, inherited]Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 76 of file reference_counter.h.

References ReferenceCounter::_n_objects.

Referenced by System::read_serialized_blocked_dof_objects(), and System::write_serialized_blocked_dof_objects().

  { return _n_objects; }
 

template<typename T > PC PetscPreconditioner< T >::pc () [inline]Returns the actual Petsc PC struct. Useful for more advanced purposes

Definition at line 90 of file petsc_preconditioner.h.

References PetscPreconditioner< T >::_pc.

{ return _pc; }
 

void ReferenceCounter::print_info () [static, inherited]Prints the reference information to std::cout.

Definition at line 83 of file reference_counter.C.

References ReferenceCounter::get_info().

{
#if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
  
  std::cout << ReferenceCounter::get_info();
  
#endif
}
 

template<typename T > void Preconditioner< T >::set_matrix (SparseMatrix< Number > &mat) [inherited]Sets the matrix P to be preconditioned.

Definition at line 161 of file preconditioner.h.

References libMesh::libMeshPrivateData::_is_initialized.

{
  //If the matrix is changing then we (probably) need to reinitialize.
  _is_initialized = false;
  _matrix = &mat;
}
 

template<typename T > void PetscPreconditioner< T >::set_petsc_preconditioner_type (const PreconditionerType &preconditioner_type, PC &pc) [static]Tells PETSC to use the user-specified preconditioner

Definition at line 85 of file petsc_preconditioner.C.

References libMeshEnums::AMG_PRECOND, libMeshEnums::ASM_PRECOND, libMeshEnums::BLOCK_JACOBI_PRECOND, libMeshEnums::CHOLESKY_PRECOND, libMesh::COMM_WORLD, libMeshEnums::EISENSTAT_PRECOND, libMeshEnums::ICC_PRECOND, libMeshEnums::IDENTITY_PRECOND, libMeshEnums::ILU_PRECOND, libMeshEnums::JACOBI_PRECOND, libMeshEnums::LU_PRECOND, libMeshEnums::SHELL_PRECOND, libMeshEnums::SOR_PRECOND, and libMeshEnums::USER_PRECOND.

Referenced by PetscLinearSolver< T >::init().

{
  int ierr = 0;
 
  switch (preconditioner_type)
  {
  case IDENTITY_PRECOND:
    ierr = PCSetType (pc, (char*) PCNONE);      CHKERRABORT(libMesh::COMM_WORLD,ierr); break;
        
  case CHOLESKY_PRECOND:
    ierr = PCSetType (pc, (char*) PCCHOLESKY);  CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case ICC_PRECOND:
    ierr = PCSetType (pc, (char*) PCICC);       CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case ILU_PRECOND:
    ierr = PCSetType (pc, (char*) PCILU);       CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case LU_PRECOND:
    ierr = PCSetType (pc, (char*) PCLU);        CHKERRABORT(libMesh::COMM_WORLD,ierr); break;
      
  case ASM_PRECOND:
    ierr = PCSetType (pc, (char*) PCASM);       CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case JACOBI_PRECOND:
    ierr = PCSetType (pc, (char*) PCJACOBI);    CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case BLOCK_JACOBI_PRECOND:
    ierr = PCSetType (pc, (char*) PCBJACOBI);   CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case SOR_PRECOND:
    ierr = PCSetType (pc, (char*) PCSOR);       CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case EISENSTAT_PRECOND:
    ierr = PCSetType (pc, (char*) PCEISENSTAT); CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  case AMG_PRECOND:
    ierr = PCSetType (pc, (char*) PCHYPRE);     CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

#if !(PETSC_VERSION_LESS_THAN(2,1,2))
    // Only available for PETSC >= 2.1.2      
  case USER_PRECOND:
    ierr = PCSetType (pc, (char*) PCMAT);       CHKERRABORT(libMesh::COMM_WORLD,ierr); break;
#endif

  case SHELL_PRECOND:
    ierr = PCSetType (pc, (char*) PCSHELL);     CHKERRABORT(libMesh::COMM_WORLD,ierr); break;

  default:
    std::cerr << 'ERROR:  Unsupported PETSC Preconditioner: '
              << preconditioner_type       << std::endl
              << 'Continuing with PETSC defaults' << std::endl;
  }

  //Let the commandline override stuff
  if( preconditioner_type != AMG_PRECOND )
    PCSetFromOptions(pc);
}
 

template<typename T > void Preconditioner< T >::set_type (const PreconditionerTypepct) [inherited]Sets the type of preconditioner to use.

Definition at line 170 of file preconditioner.h.

References libMesh::libMeshPrivateData::_is_initialized.

{
  //If the preconditioner type changes we (probably) need to reinitialize.
  _is_initialized = false;
  _preconditioner_type = pct;
}
 

template<typename T> PreconditionerType Preconditioner< T >::type () const [inline, inherited]Returns the type of preconditioner to use.

Definition at line 110 of file preconditioner.h.

  { return _preconditioner_type; }
 

Member Data Documentation

 

ReferenceCounter::Counts ReferenceCounter::_counts [static, protected, inherited]Actually holds the data.

Definition at line 110 of file reference_counter.h.

Referenced by ReferenceCounter::get_info(), ReferenceCounter::increment_constructor_count(), and ReferenceCounter::increment_destructor_count().  

template<typename T> bool Preconditioner< T >::_is_initialized [protected, inherited]Flag indicating if the data structures have been initialized.

Definition at line 134 of file preconditioner.h.

Referenced by Preconditioner< Number >::initialized().  

template<typename T > Mat PetscPreconditioner< T >::_mat [protected]Petsc Matrix that's been pulled out of the _matrix object. This happens during init...

Definition at line 108 of file petsc_preconditioner.h.  

template<typename T> SparseMatrix<T>* Preconditioner< T >::_matrix [protected, inherited]The matrix P... ie the matrix to be preconditioned. This is often the actual system matrix of a linear sytem.

Definition at line 124 of file preconditioner.h.  

Threads::spin_mutex ReferenceCounter::_mutex [static, protected, inherited]Mutual exclusion object to enable thread-safe reference counting.

Definition at line 123 of file reference_counter.h.  

Threads::atomic< unsigned int > ReferenceCounter::_n_objects [static, protected, inherited]The number of objects. Print the reference count information when the number returns to 0.

Definition at line 118 of file reference_counter.h.

Referenced by ReferenceCounter::n_objects(), ReferenceCounter::ReferenceCounter(), and ReferenceCounter::~ReferenceCounter().  

template<typename T > PC PetscPreconditioner< T >::_pc [protected]Preconditioner context

Definition at line 102 of file petsc_preconditioner.h.

Referenced by PetscPreconditioner< T >::pc().  

template<typename T> PreconditionerType Preconditioner< T >::_preconditioner_type [protected, inherited]Enum statitng with type of preconditioner to use.

Definition at line 129 of file preconditioner.h.

Referenced by Preconditioner< Number >::type().

 

Author

Generated automatically by Doxygen for libMesh from the source code.


 

Index

NAME
SYNOPSIS
Public Member Functions
Static Public Member Functions
Protected Types
Protected Member Functions
Protected Attributes
Static Protected Attributes
Detailed Description
template<typename T> class PetscPreconditioner< T >
Member Typedef Documentation
typedef std::map<std::string, std::pair<unsigned int, unsigned int> > ReferenceCounter::Counts [protected, inherited]Data structure to log the information. The log is identified by the class name.
Constructor & Destructor Documentation
template<typename T > PetscPreconditioner< T >::PetscPreconditioner () [inline]Constructor. Initializes PetscPreconditioner data structures
template<typename T > PetscPreconditioner< T >::~PetscPreconditioner () [inline, virtual]Destructor.
Member Function Documentation
template<typename T > void PetscPreconditioner< T >::apply (const NumericVector< T > &x, NumericVector< T > &y) [virtual]Computes the preconditioned vector 'y' based on input 'x'. Usually by solving Py=x to get the action of P^-1 x.
template<typename T > Preconditioner< T > * Preconditioner< T >::build (const SolverPackagesolver_package = libMesh::default_solver_package()) [static, inherited]Builds a Preconditioner using the linear solver package specified by solver_package
template<typename T > virtual void PetscPreconditioner< T >::clear () [inline, virtual]Release all memory and clear data structures.
std::string ReferenceCounter::get_info () [static, inherited]Gets a string containing the reference information.
void ReferenceCounter::increment_constructor_count (const std::string &name) [inline, protected, inherited]Increments the construction counter. Should be called in the constructor of any derived class that will be reference counted.
void ReferenceCounter::increment_destructor_count (const std::string &name) [inline, protected, inherited]Increments the destruction counter. Should be called in the destructor of any derived class that will be reference counted.
template<typename T > void PetscPreconditioner< T >::init () [virtual]Initialize data structures if not done so already.
template<typename T> bool Preconditioner< T >::initialized () const [inline, inherited]Returns:
static unsigned int ReferenceCounter::n_objects () [inline, static, inherited]Prints the number of outstanding (created, but not yet destroyed) objects.
template<typename T > PC PetscPreconditioner< T >::pc () [inline]Returns the actual Petsc PC struct. Useful for more advanced purposes
void ReferenceCounter::print_info () [static, inherited]Prints the reference information to std::cout.
template<typename T > void Preconditioner< T >::set_matrix (SparseMatrix< Number > &mat) [inherited]Sets the matrix P to be preconditioned.
template<typename T > void PetscPreconditioner< T >::set_petsc_preconditioner_type (const PreconditionerType &preconditioner_type, PC &pc) [static]Tells PETSC to use the user-specified preconditioner
template<typename T > void Preconditioner< T >::set_type (const PreconditionerTypepct) [inherited]Sets the type of preconditioner to use.
template<typename T> PreconditionerType Preconditioner< T >::type () const [inline, inherited]Returns the type of preconditioner to use.
Member Data Documentation
ReferenceCounter::Counts ReferenceCounter::_counts [static, protected, inherited]Actually holds the data.
template<typename T> bool Preconditioner< T >::_is_initialized [protected, inherited]Flag indicating if the data structures have been initialized.
template<typename T > Mat PetscPreconditioner< T >::_mat [protected]Petsc Matrix that's been pulled out of the _matrix object. This happens during init...
template<typename T> SparseMatrix<T>* Preconditioner< T >::_matrix [protected, inherited]The matrix P... ie the matrix to be preconditioned. This is often the actual system matrix of a linear sytem.
Threads::spin_mutex ReferenceCounter::_mutex [static, protected, inherited]Mutual exclusion object to enable thread-safe reference counting.
Threads::atomic< unsigned int > ReferenceCounter::_n_objects [static, protected, inherited]The number of objects. Print the reference count information when the number returns to 0.
template<typename T > PC PetscPreconditioner< T >::_pc [protected]Preconditioner context
template<typename T> PreconditionerType Preconditioner< T >::_preconditioner_type [protected, inherited]Enum statitng with type of preconditioner to use.
Author

This document was created by man2html, using the manual pages.
Time: 21:52:24 GMT, April 16, 2011