2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018,2019,2020, 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 functions for pinning memory to be suitable for
37 * efficient GPU transfers on CUDA.
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
49 #include "gromacs/gpu_utils/cudautils.cuh"
50 #include "gromacs/utility/alignedallocator.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/stringutil.h"
58 #if !defined(NDEBUG) || defined(DOXYGEN)
60 //! Is \c ptr aligned on a boundary that is a multiple of \c bytes.
61 gmx_unused static inline bool isAligned(const void* ptr, size_t bytes)
63 return (reinterpret_cast<intptr_t>(ptr) % bytes) == 0;
68 void pinBuffer(void* pointer, std::size_t numBytes) noexcept
70 const std::string errorMessage =
71 "Could not register the host memory for page locking for GPU transfers. ";
73 GMX_ASSERT(isAligned(pointer, PageAlignedAllocationPolicy::alignment()),
74 (errorMessage + "Host memory needs to be page aligned.").c_str());
76 numBytes = std::max<size_t>(
77 1, numBytes); // C++11 3.7.4.1 gurantees that every pointer is different thus at least 1 byte
79 ensureNoPendingDeviceError(errorMessage);
80 cudaError_t stat = cudaHostRegister(pointer, numBytes, cudaHostRegisterDefault);
82 // These errors can only arise from a coding error somewhere.
83 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue && stat != cudaErrorNotSupported
84 && stat != cudaErrorHostMemoryAlreadyRegistered,
85 (errorMessage + getDeviceErrorString(stat)).c_str());
87 // We always handle the error, but if it's a type we didn't expect
88 // (e.g. because CUDA changes the set of errors it returns) then
89 // we should get a descriptive assertion in Debug mode so we know
90 // to fix our expectations.
91 GMX_ASSERT(stat != cudaErrorMemoryAllocation,
92 (errorMessage + getDeviceErrorString(stat) + " which was an unexpected error").c_str());
94 // It might be preferable to throw InternalError here, because the
95 // failing condition can only happen when GROMACS is used with a
96 // CUDA API that can return some other error code. But we can't
97 // engineer GROMACS to be forward-compatible with future CUDA
98 // versions, so if this proves to be a problem in practice, then
99 // GROMACS must be patched, or a supported CUDA version used.
100 GMX_RELEASE_ASSERT(stat == cudaSuccess, (errorMessage + getDeviceErrorString(stat)).c_str());
103 void unpinBuffer(void* pointer) noexcept
105 const std::string errorMessage =
106 "Could not unregister pinned host memory used for GPU transfers. ";
108 GMX_ASSERT(pointer != nullptr, (errorMessage + "Pointer should not be nullptr when pinned.").c_str());
110 ensureNoPendingDeviceError(errorMessage);
111 cudaError_t stat = cudaHostUnregister(pointer);
112 // These errors can only arise from a coding error somewhere.
113 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue && stat != cudaErrorHostMemoryNotRegistered,
114 (errorMessage + getDeviceErrorString(stat)).c_str());
115 // If there's an error whose type we didn't expect (e.g. because a
116 // future CUDA changes the set of errors it returns) then we
117 // should assert, because our code is wrong.
119 // The approach differs from that in pin() because we might
120 // unpin() from a destructor, in which case any attempt to throw
121 // an uncaught exception would anyway terminate the program. A
122 // release assertion is a better behaviour than that.
123 GMX_RELEASE_ASSERT(stat == cudaSuccess, (errorMessage + getDeviceErrorString(stat)).c_str());