2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020, 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.
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.
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.
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.
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.
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.
35 #ifndef GROMACS_SIMULATIONCONTEXT_H
36 #define GROMACS_SIMULATIONCONTEXT_H
40 * \brief Provide ways for client code to own simulation resources.
42 * For `gmx mdrun` to be implemented as a client program, public API needs to
43 * provide a way to create and manipulate the SimulationContext.
45 * \author M. Eric Irrgang <ericirrgang@gmail.com>
46 * \ingroup module_mdrun
53 #include "gromacs/mdrunutility/multisim.h"
54 #include "gromacs/utility/gmxmpi.h"
56 struct gmx_multisim_t;
66 * \brief Simulation environment and configuration.
68 * SimulationContext allows a simulation module (\relates gmx::mdrun) to retrieve
69 * runtime parameters and resources from client code. The client retains ownership
70 * of the context and its resources, with exceptions as noted.
72 * A client must share ownership of some resources and transfer ownership of
73 * other resources to create or configure the context. The simulation may in
74 * turn consume or borrow some resources from the context. This is a new
75 * framework that will evolve in the contexts of
76 * https://gitlab.com/gromacs/gromacs/-/issues/2375
77 * https://gitlab.com/gromacs/gromacs/-/issues/2587
79 * The public interface of SimulationContext is not yet well-specified.
80 * Client code can create an instance with gmx::createSimulationContext()
82 * \todo This class should also handle aspects of simulation
83 * environment such as working directory and environment variables.
85 * \ingroup module_mdrun
89 * This is a minimal placeholder for a more complete implementation.
90 * Interfaces for different API levels are not yet final.
91 * \todo Impose sensible access restrictions.
92 * Either the SimulationContext should be passed to the Mdrunner as logically constant or
93 * a separate handle class can provide access to resources that have been
94 * allocated by (negotiated with) the client for the current simulation
95 * (or simulation segment).
96 * Non-const accessors to shared resources need to be associated with update
97 * signals that the simulation components (modules and runner) can subscribe to.
99 * Also reference https://gitlab.com/gromacs/gromacs/-/issues/2587
101 class SimulationContext final
105 * \brief Object must be initialized with non-default constructor.
107 SimulationContext() = delete;
111 * \param communicator Communicator for all collaborating simulation processes.
112 * \param multiSimDirectoryNames Names of any directories used with -multidir
114 * Caller is responsible for keeping *communicator* valid for the life of SimulationContext,
115 * and then freeing the communicator, if appropriate.
117 * With an external MPI library (GMX_LIB_MPI==1), the client promises that
118 * gmx::init() has called MPI_Init and the provided communicator is valid to use.
119 * This communicator is "borrowed" (not duplicated) from the caller.
120 * Additional communicators may be split from the provided communicator
121 * during the life of the SimulationContext or its consumers.
123 * With thread-MPI, *communicator* must be MPI_COMM_NULL.
124 * The communicator is set up later
125 * during the process of spawning the threads that will be the MPI
126 * ranks. (Multi-simulation is not supported with thread-MPI.)
128 * \todo Refine distribution of environment management.
129 * There should be a context level at which only the current simulator directory matters,
130 * and a level above which encapsulates multisim details in a specialized type.
132 explicit SimulationContext(MPI_Comm communicator, ArrayRef<const std::string> multiSimDirectoryNames);
135 * \brief MPI resources for the entire simulation context.
137 * With an external MPI library (GMX_LIB_MPI==1),
138 * gmx::init() has called MPI_Init and the provided communicator is valid to use.
139 * The communicator is "borrowed" (not duplicated) from the caller.
141 * With thread-MPI, the communicator is set up later
142 * during the process of spawning the threads that will be the MPI
143 * ranks. (Multi-simulation is not supported with thread-MPI.)
145 MPI_Comm libraryWorldCommunicator_ = MPI_COMM_NULL;
148 * \brief MPI communicator object for this simulation.
150 * With an external MPI library (GMX_LIB_MPI==1),
151 * gmx::init() has called MPI_Init and the provided communicator is valid to use.
152 * The communicator is "borrowed" (not duplicated) from the caller.
154 * With thread-MPI, the communicator is set up later
155 * during the process of spawning the threads that will be the MPI
158 MPI_Comm simulationCommunicator_ = MPI_COMM_NULL;
161 * \brief Multi-sim handler (if required by e.g. gmx mdrun
162 * -multidir; only supported with real MPI)
164 * If a multi-simulation is in use, then its
165 * communicator(s) are found in multiSimulation_. This
166 * communicator is that of all ranks from all simulations, and
167 * will later be split into one for each simulation.
168 * TODO: Perhaps (for simplicity) communicator splitting
169 * can be undertaken during multi-sim setup (acquisition of the multisim resource).
171 * Multi-simulation is not supported with thread-MPI.
173 std::unique_ptr<gmx_multisim_t> multiSimulation_;
177 } // end namespace gmx
179 #endif // GROMACS_SIMULATIONCONTEXT_H