20#include "libmesh/libmesh.h"
23#include "libmesh/getpot.h"
24#include "libmesh/reference_counter.h"
25#include "libmesh/libmesh_singleton.h"
26#include "libmesh/remote_elem.h"
27#include "libmesh/threads.h"
28#include "libmesh/parallel_only.h"
29#include "libmesh/print_trace.h"
30#include "libmesh/enum_solver_package.h"
31#include "libmesh/perf_log.h"
32#include "libmesh/thread_buffered_syncbuf.h"
43#ifdef LIBMESH_ENABLE_EXCEPTIONS
48#ifdef LIBMESH_HAVE_OPENMP
55#if defined(LIBMESH_HAVE_MPI)
56# include "libmesh/ignore_warnings.h"
58# include "libmesh/restore_warnings.h"
61#if defined(LIBMESH_HAVE_PETSC)
62# include "libmesh/petsc_solver_exception.h"
64# include <petscerror.h>
65# include "libmesh/petscdmlibmesh.h"
66# if defined(LIBMESH_HAVE_SLEPC)
68# include "libmesh/ignore_warnings.h"
69# include "libmesh/slepc_macro.h"
71# include "libmesh/restore_warnings.h"
75#ifdef LIBMESH_HAVE_NETGEN
82#include "netgen/nglib/nglib.h"
88#if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
89#include "libmesh/ignore_warnings.h"
90# include "vtkMPIController.h"
91#include "libmesh/restore_warnings.h"
94#ifdef PETSC_HAVE_UMPIRE
95#include "umpire/util/io.hpp"
104std::unique_ptr<GetPot> command_line;
106std::set<std::string> command_line_name_set;
108std::unique_ptr<std::ofstream> _ofstream;
112std::streambuf * out_buf (
nullptr);
113std::streambuf * err_buf (
nullptr);
115std::unique_ptr<libMesh::Threads::task_scheduler_init> task_scheduler;
116#if defined(LIBMESH_HAVE_PETSC)
117bool libmesh_initialized_petsc =
false;
119#if defined(LIBMESH_HAVE_SLEPC)
120bool libmesh_initialized_slepc =
false;
127#ifdef LIBMESH_HAVE_MPI
130 libmesh_not_implemented();
145namespace libMeshPrivateData {
161#ifdef LIBMESH_HAVE_MPI
167#ifdef LIBMESH_ENABLE_EXCEPTIONS
183 auto check_stored_buffer = [](
const auto *
const libmesh_dbg_var(stored_buffer)) {
184 libmesh_assert_msg(!stored_buffer,
185 "Oops, we've already stored a prewrapped buffer. We must be calling this "
186 "function a second time in which case we're going to lose the already "
187 "stored prewrapped buffer forever");
196 std::make_unique<ThreadBufferedSyncbuf>(*ob,
true);
206 std::make_unique<ThreadBufferedSyncbuf>(*eb,
true);
226#ifdef PETSC_HAVE_UMPIRE
233 umpire::util::finalize_io();
273#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
281#ifdef LIBMESH_USE_COMPLEX_NUMBERS
297#ifdef LIBMESH_HAVE_MPI
307#if defined(LIBMESH_HAVE_PETSC)
309#elif defined(LIBMESH_TRILINOS_HAVE_AZTECOO)
311#elif defined(LIBMESH_HAVE_EIGEN)
313#elif defined(LIBMESH_HAVE_LASPACK)
350#if defined(LIBMESH_HAVE_MPI)
352 MPI_Initialized (&mpi_initialized);
358#ifdef LIBMESH_ENABLE_EXCEPTIONS
377 command_line = std::make_unique<GetPot>(argc, argv);
385 auto check_empty_command_line_value = [](
auto & cl,
const std::string & option) {
386 libmesh_error_msg_if(cl.search(option),
387 "Detected option " << option <<
" with no value. Did you forget '='?");
396 std::vector<std::string> n_threads_opt(2);
397 n_threads_opt[0] =
"--n_threads";
398 n_threads_opt[1] =
"--n-threads";
404 for (
auto & option : n_threads_opt)
405 check_empty_command_line_value(*command_line, option);
411#if !LIBMESH_USING_THREADS
415 libmesh_warning(
"Warning: You requested --n-threads>1 but no threading model is active!\n"
416 <<
"Forcing --n-threads==1 instead!");
421#ifdef LIBMESH_HAVE_OPENMP
425 task_scheduler = std::make_unique<Threads::task_scheduler_init>(
libMesh::n_threads());
442 const bool handle_mpi_errors =
false;
444#if defined(LIBMESH_HAVE_MPI)
449 int mpi_thread_request = using_threads;
451 if (mpi_thread_type.empty())
453 check_empty_command_line_value(*command_line,
"--mpi-thread-type");
454#if defined(PETSC_HAVE_STRUMPACK) && defined(PETSC_HAVE_SLATE)
461 mpi_thread_request = 3;
466 if (mpi_thread_type ==
"single")
469 libmesh_error_msg(
"We are using threads, so we require more mpi thread support "
470 "than '--mpi-thread-type=single'");
471 mpi_thread_request = 0;
473 else if (mpi_thread_type ==
"funneled")
474 mpi_thread_request = 1;
475 else if (mpi_thread_type ==
"serialized")
476 mpi_thread_request = 2;
477 else if (mpi_thread_type ==
"multiple")
478 mpi_thread_request = 3;
481 "Unsupported mpi thread type '"
483 <<
"'. Allowed options are 'single', 'funneled', 'serialized', and 'multiple'");
488 handle_mpi_errors, COMM_WORLD_IN);
491 const std::string timpi_sync =
501 cast_int<processor_id_type>(this->
comm().rank());
503 cast_int<processor_id_type>(this->
comm().size());
525 libmesh_parallel_only(this->
comm());
535#if defined(LIBMESH_HAVE_PETSC)
540#
if defined(LIBMESH_HAVE_MPI)
548#ifdef LIBMESH_HAVE_MPI
552 this->
_comm->
get() = PETSC_COMM_SELF;
557 PetscBool petsc_already_initialized;
559 if (petsc_already_initialized != PETSC_TRUE)
560 libmesh_initialized_petsc =
true;
561# if defined(LIBMESH_HAVE_SLEPC)
568 if (!SlepcInitializeCalled)
571 libmesh_initialized_slepc =
true;
574 if (libmesh_initialized_petsc)
582#ifdef LIBMESH_HAVE_NETGEN
586#if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
602 command_line = std::make_unique<GetPot>(argc, argv);
615 std::ios::sync_with_stdio(
false);
626 std::ostream * newout =
new std::ostream(std::cout.rdbuf());
628 std::ostream * newerr =
new std::ostream(std::cerr.rdbuf());
639 if (cmdline_has_redirect_stdout)
640 libmesh_warning(
"The --redirect-stdout command line option has been deprecated. "
641 "Use '--redirect-output basename' instead.");
647 if (cmdline_has_redirect_stdout || cmdline_has_redirect_output)
649 std::string basename =
"stdout";
652 if (cmdline_has_redirect_output)
655 command_line->search(1,
"--redirect-output");
658 std::string next_string =
"";
659 next_string = command_line->next(next_string);
664 if (next_string.size() > 0 && next_string.find_first_of(
"-") != 0)
665 basename = next_string;
668 std::ostringstream filename;
670 _ofstream = std::make_unique<std::ofstream>(filename.str().c_str());
701#ifdef LIBMESH_ENABLE_EXCEPTIONS
717#if defined(LIBMESH_HAVE_HDF5) && !defined(_MSC_VER)
730 setenv(
"HDF5_USE_FILE_LOCKING",
"FALSE", 0);
760#ifdef LIBMESH_ENABLE_EXCEPTIONS
761 if (!std::uncaught_exceptions())
773 task_scheduler.reset();
797#if !defined(LIBMESH_ENABLE_REFERENCE_COUNTING) || defined(NDEBUG)
799 libMesh::err <<
"Compile in DEBUG mode with --enable-reference-counting"
801 <<
"for more information"
824#ifdef LIBMESH_ENABLE_EXCEPTIONS
830#ifdef LIBMESH_HAVE_NETGEN
837#if defined(LIBMESH_HAVE_PETSC)
842 for (
const auto & name : cli_names)
843 if (!name.empty() && name[0] ==
'-')
848 PetscBool used = PETSC_FALSE;
849 auto ierr = PetscOptionsUsed(NULL, name.c_str(), &used);
851 if (used == PETSC_FALSE)
853 ierr = PetscOptionsClearValue(NULL, name.c_str());
860#
if defined(LIBMESH_HAVE_MPI)
865 PetscErrorCode ierr = LIBMESH_PETSC_SUCCESS;
866# if defined(LIBMESH_HAVE_SLEPC)
867 if (libmesh_initialized_slepc)
868 ierr = SlepcFinalize();
870 if (libmesh_initialized_petsc)
871 ierr = PetscFinalize();
877#if defined(LIBMESH_HAVE_MPI) && defined(LIBMESH_HAVE_VTK)
884#if defined(LIBMESH_HAVE_MPI)
908 static std::mutex command_line_names_mutex;
909 std::scoped_lock lock(command_line_names_mutex);
911 command_line_name_set.insert(name);
918 for (
auto & getter : {&GetPot::get_requested_arguments,
919 &GetPot::get_requested_variables,
920 &GetPot::get_requested_sections})
921 for (
const std::string & name : (getpot.*getter)())
928 return std::vector<std::string>(command_line_name_set.begin(),
929 command_line_name_set.end());
940 if (!command_line.get())
946 bool found_it = command_line->search(arg);
951 std::replace(arg.begin(), arg.end(),
'_',
'-');
952 found_it = command_line->search(arg);
958 auto name_begin = arg.begin();
959 while (*name_begin ==
'-')
961 std::replace(name_begin, arg.end(),
'-',
'_');
962 found_it = command_line->search(arg);
977 if (command_line->have_variable(name))
985 const std::string stringvalue =
986 (*command_line)(name, std::string());
1000 for (
const auto & entry : names)
1006 const std::string stringvalue =
1007 (*command_line)(entry, std::string());
1012 for (
const auto & entry : names)
1013 if (command_line->have_variable(entry))
1024template <
typename T>
1036 return command_line->next(
value);
1043template <
typename T>
1053 if (command_line->have_variable(name))
1055 unsigned size = command_line->vector_variable_size(name);
1058 for (
unsigned i=0; i<size; ++i)
1059 vec[i] = (*command_line)(name, vec[i], i);
1068 static bool called =
false;
1076#ifdef LIBMESH_HAVE_PETSC
1081#ifdef LIBMESH_TRILINOS_HAVE_AZTECOO
1087#ifdef LIBMESH_HAVE_EIGEN
1089#
if defined(LIBMESH_HAVE_MPI)
1098#ifdef LIBMESH_HAVE_LASPACK
1100#
if defined(LIBMESH_HAVE_MPI)
1113#
if defined(LIBMESH_HAVE_MPI)
1171#ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
void sync_type(const SyncType st)
const Communicator & comm() const
This class is intended to be reseatable like a pointer-to-ostream for flexibility,...
streambufT * rdbuf() const
Get the associated stream buffer.
streamT * get()
Rather than implement every ostream/ios/ios_base function, we'll be lazy and make esoteric uses go th...
void reset(streamT &target)
Reset the proxy to point to a different target.
Parallel::Communicator * _comm
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.
static std::terminate_handler _old_terminate_handler
const Parallel::Communicator & comm() const
Returns a Communicator created from the TIMPIInit object we hold, which will be a compatibility shim ...
vtkMPIController * _vtk_mpi_controller
TIMPI::TIMPIInit * _timpi_init
static PerfLog & perf_log()
virtual ~LibMeshInit()
Destructor.
friend void libmesh_terminate_handler()
A terminate handler.
The PerfLog class allows monitoring of specific events.
void print_log() const
Print the log.
void disable_logging()
Disables performance logging for an active object.
void clear()
Clears all the internal data and restores the data structures to a pristine state.
static void print_info(std::ostream &out_stream=libMesh::out)
Prints the reference information, by default to libMesh::out.
static unsigned int n_objects()
Prints the number of outstanding (created, but not yet destroyed) objects.
static void disable_print_counter_info()
static void enable_print_counter_info()
Methods to enable/disable the reference counter output from print_info().
static void cleanup()
Cleanup function.
static void setup()
Setup function.
void libMesh_MPI_Handler(MPI_Comm *, int *,...)
DIE A HORRIBLE DEATH HERE typedef MPI_Comm communicator
processor_id_type _n_processors
Total number of processors used.
unsigned int _default_grainsize
Minimum number of elements/nodes/indices per thread above which we can split work onto more threads.
processor_id_type _processor_id
The local processor id.
SolverPackage _solver_package
The default solver package to use.
bool _is_initialized
Flag that tells if init() has been called.
int _n_threads
Total number of threads possible.
The libMesh namespace provides an interface to certain functionality in the library.
template LIBMESH_EXPORT void command_line_vector< int >(const std::string &, std::vector< int > &)
template LIBMESH_EXPORT char command_line_value< char >(const std::string &, char)
template LIBMESH_EXPORT void command_line_vector< unsigned int >(const std::string &, std::vector< unsigned int > &)
template LIBMESH_EXPORT void command_line_vector< unsigned short >(const std::string &, std::vector< unsigned short > &)
template LIBMESH_EXPORT int command_line_value< int >(const std::string &, int)
template LIBMESH_EXPORT void command_line_vector< short >(const std::string &, std::vector< short > &)
processor_id_type global_processor_id()
void enableSIGINT(bool on)
Toggle libMesh handling of SIGINT (Ctrl+C) interrupts.
bool warned_about_auto_ptr
template LIBMESH_EXPORT double command_line_value< double >(const std::string &, double)
template LIBMESH_EXPORT short command_line_next< short >(std::string, short)
std::streambuf * _out_prewrap_buf
template LIBMESH_EXPORT unsigned int command_line_next< unsigned int >(std::string, unsigned int)
template LIBMESH_EXPORT std::string command_line_next< std::string >(std::string, std::string)
bool closed()
Checks that the library has been closed.
void command_line_vector(const std::string &, std::vector< T > &)
void libmesh_ignore(const Args &...)
SolverPackage default_solver_package()
template LIBMESH_EXPORT float command_line_next< float >(std::string, float)
MPI_Errhandler libmesh_errhandler
void uninstall_thread_buffered_sync()
template LIBMESH_EXPORT short command_line_value< short >(const std::string &, short)
template LIBMESH_EXPORT long double command_line_value< long double >(const std::string &, long double)
void add_command_line_names(const GetPot &getpot)
Merge a GetPot object's requested names into the set of queried command-line names.
PerfLog perflog
A PerfLog object to log performance.
template LIBMESH_EXPORT void command_line_vector< unsigned char >(const std::string &, std::vector< unsigned char > &)
template LIBMESH_EXPORT float command_line_value< float >(const std::string &, float)
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
SolverPackage
Defines an enum for various linear solver packages.
template LIBMESH_EXPORT void command_line_vector< double >(const std::string &, std::vector< double > &)
void enableFPE(bool on)
Toggle hardware trap floating point exceptions.
void add_command_line_name(const std::string &name)
Add a name to the set of queried command-line names.
std::streambuf * _err_prewrap_buf
template LIBMESH_EXPORT long double command_line_next< long double >(std::string, long double)
const RemoteElem * remote_elem
template LIBMESH_EXPORT Real command_line_next< Real >(std::string, Real)
T command_line_value(const std::string &, T)
const Number imaginary
The imaginary unit, .
template LIBMESH_EXPORT void command_line_vector< long double >(const std::string &, std::vector< long double > &)
void install_thread_buffered_sync()
bool initialized()
Checks that library initialization has been done.
template LIBMESH_EXPORT std::string command_line_value< std::string >(const std::string &, std::string)
std::vector< std::string > command_line_names()
template LIBMESH_EXPORT char command_line_next< char >(std::string, char)
std::unique_ptr< ThreadBufferedSyncbuf > _err_syncd_thread_buffer
void libmesh_abort()
Abort as soon as possible.
template LIBMESH_EXPORT unsigned char command_line_next< unsigned char >(std::string, unsigned char)
template LIBMESH_EXPORT Real command_line_value< Real >(const std::string &, Real)
template LIBMESH_EXPORT void command_line_vector< Real >(const std::string &, std::vector< Real > &)
template LIBMESH_EXPORT unsigned int command_line_value< unsigned int >(const std::string &, unsigned int)
MPI_Comm GLOBAL_COMM_WORLD
MPI Communicator used to initialize libMesh.
template LIBMESH_EXPORT int command_line_next< int >(std::string, int)
bool on_command_line(std::string arg)
template LIBMESH_EXPORT void command_line_vector< char >(const std::string &, std::vector< char > &)
template LIBMESH_EXPORT double command_line_next< double >(std::string, double)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
std::unique_ptr< ThreadBufferedSyncbuf > _out_syncd_thread_buffer
template LIBMESH_EXPORT unsigned short command_line_value< unsigned short >(const std::string &, unsigned short)
void enableSEGV(bool on)
Toggle libMesh reporting of segmentation faults.
template LIBMESH_EXPORT unsigned short command_line_next< unsigned short >(std::string, unsigned short)
template LIBMESH_EXPORT void command_line_vector< float >(const std::string &, std::vector< float > &)
void cleanup_stream_buffers()
Helper to do cleanup from both destructor and terminate.
template LIBMESH_EXPORT unsigned char command_line_value< unsigned char >(const std::string &, unsigned char)
EXTERN_C_BEGIN PETSC_EXTERN PetscErrorCode DMCreate_libMesh(DM)