Support pinning in HostAllocator
[gromacs.git] / src / gromacs / gpu_utils / hostallocator.cpp
bloba23c0cb12c83321e75844f5af85512ebab50267b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief Implements gmx::HostAllocationPolicy for allocating memory
37 * suitable for e.g. GPU transfers on CUDA.
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 #include "gmxpre.h"
43 #include "hostallocator.h"
45 #include "config.h"
47 #include <cstddef>
49 #include <memory>
51 #include "gromacs/utility/alignedallocator.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/stringutil.h"
55 #include "pinning.h"
57 namespace gmx
60 //! Private implementation class.
61 class HostAllocationPolicy::Impl
63 public:
64 /*! \brief Pointer to the last unfreed allocation, or nullptr
65 * if no allocation exists.
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;
82 HostAllocationPolicy::HostAllocationPolicy() : impl_(std::make_shared<Impl>())
86 std::size_t HostAllocationPolicy::alignment()
88 return (impl_->pinningPolicy_ == PinningPolicy::CanBePinned ?
89 PageAlignedAllocationPolicy::alignment() :
90 AlignedAllocationPolicy::alignment());
92 void *HostAllocationPolicy::malloc(std::size_t bytes) const noexcept
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));
104 if (impl_->pointer_ != nullptr)
106 impl_->numBytes_ = bytes;
108 pin();
109 return impl_->pointer_;
112 void HostAllocationPolicy::free(void *buffer) const noexcept
114 unpin();
115 if (buffer == nullptr)
117 // Nothing to do
118 return;
120 if (impl_->pinningPolicy_ == PinningPolicy::CanBePinned)
122 PageAlignedAllocationPolicy::free(buffer);
124 else
126 AlignedAllocationPolicy::free(buffer);
128 impl_->pointer_ = nullptr;
129 impl_->numBytes_ = 0;
132 PinningPolicy HostAllocationPolicy::pinningPolicy() const
134 return impl_->pinningPolicy_;
137 void HostAllocationPolicy::setPinningPolicy(PinningPolicy pinningPolicy)
139 if (GMX_GPU != GMX_GPU_CUDA)
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.");
145 impl_->pinningPolicy_ = pinningPolicy;
148 void HostAllocationPolicy::pin() const noexcept
150 if (impl_->pinningPolicy_ == PinningPolicy::CannotBePinned ||
151 impl_->pointer_ == nullptr ||
152 impl_->pinnedPointer_ != nullptr)
154 // Do nothing if we're not in pinning mode, or the allocation
155 // is empty, or it is already pinned.
156 return;
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.";
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
170 void HostAllocationPolicy::unpin() const noexcept
172 if (impl_->pinnedPointer_ == nullptr)
174 return;
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.
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
190 } // namespace gmx