Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / gpu_utils / oclutils.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5  * Copyright (c) 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 /*! \internal \file
37  *  \brief Define utility routines for OpenCL
38  *
39  *  \author Anca Hamuraru <anca@streamcomputing.eu>
40  */
41 #include "gmxpre.h"
42
43 #include "oclutils.h"
44
45 #include <stdlib.h>
46
47 #include <cassert>
48 #include <cstdio>
49
50 #include <string>
51
52 #include "gromacs/gpu_utils/gpu_utils.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/smalloc.h"
55
56 int ocl_copy_H2D(cl_mem             d_dest,
57                  const void*        h_src,
58                  size_t             offset,
59                  size_t             bytes,
60                  GpuApiCallBehavior transferKind,
61                  cl_command_queue   command_queue,
62                  cl_event*          copy_event)
63 {
64     cl_int gmx_unused cl_error;
65
66     if (d_dest == nullptr || h_src == nullptr || bytes == 0)
67     {
68         return -1;
69     }
70
71     switch (transferKind)
72     {
73         case GpuApiCallBehavior::Async:
74             cl_error = clEnqueueWriteBuffer(command_queue, d_dest, CL_FALSE, offset, bytes, h_src,
75                                             0, nullptr, copy_event);
76             assert(cl_error == CL_SUCCESS);
77             // TODO: handle errors
78             break;
79
80         case GpuApiCallBehavior::Sync:
81             cl_error = clEnqueueWriteBuffer(command_queue, d_dest, CL_TRUE, offset, bytes, h_src, 0,
82                                             nullptr, copy_event);
83             assert(cl_error == CL_SUCCESS);
84             // TODO: handle errors
85             break;
86
87         default: throw;
88     }
89
90     return 0;
91 }
92
93 /*! \brief Launches asynchronous host to device memory copy.
94  *
95  *  If copy_event is not nullptr, on return it will contain an event object
96  *  identifying this particular host to device operation. The event can further
97  *  be used to queue a wait for this operation or to query profiling information.
98  */
99 int ocl_copy_H2D_async(cl_mem           d_dest,
100                        const void*      h_src,
101                        size_t           offset,
102                        size_t           bytes,
103                        cl_command_queue command_queue,
104                        cl_event*        copy_event)
105 {
106     return ocl_copy_H2D(d_dest, h_src, offset, bytes, GpuApiCallBehavior::Async, command_queue, copy_event);
107 }
108
109 /*! \brief Launches synchronous host to device memory copy.
110  */
111 int ocl_copy_H2D_sync(cl_mem d_dest, const void* h_src, size_t offset, size_t bytes, cl_command_queue command_queue)
112 {
113     return ocl_copy_H2D(d_dest, h_src, offset, bytes, GpuApiCallBehavior::Sync, command_queue, nullptr);
114 }
115
116 int ocl_copy_D2H(void*              h_dest,
117                  cl_mem             d_src,
118                  size_t             offset,
119                  size_t             bytes,
120                  GpuApiCallBehavior transferKind,
121                  cl_command_queue   command_queue,
122                  cl_event*          copy_event)
123 {
124     cl_int gmx_unused cl_error;
125
126     if (h_dest == nullptr || d_src == nullptr || bytes == 0)
127     {
128         return -1;
129     }
130
131     switch (transferKind)
132     {
133         case GpuApiCallBehavior::Async:
134             cl_error = clEnqueueReadBuffer(command_queue, d_src, CL_FALSE, offset, bytes, h_dest, 0,
135                                            nullptr, copy_event);
136             assert(cl_error == CL_SUCCESS);
137             // TODO: handle errors
138             break;
139
140         case GpuApiCallBehavior::Sync:
141             cl_error = clEnqueueReadBuffer(command_queue, d_src, CL_TRUE, offset, bytes, h_dest, 0,
142                                            nullptr, copy_event);
143             assert(cl_error == CL_SUCCESS);
144             // TODO: handle errors
145             break;
146
147         default: throw;
148     }
149
150     return 0;
151 }
152
153 /*! \brief Launches asynchronous device to host memory copy.
154  *
155  *  If copy_event is not nullptr, on return it will contain an event object
156  *  identifying this particular host to device operation. The event can further
157  *  be used to queue a wait for this operation or to query profiling information.
158  */
159 int ocl_copy_D2H_async(void*            h_dest,
160                        cl_mem           d_src,
161                        size_t           offset,
162                        size_t           bytes,
163                        cl_command_queue command_queue,
164                        cl_event*        copy_event)
165 {
166     return ocl_copy_D2H(h_dest, d_src, offset, bytes, GpuApiCallBehavior::Async, command_queue, copy_event);
167 }
168
169 /*! \brief \brief Allocates nbytes of host memory. Use ocl_free to free memory allocated with this function.
170  *
171  *  \todo
172  *  This function should allocate page-locked memory to help reduce D2H and H2D
173  *  transfer times, similar with pmalloc from pmalloc_cuda.cu.
174  *
175  * \param[in,out]    h_ptr   Pointer where to store the address of the newly allocated buffer.
176  * \param[in]        nbytes  Size in bytes of the buffer to be allocated.
177  */
178 void pmalloc(void** h_ptr, size_t nbytes)
179 {
180     /* Need a temporary type whose size is 1 byte, so that the
181      * implementation of snew_aligned can cope without issuing
182      * warnings. */
183     char** temporary = reinterpret_cast<char**>(h_ptr);
184
185     /* 16-byte alignment is required by the neighbour-searching code,
186      * because it uses four-wide SIMD for bounding-box calculation.
187      * However, when we organize using page-locked memory for
188      * device-host transfers, it will probably need to be aligned to a
189      * 4kb page, like CUDA does. */
190     snew_aligned(*temporary, nbytes, 16);
191 }
192
193 /*! \brief Frees memory allocated with pmalloc.
194  *
195  * \param[in]    h_ptr   Buffer allocated with pmalloc that needs to be freed.
196  */
197 void pfree(void* h_ptr)
198 {
199
200     if (h_ptr)
201     {
202         sfree_aligned(h_ptr);
203     }
204 }
205
206 /*! \brief Convert error code to diagnostic string */
207 std::string ocl_get_error_string(cl_int error)
208 {
209     switch (error)
210     {
211         // run-time and JIT compiler errors
212         case 0: return "CL_SUCCESS";
213         case -1: return "CL_DEVICE_NOT_FOUND";
214         case -2: return "CL_DEVICE_NOT_AVAILABLE";
215         case -3: return "CL_COMPILER_NOT_AVAILABLE";
216         case -4: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
217         case -5: return "CL_OUT_OF_RESOURCES";
218         case -6: return "CL_OUT_OF_HOST_MEMORY";
219         case -7: return "CL_PROFILING_INFO_NOT_AVAILABLE";
220         case -8: return "CL_MEM_COPY_OVERLAP";
221         case -9: return "CL_IMAGE_FORMAT_MISMATCH";
222         case -10: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
223         case -11: return "CL_BUILD_PROGRAM_FAILURE";
224         case -12: return "CL_MAP_FAILURE";
225         case -13: return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
226         case -14: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
227         case -15: return "CL_COMPILE_PROGRAM_FAILURE";
228         case -16: return "CL_LINKER_NOT_AVAILABLE";
229         case -17: return "CL_LINK_PROGRAM_FAILURE";
230         case -18: return "CL_DEVICE_PARTITION_FAILED";
231         case -19: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
232
233         // compile-time errors
234         case -30: return "CL_INVALID_VALUE";
235         case -31: return "CL_INVALID_DEVICE_TYPE";
236         case -32: return "CL_INVALID_PLATFORM";
237         case -33: return "CL_INVALID_DEVICE";
238         case -34: return "CL_INVALID_CONTEXT";
239         case -35: return "CL_INVALID_QUEUE_PROPERTIES";
240         case -36: return "CL_INVALID_COMMAND_QUEUE";
241         case -37: return "CL_INVALID_HOST_PTR";
242         case -38: return "CL_INVALID_MEM_OBJECT";
243         case -39: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
244         case -40: return "CL_INVALID_IMAGE_SIZE";
245         case -41: return "CL_INVALID_SAMPLER";
246         case -42: return "CL_INVALID_BINARY";
247         case -43: return "CL_INVALID_BUILD_OPTIONS";
248         case -44: return "CL_INVALID_PROGRAM";
249         case -45: return "CL_INVALID_PROGRAM_EXECUTABLE";
250         case -46: return "CL_INVALID_KERNEL_NAME";
251         case -47: return "CL_INVALID_KERNEL_DEFINITION";
252         case -48: return "CL_INVALID_KERNEL";
253         case -49: return "CL_INVALID_ARG_INDEX";
254         case -50: return "CL_INVALID_ARG_VALUE";
255         case -51: return "CL_INVALID_ARG_SIZE";
256         case -52: return "CL_INVALID_KERNEL_ARGS";
257         case -53: return "CL_INVALID_WORK_DIMENSION";
258         case -54: return "CL_INVALID_WORK_GROUP_SIZE";
259         case -55: return "CL_INVALID_WORK_ITEM_SIZE";
260         case -56: return "CL_INVALID_GLOBAL_OFFSET";
261         case -57: return "CL_INVALID_EVENT_WAIT_LIST";
262         case -58: return "CL_INVALID_EVENT";
263         case -59: return "CL_INVALID_OPERATION";
264         case -60: return "CL_INVALID_GL_OBJECT";
265         case -61: return "CL_INVALID_BUFFER_SIZE";
266         case -62: return "CL_INVALID_MIP_LEVEL";
267         case -63: return "CL_INVALID_GLOBAL_WORK_SIZE";
268         case -64: return "CL_INVALID_PROPERTY";
269         case -65: return "CL_INVALID_IMAGE_DESCRIPTOR";
270         case -66: return "CL_INVALID_COMPILER_OPTIONS";
271         case -67: return "CL_INVALID_LINKER_OPTIONS";
272         case -68: return "CL_INVALID_DEVICE_PARTITION_COUNT";
273
274         // extension errors
275         case -1000: return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR";
276         case -1001: return "CL_PLATFORM_NOT_FOUND_KHR";
277         case -1002: return "CL_INVALID_D3D10_DEVICE_KHR";
278         case -1003: return "CL_INVALID_D3D10_RESOURCE_KHR";
279         case -1004: return "CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR";
280         case -1005: return "CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR";
281         default: return "Unknown OpenCL error: " + std::to_string(static_cast<int32_t>(error));
282     }
283 }