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