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"
57 #pragma diag_suppress 177 // No unused attribute which works with both MSVC and NVCC
58 //! Is \c ptr aligned on a boundary that is a multiple of \c bytes.
59 gmx_unused static inline bool isAligned(const void *ptr, size_t bytes)
61 return (reinterpret_cast<intptr_t>(ptr) % bytes) == 0;
64 void pinBuffer(void *pointer, std::size_t numBytes) noexcept
66 const char *errorMessage = "Could not register the host memory for page locking for GPU transfers.";
68 GMX_ASSERT(isAligned(pointer, PageAlignedAllocationPolicy::alignment()),
69 formatString("%s Host memory needs to be page aligned.", errorMessage).c_str());
71 numBytes = std::max<size_t>(1, numBytes); //C++11 3.7.4.1 gurantees that every pointer is different thus at least 1 byte
73 ensureNoPendingCudaError(errorMessage);
74 cudaError_t stat = cudaHostRegister(pointer, numBytes, cudaHostRegisterDefault);
76 // These errors can only arise from a coding error somewhere.
77 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue &&
78 stat != cudaErrorNotSupported &&
79 stat != cudaErrorHostMemoryAlreadyRegistered,
80 formatString("%s %s: %s", errorMessage,
81 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
83 // We always handle the error, but if it's a type we didn't expect
84 // (e.g. because CUDA changes the set of errors it returns) then
85 // we should get a descriptive assertion in Debug mode so we know
86 // to fix our expectations.
87 GMX_ASSERT(stat != cudaErrorMemoryAllocation,
88 formatString("%s %s: %s which was an unexpected error", errorMessage,
89 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
91 // It might be preferable to throw InternalError here, because the
92 // failing condition can only happen when GROMACS is used with a
93 // CUDA API that can return some other error code. But we can't
94 // engineer GROMACS to be forward-compatible with future CUDA
95 // versions, so if this proves to be a problem in practice, then
96 // GROMACS must be patched, or a supported CUDA version used.
97 GMX_RELEASE_ASSERT(stat == cudaSuccess,
98 formatString("%s %s: %s", errorMessage,
99 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
102 void unpinBuffer(void *pointer) noexcept
104 const char *errorMessage = "Could not unregister pinned host memory used for GPU transfers.";
106 GMX_ASSERT(pointer != nullptr,
107 formatString("%s pointer should not be nullptr when pinned.", errorMessage).c_str());
109 ensureNoPendingCudaError(errorMessage);
110 cudaError_t stat = cudaHostUnregister(pointer);
111 // These errors can only arise from a coding error somewhere.
112 GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue && stat != cudaErrorHostMemoryNotRegistered,
113 formatString("%s %s: %s", errorMessage,
114 cudaGetErrorName(stat), cudaGetErrorString(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,
124 formatString("%s %s: %s which was an unexpected error", errorMessage,
125 cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());