Split simulationWork.useGpuBufferOps into separate x and f flags
[alexxy/gromacs.git] / src / gromacs / mdtypes / commrec.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
7  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifndef GMX_MDTYPES_COMMREC_H
39 #define GMX_MDTYPES_COMMREC_H
40
41 #include <stddef.h>
42
43 #include "gromacs/utility/gmxassert.h"
44 #include "gromacs/utility/gmxmpi.h"
45
46 struct gmx_domdec_t;
47
48 #define DUTY_PP (1U << 0U)
49 #define DUTY_PME (1U << 1U)
50
51 //! Whether the current DD role is master or slave
52 enum class DDRole
53 {
54     Master,
55     Agent
56 };
57
58 //! Whether one or more ranks are used
59 enum class NumRanks
60 {
61     Single,
62     Multiple
63 };
64
65 typedef struct
66 {
67     int      bUse;
68     MPI_Comm comm_intra;
69     int      rank_intra;
70     MPI_Comm comm_inter;
71
72 } gmx_nodecomm_t;
73
74 struct t_commrec
75 {
76     /* The nodeids in one sim are numbered sequentially from 0.
77      * All communication within some simulation should happen
78      * in mpi_comm_mysim, or its subset mpi_comm_mygroup.
79      */
80     //! The rank-id in mpi_comm_mysim;
81     int sim_nodeid;
82     //! The number of ranks in mpi_comm_mysim
83     int nnodes;
84     //! The number of separate PME ranks, 0 when no separate PME ranks are used
85     int npmenodes;
86
87     //! The rank-id in mpi_comm_mygroup;
88     int nodeid;
89
90     /* MPI communicators within a single simulation
91      * Note: other parts of the code may further subset these communicators.
92      */
93     MPI_Comm mpi_comm_mysim;   /* communicator including all ranks of
94                                   a single simulation */
95     MPI_Comm mpi_comm_mygroup; /* subset of mpi_comm_mysim including only
96                                   the ranks in the same group (PP or PME) */
97     //! The number of ranks in mpi_comm_mygroup
98     int sizeOfMyGroupCommunicator;
99
100     //! The communicator used before DD was initialized
101     MPI_Comm mpiDefaultCommunicator;
102     int      sizeOfDefaultCommunicator;
103     int      rankInDefaultCommunicator;
104
105     gmx_nodecomm_t nc;
106
107     /* For domain decomposition */
108     gmx_domdec_t* dd;
109
110     /* The duties of this node, see the DUTY_ defines above.
111      * This should be read through thisRankHasDuty() or getThisRankDuties().
112      */
113     int duty;
114 };
115
116 /*! \brief
117  * Returns the rank's duty, and asserts that it has been initialized.
118  */
119 inline int getThisRankDuties(const t_commrec* cr)
120 {
121     GMX_ASSERT(cr, "Invalid commrec pointer");
122     GMX_ASSERT(cr->duty != 0, "Commrec duty was not initialized!");
123     return cr->duty;
124 }
125
126 /*! \brief
127  * A convenience getter for the commrec duty assignment;
128  * asserts that duty is actually valid (have been initialized).
129  *
130  * \param[in] cr    Communication structure pointer
131  * \param[in] duty  A single duty's corresponding DUTY_ flag. Combinations are not supported.
132  *
133  * \returns Whether this duty is assigned to this rank.
134  */
135 inline bool thisRankHasDuty(const t_commrec* cr, int duty)
136 {
137     GMX_ASSERT((duty == DUTY_PME) || (duty == DUTY_PP), "Invalid duty type");
138     return (getThisRankDuties(cr) & duty) != 0;
139 }
140
141 /*! \brief True if this is a simulation with more than 1 rank
142  *
143  * In particular, this is true for multi-rank runs with TPI and NM, because
144  * they use a decomposition that is not the domain decomposition used by
145  * other simulation types. */
146 #define PAR(cr) ((cr)->sizeOfDefaultCommunicator > 1)
147
148 //! True of this is the master node
149 #define MASTER(cr) (((cr)->rankInDefaultCommunicator == 0) || !PAR(cr))
150
151 // Note that currently, master is always PP master, so this is equivalent to MASTER(cr)
152 //! True if this is the particle-particle master
153 #define SIMMASTER(cr) ((MASTER(cr) && thisRankHasDuty((cr), DUTY_PP)) || !PAR(cr))
154
155 //! The node id for this rank
156 #define RANK(cr, nodeid) (nodeid)
157
158 //! The node id for the master
159 #define MASTERRANK(cr) (0)
160
161 /*! \brief Returns whether the domain decomposition machinery is active and reorders atoms
162  *
163  * This tells whether atoms are reordered at pair search steps. When the return value
164  * is true, atoms are not in the order of the input and mtop.
165  *
166  * Note that when the return value is true, there are not necessarily
167  * multiple domains. The domain decomposition machinery is also active and
168  * reorders the atoms also with a single MPI rank, or 1 PP and 1 PME rank,
169  * with most integrators. Only a few special non-integrator "integrators"
170  * do not (yet) support the domain decomposition machinery and therefore
171  * this function is still needed.
172  */
173 static bool inline haveDDAtomOrdering(const t_commrec& cr)
174 {
175     return cr.dd != nullptr;
176 }
177
178 /*! \brief Returns whether we have actual domain decomposition for the particle-particle interactions
179  *
180  * Will return false when we use 1 rank for PP and 1 for PME
181  */
182 static bool inline havePPDomainDecomposition(const t_commrec* cr)
183 {
184     /* NOTE: It would be better to use cr->dd->nnodes, but we do not want
185      *       to pull in a dependency on domdec.h into this file.
186      */
187     GMX_ASSERT(cr != nullptr, "Invalid call of havePPDomainDecomposition before commrec is made");
188     GMX_ASSERT(cr->npmenodes >= 0,
189                "Invalid call of havePPDomainDecomposition before MPMD automated decomposition was "
190                "chosen.");
191     return (cr->dd != nullptr && cr->nnodes - cr->npmenodes > 1);
192 }
193
194 #endif