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 727 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::GLOBAL_COMM_WORLD, 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().

728 {
729  // Every processor had better be ready to exit at the same time.
730  // This would be a libmesh_parallel_only() function, except that
731  // libmesh_parallel_only() uses libmesh_assert() which throws an
732  // exception() which causes compilers to scream about exceptions
733  // inside destructors.
734 
735  // Even if we're not doing parallel_only debugging, we don't want
736  // one processor to try to exit until all others are done working.
737  this->comm().barrier();
738 
739  // We can't delete, finalize, etc. more than once without
740  // reinitializing in between
741  libmesh_exceptionless_assert(!libMesh::closed());
742 
743  // Delete reference counted singleton(s)
745 
746  // Clear the thread task manager we started
747  task_scheduler.reset();
748 
749  // Force the \p ReferenceCounter to print
750  // its reference count information. This allows
751  // us to find memory leaks. By default the
752  // \p ReferenceCounter only prints its information
753  // when the last created object has been destroyed.
754  // That does no good if we are leaking memory!
756 
757 
758  // Print an informative message if we detect a memory leak
759  if (ReferenceCounter::n_objects() != 0)
760  {
761  libMesh::err << "Memory leak detected!"
762  << std::endl;
763 
764 #if !defined(LIBMESH_ENABLE_REFERENCE_COUNTING) || defined(NDEBUG)
765 
766  libMesh::err << "Compile in DEBUG mode with --enable-reference-counting"
767  << std::endl
768  << "for more information"
769  << std::endl;
770 #endif
771 
772  }
773 
774  // print the perflog to individual processor's file.
776 
777  // Now clear the logging object, we don't want it to print
778  // a second time during the PerfLog destructor.
780 
781  // Reconnect the output streams
782  // (don't do this, or we will get messages from objects
783  // that go out of scope after the following return)
784  //std::cout.rdbuf(std::cerr.rdbuf());
785 
786 
787  // Set the initialized() flag to false
789 
790  if (libMesh::on_command_line ("--redirect-stdout") ||
791  libMesh::on_command_line ("--redirect-output"))
792  {
793  // If stdout/stderr were redirected to files, reset them now.
794  libMesh::out.rdbuf (out_buf);
795  libMesh::err.rdbuf (err_buf);
796  }
797 
798  // If we built our own output streams, we want to clean them up.
799  if (libMesh::on_command_line ("--separate-libmeshout"))
800  {
801  delete libMesh::out.get();
802  delete libMesh::err.get();
803 
804  libMesh::out.reset(std::cout);
805  libMesh::err.reset(std::cerr);
806  }
807 
808 #ifdef LIBMESH_ENABLE_EXCEPTIONS
809  // Reset the old terminate handler; maybe the user code wants to
810  // keep doing C++ stuff after closing libMesh stuff.
811  std::set_terminate(old_terminate_handler);
812 #endif
813 
814 #ifdef LIBMESH_HAVE_NETGEN
815  nglib::Ng_Exit();
816 #endif
817 
818  if (libMesh::on_command_line("--enable-fpe"))
819  libMesh::enableFPE(false);
820 
821 #if defined(LIBMESH_HAVE_PETSC)
822  // Let PETSc know about all the command line objects that we
823  // consumed without asking them, so we don't get unused option
824  // warnings about those.
825  std::vector<std::string> cli_names = command_line_names();
826  for (const auto & name : cli_names)
827  if (!name.empty() && name[0] == '-')
828  {
829  // Newer PETSc can give me a double-free I'm having trouble
830  // replicating; let's protect against trying to clear
831  // already-used values
832  PetscBool used = PETSC_FALSE;
833  auto ierr = PetscOptionsUsed(NULL, name.c_str(), &used);
834  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
835  if (used == PETSC_FALSE)
836  {
837  ierr = PetscOptionsClearValue(NULL, name.c_str());
838  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
839  }
840  }
841 
842  // Allow the user to bypass PETSc finalization
843  if (!libMesh::on_command_line ("--disable-petsc")
844 #if defined(LIBMESH_HAVE_MPI)
845  && !libMesh::on_command_line ("--disable-mpi")
846 #endif
847  )
848  {
849  PetscErrorCode ierr = LIBMESH_PETSC_SUCCESS;
850 # if defined(LIBMESH_HAVE_SLEPC)
851  if (libmesh_initialized_slepc)
852  ierr = SlepcFinalize();
853 # else
854  if (libmesh_initialized_petsc)
855  ierr = PetscFinalize();
856 # endif
857  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
858  }
859 #endif
860 
861 #if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
862  _vtk_mpi_controller->Finalize(/*finalized_externally=*/1);
863  _vtk_mpi_controller->Delete();
864 #endif
865 
866  delete this->_comm;
867 
868 #if defined(LIBMESH_HAVE_MPI)
869  // Allow the user to bypass MPI finalization
870  if (!libMesh::on_command_line ("--disable-mpi"))
871  {
872  delete this->_timpi_init;
873  }
874 #else
875  delete this->_timpi_init;
876 #endif
877 }
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:283
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.
MPI_Comm GLOBAL_COMM_WORLD
MPI Communicator used to initialize libMesh.
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:257
std::terminate_handler old_terminate_handler
Definition: libmesh.C:290
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:884
OStreamProxy out
std::vector< std::string > command_line_names()
Definition: libmesh.C:979
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:987
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: