Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / gpu_utils / cudautils.cu
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2014,2015,2016,2017 by the GROMACS development team.
5  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 #include "gmxpre.h"
38
39 #include "cudautils.cuh"
40
41 #include <cassert>
42 #include <cstdlib>
43
44 #include "gromacs/gpu_utils/cuda_arch_utils.cuh"
45 #include "gromacs/gpu_utils/gpu_utils.h"
46 #include "gromacs/utility/gmxassert.h"
47
48 /*** Generic CUDA data operation wrappers ***/
49
50 // TODO: template on transferKind to avoid runtime conditionals
51 int cu_copy_D2H(void* h_dest, void* d_src, size_t bytes, GpuApiCallBehavior transferKind, cudaStream_t s = nullptr)
52 {
53     cudaError_t stat;
54
55     if (h_dest == nullptr || d_src == nullptr || bytes == 0)
56     {
57         return -1;
58     }
59
60     switch (transferKind)
61     {
62         case GpuApiCallBehavior::Async:
63             GMX_ASSERT(isHostMemoryPinned(h_dest), "Destination buffer was not pinned for CUDA");
64             stat = cudaMemcpyAsync(h_dest, d_src, bytes, cudaMemcpyDeviceToHost, s);
65             CU_RET_ERR(stat, "DtoH cudaMemcpyAsync failed");
66             break;
67
68         case GpuApiCallBehavior::Sync:
69             stat = cudaMemcpy(h_dest, d_src, bytes, cudaMemcpyDeviceToHost);
70             CU_RET_ERR(stat, "DtoH cudaMemcpy failed");
71             break;
72
73         default: throw;
74     }
75
76     return 0;
77 }
78
79 int cu_copy_D2H_sync(void* h_dest, void* d_src, size_t bytes)
80 {
81     return cu_copy_D2H(h_dest, d_src, bytes, GpuApiCallBehavior::Sync);
82 }
83
84 /*!
85  *  The copy is launched in stream s or if not specified, in stream 0.
86  */
87 int cu_copy_D2H_async(void* h_dest, void* d_src, size_t bytes, cudaStream_t s = nullptr)
88 {
89     return cu_copy_D2H(h_dest, d_src, bytes, GpuApiCallBehavior::Async, s);
90 }
91
92 // TODO: template on transferKind to avoid runtime conditionals
93 int cu_copy_H2D(void* d_dest, const void* h_src, size_t bytes, GpuApiCallBehavior transferKind, cudaStream_t s = nullptr)
94 {
95     cudaError_t stat;
96
97     if (d_dest == nullptr || h_src == nullptr || bytes == 0)
98     {
99         return -1;
100     }
101
102     switch (transferKind)
103     {
104         case GpuApiCallBehavior::Async:
105             GMX_ASSERT(isHostMemoryPinned(h_src), "Source buffer was not pinned for CUDA");
106             stat = cudaMemcpyAsync(d_dest, h_src, bytes, cudaMemcpyHostToDevice, s);
107             CU_RET_ERR(stat, "HtoD cudaMemcpyAsync failed");
108             break;
109
110         case GpuApiCallBehavior::Sync:
111             stat = cudaMemcpy(d_dest, h_src, bytes, cudaMemcpyHostToDevice);
112             CU_RET_ERR(stat, "HtoD cudaMemcpy failed");
113             break;
114
115         default: throw;
116     }
117
118     return 0;
119 }
120
121 int cu_copy_H2D_sync(void* d_dest, const void* h_src, size_t bytes)
122 {
123     return cu_copy_H2D(d_dest, h_src, bytes, GpuApiCallBehavior::Sync);
124 }
125
126 /*!
127  *  The copy is launched in stream s or if not specified, in stream 0.
128  */
129 int cu_copy_H2D_async(void* d_dest, const void* h_src, size_t bytes, cudaStream_t s = nullptr)
130 {
131     return cu_copy_H2D(d_dest, h_src, bytes, GpuApiCallBehavior::Async, s);
132 }
133
134 /*! \brief Set up texture object for an array of type T.
135  *
136  * Set up texture object for an array of type T and bind it to the device memory
137  * \p d_ptr points to.
138  *
139  * \tparam[in] T        Raw data type
140  * \param[out] texObj   texture object to initialize
141  * \param[in]  d_ptr    pointer to device global memory to bind \p texObj to
142  * \param[in]  sizeInBytes  size of memory area to bind \p texObj to
143  */
144 template<typename T>
145 static void setup1DTexture(cudaTextureObject_t& texObj, void* d_ptr, size_t sizeInBytes)
146 {
147     assert(!c_disableCudaTextures);
148
149     cudaError_t      stat;
150     cudaResourceDesc rd;
151     cudaTextureDesc  td;
152
153     memset(&rd, 0, sizeof(rd));
154     rd.resType                = cudaResourceTypeLinear;
155     rd.res.linear.devPtr      = d_ptr;
156     rd.res.linear.desc        = cudaCreateChannelDesc<T>();
157     rd.res.linear.sizeInBytes = sizeInBytes;
158
159     memset(&td, 0, sizeof(td));
160     td.readMode = cudaReadModeElementType;
161     stat        = cudaCreateTextureObject(&texObj, &rd, &td, nullptr);
162     CU_RET_ERR(stat, "cudaCreateTextureObject failed");
163 }
164
165 template<typename T>
166 void initParamLookupTable(T*& d_ptr, cudaTextureObject_t& texObj, const T* h_ptr, int numElem)
167 {
168     const size_t sizeInBytes = numElem * sizeof(*d_ptr);
169     cudaError_t  stat        = cudaMalloc((void**)&d_ptr, sizeInBytes);
170     CU_RET_ERR(stat, "cudaMalloc failed in initParamLookupTable");
171     cu_copy_H2D_sync(d_ptr, (void*)h_ptr, sizeInBytes);
172
173     if (!c_disableCudaTextures)
174     {
175         setup1DTexture<T>(texObj, d_ptr, sizeInBytes);
176     }
177 }
178
179 template<typename T>
180 void destroyParamLookupTable(T* d_ptr, cudaTextureObject_t texObj)
181 {
182     if (!c_disableCudaTextures)
183     {
184         CU_RET_ERR(cudaDestroyTextureObject(texObj), "cudaDestroyTextureObject on texObj failed");
185     }
186     CU_RET_ERR(cudaFree(d_ptr), "cudaFree failed");
187 }
188
189 /*! \brief Add explicit instantiations of init/destroyParamLookupTable() here as needed.
190  * One should also verify that the result of cudaCreateChannelDesc<T>() during texture setup
191  * looks reasonable, when instantiating the templates for new types - just in case.
192  */
193 template void initParamLookupTable<float>(float*&, cudaTextureObject_t&, const float*, int);
194 template void destroyParamLookupTable<float>(float*, cudaTextureObject_t);
195 template void initParamLookupTable<int>(int*&, cudaTextureObject_t&, const int*, int);
196 template void destroyParamLookupTable<int>(int*, cudaTextureObject_t);