Fix more clang warnings
[gromacs.git] / src / gromacs / gpu_utils / devicebuffer_ocl.h
bloba8aa77b04624f33e29ffa64a73008e18e67215d3
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020, 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_OCL_H
36 #define GMX_GPU_UTILS_DEVICEBUFFER_OCL_H
38 /*! \libinternal \file
39 * \brief Implements the DeviceBuffer type and routines for OpenCL.
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>
45 * \inlibraryapi
48 #include "gromacs/gpu_utils/device_context.h"
49 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
50 #include "gromacs/gpu_utils/gpu_utils.h" //only for GpuApiCallBehavior
51 #include "gromacs/gpu_utils/gputraits_ocl.h"
52 #include "gromacs/gpu_utils/oclutils.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/stringutil.h"
56 /*! \libinternal \brief
57 * Allocates a device-side buffer.
58 * It is currently a caller's responsibility to call it only on not-yet allocated buffers.
60 * \tparam ValueType Raw value type of the \p buffer.
61 * \param[in,out] buffer Pointer to the device-side buffer.
62 * \param[in] numValues Number of values to accomodate.
63 * \param[in] deviceContext The buffer's device context-to-be.
65 template<typename ValueType>
66 void allocateDeviceBuffer(DeviceBuffer<ValueType>* buffer, size_t numValues, const DeviceContext& deviceContext)
68 GMX_ASSERT(buffer, "needs a buffer pointer");
69 void* hostPtr = nullptr;
70 cl_int clError;
71 *buffer = clCreateBuffer(deviceContext.context(), CL_MEM_READ_WRITE,
72 numValues * sizeof(ValueType), hostPtr, &clError);
73 GMX_RELEASE_ASSERT(clError == CL_SUCCESS,
74 gmx::formatString("clCreateBuffer failure (OpenCL error %d: %s)", clError,
75 ocl_get_error_string(clError).c_str())
76 .c_str());
79 /*! \brief
80 * Frees a device-side buffer.
81 * This does not reset separately stored size/capacity integers,
82 * as this is planned to be a destructor of DeviceBuffer as a proper class,
83 * and no calls on \p buffer should be made afterwards.
85 * \param[in] buffer Pointer to the buffer to free.
87 template<typename DeviceBuffer>
88 void freeDeviceBuffer(DeviceBuffer* buffer)
90 GMX_ASSERT(buffer, "needs a buffer pointer");
91 if (*buffer)
93 cl_int clError = clReleaseMemObject(*buffer);
94 GMX_RELEASE_ASSERT(clError == CL_SUCCESS,
95 gmx::formatString("clReleaseMemObject failed (OpenCL error %d: %s)",
96 clError, ocl_get_error_string(clError).c_str())
97 .c_str());
101 /*! \brief
102 * Performs the host-to-device data copy, synchronous or asynchronously on request.
104 * TODO: This is meant to gradually replace cu/ocl_copy_h2d.
106 * \tparam ValueType Raw value type of the \p buffer.
107 * \param[in,out] buffer Pointer to the device-side buffer
108 * \param[in] hostBuffer Pointer to the raw host-side memory, also typed \p ValueType
109 * \param[in] startingOffset Offset (in values) at the device-side buffer to copy into.
110 * \param[in] numValues Number of values to copy.
111 * \param[in] deviceStream GPU stream to perform asynchronous copy in.
112 * \param[in] transferKind Copy type: synchronous or asynchronous.
113 * \param[out] timingEvent A pointer to the H2D copy timing event to be filled in.
114 * If the pointer is not null, the event can further be used
115 * to queue a wait for this operation or to query profiling information.
117 template<typename ValueType>
118 void copyToDeviceBuffer(DeviceBuffer<ValueType>* buffer,
119 const ValueType* hostBuffer,
120 size_t startingOffset,
121 size_t numValues,
122 const DeviceStream& deviceStream,
123 GpuApiCallBehavior transferKind,
124 CommandEvent* timingEvent)
126 if (numValues == 0)
128 return; // such calls are actually made with empty domains
130 GMX_ASSERT(buffer, "needs a buffer pointer");
131 GMX_ASSERT(hostBuffer, "needs a host buffer pointer");
132 cl_int clError;
133 const size_t offset = startingOffset * sizeof(ValueType);
134 const size_t bytes = numValues * sizeof(ValueType);
135 switch (transferKind)
137 case GpuApiCallBehavior::Async:
138 clError = clEnqueueWriteBuffer(deviceStream.stream(), *buffer, CL_FALSE, offset, bytes,
139 hostBuffer, 0, nullptr, timingEvent);
140 GMX_RELEASE_ASSERT(
141 clError == CL_SUCCESS,
142 gmx::formatString("Asynchronous H2D copy failed (OpenCL error %d: %s)", clError,
143 ocl_get_error_string(clError).c_str())
144 .c_str());
145 break;
147 case GpuApiCallBehavior::Sync:
148 clError = clEnqueueWriteBuffer(deviceStream.stream(), *buffer, CL_TRUE, offset, bytes,
149 hostBuffer, 0, nullptr, timingEvent);
150 GMX_RELEASE_ASSERT(
151 clError == CL_SUCCESS,
152 gmx::formatString("Synchronous H2D copy failed (OpenCL error %d: %s)", clError,
153 ocl_get_error_string(clError).c_str())
154 .c_str());
155 break;
157 default: throw;
161 /*! \brief
162 * Performs the device-to-host data copy, synchronous or asynchronously on request.
164 * TODO: This is meant to gradually replace cu/ocl_copy_d2h.
166 * \tparam ValueType Raw value type of the \p buffer.
167 * \param[in,out] hostBuffer Pointer to the raw host-side memory, also typed \p ValueType
168 * \param[in] buffer Pointer to the device-side buffer
169 * \param[in] startingOffset Offset (in values) at the device-side buffer to copy from.
170 * \param[in] numValues Number of values to copy.
171 * \param[in] deviceStream GPU stream to perform asynchronous copy in.
172 * \param[in] transferKind Copy type: synchronous or asynchronous.
173 * \param[out] timingEvent A pointer to the H2D copy timing event to be filled in.
174 * If the pointer is not null, the event can further be used
175 * to queue a wait for this operation or to query profiling information.
177 template<typename ValueType>
178 void copyFromDeviceBuffer(ValueType* hostBuffer,
179 DeviceBuffer<ValueType>* buffer,
180 size_t startingOffset,
181 size_t numValues,
182 const DeviceStream& deviceStream,
183 GpuApiCallBehavior transferKind,
184 CommandEvent* timingEvent)
186 GMX_ASSERT(buffer, "needs a buffer pointer");
187 GMX_ASSERT(hostBuffer, "needs a host buffer pointer");
188 cl_int clError;
189 const size_t offset = startingOffset * sizeof(ValueType);
190 const size_t bytes = numValues * sizeof(ValueType);
191 switch (transferKind)
193 case GpuApiCallBehavior::Async:
194 clError = clEnqueueReadBuffer(deviceStream.stream(), *buffer, CL_FALSE, offset, bytes,
195 hostBuffer, 0, nullptr, timingEvent);
196 GMX_RELEASE_ASSERT(
197 clError == CL_SUCCESS,
198 gmx::formatString("Asynchronous D2H copy failed (OpenCL error %d: %s)", clError,
199 ocl_get_error_string(clError).c_str())
200 .c_str());
201 break;
203 case GpuApiCallBehavior::Sync:
204 clError = clEnqueueReadBuffer(deviceStream.stream(), *buffer, CL_TRUE, offset, bytes,
205 hostBuffer, 0, nullptr, timingEvent);
206 GMX_RELEASE_ASSERT(
207 clError == CL_SUCCESS,
208 gmx::formatString("Synchronous D2H copy failed (OpenCL error %d: %s)", clError,
209 ocl_get_error_string(clError).c_str())
210 .c_str());
211 break;
213 default: throw;
217 /*! \brief
218 * Clears the device buffer asynchronously.
220 * \tparam ValueType Raw value type of the \p buffer.
221 * \param[in,out] buffer Pointer to the device-side buffer
222 * \param[in] startingOffset Offset (in values) at the device-side buffer to start clearing at.
223 * \param[in] numValues Number of values to clear.
224 * \param[in] deviceStream GPU stream.
226 template<typename ValueType>
227 void clearDeviceBufferAsync(DeviceBuffer<ValueType>* buffer,
228 size_t startingOffset,
229 size_t numValues,
230 const DeviceStream& deviceStream)
232 GMX_ASSERT(buffer, "needs a buffer pointer");
233 const size_t offset = startingOffset * sizeof(ValueType);
234 const size_t bytes = numValues * sizeof(ValueType);
235 const int pattern = 0;
236 const cl_uint numWaitEvents = 0;
237 const cl_event* waitEvents = nullptr;
238 cl_event commandEvent;
239 cl_int clError = clEnqueueFillBuffer(deviceStream.stream(), *buffer, &pattern, sizeof(pattern),
240 offset, bytes, numWaitEvents, waitEvents, &commandEvent);
241 GMX_RELEASE_ASSERT(clError == CL_SUCCESS,
242 gmx::formatString("Couldn't clear the device buffer (OpenCL error %d: %s)",
243 clError, ocl_get_error_string(clError).c_str())
244 .c_str());
247 #if defined(__clang__)
248 # pragma clang diagnostic push
249 # pragma clang diagnostic ignored "-Wunused-template"
250 #endif
252 /*! \brief Check the validity of the device buffer.
254 * Checks if the buffer is not nullptr and if its allocation is big enough.
256 * \param[in] buffer Device buffer to be checked.
257 * \param[in] requiredSize Number of elements that the buffer will have to accommodate.
259 * \returns Whether the device buffer can be set.
261 template<typename T>
262 static bool checkDeviceBuffer(DeviceBuffer<T> buffer, int requiredSize)
264 size_t size;
265 int retval = clGetMemObjectInfo(buffer, CL_MEM_SIZE, sizeof(size), &size, nullptr);
266 GMX_ASSERT(retval == CL_SUCCESS,
267 gmx::formatString("clGetMemObjectInfo failed with error code #%d", retval).c_str());
268 GMX_ASSERT(static_cast<int>(size) >= requiredSize,
269 "Number of atoms in device buffer is smaller then required size.");
270 return retval == CL_SUCCESS && static_cast<int>(size) >= requiredSize;
273 #if defined(__clang__)
274 # pragma clang diagnostic pop
275 #endif
277 #endif