Apply clang-format to source tree
[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,2019, 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,
37  * gmx::HostVector and gmx::PaddedHostVector, which are used to make/be
38  * standard library 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
55 #include "gromacs/math/paddedvector.h"
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 //! Convenience alias for PaddedVector that uses HostAllocator.
101 template<class T>
102 using PaddedHostVector = PaddedVector<T, HostAllocator<T>>;
103
104 /*! \libinternal
105  * \brief Policy class for configuring gmx::Allocator, to manage
106  * allocations of memory that may be needed for e.g. GPU transfers.
107  *
108  * This allocator has state, so is most useful in cases where it is
109  * not known at compile time whether the allocated memory will be
110  * transferred to some device. It will increase the size of containers
111  * that use it. If the GROMACS build is configured with CUDA support,
112  * then memory will be allocated with PageAlignedAllocator, and that
113  * page pinned to physical memory if the pinning mode has been
114  * activated. If pinning mode is deactivated, or the GROMACS build
115  * does not support CUDA, then the memory will be allocated with
116  * AlignedAllocator. The pin() and unpin() methods work with the CUDA
117  * build, and silently do nothing otherwise. In future, we may modify
118  * or generalize this to work differently in other cases.
119  *
120  * The intended use is to configure gmx::Allocator with this class as
121  * its policy class, and then to use e.g.
122  * std::vector::get_allocator().getPolicy() to control whether the
123  * allocation policy should activate its pinning mode. The policy
124  * object can also be used to explicitly pin() and unpin() the buffer
125  * when it is using PinningPolicy::PinnedIfSupported. The policy object is
126  * returned by value (as required by the C++ standard for
127  * get_allocator(), which copies a std::shared_ptr, so the policy
128  * object should be retrieved sparingly, e.g. only upon resize of the
129  * allocation. (Normal operation of the vector, e.g. during resize,
130  * incurs only the cost of the pointer indirection needed to consult
131  * the current state of the allocation policy.)
132  *
133  * \todo As a minor optimization, consider also having a stateless
134  * version of this policy, which might be slightly faster or more
135  * convenient to use in the cases where it is known at compile time
136  * that the allocation will be used to transfer to a GPU.
137  */
138 class HostAllocationPolicy
139 {
140 public:
141     //! Constructor
142     HostAllocationPolicy(PinningPolicy policy = PinningPolicy::CannotBePinned);
143     /*! \brief Return the alignment size currently used by the active pinning policy. */
144     std::size_t alignment() const noexcept;
145     /*! \brief Allocate and perhaps pin page-aligned memory suitable for
146      * e.g. GPU transfers.
147      *
148      * Before attempting to allocate, unpin() is called. After a
149      * successful allocation, pin() is called. (Whether these do
150      * things depends on the PinningPolicy that is in effect.)
151      *
152      *  \param bytes Amount of memory (bytes) to allocate. It is valid to ask for
153      *               0 bytes, which will return a non-null pointer that is properly
154      *               aligned and padded (but that you should not use).
155      *
156      *  \return Valid pointer if the allocation+optional pinning worked, otherwise nullptr.
157      *
158      *  \note Memory allocated with this routine must be released
159      *        with gmx::HostAllocationPolicy::free(), and
160      *        absolutely not the system free().
161      *
162      * Does not throw.
163      */
164     void* malloc(std::size_t bytes) const noexcept;
165     /*! \brief Free the memory, after unpinning (if appropriate).
166      *
167      *  \param buffer  Memory pointer previously returned from gmx::HostAllocationPolicy::malloc()
168      *
169      *  \note This routine should only be called with pointers
170      *        obtained from gmx:HostAllocationPolicy::malloc(),
171      *        and absolutely not any pointers obtained the system
172      *        malloc().
173      *
174      * Does not throw.
175      */
176     void free(void* buffer) const noexcept;
177     /*! \brief Return the active pinning policy.
178      *
179      * Does not throw.
180      */
181     PinningPolicy pinningPolicy() const { return pinningPolicy_; }
182     //! Don't propagate for copy
183     using propagate_on_container_copy_assignment = std::false_type;
184     //! Propagate for move
185     using propagate_on_container_move_assignment = std::true_type;
186     //! Propagate for move
187     using propagate_on_container_swap = std::true_type;
188     //! Use default allocator for copy (same as construct+copy)
189     HostAllocationPolicy select_on_container_copy_construction() const { return {}; }
190
191 private:
192     //! Pinning policy
193     PinningPolicy pinningPolicy_;
194 };
195
196 /*! \brief Return true if two allocators are identical
197  *
198  * True if pinning policy is the same.
199  */
200 template<class T1, class T2>
201 bool operator==(const Allocator<T1, HostAllocationPolicy>& a, const Allocator<T2, HostAllocationPolicy>& b)
202 {
203     return a.pinningPolicy() == b.pinningPolicy();
204 }
205
206 /*! \brief Helper function for changing the pinning policy of a pinnable vector.
207  *
208  * If the vector has contents, then a full reallocation and buffer
209  * copy are needed if the policy change requires tighter restrictions,
210  * and desirable even if the policy change requires looser
211  * restrictions. That cost is OK, because GROMACS will do this
212  * operation very rarely (e.g. when auto-tuning and deciding to switch
213  * whether a task will run on a GPU, or not). */
214 template<typename PinnableVector>
215 void changePinningPolicy(PinnableVector* v, PinningPolicy pinningPolicy)
216 {
217     // Force reallocation by element-wise move (because policy is
218     // different container is forced to realloc). Does nothing if
219     // policy is the same.
220     *v = PinnableVector(std::move(*v), { pinningPolicy });
221 }
222
223 } // namespace gmx
224
225 #endif