11#include "Tpetra_Details_mpiIsInitialized.hpp"
13#ifdef HAVE_TPETRACORE_MPI
14#include <Teuchos_DefaultMpiComm.hpp>
16#include <Teuchos_DefaultSerialComm.hpp>
18#include <Kokkos_Core.hpp>
19#include "Tpetra_Details_checkLaunchBlocking.hpp"
22#include "KokkosKernels_EagerInitialize.hpp"
29#if defined(HAVE_TPETRACORE_MPI)
30bool mpiIsInitializedAndNotFinalized() {
41 (void)MPI_Finalized(&isFinalized);
50bool tpetraIsInitialized_ =
false;
56bool tpetraInitializedKokkos_ =
false;
58#ifdef HAVE_TPETRACORE_MPI
62bool tpetraInitializedMpi_ =
false;
67Teuchos::RCP<const Teuchos::Comm<int> > wrappedDefaultComm_;
69#ifdef HAVE_TPETRACORE_MPI
71int getRankHarmlessly(MPI_Comm comm) {
73 if (mpiIsInitializedAndNotFinalized()) {
75 (void)MPI_Comm_rank(comm, &myRank);
86void initMpiIfNeeded(
int* argc,
char*** argv) {
95 const bool mpiReady = mpiIsInitializedAndNotFinalized();
102 const int err = MPI_Init(argc, argv);
103 TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
104 "MPI_Init failed with "
106 << err <<
" != MPI_SUCCESS. If MPI was set up "
107 "correctly, then this should not happen, since we have already "
108 "checked that MPI_Init (or MPI_Init_thread) has not yet been "
109 "called. This may indicate that your MPI library is corrupted "
110 "or that it is incorrectly linked to your program.");
111 tpetraInitializedMpi_ =
true;
130 Teuchos::RCP<const Teuchos::Comm<int> > comm;
131#ifdef HAVE_TPETRACORE_MPI
139 comm = Teuchos::rcp(
new Teuchos::SerialComm<int>());
142 comm = Teuchos::rcp(
new Teuchos::SerialComm<int>());
151#if defined(HAVE_TPETRACORE_MPI)
156 if (!Kokkos::is_initialized()) {
166#ifdef HAVE_TPETRACORE_MPI
167void initialize(
int* argc,
char*** argv, MPI_Comm comm) {
168 if (!tpetraIsInitialized_) {
169#if defined(HAVE_TPETRACORE_MPI)
170 initMpiIfNeeded(argc, argv);
174 const int myRank = getRankHarmlessly(comm);
176 const int myRank = 0;
180 if (!Kokkos::is_initialized()) {
182 tpetraInitializedKokkos_ =
true;
187 tpetraIsInitialized_ =
true;
211 wrappedDefaultComm_ = Teuchos::rcp(
new Teuchos::MpiComm<int>(comm));
216 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm) {
218#if defined(HAVE_TPETRACORE_MPI)
223 if (!Kokkos::is_initialized()) {
250#ifdef HAVE_TPETRACORE_MPI
276#ifdef HAVE_TPETRA_MPI
Functions for initializing and finalizing Tpetra.
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
Declaration functions that use Kokkos' profiling library to add deep copies between memory spaces,...
Declaration of Tpetra::Details::initializeKokkos.
Struct that holds views of the contents of a CrsMatrix.
static void reject_unrecognized_env_vars()
Search the environment for TPETRA_ variables and reject unrecognized ones.
ScopeGuard()=delete
Default constructor (FORBIDDEN)
~ScopeGuard()
Finalize Tpetra.
bool mpiIsInitialized()
Has MPI_Init been called (on this process)?
void initializeKokkos(int *argc, char ***argv, int myRank)
Initialize Kokkos, using command-line arguments (if any) given to Teuchos::GlobalMPISession.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
void initialize(int *argc, char ***argv)
Initialize Tpetra.
bool isInitialized()
Whether Tpetra is in an initialized state.
void finalize()
Finalize Tpetra.
Teuchos::RCP< const Teuchos::Comm< int > > getDefaultComm()
Get Tpetra's default communicator.