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 794 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(), libMesh::BasicOStreamProxy< charT, traits >::reset(), and libMesh::uninstall_thread_buffered_sync().

795 {
796  // Every processor had better be ready to exit at the same time.
797  // This would be a libmesh_parallel_only() function, except that
798  // libmesh_parallel_only() uses libmesh_assert() which throws an
799  // exception() which causes compilers to scream about exceptions
800  // inside destructors.
801 
802  // Even if we're not doing parallel_only debugging, we don't want
803  // one processor to try to exit until all others are done working.
804  this->comm().barrier();
805 
806  // We can't delete, finalize, etc. more than once without
807  // reinitializing in between
808  libmesh_exceptionless_assert(!libMesh::closed());
809 
810  // Delete reference counted singleton(s)
812 
813  // Clear the thread task manager we started
814  task_scheduler.reset();
815 
816  // Force the \p ReferenceCounter to print
817  // its reference count information. This allows
818  // us to find memory leaks. By default the
819  // \p ReferenceCounter only prints its information
820  // when the last created object has been destroyed.
821  // That does no good if we are leaking memory!
823 
824 
825  // Print an informative message if we detect a memory leak
826  if (ReferenceCounter::n_objects() != 0)
827  {
828  libMesh::err << "Memory leak detected!"
829  << std::endl;
830 
831 #if !defined(LIBMESH_ENABLE_REFERENCE_COUNTING) || defined(NDEBUG)
832 
833  libMesh::err << "Compile in DEBUG mode with --enable-reference-counting"
834  << std::endl
835  << "for more information"
836  << std::endl;
837 #endif
838 
839  }
840 
841  // print the perflog to individual processor's file.
843 
844  // Now clear the logging object, we don't want it to print
845  // a second time during the PerfLog destructor.
847 
848  // Reconnect the output streams
849  // (don't do this, or we will get messages from objects
850  // that go out of scope after the following return)
851  //std::cout.rdbuf(std::cerr.rdbuf());
852 
853 
854  // Set the initialized() flag to false
856 
857  // Before resetting the stream buffers, let's remove our thread wrappers
858  if (!libMesh::on_command_line ("--disable-thread-safe-output"))
860 
861  if (libMesh::on_command_line ("--redirect-stdout") ||
862  libMesh::on_command_line ("--redirect-output"))
863  {
864  // If stdout/stderr were redirected to files, reset them now.
865  libMesh::out.rdbuf (out_buf);
866  libMesh::err.rdbuf (err_buf);
867  }
868 
869  // If we built our own output streams, we want to clean them up.
870  if (libMesh::on_command_line ("--separate-libmeshout"))
871  {
872  delete libMesh::out.get();
873  delete libMesh::err.get();
874 
875  libMesh::out.reset(std::cout);
876  libMesh::err.reset(std::cerr);
877  }
878 
879 #ifdef LIBMESH_ENABLE_EXCEPTIONS
880  // Reset the old terminate handler; maybe the user code wants to
881  // keep doing C++ stuff after closing libMesh stuff.
882  std::set_terminate(old_terminate_handler);
883 #endif
884 
885 #ifdef LIBMESH_HAVE_NETGEN
886  nglib::Ng_Exit();
887 #endif
888 
889  if (libMesh::on_command_line("--enable-fpe"))
890  libMesh::enableFPE(false);
891 
892 #if defined(LIBMESH_HAVE_PETSC)
893  // Let PETSc know about all the command line objects that we
894  // consumed without asking them, so we don't get unused option
895  // warnings about those.
896  std::vector<std::string> cli_names = command_line_names();
897  for (const auto & name : cli_names)
898  if (!name.empty() && name[0] == '-')
899  {
900  // Newer PETSc can give me a double-free I'm having trouble
901  // replicating; let's protect against trying to clear
902  // already-used values
903  PetscBool used = PETSC_FALSE;
904  auto ierr = PetscOptionsUsed(NULL, name.c_str(), &used);
905  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
906  if (used == PETSC_FALSE)
907  {
908  ierr = PetscOptionsClearValue(NULL, name.c_str());
909  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
910  }
911  }
912 
913  // Allow the user to bypass PETSc finalization
914  if (!libMesh::on_command_line ("--disable-petsc")
915 #if defined(LIBMESH_HAVE_MPI)
916  && !libMesh::on_command_line ("--disable-mpi")
917 #endif
918  )
919  {
920  PetscErrorCode ierr = LIBMESH_PETSC_SUCCESS;
921 # if defined(LIBMESH_HAVE_SLEPC)
922  if (libmesh_initialized_slepc)
923  ierr = SlepcFinalize();
924 # else
925  if (libmesh_initialized_petsc)
926  ierr = PetscFinalize();
927 # endif
928  CHKERRABORT(libMesh::GLOBAL_COMM_WORLD, ierr);
929  }
930 #endif
931 
932 #if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
933  _vtk_mpi_controller->Finalize(/*finalized_externally=*/1);
934  _vtk_mpi_controller->Delete();
935 #endif
936 
937  delete this->_comm;
938 
939 #if defined(LIBMESH_HAVE_MPI)
940  // Allow the user to bypass MPI finalization
941  if (!libMesh::on_command_line ("--disable-mpi"))
942  {
943  delete this->_timpi_init;
944  }
945 #else
946  delete this->_timpi_init;
947 #endif
948 }
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:341
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:315
std::terminate_handler old_terminate_handler
Definition: libmesh.C:348
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:955
OStreamProxy out
std::vector< std::string > command_line_names()
Definition: libmesh.C:1050
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:1058
void uninstall_thread_buffered_sync()
Definition: libmesh.C:262
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: