43#include "Teuchos_Assert.hpp"
52#ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
53# include "Kokkos_Core.hpp"
61bool GlobalMPISession::haveMPIState_ =
false;
62bool GlobalMPISession::mpiIsFinalized_ =
false;
63int GlobalMPISession::rank_ = 0 ;
64int GlobalMPISession::nProc_ = 1 ;
66#ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
70std::vector<std::string> GlobalMPISession::argvCopy_;
77 std::ostringstream
oss;
91 *out <<
"GlobalMPISession(): Error, MPI_Intialized() return true,"
92 <<
" calling std::terminate()!\n"
102 *out <<
"GlobalMPISession(): Error, MPI_Init() returned error code="
103 <<
mpierr <<
"!=0, calling std::terminate()!\n"
116 *out <<
"GlobalMPISession(): Error, MPI_Get_processor_name() error code="
117 <<
mpierr <<
"!=0, calling std::terminate()!\n"
123 oss <<
"Teuchos::GlobalMPISession::GlobalMPISession(): started processor with name "
124 <<
procName <<
" and rank " << rank_ <<
"!" << std::endl;
128 oss <<
"Teuchos::GlobalMPISession::GlobalMPISession(): started serial run"
133#ifndef TEUCHOS_SUPPRESS_PROC_STARTUP_BANNER
137 const std::string
suppress_option(
"--teuchos-suppress-startup-banner");
145 (*
argv)[
i] = (*argv)[
i+1];
150 *out <<
oss.str() << std::flush;
155#ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
179#ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
180std::vector<std::string> GlobalMPISession::getArgv ()
190#ifdef HAVE_TEUCHOSCORE_KOKKOSCORE
192 if (Kokkos::is_initialized())
195 catch (
const std::runtime_error&
e) {
196 std::cerr <<
"Kokkos::finalize failed:\n"
201 haveMPIState_ =
false;
203 const int mpierr = ::MPI_Finalize();
204 mpiIsFinalized_ = (
mpierr == 0);
206 std::cerr <<
"Error code " <<
mpierr <<
" returned from MPI_Finalize()\n";
208 mpiIsFinalized_ =
true;
213 justInTimeInitialize();
222 justInTimeInitialize();
223 return haveMPIState_;
229 return mpiIsFinalized_;
235 justInTimeInitialize();
241 justInTimeInitialize();
248 justInTimeInitialize();
257 justInTimeInitialize();
270 justInTimeInitialize();
284void GlobalMPISession::initialize( std::ostream *out )
288 if(mpiIsFinalized_) {
303 int mpiHasBeenStarted = 0;
304 MPI_Initialized(&mpiHasBeenStarted);
306 if(!mpiHasBeenStarted)
314 mpierr = ::MPI_Comm_rank( MPI_COMM_WORLD, &rank_ );
316 *out <<
"Error code=" << mpierr <<
" detected in MPI_Comm_rank()"
320 mpierr = ::MPI_Comm_size( MPI_COMM_WORLD, &nProc_ );
322 *out <<
"Error code=" << mpierr <<
" detected in MPI_Comm_size()"
326 haveMPIState_ =
true;
327 mpiIsFinalized_ =
false;
334void GlobalMPISession::justInTimeInitialize()
337 initialize(&std::cerr);
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
static int sum(int localVal)
Sum a set of integers across processes.
static void abort()
abort the program
static void barrier()
Call MPI_Barrier() on MPI_COMM_WORLD.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
GlobalMPISession(int *argc, char ***argv, std::ostream *out=&std::cout)
Calls MPI_Init() if MPI is enabled.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
static bool mpiIsInitialized()
Return whether MPI was initialized.
static bool mpiIsFinalized()
Return whether MPI was already finalized.
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.
~GlobalMPISession()
Call MPI_Finalize() if MPI is enabled.
Smart reference counting pointer class for automatic garbage collection.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...