Support pinning in HostAllocator
[alexxy/gromacs.git] / src / gromacs / gpu_utils / hostallocator.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017, 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 /*! \internal \file
36  * \brief Implements gmx::HostAllocationPolicy for allocating memory
37  * suitable for e.g. GPU transfers on CUDA.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  */
41 #include "gmxpre.h"
42
43 #include "hostallocator.h"
44
45 #include "config.h"
46
47 #include <cstddef>
48
49 #include <memory>
50
51 #include "gromacs/utility/alignedallocator.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/stringutil.h"
54
55 #include "pinning.h"
56
57 namespace gmx
58 {
59
60 //! Private implementation class.
61 class HostAllocationPolicy::Impl
62 {
63     public:
64         /*! \brief Pointer to the last unfreed allocation, or nullptr
65          * if no allocation exists.
66          *
67          * Note that during e.g. std::vector.resize() a call to its
68          * allocator's allocate() function precedes the call to its
69          * allocator's deallocate() function for freeing the old
70          * buffer after the data has been copied from it. So in
71          * general, pointer_ will not match the argument received by
72          * free(). */
73         void         *pointer_ = nullptr;
74         //! Number of bytes in the last unfreed allocation.
75         std::size_t   numBytes_ = 0;
76         //! The pointer to any storage that has been pinned, or nullptr if none has been pinned.
77         void         *pinnedPointer_ = nullptr;
78         //! Whether this object is in mode where new allocations will be pinned by default.
79         PinningPolicy pinningPolicy_ = PinningPolicy::CannotBePinned;
80 };
81
82 HostAllocationPolicy::HostAllocationPolicy() : impl_(std::make_shared<Impl>())
83 {
84 }
85
86 std::size_t HostAllocationPolicy::alignment()
87 {
88     return (impl_->pinningPolicy_ == PinningPolicy::CanBePinned ?
89             PageAlignedAllocationPolicy::alignment() :
90             AlignedAllocationPolicy::alignment());
91 }
92 void *HostAllocationPolicy::malloc(std::size_t bytes) const noexcept
93 {
94     // A container could have a pinned allocation that is being
95     // extended, in which case we must un-pin while we still know the
96     // old pinned vector, and which also ensures we don't pin two
97     // buffers at the same time. If there's no allocation, or it isn't
98     // pinned, then attempting to unpin it is OK, too.
99     unpin();
100     impl_->pointer_ = (impl_->pinningPolicy_ == PinningPolicy::CanBePinned ?
101                        PageAlignedAllocationPolicy::malloc(bytes) :
102                        AlignedAllocationPolicy::malloc(bytes));
103
104     if (impl_->pointer_ != nullptr)
105     {
106         impl_->numBytes_ = bytes;
107     }
108     pin();
109     return impl_->pointer_;
110 }
111
112 void HostAllocationPolicy::free(void *buffer) const noexcept
113 {
114     unpin();
115     if (buffer == nullptr)
116     {
117         // Nothing to do
118         return;
119     }
120     if (impl_->pinningPolicy_ == PinningPolicy::CanBePinned)
121     {
122         PageAlignedAllocationPolicy::free(buffer);
123     }
124     else
125     {
126         AlignedAllocationPolicy::free(buffer);
127     }
128     impl_->pointer_  = nullptr;
129     impl_->numBytes_ = 0;
130 }
131
132 PinningPolicy HostAllocationPolicy::pinningPolicy() const
133 {
134     return impl_->pinningPolicy_;
135 }
136
137 void HostAllocationPolicy::setPinningPolicy(PinningPolicy pinningPolicy)
138 {
139     if (GMX_GPU != GMX_GPU_CUDA)
140     {
141         GMX_RELEASE_ASSERT(pinningPolicy == PinningPolicy::CannotBePinned,
142                            "A suitable build of GROMACS (e.g. with CUDA) is required for a "
143                            "HostAllocationPolicy to be set to a mode that produces pinning.");
144     }
145     impl_->pinningPolicy_ = pinningPolicy;
146 }
147
148 void HostAllocationPolicy::pin() const noexcept
149 {
150     if (impl_->pinningPolicy_ == PinningPolicy::CannotBePinned ||
151         impl_->pointer_ == nullptr ||
152         impl_->pinnedPointer_ != nullptr)
153     {
154         // Do nothing if we're not in pinning mode, or the allocation
155         // is empty, or it is already pinned.
156         return;
157     }
158 #if GMX_GPU == GMX_GPU_CUDA
159     pinBuffer(impl_->pointer_, impl_->numBytes_);
160     impl_->pinnedPointer_ = impl_->pointer_;
161 #else
162     const char *errorMessage = "Could not register the host memory for pinning.";
163
164     GMX_RELEASE_ASSERT(impl_->pinningPolicy_ == PinningPolicy::CannotBePinned,
165                        formatString("%s This build configuration must only have pinning policy "
166                                     "that leads to no pinning.", errorMessage).c_str());
167 #endif
168 }
169
170 void HostAllocationPolicy::unpin() const noexcept
171 {
172     if (impl_->pinnedPointer_ == nullptr)
173     {
174         return;
175     }
176
177 #if GMX_GPU == GMX_GPU_CUDA
178     // Note that if the caller deactivated pinning mode, we still want
179     // to be able to unpin if the allocation is still pinned.
180
181     unpinBuffer(impl_->pointer_);
182     impl_->pinnedPointer_ = nullptr;
183 #else
184     GMX_RELEASE_ASSERT(impl_->pinnedPointer_ == nullptr,
185                        "Since the build configuration does not support pinning, then "
186                        "the pinned pointer must be nullptr.");
187 #endif
188 }
189
190 } // namespace gmx