2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014,2015,2016,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.
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.
38 #include "cudautils.cuh"
43 #include "gromacs/gpu_utils/cuda_arch_utils.cuh"
44 #include "gromacs/utility/smalloc.h"
46 /*** Generic CUDA data operation wrappers ***/
48 /*! Launches synchronous or asynchronous host to device memory copy.
50 * The copy is launched in stream s or if not specified, in stream 0.
52 static int cu_copy_D2H_generic(void * h_dest, void * d_src, size_t bytes,
53 bool bAsync = false, cudaStream_t s = 0)
57 if (h_dest == NULL || d_src == NULL || bytes == 0)
64 stat = cudaMemcpyAsync(h_dest, d_src, bytes, cudaMemcpyDeviceToHost, s);
65 CU_RET_ERR(stat, "DtoH cudaMemcpyAsync failed");
70 stat = cudaMemcpy(h_dest, d_src, bytes, cudaMemcpyDeviceToHost);
71 CU_RET_ERR(stat, "DtoH cudaMemcpy failed");
77 int cu_copy_D2H(void * h_dest, void * d_src, size_t bytes)
79 return cu_copy_D2H_generic(h_dest, d_src, bytes, false);
83 * The copy is launched in stream s or if not specified, in stream 0.
85 int cu_copy_D2H_async(void * h_dest, void * d_src, size_t bytes, cudaStream_t s = 0)
87 return cu_copy_D2H_generic(h_dest, d_src, bytes, true, s);
90 /*! Launches synchronous or asynchronous device to host memory copy.
92 * The copy is launched in stream s or if not specified, in stream 0.
94 static int cu_copy_H2D_generic(void * d_dest, void * h_src, size_t bytes,
95 bool bAsync = false, cudaStream_t s = 0)
99 if (d_dest == NULL || h_src == NULL || bytes == 0)
106 stat = cudaMemcpyAsync(d_dest, h_src, bytes, cudaMemcpyHostToDevice, s);
107 CU_RET_ERR(stat, "HtoD cudaMemcpyAsync failed");
111 stat = cudaMemcpy(d_dest, h_src, bytes, cudaMemcpyHostToDevice);
112 CU_RET_ERR(stat, "HtoD cudaMemcpy failed");
118 int cu_copy_H2D(void * d_dest, void * h_src, size_t bytes)
120 return cu_copy_H2D_generic(d_dest, h_src, bytes, false);
124 * The copy is launched in stream s or if not specified, in stream 0.
126 int cu_copy_H2D_async(void * d_dest, void * h_src, size_t bytes, cudaStream_t s = 0)
128 return cu_copy_H2D_generic(d_dest, h_src, bytes, true, s);
131 float cu_event_elapsed(cudaEvent_t start, cudaEvent_t end)
136 stat = cudaEventElapsedTime(&t, start, end);
137 CU_RET_ERR(stat, "cudaEventElapsedTime failed in cu_event_elapsed");
142 int cu_wait_event(cudaEvent_t e)
146 s = cudaEventSynchronize(e);
147 CU_RET_ERR(s, "cudaEventSynchronize failed in cu_wait_event");
153 * If time != NULL it also calculates the time elapsed between start and end and
154 * return this is milliseconds.
156 int cu_wait_event_time(cudaEvent_t end, cudaEvent_t start, float *time)
160 s = cudaEventSynchronize(end);
161 CU_RET_ERR(s, "cudaEventSynchronize failed in cu_wait_event");
165 *time = cu_event_elapsed(start, end);
171 /**** Operation on buffered arrays (arrays with "over-allocation" in gmx wording) *****/
174 * If the pointers to the size variables are NULL no resetting happens.
176 void cu_free_buffered(void *d_ptr, int *n, int *nalloc)
182 stat = cudaFree(d_ptr);
183 CU_RET_ERR(stat, "cudaFree failed");
198 * Reallocation of the memory pointed by d_ptr and copying of the data from
199 * the location pointed by h_src host-side pointer is done. Allocation is
200 * buffered and therefore freeing is only needed if the previously allocated
201 * space is not enough.
202 * The H2D copy is launched in stream s and can be done synchronously or
203 * asynchronously (the default is the latter).
205 void cu_realloc_buffered(void **d_dest, void *h_src,
207 int *curr_size, int *curr_alloc_size,
214 if (d_dest == NULL || req_size < 0)
219 /* reallocate only if the data does not fit = allocation size is smaller
220 than the current requested size */
221 if (req_size > *curr_alloc_size)
223 /* only free if the array has already been initialized */
224 if (*curr_alloc_size >= 0)
226 cu_free_buffered(*d_dest, curr_size, curr_alloc_size);
229 *curr_alloc_size = over_alloc_large(req_size);
231 stat = cudaMalloc(d_dest, *curr_alloc_size * type_size);
232 CU_RET_ERR(stat, "cudaMalloc failed in cu_free_buffered");
235 /* size could have changed without actual reallocation */
236 *curr_size = req_size;
238 /* upload to device */
243 cu_copy_H2D_async(*d_dest, h_src, *curr_size * type_size, s);
247 cu_copy_H2D(*d_dest, h_src, *curr_size * type_size);
252 bool use_texobj(const gmx_device_info_t *dev_info)
254 assert(!c_disableCudaTextures);
255 /* Only device CC >= 3.0 (Kepler and later) support texture objects */
256 return (dev_info->prop.major >= 3);
259 /*! \brief Set up texture object for an array of type T.
261 * Set up texture object for an array of type T and bind it to the device memory
262 * \p d_ptr points to.
264 * \tparam[in] T Raw data type
265 * \param[out] texObj texture object to initialize
266 * \param[in] d_ptr pointer to device global memory to bind \p texObj to
267 * \param[in] sizeInBytes size of memory area to bind \p texObj to
269 template <typename T>
270 static void setup1DTexture(cudaTextureObject_t &texObj,
274 assert(!c_disableCudaTextures);
280 memset(&rd, 0, sizeof(rd));
281 rd.resType = cudaResourceTypeLinear;
282 rd.res.linear.devPtr = d_ptr;
283 rd.res.linear.desc = cudaCreateChannelDesc<T>();
284 rd.res.linear.sizeInBytes = sizeInBytes;
286 memset(&td, 0, sizeof(td));
287 td.readMode = cudaReadModeElementType;
288 stat = cudaCreateTextureObject(&texObj, &rd, &td, NULL);
289 CU_RET_ERR(stat, "cudaCreateTextureObject failed");
292 /*! \brief Set up texture reference for an array of type T.
294 * Set up texture object for an array of type T and bind it to the device memory
295 * \p d_ptr points to.
297 * \tparam[in] T Raw data type
298 * \param[out] texObj texture reference to initialize
299 * \param[in] d_ptr pointer to device global memory to bind \p texObj to
300 * \param[in] sizeInBytes size of memory area to bind \p texObj to
302 template <typename T>
303 static void setup1DTexture(const struct texture<T, 1, cudaReadModeElementType> *texRef,
307 assert(!c_disableCudaTextures);
310 cudaChannelFormatDesc cd;
312 cd = cudaCreateChannelDesc<T>();
313 stat = cudaBindTexture(nullptr, texRef, d_ptr, &cd, sizeInBytes);
314 CU_RET_ERR(stat, "cudaBindTexture failed");
317 template <typename T>
318 void initParamLookupTable(T * &d_ptr,
319 cudaTextureObject_t &texObj,
320 const struct texture<T, 1, cudaReadModeElementType> *texRef,
323 const gmx_device_info_t *devInfo)
325 const size_t sizeInBytes = numElem * sizeof(*d_ptr);
326 cudaError_t stat = cudaMalloc((void **)&d_ptr, sizeInBytes);
327 CU_RET_ERR(stat, "cudaMalloc failed in initParamLookupTable");
328 cu_copy_H2D(d_ptr, (void *)h_ptr, sizeInBytes);
330 if (!c_disableCudaTextures)
332 if (use_texobj(devInfo))
334 setup1DTexture<T>(texObj, d_ptr, sizeInBytes);
338 setup1DTexture<T>(texRef, d_ptr, sizeInBytes);
343 //! Add explicit instantiations of initParamLookupTable() here as needed
344 template void initParamLookupTable<float>(float * &, cudaTextureObject_t &, const texture<float, 1, cudaReadModeElementType> *, const float *, int, const gmx_device_info_t *);