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.
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>
43 #include "hostallocator.h"
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"
59 //! Private implementation class.
60 class HostAllocationPolicy::Impl
63 /*! \brief Pointer to the last unfreed allocation, or nullptr
64 * if no allocation exists.
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
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;
81 HostAllocationPolicy::HostAllocationPolicy() : impl_(std::make_shared<Impl>())
85 std::size_t HostAllocationPolicy::alignment()
87 return (impl_->pinningPolicy_ == PinningPolicy::CanBePinned ?
88 PageAlignedAllocationPolicy::alignment() :
89 AlignedAllocationPolicy::alignment());
91 void *HostAllocationPolicy::malloc(std::size_t bytes) const noexcept
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.
99 impl_->pointer_ = (impl_->pinningPolicy_ == PinningPolicy::CanBePinned ?
100 PageAlignedAllocationPolicy::malloc(bytes) :
101 AlignedAllocationPolicy::malloc(bytes));
103 if (impl_->pointer_ != nullptr)
105 impl_->numBytes_ = bytes;
108 return impl_->pointer_;
111 void HostAllocationPolicy::free(void *buffer) const noexcept
114 if (buffer == nullptr)
119 if (impl_->pinningPolicy_ == PinningPolicy::CanBePinned)
121 PageAlignedAllocationPolicy::free(buffer);
125 AlignedAllocationPolicy::free(buffer);
127 impl_->pointer_ = nullptr;
128 impl_->numBytes_ = 0;
131 PinningPolicy HostAllocationPolicy::pinningPolicy() const
133 return impl_->pinningPolicy_;
136 void HostAllocationPolicy::setPinningPolicy(PinningPolicy pinningPolicy)
138 if (GMX_GPU != GMX_GPU_CUDA)
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.");
144 impl_->pinningPolicy_ = pinningPolicy;
147 void HostAllocationPolicy::pin() const noexcept
149 if (impl_->pinningPolicy_ == PinningPolicy::CannotBePinned ||
150 impl_->pointer_ == nullptr ||
151 impl_->pinnedPointer_ != nullptr)
153 // Do nothing if we're not in pinning mode, or the allocation
154 // is empty, or it is already pinned.
157 #if GMX_GPU == GMX_GPU_CUDA
158 pinBuffer(impl_->pointer_, impl_->numBytes_);
159 impl_->pinnedPointer_ = impl_->pointer_;
161 const char *errorMessage = "Could not register the host memory for pinning.";
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());
169 void HostAllocationPolicy::unpin() const noexcept
171 if (impl_->pinnedPointer_ == nullptr)
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.
180 unpinBuffer(impl_->pointer_);
181 impl_->pinnedPointer_ = nullptr;
183 GMX_RELEASE_ASSERT(impl_->pinnedPointer_ == nullptr,
184 "Since the build configuration does not support pinning, then "
185 "the pinned pointer must be nullptr.");