Move functionality to mdrunutility
[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,2019, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_MDTYPES_COMMREC_H
38 #define GMX_MDTYPES_COMMREC_H
39
40 #include <stddef.h>
41
42 #include "gromacs/math/vectypes.h"
43 #include "gromacs/utility/basedefinitions.h"
44 #include "gromacs/utility/gmxassert.h"
45 #include "gromacs/utility/gmxmpi.h"
46 #include "gromacs/utility/real.h"
47
48 struct gmx_domdec_t;
49
50 typedef struct {
51     /* these buffers are used as destination buffers if MPI_IN_PLACE isn't
52        supported.*/
53     int             *ibuf; /* for ints */
54     int              ibuf_alloc;
55
56     int64_t         *libuf;
57     int              libuf_alloc;
58
59     float           *fbuf; /* for floats */
60     int              fbuf_alloc;
61
62     double          *dbuf; /* for doubles */
63     int              dbuf_alloc;
64 } mpi_in_place_buf_t;
65
66 void done_mpi_in_place_buf(mpi_in_place_buf_t *buf);
67
68 struct gmx_multisim_t
69 {
70     int       nsim              = 1;
71     int       sim               = 0;
72     MPI_Group mpi_group_masters = MPI_GROUP_NULL;
73     MPI_Comm  mpi_comm_masters  = MPI_COMM_NULL;
74     /* these buffers are used as destination buffers if MPI_IN_PLACE isn't
75        supported.*/
76     mpi_in_place_buf_t *mpb = nullptr;
77 };
78
79 #define DUTY_PP  (1<<0)
80 #define DUTY_PME (1<<1)
81
82 typedef struct {
83     int      bUse;
84     MPI_Comm comm_intra;
85     int      rank_intra;
86     MPI_Comm comm_inter;
87
88 } gmx_nodecomm_t;
89
90 struct t_commrec {
91     /* The nodeids in one sim are numbered sequentially from 0.
92      * All communication within some simulation should happen
93      * in mpi_comm_mysim, or its subset mpi_comm_mygroup.
94      */
95     int sim_nodeid, nnodes, npmenodes;
96
97     /* thread numbers: */
98     /* Not used yet: int threadid, nthreads; */
99     /* The nodeid in the PP/PME, PP or PME group */
100     int      nodeid;
101
102     /* MPI communicators within a single simulation
103      * Note: other parts of the code may further subset these communicators.
104      */
105     MPI_Comm mpi_comm_mysim;           /* communicator including all ranks of
106                                           a single simulation */
107     MPI_Comm mpi_comm_mygroup;         /* subset of mpi_comm_mysim including only
108                                           the ranks in the same group (PP or PME) */
109
110     gmx_nodecomm_t nc;
111
112     /* For domain decomposition */
113     gmx_domdec_t *dd;
114
115     /* The duties of this node, see the DUTY_ defines above.
116      * This should be read through thisRankHasDuty() or getThisRankDuties().
117      */
118     int                    duty;
119
120     /* these buffers are used as destination buffers if MPI_IN_PLACE isn't
121        supported.*/
122     mpi_in_place_buf_t *mpb;
123 };
124
125 /*! \brief
126  * Returns the rank's duty, and asserts that it has been initialized.
127  */
128 inline int getThisRankDuties(const t_commrec *cr)
129 {
130     GMX_ASSERT(cr, "Invalid commrec pointer");
131     GMX_ASSERT(cr->duty != 0, "Commrec duty was not initialized!");
132     return cr->duty;
133 }
134
135 /*! \brief
136  * A convenience getter for the commrec duty assignment;
137  * asserts that duty is actually valid (have been initialized).
138  *
139  * \param[in] cr    Communication structure pointer
140  * \param[in] duty  A single duty's corresponding DUTY_ flag. Combinations are not supported.
141  *
142  * \returns Whether this duty is assigned to this rank.
143  */
144 inline bool thisRankHasDuty(const t_commrec *cr, int duty)
145 {
146     GMX_ASSERT((duty == DUTY_PME) || (duty == DUTY_PP), "Invalid duty type");
147     return (getThisRankDuties(cr) & duty) != 0;
148 }
149
150 //! True if this is a simulation with more than 1 node
151 #define PAR(cr)        ((cr)->nnodes > 1)
152
153 //! True of this is the master node
154 #define MASTER(cr)     (((cr)->nodeid == 0) || !PAR(cr))
155
156 //! True if this is the particle-particle master
157 #define SIMMASTER(cr)  ((MASTER(cr) && thisRankHasDuty((cr), DUTY_PP)) || !PAR(cr))
158
159 //! The node id for this rank
160 #define RANK(cr, nodeid)    (nodeid)
161
162 //! The node id for the master
163 #define MASTERRANK(cr)     (0)
164
165 /*! \brief Do we use domain decomposition
166  *
167  * Note that even with particle decomposition removed, the use of
168  * non-DD parallelization in TPI, NM and multi-simulations means that
169  * PAR(cr) and DOMAINDECOMP(cr) are not universally synonymous. In
170  * particular, DOMAINDECOMP(cr) == true indicates that there is more
171  * than one domain, not just that the dd algorithm is active. */
172 #define DOMAINDECOMP(cr)   (((cr)->dd != nullptr) && PAR(cr))
173
174 /*! \brief Returns whether we have actual domain decomposition for the particle-particle interactions
175  *
176  * Will return false when we use 1 rank for PP and 1 for PME
177  */
178 static bool inline havePPDomainDecomposition(const t_commrec *cr)
179 {
180     /* NOTE: It would be better to use cr->dd->nnodes, but we do not want
181      *       to pull in a dependency on domdec.h into this file.
182      */
183     return (cr != nullptr &&
184             cr->dd != nullptr &&
185             cr->nnodes - cr->npmenodes > 1);
186 }
187
188 #endif