Skip to content

Commit fbf2464

Browse files
committed
More stable MPI; resolve potential issue with global MPIController
life-time
1 parent 3e331d5 commit fbf2464

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.11.4 FATAL_ERROR)
22

3-
project(neofaunus)
3+
project(faunus)
44

55
set(CMAKE_CXX_STANDARD 14)
66
set(CMAKE_CXX_STANDARD_REQUIRED YES)
@@ -185,19 +185,19 @@ include_directories(SYSTEM ${eigen_SOURCE_DIR} ${doctest_SOURCE_DIR} ${modernjso
185185

186186
# faunus cpp files
187187
set(objs
188+
${CMAKE_SOURCE_DIR}/src/random.cpp
189+
${CMAKE_SOURCE_DIR}/src/mpi.cpp
190+
${CMAKE_SOURCE_DIR}/src/core.cpp
191+
${CMAKE_SOURCE_DIR}/src/units.cpp
188192
${CMAKE_SOURCE_DIR}/src/analysis.cpp
189193
${CMAKE_SOURCE_DIR}/src/atomdata.cpp
190-
${CMAKE_SOURCE_DIR}/src/core.cpp
191194
${CMAKE_SOURCE_DIR}/src/energy.cpp
192195
${CMAKE_SOURCE_DIR}/src/geometry.cpp
193196
${CMAKE_SOURCE_DIR}/src/io.cpp
194197
${CMAKE_SOURCE_DIR}/src/move.cpp
195-
${CMAKE_SOURCE_DIR}/src/mpi.cpp
196198
${CMAKE_SOURCE_DIR}/src/particle.cpp
197199
${CMAKE_SOURCE_DIR}/src/penalty.cpp
198-
${CMAKE_SOURCE_DIR}/src/potentials.cpp
199-
${CMAKE_SOURCE_DIR}/src/random.cpp
200-
${CMAKE_SOURCE_DIR}/src/units.cpp)
200+
${CMAKE_SOURCE_DIR}/src/potentials.cpp)
201201

202202
set_source_files_properties(${objs} PROPERTIES LANGUAGE CXX)
203203

@@ -247,7 +247,7 @@ target_compile_definitions(faunus PRIVATE
247247
FAUNUS_TIPSFILE=\"${CMAKE_INSTALL_PREFIX}/share/faunus/tips.json\",\"${CMAKE_SOURCE_DIR}/tips.json\" )
248248

249249
if (MPI_CXX_FOUND)
250-
target_link_libraries(faunus PRIVATE MPI::MPI_C)
250+
target_link_libraries(faunus PRIVATE MPI::MPI_CXX)
251251
target_compile_definitions(faunus PRIVATE ENABLE_MPI)
252252
endif()
253253

src/faunus.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "core.h"
2+
#include "mpi.h"
23
#include "move.h"
34
#include "analysis.h"
4-
#include "mpi.h"
55
#include "multipole.h"
66
#include "docopt.h"
77
#include <cstdlib>
@@ -62,6 +62,8 @@ int main( int argc, char **argv )
6262
auto args = docopt::docopt( USAGE,
6363
{ argv + 1, argv + argc }, true, version);
6464

65+
mpi.init(); // initialize MPI, if available
66+
6567
// --notips
6668
if (not args["--notips"].asBool())
6769
usageTip.load( {FAUNUS_TIPSFILE} );
@@ -164,6 +166,8 @@ int main( int argc, char **argv )
164166
f << std::setw(4) << j << endl;
165167
}
166168

169+
mpi.finalize();
170+
167171
} catch (std::exception &e) {
168172
std::cerr << e.what() << endl;
169173
return EXIT_FAILURE;

src/mpi.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ namespace Faunus {
1111
* data (parallel tempering, for example). The prefix format is
1212
* \li \c "prefix + mpi%r." where \c \%r is the rank number.
1313
*/
14-
//inline MPIController::MPIController(MPI_Comm c) : comm(MPI_COMM_WORLD), _master(0) {
15-
MPIController::MPIController() {
14+
MPIController::~MPIController() {
15+
finalize();
16+
}
17+
18+
void MPIController::finalize() {
19+
if (mpi_initialized) {
20+
#ifdef ENABLE_MPI
21+
MPI_Finalize();
22+
#endif
23+
mpi_initialized=false;
24+
}
25+
}
26+
27+
void MPIController::init() {
28+
mpi_initialized=true;
1629
#ifdef ENABLE_MPI
1730
MPI_Init(NULL,NULL);
1831
MPI_Comm_size(comm, &_nproc);
@@ -25,14 +38,8 @@ namespace Faunus {
2538
}
2639
}
2740

28-
MPIController::~MPIController() {
29-
f.close();
30-
#ifdef ENABLE_MPI
31-
MPI_Finalize();
32-
#endif
33-
}
34-
3541
std::ostream& MPIController::cout() {
42+
assert(mpi_initialized);
3643
#ifdef ENABLE_MPI
3744
if (_nproc>1)
3845
return f;

src/mpi.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace Faunus {
3636
*
3737
* This is the MPI controller required for all MPI programs.
3838
*
39-
* MPIController mpi; // call this very first thing in your program
39+
* MPIController mpi;
40+
* mpi.init();
4041
* std::cout << "I'm rank " << mpi.rank << " out of " << mpi.nproc;
4142
* mpi.cout << "This will go to a file called mpi%r.stdout where %r is my rank"
4243
* if (mpi.isMaster())
@@ -51,7 +52,8 @@ namespace Faunus {
5152
*/
5253
class MPIController {
5354
public:
54-
MPIController(); //!< Constructor
55+
void init(); //!< Initialize MPI and file IO
56+
void finalize(); //!< Finalize MPI
5557
~MPIController(); //!< End of all MPI calls!
5658
int nproc() const; //!< Number of processors in communicator
5759
int rank() const; //!< Rank of process
@@ -68,6 +70,7 @@ namespace Faunus {
6870
int _nproc=1; //!< Number of processors in communicator
6971
int _rank=0; //!< Rank of process
7072
int _master=0; //!< Rank number of the master
73+
bool mpi_initialized=false;
7174
};
7275

7376
void to_json(json&, const MPIController&);

0 commit comments

Comments
 (0)