2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018, 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 char *errorMessage = "Could not register the host memory for page locking for GPU transfers.";
72 GMX_ASSERT(isAligned(pointer, PageAlignedAllocationPolicy::alignment()),
73 formatString("%s Host memory needs to be page aligned.", errorMessage).c_str());
75 numBytes = std::max<size_t>(1, numBytes); //C++11 3.7.4.1 gurantees that every pointer is different thus at least 1 byte
77 ensureNoPendingCudaError(errorMessage);
78 cudaError_t stat = cudaHostRegister(pointer, numBytes, cudaHostRegisterDefault);
80 // These errors can only arise from a coding error somewhere.
81 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue &&
82 stat != cudaErrorNotSupported &&
83 stat != cudaErrorHostMemoryAlreadyRegistered,
84 formatString("%s %s: %s", errorMessage,
85 cudaGetErrorName(stat), cudaGetErrorString(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 formatString("%s %s: %s which was an unexpected error", errorMessage,
93 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
95 // It might be preferable to throw InternalError here, because the
96 // failing condition can only happen when GROMACS is used with a
97 // CUDA API that can return some other error code. But we can't
98 // engineer GROMACS to be forward-compatible with future CUDA
99 // versions, so if this proves to be a problem in practice, then
100 // GROMACS must be patched, or a supported CUDA version used.
101 GMX_RELEASE_ASSERT(stat == cudaSuccess,
102 formatString("%s %s: %s", errorMessage,
103 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
106 void unpinBuffer(void *pointer) noexcept
108 const char *errorMessage = "Could not unregister pinned host memory used for GPU transfers.";
110 GMX_ASSERT(pointer != nullptr,
111 formatString("%s pointer should not be nullptr when pinned.", errorMessage).c_str());
113 ensureNoPendingCudaError(errorMessage);
114 cudaError_t stat = cudaHostUnregister(pointer);
115 // These errors can only arise from a coding error somewhere.
116 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue && stat != cudaErrorHostMemoryNotRegistered,
117 formatString("%s %s: %s", errorMessage,
118 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
119 // If there's an error whose type we didn't expect (e.g. because a
120 // future CUDA changes the set of errors it returns) then we
121 // should assert, because our code is wrong.
123 // The approach differs from that in pin() because we might
124 // unpin() from a destructor, in which case any attempt to throw
125 // an uncaught exception would anyway terminate the program. A
126 // release assertion is a better behaviour than that.
127 GMX_RELEASE_ASSERT(stat == cudaSuccess,
128 formatString("%s %s: %s which was an unexpected error", errorMessage,
129 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());