7b0c0cd53bf4027fa9c3774bd7024478460c0ec3
[alexxy/gromacs.git] / src / gromacs / gpu_utils / hostallocator.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018, 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 Declares gmx::HostAllocationPolicy, gmx::HostAllocator, and
37  * gmx::HostVector, which are used to make/be standard library
38  * containers that can allocate memory suitable for transfers.
39  * Currently the only supported transfers using pinned memory are
40  * to CUDA GPUs, but other possibilities exist in future.
41  *
42  * \todo This should not be in the public API, but it needs to be
43  * for the moment because state.h is in that API.
44  *
45  * \author Mark Abraham <mark.j.abraham@gmail.com>
46  * \inpublicapi
47  */
48 #ifndef GMX_GPU_UTILS_HOSTALLOCATOR_H
49 #define GMX_GPU_UTILS_HOSTALLOCATOR_H
50
51 #include <cstddef>
52
53 #include <memory>
54 #include <vector>
55
56 #include "gromacs/utility/alignedallocator.h"
57 #include "gromacs/utility/exceptions.h"
58
59 namespace gmx
60 {
61
62 /*! \brief Helper enum for pinning policy of the allocation of
63  * HostAllocationPolicy.
64  *
65  * For an efficient non-blocking transfer (e.g. to a GPU), the memory
66  * pages for a buffer need to be pinned to a physical page. Aligning
67  * such buffers to a physical page should miminize the number of pages
68  * that need to be pinned. However, some buffers that may be used for
69  * such transfers may also be used in either GROMACS builds or run
70  * paths that cannot use such a device, so the policy can be
71  * configured so that the resource consumption is no higher than
72  * required for correct, efficient operation in all cases. */
73 enum class PinningPolicy : int
74 {
75     CannotBePinned,     // Memory is not known to be suitable for pinning.
76     PinnedIfSupported,  // Memory is suitable for efficient pinning, e.g. because it is
77                         // allocated to be page aligned, and will be pinned when supported.
78 };
79
80 //! Forward declaration of host allocation policy class.
81 class HostAllocationPolicy;
82
83 /*! \brief Memory allocator that uses HostAllocationPolicy.
84  *
85  *  \tparam T          Type of objects to allocate
86  *
87  * This convenience partial specialization can be used for the
88  * optional allocator template parameter in standard library
89  * containers whose memory may be used for e.g. GPU transfers. The
90  * memory will always be allocated according to the behavior of
91  * HostAllocationPolicy.
92  */
93 template <class T>
94 using HostAllocator = Allocator<T, HostAllocationPolicy>;
95
96 //! Convenience alias for std::vector that uses HostAllocator.
97 template <class T>
98 using HostVector = std::vector<T, HostAllocator<T> >;
99
100 /*! \libinternal
101  * \brief Policy class for configuring gmx::Allocator, to manage
102  * allocations of memory that may be needed for e.g. GPU transfers.
103  *
104  * This allocator has state, so is most useful in cases where it is
105  * not known at compile time whether the allocated memory will be
106  * transferred to some device. It will increase the size of containers
107  * that use it. If the GROMACS build is configured with CUDA support,
108  * then memory will be allocated with PageAlignedAllocator, and that
109  * page pinned to physical memory if the pinning mode has been
110  * activated. If pinning mode is deactivated, or the GROMACS build
111  * does not support CUDA, then the memory will be allocated with
112  * AlignedAllocator. The pin() and unpin() methods work with the CUDA
113  * build, and silently do nothing otherwise. In future, we may modify
114  * or generalize this to work differently in other cases.
115  *
116  * The intended use is to configure gmx::Allocator with this class as
117  * its policy class, and then to use e.g.
118  * std::vector::get_allocator().getPolicy() to control whether the
119  * allocation policy should activate its pinning mode. The policy
120  * object can also be used to explicitly pin() and unpin() the buffer
121  * when it is using PinningPolicy::PinnedIfSupported. The policy object is
122  * returned by value (as required by the C++ standard for
123  * get_allocator(), which copies a std::shared_ptr, so the policy
124  * object should be retrieved sparingly, e.g. only upon resize of the
125  * allocation. (Normal operation of the vector, e.g. during resize,
126  * incurs only the cost of the pointer indirection needed to consult
127  * the current state of the allocation policy.)
128  *
129  * \todo As a minor optimization, consider also having a stateless
130  * version of this policy, which might be slightly faster or more
131  * convenient to use in the cases where it is known at compile time
132  * that the allocation will be used to transfer to a GPU.
133  */
134 class HostAllocationPolicy
135 {
136     public:
137         //! Constructor
138         HostAllocationPolicy(PinningPolicy policy = PinningPolicy::CannotBePinned);
139         /*! \brief Return the alignment size currently used by the active pinning policy. */
140         std::size_t alignment();
141         /*! \brief Allocate and perhaps pin page-aligned memory suitable for
142          * e.g. GPU transfers.
143          *
144          * Before attempting to allocate, unpin() is called. After a
145          * successful allocation, pin() is called. (Whether these do
146          * things depends on the PinningPolicy that is in effect.)
147          *
148          *  \param bytes Amount of memory (bytes) to allocate. It is valid to ask for
149          *               0 bytes, which will return a non-null pointer that is properly
150          *               aligned and padded (but that you should not use).
151          *
152          *  \return Valid pointer if the allocation+optional pinning worked, otherwise nullptr.
153          *
154          *  \note Memory allocated with this routine must be released
155          *        with gmx::HostAllocationPolicy::free(), and
156          *        absolutely not the system free().
157          *
158          * Does not throw.
159          */
160         void *malloc(std::size_t bytes) const noexcept;
161         /*! \brief Free the memory, after unpinning (if appropriate).
162          *
163          *  \param buffer  Memory pointer previously returned from gmx::HostAllocationPolicy::malloc()
164          *
165          *  \note This routine should only be called with pointers
166          *        obtained from gmx:HostAllocationPolicy::malloc(),
167          *        and absolutely not any pointers obtained the system
168          *        malloc().
169          *
170          * Does not throw.
171          */
172         void free(void *buffer) const noexcept;
173         /*! \brief Pin the allocation to physical memory, if appropriate.
174          *
175          * If the allocation policy is not in pinning mode, or the
176          * allocation is empty, ot the allocation is already pinned,
177          * then do nothing.
178          *
179          * Does not throw.
180          */
181         void pin(void* p, size_t n) const noexcept;
182         /*! \brief Unpin the allocation, if appropriate.
183          *
184          * Regardless of the allocation policy, unpin the memory if
185          * previously pinned, otherwise do nothing.
186          *
187          * Does not throw.
188          */
189         void unpin(void* p) const noexcept;
190         /*! \brief Return the current pinning policy (which is semi-independent
191          * of whether the buffer is actually pinned).
192          *
193          * Does not throw.
194          */
195         PinningPolicy pinningPolicy() const { return pinningPolicy_; }
196         //! Don't propagate for copy
197         using propagate_on_container_copy_assignment = std::false_type;
198         //! Propagate for move
199         using propagate_on_container_move_assignment = std::true_type;
200         //! Propagate for move
201         using propagate_on_container_swap = std::true_type;
202         //! Use default allocator for copy (same as construct+copy)
203         template<typename U = void>
204         HostAllocationPolicy select_on_container_copy_construction() const
205         {
206             return {};
207         }
208     private:
209         //! Pinning policy
210         PinningPolicy pinningPolicy_;
211 };
212
213 /*! \brief Helper function for changing the pinning policy of a HostVector.
214  *
215  * If the vector has contents, then a full reallocation and buffer
216  * copy are needed if the policy change requires tighter restrictions,
217  * and desirable even if the policy change requires looser
218  * restrictions. That cost is OK, because GROMACS will do this
219  * operation very rarely (e.g. when auto-tuning and deciding to switch
220  * whether a task will run on a GPU, or not). */
221 template <class T>
222 void changePinningPolicy(HostVector<T> *v, PinningPolicy pinningPolicy)
223 {
224     if (v->get_allocator().pinningPolicy() == pinningPolicy)
225     {
226         return;
227     }
228     //Force reallocation by creating copy
229     *v = HostVector<T>(*v, {pinningPolicy});
230 }
231 }      // namespace gmx
232
233 #endif