Create and use SimulationInput module.
[alexxy/gromacs.git] / src / gromacs / mdrun / simulationinputhandle.cpp
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
36 #include "gmxpre.h"
37
38 #include "simulationinputhandle.h"
39
40 #include <utility>
41
42 #include "gromacs/mdrun/legacymdrunoptions.h"
43
44 namespace gmx
45 {
46
47 /*! \cond
48  * SimulationInput needs much more discussion and refinement before it should be used directly.
49  * Use the interface defined in the headers and refer to #3652 for updates.
50  *
51  * Design note: It is probably sufficient for future versions to compose SimulationInput
52  * through a Builder rather than to subclass an Interface or base class. Outside of this
53  * translation unit, we should avoid coupling to the class definition until/unless we
54  * develop a much better understanding of simulation input portability.
55  *
56  */
57 class SimulationInput
58 {
59 public:
60     SimulationInput(const char* tprFilename, const char* cpiFilename) :
61         tprFilename_(tprFilename),
62         cpiFilename_(cpiFilename)
63     {
64     }
65
66     std::string tprFilename_;
67     std::string cpiFilename_;
68 };
69 /*! \endcond */
70
71 class detail::SimulationInputHandleImpl final
72 {
73 public:
74     explicit SimulationInputHandleImpl(std::shared_ptr<SimulationInput> input) :
75         simulationInput_{ std::move(input) }
76     {
77     }
78
79     SimulationInputHandleImpl(const SimulationInputHandleImpl&) = default;
80     SimulationInputHandleImpl& operator=(const SimulationInputHandleImpl&) = default;
81     SimulationInputHandleImpl(SimulationInputHandleImpl&&) noexcept        = default;
82     SimulationInputHandleImpl& operator=(SimulationInputHandleImpl&&) noexcept = default;
83
84     [[nodiscard]] SimulationInput* simulationInput() const { return simulationInput_.get(); }
85
86     /*! \brief Boolean interpretation checks for presence of payload.
87      *
88      * \return true if handle has a SimulationInput, else false.
89      */
90     operator bool() const { return bool(simulationInput_); }
91
92 private:
93     // Note that we should not need to worry about managing the SimulationInput
94     // Deleter as long as we manage the SimulationInputHolderImpl Deleter and
95     // SimulationInputHolderImpl is the only way to own a SimulationInput.
96     // BUT these semantics may change, and we should try to be responsible about
97     // consistent Allocator implementation.
98     std::shared_ptr<SimulationInput> simulationInput_;
99 };
100
101 /*! \cond
102  *  TODO: Improve doxygen source scoping.
103  *  It seems surprising that doxygen should be looking at this source file, but we
104  *  need to exclude the following definitions to avoid warnings.
105  *  Possibly related to https://gitlab.com/gromacs/gromacs/-/issues/3402
106  */
107 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter() = default;
108
109 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
110         const SimulationInputHandleImplDeleter&) noexcept = default;
111
112 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
113         SimulationInputHandleImplDeleter&&) noexcept = default;
114
115 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::
116                                           operator=(const SimulationInputHandleImplDeleter&) noexcept = default;
117
118 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::
119                                           operator=(SimulationInputHandleImplDeleter&&) noexcept = default;
120
121 void detail::SimulationInputHandleImplDeleter::operator()(SimulationInputHandleImpl* impl) const
122 {
123     delete impl;
124 }
125
126 SimulationInputHandle::SimulationInputHandle() = default;
127
128 SimulationInputHandle::~SimulationInputHandle() = default;
129
130 SimulationInputHandle::SimulationInputHandle(std::unique_ptr<detail::SimulationInputHandleImpl> impl) :
131     impl_(impl.release())
132 {
133 }
134
135 SimulationInputHandle::SimulationInputHandle(const SimulationInputHandle& source)
136 {
137     // Dynamically allocate a new holder implementation object using the copy constructor.
138     // Note that make_unique does not provide for custom deleters, and there is no default
139     // overload to move construct/assign from basic unique_ptr to one with custom deleter,
140     // but more elaborate creation functions for SimulationInputHolderImpl seem like
141     // overkill at this point. Still, we use make_unique to confine constructor errors
142     // to the following exception-safe line.
143     auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
144     // Move ownership of the heap object to the handle with a custom deleter.
145     impl_.reset(impl.release());
146 }
147
148 SimulationInputHandle& SimulationInputHandle::operator=(const SimulationInputHandle& source)
149 {
150     if (this != &source)
151     {
152         // Dynamically allocate a new holder implementation object using the copy constructor.
153         // Note that make_unique does not provide for custom deleters, and there is no default
154         // overload to move construct/assign from basic unique_ptr to one with custom deleter,
155         // but more elaborate creation functions for SimulationInputHolderImpl seem like
156         // overkill at this point. Still, we use make_unique to confine constructor errors
157         // to the following exception-safe line.
158         auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
159         // Move ownership of the heap object to the handle with a custom deleter.
160         impl_.reset(impl.release());
161     }
162     return *this;
163 }
164 /*! \endcond */
165
166 SimulationInput* SimulationInputHandle::get() const noexcept
167 {
168     if (!impl_)
169     {
170         return nullptr;
171     }
172     return impl_->simulationInput();
173 }
174
175 SimulationInputHandle::operator bool() const
176 {
177     if (impl_)
178     {
179         return bool(*impl_);
180     }
181     return false;
182 }
183
184 SimulationInputHandle makeSimulationInput(const LegacyMdrunOptions& options)
185 {
186     // Note: it seems clear that we will want a SimulationInput to be linked to
187     // a communications context (whether the SimulationContext or something higher level)
188     // so that it can encapsulate the data locality details. Otherwise, we have
189     // to choose whether to read the files everywhere or just to store the
190     // filenames until a communications context is known.
191     const auto* const tprFilename = ftp2fn(efTPR, options.filenames.size(), options.filenames.data());
192     const auto* const cpiFilename = opt2fn("-cpi", options.filenames.size(), options.filenames.data());
193     auto              simulationInput = std::make_unique<SimulationInput>(tprFilename, cpiFilename);
194     auto impl = std::make_unique<detail::SimulationInputHandleImpl>(std::move(simulationInput));
195
196     return SimulationInputHandle(std::move(impl));
197 }
198
199 } // end namespace gmx