Rename synchronous GPU transfer functions to match the asynchronous ones
[alexxy/gromacs.git] / src / gromacs / gpu_utils / oclutils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  *  \brief Declare utility routines for OpenCL
37  *
38  *  \author Anca Hamuraru <anca@streamcomputing.eu>
39  *  \inlibraryapi
40  */
41 #ifndef GMX_GPU_UTILS_OCLUTILS_H
42 #define GMX_GPU_UTILS_OCLUTILS_H
43
44 #include <string>
45
46 #include "gromacs/gpu_utils/gmxopencl.h"
47 #include "gromacs/utility/gmxassert.h"
48
49 /*! \brief OpenCL vendor IDs */
50 typedef enum {
51     OCL_VENDOR_NVIDIA = 0,
52     OCL_VENDOR_AMD,
53     OCL_VENDOR_INTEL,
54     OCL_VENDOR_UNKNOWN
55 } ocl_vendor_id_t;
56
57 /*! \internal
58  * \brief OpenCL GPU device identificator
59  *
60  * An OpenCL device is identified by its ID.
61  * The platform ID is also included for caching reasons.
62  */
63 typedef struct
64 {
65     cl_platform_id      ocl_platform_id; /**< Platform ID */
66     cl_device_id        ocl_device_id;   /**< Device ID */
67 } ocl_gpu_id_t;
68
69 /*! \internal
70  * \brief OpenCL device information.
71  *
72  * The OpenCL device information is queried and set at detection and contains
73  * both information about the device/hardware returned by the runtime as well
74  * as additional data like support status.
75  */
76 struct gmx_device_info_t
77 {
78     ocl_gpu_id_t        ocl_gpu_id;          /**< device ID assigned at detection   */
79     char                device_name[256];    /**< device name */
80     char                device_version[256]; /**< device version */
81     char                device_vendor[256];  /**< device vendor */
82     int                 compute_units;       /**< number of compute units */
83     int                 adress_bits;         /**< number of adress bits the device is capable of */
84     int                 stat;                /**< device status takes values of e_gpu_detect_res_t */
85     ocl_vendor_id_t     vendor_e;            /**< device vendor as defined by ocl_vendor_id_t */
86 };
87
88 /*! \internal
89  * \brief OpenCL GPU runtime data
90  *
91  * The device runtime data is meant to hold objects associated with a GROMACS rank's
92  * (thread or process) use of a single device (multiple devices per rank is not
93  * implemented). These objects should be constructed at ther point where a device
94  * dets assigned to a rank and released at when this assignment is no longer valid
95  * (i.e. at cleanup in the current implementation).
96  *
97  */
98 struct gmx_device_runtime_data_t
99 {
100     cl_context context; /**< OpenCL context */
101     cl_program program; /**< OpenCL program */
102 };
103
104
105 /*! \brief Launches asynchronous host to device memory copy. */
106 int ocl_copy_H2D_async(cl_mem d_dest, void * h_src,
107                        size_t offset, size_t bytes,
108                        cl_command_queue command_queue,
109                        cl_event *copy_event);
110
111 /*! \brief Launches asynchronous device to host memory copy. */
112 int ocl_copy_D2H_async(void * h_dest, cl_mem d_src,
113                        size_t offset, size_t bytes,
114                        cl_command_queue command_queue,
115                        cl_event *copy_event);
116
117 /*! \brief Launches synchronous host to device memory copy. */
118 int ocl_copy_H2D_sync(cl_mem d_dest, void * h_src,
119                       size_t offset, size_t bytes,
120                       cl_command_queue command_queue);
121
122 /*! \brief Allocate host memory in malloc style */
123 void ocl_pmalloc(void **h_ptr, size_t nbytes);
124
125 /*! \brief Free host memory in malloc style */
126 void ocl_pfree(void *h_ptr);
127
128 /*! \brief Convert error code to diagnostic string */
129 std::string ocl_get_error_string(cl_int error);
130
131 /*! \brief Calls clFinish() in the stream \p s.
132  *
133  * \param[in] s stream to synchronize with
134  */
135 static inline void gpuStreamSynchronize(cl_command_queue s)
136 {
137     cl_int cl_error = clFinish(s);
138     GMX_RELEASE_ASSERT(CL_SUCCESS == cl_error,
139                        ("Error caught during clFinish:" + ocl_get_error_string(cl_error)).c_str());
140 }
141
142 #endif