2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018, 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 /*! \libinternal \file
36 * \brief Declare utility routines for OpenCL
38 * \author Anca Hamuraru <anca@streamcomputing.eu>
41 #ifndef GMX_GPU_UTILS_OCLUTILS_H
42 #define GMX_GPU_UTILS_OCLUTILS_H
46 #include "gromacs/gpu_utils/gmxopencl.h"
47 #include "gromacs/utility/gmxassert.h"
49 enum class GpuApiCallBehavior;
51 /*! \brief OpenCL vendor IDs */
53 OCL_VENDOR_NVIDIA = 0,
60 * \brief OpenCL GPU device identificator
62 * An OpenCL device is identified by its ID.
63 * The platform ID is also included for caching reasons.
67 cl_platform_id ocl_platform_id; /**< Platform ID */
68 cl_device_id ocl_device_id; /**< Device ID */
72 * \brief OpenCL device information.
74 * The OpenCL device information is queried and set at detection and contains
75 * both information about the device/hardware returned by the runtime as well
76 * as additional data like support status.
78 struct gmx_device_info_t
80 ocl_gpu_id_t ocl_gpu_id; /**< device ID assigned at detection */
81 char device_name[256]; /**< device name */
82 char device_version[256]; /**< device version */
83 char device_vendor[256]; /**< device vendor */
84 int compute_units; /**< number of compute units */
85 int adress_bits; /**< number of adress bits the device is capable of */
86 int stat; /**< device status takes values of e_gpu_detect_res_t */
87 ocl_vendor_id_t vendor_e; /**< device vendor as defined by ocl_vendor_id_t */
91 * \brief OpenCL GPU runtime data
93 * The device runtime data is meant to hold objects associated with a GROMACS rank's
94 * (thread or process) use of a single device (multiple devices per rank is not
95 * implemented). These objects should be constructed at ther point where a device
96 * dets assigned to a rank and released at when this assignment is no longer valid
97 * (i.e. at cleanup in the current implementation).
100 struct gmx_device_runtime_data_t
102 cl_context context; /**< OpenCL context */
103 cl_program program; /**< OpenCL program */
106 /*! \brief Launches synchronous or asynchronous device to host memory copy.
108 * If copy_event is not NULL, on return it will contain an event object
109 * identifying this particular device to host operation. The event can further
110 * be used to queue a wait for this operation or to query profiling information.
112 int ocl_copy_D2H(void * h_dest, cl_mem d_src,
113 size_t offset, size_t bytes,
114 GpuApiCallBehavior transferKind,
115 cl_command_queue command_queue,
116 cl_event *copy_event);
119 /*! \brief Launches asynchronous device to host memory copy. */
120 int ocl_copy_D2H_async(void * h_dest, cl_mem d_src,
121 size_t offset, size_t bytes,
122 cl_command_queue command_queue,
123 cl_event *copy_event);
125 /*! \brief Launches synchronous or asynchronous host to device memory copy.
127 * If copy_event is not NULL, on return it will contain an event object
128 * identifying this particular host to device operation. The event can further
129 * be used to queue a wait for this operation or to query profiling information.
131 int ocl_copy_H2D(cl_mem d_dest, void* h_src,
132 size_t offset, size_t bytes,
133 GpuApiCallBehavior transferKind,
134 cl_command_queue command_queue,
135 cl_event *copy_event);
137 /*! \brief Launches asynchronous host to device memory copy. */
138 int ocl_copy_H2D_async(cl_mem d_dest, void * h_src,
139 size_t offset, size_t bytes,
140 cl_command_queue command_queue,
141 cl_event *copy_event);
143 /*! \brief Launches synchronous host to device memory copy. */
144 int ocl_copy_H2D_sync(cl_mem d_dest, void * h_src,
145 size_t offset, size_t bytes,
146 cl_command_queue command_queue);
148 /*! \brief Allocate host memory in malloc style */
149 void ocl_pmalloc(void **h_ptr, size_t nbytes);
151 /*! \brief Free host memory in malloc style */
152 void ocl_pfree(void *h_ptr);
154 /*! \brief Convert error code to diagnostic string */
155 std::string ocl_get_error_string(cl_int error);
157 /*! \brief Calls clFinish() in the stream \p s.
159 * \param[in] s stream to synchronize with
161 static inline void gpuStreamSynchronize(cl_command_queue s)
163 cl_int cl_error = clFinish(s);
164 GMX_RELEASE_ASSERT(CL_SUCCESS == cl_error,
165 ("Error caught during clFinish:" + ocl_get_error_string(cl_error)).c_str());
168 /*! \brief Pretend to synchronize an OpenCL stream (dummy implementation).
170 * \param[in] s queue to check
172 * \returns True if all tasks enqueued in the stream \p s (at the time of this call) have completed.
174 static inline bool haveStreamTasksCompleted(cl_command_queue gmx_unused s)
176 GMX_RELEASE_ASSERT(false, "haveStreamTasksCompleted is not implemented for OpenCL");
180 /*! \brief Free a device-side buffer.
181 * This does not reset separately stored size/capacity integers,
182 * as this is planned to be a destructor of DeviceBuffer as a proper class,
183 * and no calls on \p buffer should be made afterwards.
185 * \param[in] buffer Pointer to the buffer to free.
187 template <typename DeviceBuffer>
188 void freeDeviceBuffer(DeviceBuffer *buffer)
190 GMX_ASSERT(buffer, "needs a buffer pointer");
193 GMX_RELEASE_ASSERT(clReleaseMemObject(*buffer) == CL_SUCCESS, "clReleaseMemObject failed");