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