Split simulationWork.useGpuBufferOps into separate x and f flags
[alexxy/gromacs.git] / src / gromacs / mdrun / simulationinputhandle.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \file
36  * \brief Public interface for SimulationInput facilities.
37  *
38  * \author M. Eric Irrgang <ericirrgang@gmail.com>
39  * \ingroup module_mdrun
40  * \inpublicapi
41  */
42
43 #ifndef GMX_MDRUN_SIMULATIONINPUTHANDLE_H
44 #define GMX_MDRUN_SIMULATIONINPUTHANDLE_H
45
46 #include <memory>
47
48 namespace gmx
49 {
50
51 // Forward declarations for types from other modules that are opaque to the public API.
52 class LegacyMdrunOptions;
53
54 /*!
55  * \brief Prescription for molecular simulation.
56  *
57  * Represent the complete and unique information needed to generate a simulation
58  * trajectory segment. SimulationInput objects are opaque to the public API.
59  * Ownership can be managed with SimulationInputHolder objects. Clients can
60  * acquire owning references to SimulationInput objects (as SimulationInputHolder)
61  * through makeSimulationInput() or from other SimulationInputHolders.
62  *
63  * A SimulationInput object represents an immutable source of data, and is safe
64  * to share. A SimulationInput object may have internal state to support
65  * performance optimizations when shared by multiple SimulationInputHolders.
66  * The SimulationInput is guaranteed to live at least as long as any associated
67  * SimulationInputHolders. The API does not specify whether it may persist
68  * longer internally or be reused for later equivalent requests.
69  *
70  * \see SimulationInputHandle
71  * \see makeSimulationInput()
72  *
73  * \internal
74  * SimulationInput has no public interface yet, but we need a forward declaration for the
75  * library symbol. Library interface provided through simulationinput.h
76  * See also https://gitlab.com/gromacs/gromacs/-/issues/3379 for design and development road map.
77  */
78 class SimulationInput;
79
80 /*! \cond internal
81  * Client software developers do not interact directly with the contents of gmx::detail,
82  * but some declarations and definitions are necessary in the public headers, such as
83  * forward declarations of implementation classes and definitions of custom deleters.
84  */
85 namespace detail
86 {
87 /*!
88  * \brief Private implementation class;
89  */
90 class SimulationInputHandleImpl;
91
92 /*!
93  * \brief Explicit deleter details for SimulationInputHolderImpl.
94  *
95  * SimulationInputHolderImpl objects are created by the GROMACS library, but
96  * are destroyed when the SimulationInputHolder goes out of scope in the client
97  * code, which may be linked to a different allocator.
98  * We want to make sure that objects are allocated and deallocated with the same
99  * allocator, so we avoid the default deleter in unique_ptrs and compile allocation
100  * and deallocation code in the same translation unit.
101  *
102  * Note that this does not solve potential ABI incompatibilities between the
103  * unique_ptr implementations themselves, but we need to consider ABI
104  * compatibility goals and challenges as well as supported use cases and
105  * ownership semantics.
106  */
107 struct SimulationInputHandleImplDeleter
108 {
109     /*! \cond */
110     SimulationInputHandleImplDeleter();
111     SimulationInputHandleImplDeleter(const SimulationInputHandleImplDeleter& deleter) noexcept;
112     SimulationInputHandleImplDeleter(SimulationInputHandleImplDeleter&& deleter) noexcept;
113     SimulationInputHandleImplDeleter& operator=(const SimulationInputHandleImplDeleter& deleter) noexcept;
114     SimulationInputHandleImplDeleter& operator=(SimulationInputHandleImplDeleter&& deleter) noexcept;
115     void                              operator()(SimulationInputHandleImpl* impl) const;
116     /*! \endcond */
117 };
118 } // end namespace detail
119 /*! \endcond internal */
120
121 /*!
122  * \brief Owning handle to a SimulationInput object.
123  *
124  * SimulationInput objects are logically immutable, so ownership may be shared
125  * by multiple SimulationInputHolders.
126  *
127  * Acquire a SimulationInputHolder with makeSimulationInput() and pass to (e.g.)
128  * gmx::MdrunnerBuilder::addInput()
129  *
130  * SimulationInput has no public API yet.
131  * \see https://gitlab.com/gromacs/gromacs/-/issues/3379
132  */
133 class SimulationInputHandle
134 {
135 public:
136     /*! \cond internal */
137     SimulationInputHandle();
138     ~SimulationInputHandle();
139
140     SimulationInputHandle(const SimulationInputHandle& source);
141     SimulationInputHandle(SimulationInputHandle&&) noexcept = default;
142
143     SimulationInputHandle& operator=(const SimulationInputHandle& rhs);
144     SimulationInputHandle& operator=(SimulationInputHandle&&) noexcept = default;
145
146     /*!
147      * \brief Take ownership of private implementation object to produce a new public holder.
148      */
149     explicit SimulationInputHandle(std::unique_ptr<detail::SimulationInputHandleImpl> impl);
150     /*! \endcond */
151
152     /*!
153      * \brief Access opaque SimulationInput pointer.
154      *
155      * \return Borrowed access to the SimulationInput, if present.
156      */
157     [[nodiscard]] SimulationInput* get() const noexcept;
158
159     /*!
160      * \brief Boolean context returns true if an input is held, else false.
161      */
162     operator bool() const;
163
164 private:
165     std::unique_ptr<detail::SimulationInputHandleImpl, detail::SimulationInputHandleImplDeleter> impl_;
166 };
167
168 /*! \brief Direct the construction of a SimulationInput.
169  *
170  * \warning Creation methods for SimulationInput resources are under rapid development.
171  * Reference https://gitlab.com/gromacs/gromacs/-/issues/3652
172  *
173  * \param options library-internal container holding partially processed user input.
174  *
175  * \ingroup module_mdrun
176  *
177  * \internal
178  * This isn't really a public API function until its arguments are obtainable
179  * through the public API.
180  *
181  * Design notes: SimulationInput creation will warrant a builder protocol, and
182  * this helper can evolve into a director to apply the contents of LegacyMdrunOptions,
183  * while such an operation is still relevant.
184  *
185  * Example:
186  *     // After preparing a LegacyMdrunOptions and calling handleRestart()...
187  *     SimulationInputBuilder builder;
188  *     auto simulationInputHandle = makeSimulationInput(options, &builder);
189  *
190  *     // In addition to MdrunnerBuilder::addFiles(),
191  *     mdrunnerBuilder.addInput(simulationInputHandle.get());
192  *
193  */
194 SimulationInputHandle makeSimulationInput(const LegacyMdrunOptions& options);
195
196 } // end namespace gmx
197
198 #endif // GMX_MDRUN_SIMULATIONINPUTHANDLE_H