Support pinning in HostAllocator
[gromacs.git] / src / gromacs / gpu_utils / pinning.cu
blobfb95d90f245dd76344b7a62c362ec68ad9d10a4c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Implements functions for pinning memory to be suitable for
37  * efficient GPU transfers on CUDA.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  */
41 #include "gmxpre.h"
43 #include "pinning.h"
45 #include <cstddef>
47 #include "gromacs/gpu_utils/cudautils.cuh"
48 #include "gromacs/utility/alignedallocator.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/stringutil.h"
53 namespace gmx
56 //! Is \c ptr aligned on a boundary that is a multiple of \c bytes.
57 static inline bool isAligned(const void *ptr, size_t bytes)
59     return (reinterpret_cast<intptr_t>(ptr) % bytes) == 0;
62 void pinBuffer(void *pointer, std::size_t numBytes) noexcept
64     const char *errorMessage = "Could not register the host memory for page locking for GPU transfers.";
66     GMX_ASSERT(isAligned(pointer, PageAlignedAllocationPolicy::alignment()),
67                formatString("%s Host memory needs to be page aligned.", errorMessage).c_str());
69     ensureNoPendingCudaError(errorMessage);
70     cudaError_t stat = cudaHostRegister(pointer, numBytes, cudaHostRegisterDefault);
72     // These errors can only arise from a coding error somewhere.
73     GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue &&
74                        stat != cudaErrorNotSupported &&
75                        stat != cudaErrorHostMemoryAlreadyRegistered,
76                        formatString("%s %s: %s", errorMessage,
77                                     cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
79     // We always handle the error, but if it's a type we didn't expect
80     // (e.g. because CUDA changes the set of errors it returns) then
81     // we should get a descriptive assertion in Debug mode so we know
82     // to fix our expectations.
83     GMX_ASSERT(stat != cudaErrorMemoryAllocation,
84                formatString("%s %s: %s which was an unexpected error", errorMessage,
85                             cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
87     // It might be preferable to throw InternalError here, because the
88     // failing condition can only happen when GROMACS is used with a
89     // CUDA API that can return some other error code. But we can't
90     // engineer GROMACS to be forward-compatible with future CUDA
91     // versions, so if this proves to be a problem in practice, then
92     // GROMACS must be patched, or a supported CUDA version used.
93     GMX_RELEASE_ASSERT(stat == cudaSuccess,
94                        formatString("%s %s: %s", errorMessage,
95                                     cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
98 void unpinBuffer(void *pointer) noexcept
100     const char *errorMessage = "Could not unregister pinned host memory used for GPU transfers.";
102     GMX_ASSERT(pointer != nullptr,
103                formatString("%s pointer should not be nullptr when pinned.", errorMessage).c_str());
105     ensureNoPendingCudaError(errorMessage);
106     cudaError_t stat = cudaHostUnregister(pointer);
107     // These errors can only arise from a coding error somewhere.
108     GMX_RELEASE_ASSERT(stat != cudaErrorInvalidValue && stat != cudaErrorHostMemoryNotRegistered,
109                        formatString("%s %s: %s", errorMessage,
110                                     cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
111     // If there's an error whose type we didn't expect (e.g. because a
112     // future CUDA changes the set of errors it returns) then we
113     // should assert, because our code is wrong.
114     //
115     // The approach differs from that in pin() because we might
116     // unpin() from a destructor, in which case any attempt to throw
117     // an uncaught exception would anyway terminate the program. A
118     // release assertion is a better behaviour than that.
119     GMX_RELEASE_ASSERT(stat == cudaSuccess,
120                        formatString("%s %s: %s which was an unexpected error", errorMessage,
121                                     cudaGetErrorName(stat), cudaGetErrorString(stat)).c_str());
124 } // namespace gmx