Skip to content

Commit 9570b54

Browse files
committed
bug fix in freeing MPI communicator. MDI is now able to exit cleanly after solving the DFT problem
1 parent c1404b7 commit 9570b54

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

include/dftfeWrapper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace dftfe
3939
* after calling MPI_Init
4040
*/
4141
static void
42-
globalHandlesInitialize();
42+
globalHandlesInitialize(const MPI_Comm &mpi_comm_world);
4343

4444
/**
4545
* @brief must be called only once at end of program from all processors

src/dftfeWrapper.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ namespace dftfe
130130
} // namespace internalWrapper
131131

132132
void
133-
dftfeWrapper::globalHandlesInitialize()
133+
dftfeWrapper::globalHandlesInitialize(const MPI_Comm &mpi_comm_world)
134134
{
135-
sc_init(MPI_COMM_WORLD, 0, 0, nullptr, SC_LP_SILENT);
135+
sc_init(mpi_comm_world, 0, 0, nullptr, SC_LP_SILENT);
136136
p4est_init(nullptr, SC_LP_SILENT);
137137

138138
#ifdef USE_PETSC
@@ -358,7 +358,13 @@ namespace dftfe
358358
{
359359
clear();
360360
if (mpi_comm_parent != MPI_COMM_NULL)
361-
MPI_Comm_dup(mpi_comm_parent, &d_mpi_comm_parent);
361+
{
362+
int ierr = MPI_Comm_dup(mpi_comm_parent, &d_mpi_comm_parent);
363+
if (ierr != 0)
364+
{
365+
throw std::runtime_error("MPI_Comm_dup failed.");
366+
}
367+
}
362368

363369
createScratchFolder();
364370

@@ -863,6 +869,9 @@ namespace dftfe
863869
delete d_dftfeParamsPtr;
864870
MPI_Comm_free(&d_mpi_comm_parent);
865871
}
872+
d_dftfeBasePtr = nullptr;
873+
d_dftfeParamsPtr = nullptr;
874+
d_mpi_comm_parent = MPI_COMM_NULL;
866875
}
867876

868877
void

src/main.cc

+7-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ main(int argc, char *argv[])
4545
//
4646
MPI_Init(&argc, &argv);
4747

48-
dftfe::dftfeWrapper::globalHandlesInitialize();
49-
5048
#if defined(DFTFE_WITH_MDI)
51-
5249
MPI_Comm mpi_world_comm;
53-
54-
55-
int ret;
50+
int ret;
5651
// Initialize MDI
5752
ret = MDI_Init(&argc, &argv);
5853
if (ret != 0)
@@ -83,8 +78,12 @@ main(int argc, char *argv[])
8378
throw std::runtime_error("MDI_MPI_get_world_comm failed.");
8479
}
8580

81+
dftfe::dftfeWrapper::globalHandlesInitialize(mpi_world_comm);
8682
dftfe::MDIEngine mdiEngine(mpi_world_comm, argc, argv);
83+
dftfe::dftfeWrapper::globalHandlesFinalize();
84+
MPI_Barrier(mpi_world_comm);
8785
#else
86+
dftfe::dftfeWrapper::globalHandlesInitialize(MPI_COMM_WORLD);
8887
const double start = MPI_Wtime();
8988
int world_rank;
9089
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
@@ -205,10 +204,10 @@ main(int argc, char *argv[])
205204
<< "============================================================================================="
206205
<< std::endl;
207206
}
208-
#endif
209207

210208
dftfe::dftfeWrapper::globalHandlesFinalize();
211-
209+
MPI_Barrier(MPI_COMM_WORLD);
210+
#endif
212211
MPI_Finalize();
213212
return 0;
214213
}

src/mdi/MDIEngine.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace dftfe
6565
// root = 1 for proc 0, otherwise 0
6666
int rank;
6767
MPI_Comm_rank(d_dftfeMPIComm, &rank);
68+
std::cout << "rank: " << rank << std::endl;
6869
d_root = (rank == 0) ? 1 : 0;
6970

7071
// MDI setup
@@ -278,7 +279,7 @@ namespace dftfe
278279
else if (strcmp(command, "EXIT") == 0)
279280
{
280281
d_exit_command = true;
281-
282+
d_dftfeWrapper.clear();
282283
// -------------------------------------------------------
283284
// unknown command
284285
// -------------------------------------------------------

src/mdi/libraryMDI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ MDI_Plugin_init_dftfe()
7575
MPI_Abort(MPI_COMM_WORLD, 1);
7676

7777
// open DFT-FE
78-
dftfe::dftfeWrapper::globalHandlesInitialize();
78+
// dftfe::dftfeWrapper::globalHandlesInitialize();
7979

8080
// launch MDI engine in endless loop
8181
dftfe::MDIEngine mdiEngine(mpi_world_comm, mdi_argc, mdi_argv);

0 commit comments

Comments
 (0)