Move functionality to mdrunutility
[alexxy/gromacs.git] / src / gromacs / mdlib / simulationsignal.cpp
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,2016,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 /*! \internal \file
38  *
39  * \brief This file defines functions for inter- and intra-simulation
40  * signalling by mdrun.
41  *
42  * This handles details of responding to termination conditions,
43  * coordinating checkpoints, and coordinating multi-simulations.
44  *
45  * \todo Move this to mdrunutility module alongside gathering
46  * multi-simulation communication infrastructure there.
47  *
48  * \author Berk Hess <hess@kth.se>
49  * \author Mark Abraham <mark.j.abraham@gmail.com>
50  * \ingroup module_mdlib
51  */
52
53 #include "gmxpre.h"
54
55 #include "simulationsignal.h"
56
57 #include <algorithm>
58
59 #include "gromacs/gmxlib/network.h"
60 #include "gromacs/mdrunutility/multisim.h"
61 #include "gromacs/mdtypes/commrec.h"
62 #include "gromacs/utility/arrayref.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/real.h"
65
66 namespace gmx
67 {
68
69 SimulationSignaller::SimulationSignaller(SimulationSignals    *signals,
70                                          const t_commrec      *cr,
71                                          const gmx_multisim_t *ms,
72                                          bool                  doInterSim,
73                                          bool                  doIntraSim)
74     : signals_(signals), cr_(cr), ms_(ms),
75       doInterSim_(doInterSim),
76       doIntraSim_(doInterSim || doIntraSim),
77       mpiBuffer_ {}
78 {}
79
80 gmx::ArrayRef<real>
81 SimulationSignaller::getCommunicationBuffer()
82 {
83     if (doIntraSim_)
84     {
85         std::transform(std::begin(*signals_), std::end(*signals_), std::begin(mpiBuffer_),
86                        [](const SimulationSignals::value_type &s) { return s.sig; });
87
88         return mpiBuffer_;
89     }
90     else
91     {
92         return {};
93     }
94 }
95
96 void
97 SimulationSignaller::signalInterSim()
98 {
99     if (!doInterSim_)
100     {
101         return;
102     }
103     // The situations that lead to doInterSim_ == true without a
104     // multi-simulation begin active should already have issued an
105     // error at mdrun time in release mode, so there's no need for a
106     // release-mode assertion.
107     GMX_ASSERT(isMultiSim(ms_), "Cannot do inter-simulation signalling without a multi-simulation");
108     if (MASTER(cr_))
109     {
110         // Communicate the signals between the simulations.
111         gmx_sum_sim(eglsNR, mpiBuffer_.data(), ms_);
112     }
113     // Communicate the signals from the master to the others.
114     gmx_bcast(eglsNR*sizeof(mpiBuffer_[0]), mpiBuffer_.data(), cr_);
115 }
116
117 void SimulationSignaller::setSignals()
118 {
119     if (!doIntraSim_)
120     {
121         return;
122     }
123
124     SimulationSignals &s = *signals_;
125     for (size_t i = 0; i < s.size(); i++)
126     {
127         if (doInterSim_ || s[i].isLocal)
128         {
129             // Floating-point reduction of integer values is always
130             // exact, so we can use a simple cast.
131             signed char gsi = static_cast<signed char>(mpiBuffer_[i]);
132
133             /* Set the communicated signal only when it is non-zero,
134              * since a previously set signal might not have been
135              * handled immediately. */
136             if (gsi != 0)
137             {
138                 s[i].set = gsi;
139             }
140             // Turn off any local signal now that it has been processed.
141             s[i].sig = 0;
142         }
143     }
144 }
145
146 void SimulationSignaller::finalizeSignals()
147 {
148     signalInterSim();
149     setSignals();
150 }
151
152 }  // namespace gmx