libMesh
Public Member Functions | Private Attributes | List of all members
libMesh::LibMeshInit Class Reference

The LibMeshInit class, when constructed, initializes the dependent libraries (e.g. More...

#include <libmesh.h>

Public Member Functions

 LibMeshInit (int argc, const char *const *argv, MPI_Comm COMM_WORLD_IN=MPI_COMM_WORLD, int n_threads=-1)
 Initialize the library for use, with the command line options provided. More...
 
 LibMeshInit (int argc, const char *const *argv, int COMM_WORLD_IN=0, int n_threads=-1)
 
virtual ~LibMeshInit ()
 Destructor. More...
 
const Parallel::Communicatorcomm () const
 Returns a Communicator created from the TIMPIInit object we hold, which will be a compatibility shim if MPI is not enabled. More...
 
Parallel::Communicatorcomm ()
 

Private Attributes

TIMPI::TIMPIInit_timpi_init
 
Parallel::Communicator_comm
 
vtkMPIController * _vtk_mpi_controller
 

Detailed Description

The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.

MPI or PETSC) and does the command line parsing needed by libMesh. The LibMeshInit destructor closes those libraries properly.

For most users, a single LibMeshInit object should be created at the start of your main() function.

All libMesh functionality should be used only when a LibMeshInit object exists. Dependent library functionality, likewise, except in codes which manually initialize those libraries before LibMeshInit creation and finalize them after LibMeshInit destruction.

Since "it is best not to perform much more than a return rc after calling MPI_Finalize", applications which want to do anything after LibMeshInit destruction should manage MPI initialization and finalization manually.

Definition at line 90 of file libmesh.h.

Constructor & Destructor Documentation

◆ LibMeshInit() [1/2]

libMesh::LibMeshInit::LibMeshInit ( int  argc,
const char *const *  argv,
MPI_Comm  COMM_WORLD_IN = MPI_COMM_WORLD,
int  n_threads = -1 
)

Initialize the library for use, with the command line options provided.

This will e.g. call MPI_Init if MPI is available and enabled and has not already been initialized; similar initialization may take place for Petsc, Slepc, multithreading support, libMesh Singleton objects, the libMesh::out/err IO streams, and any libMesh handlers for floating-point exceptions, signals, and/or C++ aborts.

You must create a LibMeshInit object before using any of the library functionality. This method may take an optional parameter to use a user-specified MPI communicator.

◆ LibMeshInit() [2/2]

libMesh::LibMeshInit::LibMeshInit ( int  argc,
const char *const *  argv,
int  COMM_WORLD_IN = 0,
int  n_threads = -1 
)

◆ ~LibMeshInit()

libMesh::LibMeshInit::~LibMeshInit ( )
virtual

Destructor.

Cleans up libMesh Singleton objects, and thread manager if threading is in use. Prints reference count and performance logging information if enabled. Restores pre-LibMeshInit terminate handler and floating-point-exception handling. Finalizes any of Slepc, Petsc, and MPI which were initialized by LibMeshInit.

Definition at line 673 of file libmesh.C.

References _comm, libMesh::libMeshPrivateData::_is_initialized, _timpi_init, _vtk_mpi_controller, TIMPI::Communicator::barrier(), libMesh::Singleton::cleanup(), libMesh::PerfLog::clear(), libMesh::closed(), comm(), libMesh::command_line_names(), libMesh::enableFPE(), libMesh::err, libMesh::BasicOStreamProxy< charT, traits >::get(), libMesh::ReferenceCounter::n_objects(), libMesh::Quality::name(), libMesh::old_terminate_handler, libMesh::on_command_line(), libMesh::out, libMesh::perflog, libMesh::ReferenceCounter::print_info(), libMesh::PerfLog::print_log(), libMesh::BasicOStreamProxy< charT, traits >::rdbuf(), and libMesh::BasicOStreamProxy< charT, traits >::reset().

674 {
675  // Every processor had better be ready to exit at the same time.
676  // This would be a libmesh_parallel_only() function, except that
677  // libmesh_parallel_only() uses libmesh_assert() which throws an
678  // exception() which causes compilers to scream about exceptions
679  // inside destructors.
680 
681  // Even if we're not doing parallel_only debugging, we don't want
682  // one processor to try to exit until all others are done working.
683  this->comm().barrier();
684 
685  // We can't delete, finalize, etc. more than once without
686  // reinitializing in between
687  libmesh_exceptionless_assert(!libMesh::closed());
688 
689  // Delete reference counted singleton(s)
691 
692  // Clear the thread task manager we started
693  task_scheduler.reset();
694 
695  // Force the \p ReferenceCounter to print
696  // its reference count information. This allows
697  // us to find memory leaks. By default the
698  // \p ReferenceCounter only prints its information
699  // when the last created object has been destroyed.
700  // That does no good if we are leaking memory!
702 
703 
704  // Print an informative message if we detect a memory leak
705  if (ReferenceCounter::n_objects() != 0)
706  {
707  libMesh::err << "Memory leak detected!"
708  << std::endl;
709 
710 #if !defined(LIBMESH_ENABLE_REFERENCE_COUNTING) || defined(NDEBUG)
711 
712  libMesh::err << "Compile in DEBUG mode with --enable-reference-counting"
713  << std::endl
714  << "for more information"
715  << std::endl;
716 #endif
717 
718  }
719 
720  // print the perflog to individual processor's file.
722 
723  // Now clear the logging object, we don't want it to print
724  // a second time during the PerfLog destructor.
726 
727  // Reconnect the output streams
728  // (don't do this, or we will get messages from objects
729  // that go out of scope after the following return)
730  //std::cout.rdbuf(std::cerr.rdbuf());
731 
732 
733  // Set the initialized() flag to false
735 
736  if (libMesh::on_command_line ("--redirect-stdout") ||
737  libMesh::on_command_line ("--redirect-output"))
738  {
739  // If stdout/stderr were redirected to files, reset them now.
740  libMesh::out.rdbuf (out_buf);
741  libMesh::err.rdbuf (err_buf);
742  }
743 
744  // If we built our own output streams, we want to clean them up.
745  if (libMesh::on_command_line ("--separate-libmeshout"))
746  {
747  delete libMesh::out.get();
748  delete libMesh::err.get();
749 
750  libMesh::out.reset(std::cout);
751  libMesh::err.reset(std::cerr);
752  }
753 
754 #ifdef LIBMESH_ENABLE_EXCEPTIONS
755  // Reset the old terminate handler; maybe the user code wants to
756  // keep doing C++ stuff after closing libMesh stuff.
757  std::set_terminate(old_terminate_handler);
758 #endif
759 
760 
761  if (libMesh::on_command_line("--enable-fpe"))
762  libMesh::enableFPE(false);
763 
764 #if defined(LIBMESH_HAVE_PETSC)
765  // Let PETSc know about all the command line objects that we
766  // consumed without asking them, so we don't get unused option
767  // warnings about those.
768  std::vector<std::string> cli_names = command_line_names();
769  for (const auto & name : cli_names)
770  if (!name.empty() && name[0] == '-')
771  {
772  // Newer PETSc can give me a double-free I'm having trouble
773  // replicating; let's protect against trying to clear
774  // already-used values
775  PetscBool used = PETSC_FALSE;
776  PetscOptionsUsed(NULL, name.c_str(), &used);
777  if (used == PETSC_FALSE)
778  PetscOptionsClearValue(NULL, name.c_str());
779  }
780 
781  // Allow the user to bypass PETSc finalization
782  if (!libMesh::on_command_line ("--disable-petsc")
783 #if defined(LIBMESH_HAVE_MPI)
784  && !libMesh::on_command_line ("--disable-mpi")
785 #endif
786  )
787  {
788 # if defined(LIBMESH_HAVE_SLEPC)
789  if (libmesh_initialized_slepc)
790  SlepcFinalize();
791 # else
792  if (libmesh_initialized_petsc)
793  PetscFinalize();
794 # endif
795  }
796 #endif
797 
798 #if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
799  _vtk_mpi_controller->Finalize(/*finalized_externally=*/1);
800  _vtk_mpi_controller->Delete();
801 #endif
802 
803  delete this->_comm;
804 
805 #if defined(LIBMESH_HAVE_MPI)
806  // Allow the user to bypass MPI finalization
807  if (!libMesh::on_command_line ("--disable-mpi"))
808  {
809  delete this->_timpi_init;
810  }
811 #else
812  delete this->_timpi_init;
813 #endif
814 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
OStreamProxy err
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:273
static unsigned int n_objects()
Prints the number of outstanding (created, but not yet destroyed) objects.
void barrier() const
PerfLog perflog
A PerfLog object to log performance.
static void print_info(std::ostream &out_stream=libMesh::out)
Prints the reference information, by default to libMesh::out.
void reset(streamT &target)
Reset the proxy to point to a different target.
vtkMPIController * _vtk_mpi_controller
Definition: libmesh.h:148
const Parallel::Communicator & comm() const
Returns a Communicator created from the TIMPIInit object we hold, which will be a compatibility shim ...
Definition: libmesh.h:128
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:247
std::terminate_handler old_terminate_handler
Definition: libmesh.C:280
streambufT * rdbuf() const
Get the associated stream buffer.
TIMPI::TIMPIInit * _timpi_init
Definition: libmesh.h:135
void clear()
Clears all the internal data and restores the data structures to a pristine state.
Definition: perf_log.C:78
void enableFPE(bool on)
Toggle hardware trap floating point exceptions.
Definition: libmesh.C:821
OStreamProxy out
std::vector< std::string > command_line_names()
Definition: libmesh.C:916
void print_log() const
Print the log.
Definition: perf_log.C:697
streamT * get()
Rather than implement every ostream/ios/ios_base function, we&#39;ll be lazy and make esoteric uses go th...
static void cleanup()
Cleanup function.
bool on_command_line(std::string arg)
Definition: libmesh.C:924
Parallel::Communicator * _comm
Definition: libmesh.h:142

Member Function Documentation

◆ comm() [1/2]

const Parallel::Communicator& libMesh::LibMeshInit::comm ( ) const
inline

Returns a Communicator created from the TIMPIInit object we hold, which will be a compatibility shim if MPI is not enabled.

Definition at line 128 of file libmesh.h.

References _comm.

Referenced by ~LibMeshInit().

128 { return *_comm; }
Parallel::Communicator * _comm
Definition: libmesh.h:142

◆ comm() [2/2]

Parallel::Communicator& libMesh::LibMeshInit::comm ( )
inline

Definition at line 130 of file libmesh.h.

References _comm.

130 { return *_comm; }
Parallel::Communicator * _comm
Definition: libmesh.h:142

Member Data Documentation

◆ _comm

Parallel::Communicator* libMesh::LibMeshInit::_comm
private

Definition at line 142 of file libmesh.h.

Referenced by comm(), and ~LibMeshInit().

◆ _timpi_init

TIMPI::TIMPIInit* libMesh::LibMeshInit::_timpi_init
private

Definition at line 135 of file libmesh.h.

Referenced by ~LibMeshInit().

◆ _vtk_mpi_controller

vtkMPIController* libMesh::LibMeshInit::_vtk_mpi_controller
private

Definition at line 148 of file libmesh.h.

Referenced by ~LibMeshInit().


The documentation for this class was generated from the following files: