Commit 4d6fcc78 authored by Jianying Lang's avatar Jianying Lang Committed by Scott Cyphers

Langjian/mpi finalize (#2884)

* Rewrite the MPI_Finalize

* Change destructor to virtual finalize

* Test

* More bug fix

* Delete space

* Add variable to control initialization and finalization

* Add a bool variable to control initialization and finalization for openmpi and mlsl
parent 7c337e5d
...@@ -33,17 +33,19 @@ namespace ngraph ...@@ -33,17 +33,19 @@ namespace ngraph
MLSLDistributedInterface(const std::string& name = "MLSL") MLSLDistributedInterface(const std::string& name = "MLSL")
: m_name(name) : m_name(name)
{ {
if (!MLSL::Environment::GetEnv().IsInitialized()) if (!MLSL::Environment::GetEnv().IsInitialized() && !m_initialized_mlsl)
{ {
MLSL::Environment::GetEnv().Init(nullptr, nullptr); MLSL::Environment::GetEnv().Init(nullptr, nullptr);
m_initialized_mlsl = true;
} }
} }
~MLSLDistributedInterface() override ~MLSLDistributedInterface() override
{ {
if (MLSL::Environment::GetEnv().IsInitialized()) if (MLSL::Environment::GetEnv().IsInitialized() && m_initialized_mlsl)
{ {
MLSL::Environment::GetEnv().Finalize(); MLSL::Environment::GetEnv().Finalize();
m_initialized_mlsl = false;
} }
} }
...@@ -107,6 +109,7 @@ namespace ngraph ...@@ -107,6 +109,7 @@ namespace ngraph
protected: protected:
std::string m_name{"MLSL"}; std::string m_name{"MLSL"};
bool m_initialized_mlsl = false;
}; };
} }
} }
......
...@@ -37,20 +37,21 @@ namespace ngraph ...@@ -37,20 +37,21 @@ namespace ngraph
{ {
int flag = 0; int flag = 0;
MPI_Initialized(&flag); MPI_Initialized(&flag);
if (!flag) if (!flag && !m_initialized_mpi)
{ {
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
m_initialized_mpi = true;
} }
} }
~OpenMPIDistributedInterface() override ~OpenMPIDistributedInterface() override
{ {
int flag = 0; int is_mpi_finalized = 0;
MPI_Initialized(&flag); MPI_Finalized(&is_mpi_finalized);
if (!is_mpi_finalized && m_initialized_mpi)
if (flag)
{ {
MPI_Finalize(); MPI_Finalize();
m_initialized_mpi = false;
} }
} }
...@@ -108,6 +109,7 @@ namespace ngraph ...@@ -108,6 +109,7 @@ namespace ngraph
protected: protected:
std::string m_name; std::string m_name;
bool m_initialized_mpi = false;
}; };
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment