2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016,2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements functions from init.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/gmxassert.h"
53 # include "gromacs/utility/gmxmpi.h"
62 //! Maintains global counter of attempts to initialize MPI
63 int g_initializationCounter
= 0;
67 void init(int* argc
, char*** argv
) // NOLINT(readability-non-const-parameter)
70 int isInitialized
= 0, isFinalized
= 0;
71 MPI_Finalized(&isFinalized
);
72 GMX_RELEASE_ASSERT(!isFinalized
, "Invalid attempt to initialize MPI after finalization");
73 MPI_Initialized(&isInitialized
);
76 if (0 == g_initializationCounter
)
78 // Some other code has already initialized MPI, so bump the counter so that
79 // we know not to finalize MPI ourselves later.
80 g_initializationCounter
++;
86 /* Formally we need to use MPI_Init_thread and ask for MPI_THREAD_FUNNELED
87 * level of thread support when using OpenMP. However, in practice we
88 * have never seen any problems with just using MPI_Init(), and some MPI
89 * versions that only return MPI_THREAD_SINGLE as their support level
90 * still return an OK initialization code when we request MPI_THREAD_FUNNELED.
92 * To avoid requiring users to recompile and install new libraries for something
93 * that probably isn't a problem, we don't make a fuss about it (no warning)
94 * if MPI_Init_thread() returns ok. If it does not return OK we
95 * call a second init call without thread support, but since the library
96 * apparently tried to prevent this we do issue a warning in this case.
98 * support, and if that doesn't work we simply use the simple MPI_Init().
99 * Note that MPICH does not allow us to call MPI_Query_thread() before
100 * we have initialized the library, and calling MPI_Init twice could
101 * be a bit fragile. However, some implementations also advice against
102 * doing anything else after calling MPI_Finalize, so at the end of the
103 * day we'll have to cross our fingers...
106 rc
= MPI_Init_thread(argc
, argv
, MPI_THREAD_FUNNELED
, &provided
);
107 if (rc
!= 0 && provided
< MPI_THREAD_FUNNELED
)
110 "GROMACS was compiled with OpenMP support, but there is no thread support in "
111 "the MPI library. Keep your fingers crossed.");
112 MPI_Init(argc
, argv
);
115 MPI_Init(argc
, argv
);
118 // Bump the counter to record this initialization event
119 g_initializationCounter
++;
122 GMX_UNUSED_VALUE(argc
);
123 GMX_UNUSED_VALUE(argv
);
130 GMX_RELEASE_ASSERT(0 < g_initializationCounter
, "Excess attempt to finalize MPI");
131 // Bump the counter to record this finalization event
132 g_initializationCounter
--;
134 if (0 == g_initializationCounter
)
136 /* We sync the processes here to try to avoid problems
137 * with buggy MPI implementations that could cause
138 * unfinished processes to terminate.
140 MPI_Barrier(MPI_COMM_WORLD
);
142 /* Apparently certain mpich implementations cause problems
143 * with MPI_Finalize. In that case comment out MPI_Finalize.