Remove unused canComputeOnGpu(...) function from device management
[alexxy/gromacs.git] / src / gromacs / utility / init.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2018 by 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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements functions from init.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_utility
42  */
43 #include "gmxpre.h"
44
45 #include "init.h"
46
47 #include "config.h"
48
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/gmxassert.h"
52 #if GMX_LIB_MPI
53 #    include "gromacs/utility/gmxmpi.h"
54 #endif
55
56 namespace gmx
57 {
58
59 namespace
60 {
61 #if GMX_LIB_MPI
62 //! Maintains global counter of attempts to initialize MPI
63 int g_initializationCounter = 0;
64 #endif
65 } // namespace
66
67 void init(int* argc, char*** argv) // NOLINT(readability-non-const-parameter)
68 {
69 #if GMX_LIB_MPI
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);
74     if (isInitialized)
75     {
76         if (0 == g_initializationCounter)
77         {
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++;
81         }
82     }
83     else
84     {
85 #    if GMX_FAHCORE
86         fah_MPI_Init(argc, argv);
87 #    else
88 #        if GMX_OPENMP
89         /* Formally we need to use MPI_Init_thread and ask for MPI_THREAD_FUNNELED
90          * level of thread support when using OpenMP. However, in practice we
91          * have never seen any problems with just using MPI_Init(), and some MPI
92          * versions that only return MPI_THREAD_SINGLE as their support level
93          * still return an OK initialization code when we request MPI_THREAD_FUNNELED.
94          *
95          * To avoid requiring users to recompile and install new libraries for something
96          * that probably isn't a problem, we don't make a fuss about it (no warning)
97          * if MPI_Init_thread() returns ok. If it does not return OK we
98          * call a second init call without thread support, but since the library
99          * apparently tried to prevent this we do issue a warning in this case.
100          *
101          * support, and if that doesn't work we simply use the simple MPI_Init().
102          * Note that MPICH does not allow us to call MPI_Query_thread() before
103          * we have initialized the library, and calling MPI_Init twice could
104          * be a bit fragile. However, some implementations also advice against
105          * doing anything else after calling MPI_Finalize, so at the end of the
106          * day we'll have to cross our fingers...
107          */
108         int provided, rc;
109         rc = MPI_Init_thread(argc, argv, MPI_THREAD_FUNNELED, &provided);
110         if (rc != 0 && provided < MPI_THREAD_FUNNELED)
111         {
112             gmx_warning(
113                     "GROMACS was compiled with OpenMP support, but there is no thread support in "
114                     "the MPI library. Keep your fingers crossed.");
115             MPI_Init(argc, argv);
116         }
117 #        else
118         MPI_Init(argc, argv);
119 #        endif
120 #    endif
121     }
122     // Bump the counter to record this initialization event
123     g_initializationCounter++;
124
125 #else
126     GMX_UNUSED_VALUE(argc);
127     GMX_UNUSED_VALUE(argv);
128 #endif
129 }
130
131 void finalize()
132 {
133 #if GMX_LIB_MPI
134     GMX_RELEASE_ASSERT(0 < g_initializationCounter, "Excess attempt to finalize MPI");
135     // Bump the counter to record this finalization event
136     g_initializationCounter--;
137
138     if (0 == g_initializationCounter)
139     {
140         /* We sync the processes here to try to avoid problems
141          * with buggy MPI implementations that could cause
142          * unfinished processes to terminate.
143          */
144         MPI_Barrier(MPI_COMM_WORLD);
145
146         /* Apparently certain mpich implementations cause problems
147          * with MPI_Finalize. In that case comment out MPI_Finalize.
148          */
149         MPI_Finalize();
150     }
151 #endif
152 }
153
154 } // namespace gmx