From c2aab68b3f3c85b2d3b18a0990a615330500aa65 Mon Sep 17 00:00:00 2001 From: John Abraham Date: Fri, 13 May 2011 16:31:25 -0700 Subject: [PATCH] Bug 1087 Call MPI_Finalize to exit MPI environment --- src/mpi/examples/nms-p2p-nix-distributed.cc | 3 ++- src/mpi/examples/simple-distributed.cc | 2 ++ src/mpi/examples/third-distributed.cc | 2 ++ src/mpi/model/mpi-interface.cc | 22 +++++++++++++++++++++ src/mpi/model/mpi-interface.h | 7 ++++++- 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/mpi/examples/nms-p2p-nix-distributed.cc b/src/mpi/examples/nms-p2p-nix-distributed.cc index f0f71e07a..381dd0f45 100644 --- a/src/mpi/examples/nms-p2p-nix-distributed.cc +++ b/src/mpi/examples/nms-p2p-nix-distributed.cc @@ -601,7 +601,8 @@ main (int argc, char *argv[]) TIMER_NOW (t2); cout << "Simulator finished." << endl; Simulator::Destroy (); - + // Exit the MPI execution environment + MpiInterface::Disable (); double d1 = TIMER_DIFF (t1, t0), d2 = TIMER_DIFF (t2, t1); cout << "-----" << endl << "Runtime Stats:" << endl; cout << "Simulator init time: " << d1 << endl; diff --git a/src/mpi/examples/simple-distributed.cc b/src/mpi/examples/simple-distributed.cc index 9fd237165..c16fbea1b 100644 --- a/src/mpi/examples/simple-distributed.cc +++ b/src/mpi/examples/simple-distributed.cc @@ -245,6 +245,8 @@ main (int argc, char *argv[]) Simulator::Stop (Seconds (5)); Simulator::Run (); Simulator::Destroy (); + // Exit the MPI execution environment + MpiInterface::Disable (); return 0; #else NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in"); diff --git a/src/mpi/examples/third-distributed.cc b/src/mpi/examples/third-distributed.cc index 31c581584..681c08bc3 100644 --- a/src/mpi/examples/third-distributed.cc +++ b/src/mpi/examples/third-distributed.cc @@ -239,6 +239,8 @@ main (int argc, char *argv[]) Simulator::Run (); Simulator::Destroy (); + // Exit the MPI execution environment + MpiInterface::Disable (); return 0; #else diff --git a/src/mpi/model/mpi-interface.cc b/src/mpi/model/mpi-interface.cc index 0b8fba514..4199bc36d 100644 --- a/src/mpi/model/mpi-interface.cc +++ b/src/mpi/model/mpi-interface.cc @@ -284,4 +284,26 @@ MpiInterface::TestSendComplete () #endif } +void +MpiInterface::Disable () +{ +#ifdef NS3_MPI + int flag = 0 ; + MPI_Initialized (&flag); + if (flag) + { + MPI_Finalize (); + m_enabled = false; + m_initialized = false; + } + else + { + NS_FATAL_ERROR ("Cannot disable MPI environment without Initializing it first"); + } +#else + NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in"); +#endif +} + + } // namespace ns3 diff --git a/src/mpi/model/mpi-interface.h b/src/mpi/model/mpi-interface.h index c29126758..a361dfbea 100644 --- a/src/mpi/model/mpi-interface.h +++ b/src/mpi/model/mpi-interface.h @@ -97,7 +97,12 @@ public: * Sets up MPI interface */ static void Enable (int* pargc, char*** pargv); - + /** + * Terminates the MPI environment by calling MPI_Finalize + * This function must be called after Destroy () + * It also resets m_initialized, m_enabled + */ + static void Disable (); /** * \param p packet to send * \param rxTime received time at destination node