Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdrunutility / multisim.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  *
37  * \brief Declares the multi-simulation support routines.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_mdrunutility
42  */
43 #ifndef GMX_MDRUNUTILITY_MULTISIM_H
44 #define GMX_MDRUNUTILITY_MULTISIM_H
45
46 #include <memory>
47 #include <string>
48
49 #include "gromacs/utility/arrayref.h"
50 #include "gromacs/utility/gmxmpi.h"
51 #include "gromacs/utility/mpiinplacebuffers.h"
52
53 /*! \libinternal
54  * \brief Coordinate multi-simulation resources for mdrun
55  *
56  * \todo Change this to class
57  */
58 struct gmx_multisim_t
59 {
60     //! Default constructor
61     gmx_multisim_t();
62     /*! \brief Constructor useful for mdrun simulations
63      *
64      * Splits the communicator into multidirs.size() separate
65      * simulations, if >1, and creates a communication structure
66      * between the master these simulations.
67      *
68      * Valid to call regardless of build configuration, but \c
69      * multidirs must be empty unless a real MPI build is used. */
70     gmx_multisim_t(MPI_Comm comm, gmx::ArrayRef<const std::string> multidirs);
71     //! Destructor
72     ~gmx_multisim_t();
73
74     //! The number of simulations in the set of multi-simulations
75     int nsim = 1;
76     //! The index of the simulation that owns this object within the set
77     int sim = 0;
78     //! The MPI Group between master ranks of simulations, valid only on master ranks.
79     MPI_Group mpi_group_masters = MPI_GROUP_NULL;
80     //! The MPI communicator between master ranks of simulations, valid only on master ranks.
81     MPI_Comm mpi_comm_masters = MPI_COMM_NULL;
82     //! Communication buffers needed if MPI_IN_PLACE isn't supported
83     mpi_in_place_buf_t* mpb = nullptr;
84 };
85
86 //! Calculate the sum over the simulations of an array of ints
87 void gmx_sumi_sim(int nr, int r[], const gmx_multisim_t* ms);
88
89 //! Calculate the sum over the simulations of an array of large ints
90 void gmx_sumli_sim(int nr, int64_t r[], const gmx_multisim_t* ms);
91
92 //! Calculate the sum over the simulations of an array of floats
93 void gmx_sumf_sim(int nr, float r[], const gmx_multisim_t* ms);
94
95 //! Calculate the sum over the simulations of an array of doubles
96 void gmx_sumd_sim(int nr, double r[], const gmx_multisim_t* ms);
97
98 /*! \brief Return a vector containing the gathered values of \c
99  * localValue found on the master rank of each simulation. */
100 std::vector<int> gatherIntFromMultiSimulation(const gmx_multisim_t* ms, int localValue);
101
102 /*! \brief Check if val is the same on all simulations for a mdrun
103  * -multidir run
104  *
105  * The string name is used to print to the log file and in a fatal error
106  * if the val's don't match. If bQuiet is true and the check passes,
107  * no output is written. */
108 void check_multi_int(FILE* log, const gmx_multisim_t* ms, int val, const char* name, gmx_bool bQuiet);
109 /*! \copydoc check_multi_int() */
110 void check_multi_int64(FILE* log, const gmx_multisim_t* ms, int64_t val, const char* name, gmx_bool bQuiet);
111
112 #if GMX_DOUBLE
113 //! Convenience define for sum of reals
114 #    define gmx_sum_sim gmx_sumd_sim
115 #else
116 //! Convenience define for sum of reals
117 #    define gmx_sum_sim gmx_sumf_sim
118 #endif
119
120 //! Are we doing multiple independent simulations?
121 static bool inline isMultiSim(const gmx_multisim_t* ms)
122 {
123     return ms != nullptr;
124 }
125
126 /*! \brief Return whether this rank is the master rank of a
127  * simulation, using \c ms (if it is valid) and otherwise \c
128  * communicator */
129 bool findIsSimulationMasterRank(const gmx_multisim_t* ms, MPI_Comm communicator);
130
131 //! Are we the master simulation of a possible multi-simulation?
132 bool isMasterSim(const gmx_multisim_t* ms);
133
134 /*! \brief Are we the master rank (of the master simulation, for a multi-sim).
135  *
136  * This rank prints the remaining run time etc. */
137 bool isMasterSimMasterRank(const gmx_multisim_t* ms, bool isMaster);
138
139 //! Make a barrier across all multi-simulation master ranks
140 void multiSimBarrier(const gmx_multisim_t* ms);
141
142 #endif