43 #ifdef HAVE_TPETRA_MPI 44 # include <Teuchos_DefaultMpiComm.hpp> 46 # include <Teuchos_DefaultSerialComm.hpp> 47 #endif // HAVE_TPETRA_MPI 48 #include <Kokkos_Core.hpp> 53 bool tpetraIsInitialized_ =
false;
57 Teuchos::RCP<const Teuchos::Comm<int> > wrappedDefaultComm_;
59 #ifdef HAVE_TPETRA_MPI 61 void initMpi (
int* argc,
char*** argv)
64 int err = MPI_Initialized (&isInitialized);
66 TEUCHOS_TEST_FOR_EXCEPTION(
67 err != MPI_SUCCESS, std::runtime_error,
"MPI_Initialized failed with " 68 "error code " << err <<
" != MPI_SUCCESS. This probably indicates " 69 "that your MPI library is corrupted or that it is incorrectly linked " 70 "to your program, since this function should otherwise always " 73 if (isInitialized == 0) {
78 err = MPI_Init (argc, argv);
81 TEUCHOS_TEST_FOR_EXCEPTION(
82 err != MPI_SUCCESS, std::runtime_error,
"MPI_Init failed with error " 83 "code " << err <<
" != MPI_SUCCESS. If MPI was set up correctly, this " 84 "should not happen, since we have already checked that MPI_Init (or " 85 "MPI_Init_thread) has not yet been called. This may indicate that " 86 "your MPI library is corrupted or that it is incorrectly linked to " 89 #endif // HAVE_TPETRA_MPI 94 return tpetraIsInitialized_;
99 TEUCHOS_TEST_FOR_EXCEPTION(
100 tpetraIsInitialized_, std::runtime_error,
"Tpetra::getDefaultComm: " 101 "You must call Tpetra::initialize before you may get a default " 103 TEUCHOS_TEST_FOR_EXCEPTION(
104 wrappedDefaultComm_.is_null (), std::logic_error,
105 "Tpetra::getDefaultComm: wrappedDefaultComm_ is null! " 106 "This should never happen, because Tpetra claims that it has been " 107 "initialized. Please report this bug to the Tpetra developers.");
108 return wrappedDefaultComm_;
114 #ifdef HAVE_TPETRA_MPI 115 initMpi (argc, argv);
116 #endif // HAVE_TPETRA_MPI 118 if (! tpetraIsInitialized_) {
120 Kokkos::initialize (*argc, *argv);
122 #ifdef HAVE_TPETRA_MPI 123 wrappedDefaultComm_ =
124 Teuchos::rcp (
new Teuchos::MpiComm<int> (MPI_COMM_WORLD));
126 wrappedDefaultComm_ = Teuchos::rcp (
new Teuchos::SerialComm<int> ());
127 #endif // HAVE_TPETRA_MPI 128 tpetraIsInitialized_ =
true;
133 #ifdef HAVE_TPETRA_MPI 134 void initialize (
int* argc,
char*** argv, MPI_Comm comm)
136 #ifdef HAVE_TPETRA_MPI 137 initMpi (argc, argv);
138 #endif // HAVE_TPETRA_MPI 157 wrappedDefaultComm_ = Teuchos::rcp (
new Teuchos::MpiComm<int> (comm));
159 if (! tpetraIsInitialized_) {
161 Kokkos::initialize (*argc, *argv);
162 tpetraIsInitialized_ =
true;
165 #endif // HAVE_TPETRA_MPI 170 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm)
172 #ifdef HAVE_TPETRA_MPI 173 initMpi (argc, argv);
174 #endif // HAVE_TPETRA_MPI 193 wrappedDefaultComm_ = comm;
195 if (! tpetraIsInitialized_) {
197 Kokkos::initialize (*argc, *argv);
198 tpetraIsInitialized_ =
true;
208 if (! tpetraIsInitialized_) {
223 wrappedDefaultComm_ = Teuchos::null;
225 #ifdef HAVE_TPETRA_MPI 227 int err = MPI_Initialized (&isInitialized);
232 if (err != MPI_SUCCESS) {
233 cerr <<
"MPI_Initialized failed with error code " << err <<
" != " 234 "MPI_SUCCESS. This probably indicates that your MPI library is " 235 "corrupted or that it is incorrectly linked to your program, since " 236 "this function should otherwise always succeed." << endl;
238 else if (isInitialized != 0) {
241 err = MPI_Finalize ();
246 if (err != MPI_SUCCESS) {
247 cerr <<
"MPI_Finalize failed with error code " << err <<
" != " 248 "MPI_SUCCESS. I'm not sure how to recover from this safely, " 249 "so I will keep going and hope for the best." << endl;
252 #endif // HAVE_TPETRA_MPI 254 tpetraIsInitialized_ =
false;
bool isInitialized()
Whether Tpetra is in an initialized state.
void initialize(int *argc, char ***argv)
Initialize Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Functions for initializing and finalizing Tpetra.
Teuchos::RCP< const Teuchos::Comm< int > > getDefaultComm()
Get Tpetra's default communicator.