2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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_SYCL_H
36 #define GMX_GPU_UTILS_DEVICEBUFFER_SYCL_H
38 /*! \libinternal \file
39 * \brief Implements the DeviceBuffer type and routines for SYCL.
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 Artem Zhmurov <zhmurov@gmail.com>
48 #include "gromacs/gpu_utils/device_context.h"
49 #include "gromacs/gpu_utils/device_stream.h"
50 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
51 #include "gromacs/gpu_utils/gpu_utils.h" //only for GpuApiCallBehavior
52 #include "gromacs/gpu_utils/gputraits_ocl.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>* gmx_unused buffer,
67 size_t gmx_unused numValues,
68 const DeviceContext& gmx_unused deviceContext)
74 * Frees a device-side buffer.
75 * This does not reset separately stored size/capacity integers,
76 * as this is planned to be a destructor of DeviceBuffer as a proper class,
77 * and no calls on \p buffer should be made afterwards.
79 * \param[in] buffer Pointer to the buffer to free.
81 template<typename DeviceBuffer>
82 void freeDeviceBuffer(DeviceBuffer* gmx_unused buffer)
88 * Performs the host-to-device data copy, synchronous or asynchronously on request.
90 * Note that synchronous copy will not synchronize the stream in case of zero \p numValues
91 * because of the early return.
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] deviceStream GPU stream to perform asynchronous copy in.
99 * \param[in] transferKind Copy type: synchronous or asynchronous.
100 * \param[out] timingEvent A pointer to the H2D copy timing event to be filled in.
101 * If the pointer is not null, the event can further be used
102 * to queue a wait for this operation or to query profiling information.
104 template<typename ValueType>
105 void copyToDeviceBuffer(DeviceBuffer<ValueType>* gmx_unused buffer,
106 const ValueType* gmx_unused hostBuffer,
107 size_t gmx_unused startingOffset,
108 size_t gmx_unused numValues,
109 const DeviceStream& gmx_unused deviceStream,
110 GpuApiCallBehavior gmx_unused transferKind,
111 CommandEvent* gmx_unused timingEvent)
117 * Performs the device-to-host data copy, synchronous or asynchronously on request.
119 * Note that synchronous copy will not synchronize the stream in case of zero \p numValues
120 * because of the early return.
122 * \tparam ValueType Raw value type of the \p buffer.
123 * \param[in,out] hostBuffer Pointer to the raw host-side memory, also typed \p ValueType
124 * \param[in] buffer Pointer to the device-side buffer
125 * \param[in] startingOffset Offset (in values) at the device-side buffer to copy from.
126 * \param[in] numValues Number of values to copy.
127 * \param[in] deviceStream GPU stream to perform asynchronous copy in.
128 * \param[in] transferKind Copy type: synchronous or asynchronous.
129 * \param[out] timingEvent A pointer to the H2D copy timing event to be filled in.
130 * If the pointer is not null, the event can further be used
131 * to queue a wait for this operation or to query profiling information.
133 template<typename ValueType>
134 void copyFromDeviceBuffer(ValueType* gmx_unused hostBuffer,
135 DeviceBuffer<ValueType>* gmx_unused buffer,
136 size_t gmx_unused startingOffset,
137 size_t gmx_unused numValues,
138 const DeviceStream& gmx_unused deviceStream,
139 GpuApiCallBehavior gmx_unused transferKind,
140 CommandEvent* gmx_unused timingEvent)
146 * Clears the device buffer asynchronously.
148 * \tparam ValueType Raw value type of the \p buffer.
149 * \param[in,out] buffer Pointer to the device-side buffer
150 * \param[in] startingOffset Offset (in values) at the device-side buffer to start clearing at.
151 * \param[in] numValues Number of values to clear.
152 * \param[in] deviceStream GPU stream.
154 template<typename ValueType>
155 void clearDeviceBufferAsync(DeviceBuffer<ValueType>* gmx_unused buffer,
156 size_t gmx_unused startingOffset,
157 size_t gmx_unused numValues,
158 const DeviceStream& gmx_unused deviceStream)
163 /*! \brief Check the validity of the device buffer.
165 * Checks if the buffer is not nullptr and if its allocation is big enough.
167 * \param[in] buffer Device buffer to be checked.
168 * \param[in] requiredSize Number of elements that the buffer will have to accommodate.
170 * \returns Whether the device buffer can be set.
173 static bool gmx_unused checkDeviceBuffer(DeviceBuffer<T> gmx_unused buffer, int gmx_unused requiredSize)
178 //! Device texture wrapper.
179 using DeviceTexture = void*;
181 /*! \brief Create a texture object for an array of type ValueType.
183 * Creates the device buffer and copies read-only data for an array of type ValueType.
185 * \todo Decide if using image2d is most efficient.
187 * \tparam ValueType Raw data type.
189 * \param[out] deviceBuffer Device buffer to store data in.
190 * \param[out] deviceTexture New texture object
191 * \param[in] hostBuffer Host buffer to get date from.
192 * \param[in] numValues Number of elements in the buffer.
193 * \param[in] deviceContext GPU device context.
195 template<typename ValueType>
196 void initParamLookupTable(DeviceBuffer<ValueType>* gmx_unused deviceBuffer,
197 DeviceTexture* gmx_unused deviceTexture,
198 const ValueType* gmx_unused hostBuffer,
199 int gmx_unused numValues,
200 const DeviceContext& gmx_unused deviceContext)
205 /*! \brief Release the OpenCL device buffer.
207 * \tparam ValueType Raw data type.
209 * \param[in,out] deviceBuffer Device buffer to store data in.
210 * \param[in] deviceTexture Reference to texture object
212 template<typename ValueType>
213 void destroyParamLookupTable(DeviceBuffer<ValueType>* gmx_unused deviceBuffer,
214 DeviceTexture& gmx_unused deviceTexture)
219 #endif // GMX_GPU_UTILS_DEVICEBUFFER_SYCL_H