43 #ifdef HAVE_TPETRACORE_MPI 44 # include <Teuchos_DefaultMpiComm.hpp> 46 # include <Teuchos_DefaultSerialComm.hpp> 47 #endif // HAVE_TPETRACORE_MPI 48 #include <Kokkos_Core.hpp> 53 bool tpetraIsInitialized_ =
false;
59 bool tpetraInitializedKokkos_ =
false;
61 #ifdef HAVE_TPETRACORE_MPI 65 bool tpetraInitializedMpi_ =
false;
66 #endif // HAVE_TPETRACORE_MPI 70 Teuchos::RCP<const Teuchos::Comm<int> > wrappedDefaultComm_;
74 void initKokkos (
int* argc,
char*** argv)
76 if (! tpetraInitializedKokkos_) {
80 const bool kokkosIsInitialized =
81 Kokkos::DefaultExecutionSpace::is_initialized ();
82 if (! kokkosIsInitialized) {
84 Kokkos::initialize (*argc, *argv);
85 tpetraInitializedKokkos_ =
true;
89 const bool kokkosIsInitialized =
90 Kokkos::DefaultExecutionSpace::is_initialized ();
91 TEUCHOS_TEST_FOR_EXCEPTION
92 (! kokkosIsInitialized, std::logic_error,
"At the end of initKokkos, " 93 "Kokkos is not initialized. Please report this bug to the Tpetra " 97 #ifdef HAVE_TPETRACORE_MPI 98 bool mpiIsInitialized (
const bool throwExceptionOnFailure =
true)
103 if (throwExceptionOnFailure) {
104 TEUCHOS_TEST_FOR_EXCEPTION
105 (err != MPI_SUCCESS, std::runtime_error,
"MPI_Initialized failed with " 106 "error code " << err <<
" != MPI_SUCCESS. This probably indicates " 107 "that your MPI library is corrupted or that it is incorrectly linked " 108 "to your program, since this function should otherwise always " 111 else if (err != MPI_SUCCESS) {
114 cerr <<
"MPI_Initialized failed with error code " << err <<
" != " 115 "MPI_SUCCESS. This probably indicates that your MPI library is " 116 "corrupted or that it is incorrectly linked to your program, since " 117 "this function should otherwise always succeed." << endl;
126 void initMpi (
int* argc,
char*** argv)
128 if (! tpetraInitializedMpi_) {
129 int err = MPI_SUCCESS;
130 const bool initialized = mpiIsInitialized ();
136 err = MPI_Init (argc, argv);
138 TEUCHOS_TEST_FOR_EXCEPTION
139 (err != MPI_SUCCESS, std::runtime_error,
"MPI_Init failed with error " 140 "code " << err <<
" != MPI_SUCCESS. If MPI was set up correctly, this " 141 "should not happen, since we have already checked that MPI_Init (or " 142 "MPI_Init_thread) has not yet been called. This may indicate that " 143 "your MPI library is corrupted or that it is incorrectly linked to " 145 tpetraInitializedMpi_ =
true;
148 const bool initialized = mpiIsInitialized ();
149 TEUCHOS_TEST_FOR_EXCEPTION
150 (! initialized, std::logic_error,
"At the end of initMpi, MPI is not " 151 "initialized. Please report this bug to the Tpetra developers.");
153 #endif // HAVE_TPETRACORE_MPI 158 return tpetraIsInitialized_;
169 #ifdef HAVE_TPETRACORE_MPI 170 const bool mpiInitd = mpiIsInitialized (
false);
171 TEUCHOS_TEST_FOR_EXCEPTION
172 (! mpiInitd, std::runtime_error,
173 "Tpetra::getDefaultComm: MPI has not been initialized. Before " 174 "calling this method, you must either initialize MPI (by calling " 175 "MPI_Init), or you must call Tpetra::initialize (which initializes " 176 "MPI, if it has not yet been initialized).");
177 #endif // HAVE_TPETRACORE_MPI 182 if (wrappedDefaultComm_.is_null ()) {
183 Teuchos::RCP<const Teuchos::Comm<int> > comm;
184 #ifdef HAVE_TPETRACORE_MPI 185 comm = Teuchos::rcp (
new Teuchos::MpiComm<int> (MPI_COMM_WORLD));
187 comm = Teuchos::rcp (
new Teuchos::SerialComm<int> ());
188 #endif // HAVE_TPETRACORE_MPI 189 wrappedDefaultComm_ = comm;
191 return wrappedDefaultComm_;
196 if (! tpetraIsInitialized_) {
197 #ifdef HAVE_TPETRACORE_MPI 198 initMpi (argc, argv);
199 #endif // HAVE_TPETRACORE_MPI 200 initKokkos (argc, argv);
202 tpetraIsInitialized_ =
true;
205 #ifdef HAVE_TPETRACORE_MPI 206 void initialize (
int* argc,
char*** argv, MPI_Comm comm)
226 wrappedDefaultComm_ = Teuchos::rcp (
new Teuchos::MpiComm<int> (comm));
228 #endif // HAVE_TPETRACORE_MPI 232 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm)
252 wrappedDefaultComm_ = comm;
257 if (! tpetraIsInitialized_) {
263 if (tpetraInitializedKokkos_) {
270 wrappedDefaultComm_ = Teuchos::null;
272 #ifdef HAVE_TPETRACORE_MPI 275 if (tpetraInitializedMpi_) {
279 const bool throwOnFail =
false;
280 const bool mpiInitd = mpiIsInitialized (throwOnFail);
285 const int err = MPI_Finalize ();
290 if (err != MPI_SUCCESS) {
291 std::cerr <<
"MPI_Finalize failed with error code " << err <<
" != " 292 "MPI_SUCCESS. I'm not sure how to recover from this safely, " 293 "so I will keep going and hope for the best." << std::endl;
297 #endif // HAVE_TPETRACORE_MPI 299 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.
void finalize()
Finalize Tpetra.
Functions for initializing and finalizing Tpetra.
Teuchos::RCP< const Teuchos::Comm< int > > getDefaultComm()
Get Tpetra's default communicator.