f3c3ef0688ee0cd6ec0ed4abf7bd35d6788e96c1
[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/mdtypes/commrec.h"
61 #include "gromacs/utility/arrayref.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/real.h"
64
65 namespace gmx
66 {
67
68 SimulationSignaller::SimulationSignaller(SimulationSignals    *signals,
69                                          const t_commrec      *cr,
70                                          const gmx_multisim_t *ms,
71                                          bool                  doInterSim,
72                                          bool                  doIntraSim)
73     : signals_(signals), cr_(cr), ms_(ms),
74       doInterSim_(doInterSim),
75       doIntraSim_(doInterSim || doIntraSim),
76       mpiBuffer_ {}
77 {}
78
79 gmx::ArrayRef<real>
80 SimulationSignaller::getCommunicationBuffer()
81 {
82     if (doIntraSim_)
83     {
84         std::transform(std::begin(*signals_), std::end(*signals_), std::begin(mpiBuffer_),
85                        [](const SimulationSignals::value_type &s) { return s.sig; });
86
87         return mpiBuffer_;
88     }
89     else
90     {
91         return {};
92     }
93 }
94
95 void
96 SimulationSignaller::signalInterSim()
97 {
98     if (!doInterSim_)
99     {
100         return;
101     }
102     // The situations that lead to doInterSim_ == true without a
103     // multi-simulation begin active should already have issued an
104     // error at mdrun time in release mode, so there's no need for a
105     // release-mode assertion.
106     GMX_ASSERT(isMultiSim(ms_), "Cannot do inter-simulation signalling without a multi-simulation");
107     if (MASTER(cr_))
108     {
109         // Communicate the signals between the simulations.
110         gmx_sum_sim(eglsNR, mpiBuffer_.data(), ms_);
111     }
112     // Communicate the signals from the master to the others.
113     gmx_bcast(eglsNR*sizeof(mpiBuffer_[0]), mpiBuffer_.data(), cr_);
114 }
115
116 void SimulationSignaller::setSignals()
117 {
118     if (!doIntraSim_)
119     {
120         return;
121     }
122
123     SimulationSignals &s = *signals_;
124     for (size_t i = 0; i < s.size(); i++)
125     {
126         if (doInterSim_ || s[i].isLocal)
127         {
128             // Floating-point reduction of integer values is always
129             // exact, so we can use a simple cast.
130             signed char gsi = static_cast<signed char>(mpiBuffer_[i]);
131
132             /* Set the communicated signal only when it is non-zero,
133              * since a previously set signal might not have been
134              * handled immediately. */
135             if (gsi != 0)
136             {
137                 s[i].set = gsi;
138             }
139             // Turn off any local signal now that it has been processed.
140             s[i].sig = 0;
141         }
142     }
143 }
144
145 void SimulationSignaller::finalizeSignals()
146 {
147     signalInterSim();
148     setSignals();
149 }
150
151 }  // namespace gmx