2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, 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.
35 #ifndef GMX_GPU_UTILS_DEVICEBUFFER_CUH
36 #define GMX_GPU_UTILS_DEVICEBUFFER_CUH
38 /*! \libinternal \file
39 * \brief Implements the DeviceBuffer type and routines for CUDA.
40 * Should only be included directly by the main DeviceBuffer file devicebuffer.h.
41 * TODO: the intent is for DeviceBuffer to become a class.
43 * \author Aleksei Iupinov <a.yupinov@gmail.com>
48 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
49 #include "gromacs/gpu_utils/gpu_utils.h" //only for GpuApiCallBehavior
50 #include "gromacs/gpu_utils/gputraits.cuh"
51 #include "gromacs/utility/gmxassert.h"
54 * Allocates a device-side buffer.
55 * It is currently a caller's responsibility to call it only on not-yet allocated buffers.
57 * \tparam ValueType Raw value type of the \p buffer.
58 * \param[in,out] buffer Pointer to the device-side buffer.
59 * \param[in] numValues Number of values to accomodate.
60 * \param[in] deviceContext The buffer's dummy device context - not managed explicitly in CUDA RT.
62 template<typename ValueType>
63 void allocateDeviceBuffer(DeviceBuffer<ValueType>* buffer, size_t numValues, DeviceContext /* deviceContext */)
65 GMX_ASSERT(buffer, "needs a buffer pointer");
66 cudaError_t stat = cudaMalloc((void**)buffer, numValues * sizeof(ValueType));
67 GMX_RELEASE_ASSERT(stat == cudaSuccess, "cudaMalloc failure");
71 * Frees a device-side buffer.
72 * This does not reset separately stored size/capacity integers,
73 * as this is planned to be a destructor of DeviceBuffer as a proper class,
74 * and no calls on \p buffer should be made afterwards.
76 * \param[in] buffer Pointer to the buffer to free.
78 template<typename DeviceBuffer>
79 void freeDeviceBuffer(DeviceBuffer* buffer)
81 GMX_ASSERT(buffer, "needs a buffer pointer");
84 GMX_RELEASE_ASSERT(cudaFree(*buffer) == cudaSuccess, "cudaFree failed");
89 * Performs the host-to-device data copy, synchronous or asynchronously on request.
91 * TODO: This is meant to gradually replace cu/ocl_copy_h2d.
93 * \tparam ValueType Raw value type of the \p buffer.
94 * \param[in,out] buffer Pointer to the device-side buffer
95 * \param[in] hostBuffer Pointer to the raw host-side memory, also typed \p ValueType
96 * \param[in] startingOffset Offset (in values) at the device-side buffer to copy into.
97 * \param[in] numValues Number of values to copy.
98 * \param[in] stream GPU stream to perform asynchronous copy in.
99 * \param[in] transferKind Copy type: synchronous or asynchronous.
100 * \param[out] timingEvent A dummy pointer to the H2D copy timing event to be filled in.
101 * Not used in CUDA implementation.
103 template<typename ValueType>
104 void copyToDeviceBuffer(DeviceBuffer<ValueType>* buffer,
105 const ValueType* hostBuffer,
106 size_t startingOffset,
108 CommandStream stream,
109 GpuApiCallBehavior transferKind,
110 CommandEvent* /*timingEvent*/)
114 return; // such calls are actually made with empty domains
116 GMX_ASSERT(buffer, "needs a buffer pointer");
117 GMX_ASSERT(hostBuffer, "needs a host buffer pointer");
119 const size_t bytes = numValues * sizeof(ValueType);
121 switch (transferKind)
123 case GpuApiCallBehavior::Async:
124 GMX_ASSERT(isHostMemoryPinned(hostBuffer),
125 "Source host buffer was not pinned for CUDA");
126 stat = cudaMemcpyAsync(*((ValueType**)buffer) + startingOffset, hostBuffer, bytes,
127 cudaMemcpyHostToDevice, stream);
128 GMX_RELEASE_ASSERT(stat == cudaSuccess, "Asynchronous H2D copy failed");
131 case GpuApiCallBehavior::Sync:
132 stat = cudaMemcpy(*((ValueType**)buffer) + startingOffset, hostBuffer, bytes,
133 cudaMemcpyHostToDevice);
134 GMX_RELEASE_ASSERT(stat == cudaSuccess, "Synchronous H2D copy failed");
143 * Performs the device-to-host data copy, synchronous or asynchronously on request.
145 * TODO: This is meant to gradually replace cu/ocl_copy_d2h.
147 * \tparam ValueType Raw value type of the \p buffer.
148 * \param[in,out] hostBuffer Pointer to the raw host-side memory, also typed \p ValueType
149 * \param[in] buffer Pointer to the device-side buffer
150 * \param[in] startingOffset Offset (in values) at the device-side buffer to copy from.
151 * \param[in] numValues Number of values to copy.
152 * \param[in] stream GPU stream to perform asynchronous copy in.
153 * \param[in] transferKind Copy type: synchronous or asynchronous.
154 * \param[out] timingEvent A dummy pointer to the H2D copy timing event to be filled in.
155 * Not used in CUDA implementation.
157 template<typename ValueType>
158 void copyFromDeviceBuffer(ValueType* hostBuffer,
159 DeviceBuffer<ValueType>* buffer,
160 size_t startingOffset,
162 CommandStream stream,
163 GpuApiCallBehavior transferKind,
164 CommandEvent* /*timingEvent*/)
166 GMX_ASSERT(buffer, "needs a buffer pointer");
167 GMX_ASSERT(hostBuffer, "needs a host buffer pointer");
170 const size_t bytes = numValues * sizeof(ValueType);
171 switch (transferKind)
173 case GpuApiCallBehavior::Async:
174 GMX_ASSERT(isHostMemoryPinned(hostBuffer),
175 "Destination host buffer was not pinned for CUDA");
176 stat = cudaMemcpyAsync(hostBuffer, *((ValueType**)buffer) + startingOffset, bytes,
177 cudaMemcpyDeviceToHost, stream);
178 GMX_RELEASE_ASSERT(stat == cudaSuccess, "Asynchronous D2H copy failed");
181 case GpuApiCallBehavior::Sync:
182 stat = cudaMemcpy(hostBuffer, *((ValueType**)buffer) + startingOffset, bytes,
183 cudaMemcpyDeviceToHost);
184 GMX_RELEASE_ASSERT(stat == cudaSuccess, "Synchronous D2H copy failed");
192 * Clears the device buffer asynchronously.
194 * \tparam ValueType Raw value type of the \p buffer.
195 * \param[in,out] buffer Pointer to the device-side buffer
196 * \param[in] startingOffset Offset (in values) at the device-side buffer to start clearing
197 * at. \param[in] numValues Number of values to clear. \param[in] stream GPU stream.
199 template<typename ValueType>
200 void clearDeviceBufferAsync(DeviceBuffer<ValueType>* buffer, size_t startingOffset, size_t numValues, CommandStream stream)
202 GMX_ASSERT(buffer, "needs a buffer pointer");
203 const size_t bytes = numValues * sizeof(ValueType);
204 const char pattern = 0;
206 cudaError_t stat = cudaMemsetAsync(*((ValueType**)buffer) + startingOffset, pattern, bytes, stream);
207 GMX_RELEASE_ASSERT(stat == cudaSuccess, "Couldn't clear the device buffer");