Implement OpenCL support
[alexxy/gromacs.git] / src / gromacs / gmxlib / ocl_tools / oclutils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015, 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
42 #ifndef GMX_GMXLIB_OCL_TOOLS_OCLUTILS_H
43 #define GMX_GMXLIB_OCL_TOOLS_OCLUTILS_H
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #ifdef __APPLE__
50 #    include <OpenCL/opencl.h>
51 #else
52 #    include <CL/opencl.h>
53 #endif
54
55 /*! \brief OpenCL vendor IDs */
56 typedef enum {
57     OCL_VENDOR_NVIDIA = 0,
58     OCL_VENDOR_AMD,
59     OCL_VENDOR_INTEL,
60     OCL_VENDOR_UNKNOWN
61 } ocl_vendor_id_t;
62
63 /*! \internal \brief OpenCL GPU device identificator
64  * An OpenCL device is identified by its ID.
65  * The platform ID is also included for caching reasons.
66  */
67 typedef struct
68 {
69     cl_platform_id      ocl_platform_id; /**< Platform ID */
70     cl_device_id        ocl_device_id;   /**< Device ID */
71 } ocl_gpu_id_t;
72
73 /*! \internal \brief OpenCL GPU information
74  *
75  * \todo Move context and program outside this data structure.
76  * They are specific to a certain usage of the device (e.g. with/without OpenGL
77  * interop) and do not provide general device information as the data structure
78  * name indicates.
79  *
80  * TODO Document fields
81  */
82 struct gmx_device_info_t
83 {
84     //! @cond Doxygen_Suppress
85     ocl_gpu_id_t        ocl_gpu_id;
86     char                device_name[256];
87     char                device_version[256];
88     char                device_vendor[256];
89     int                 compute_units;
90     int                 adress_bits;
91     int                 stat;
92     ocl_vendor_id_t     vendor_e;
93
94     cl_context          context;
95     cl_program          program;
96     //! @endcond Doxygen_Suppress
97
98 };
99
100 #if !defined(NDEBUG)
101 /* Debugger callable function that prints the name of a kernel function pointer */
102 cl_int dbg_ocl_kernel_name(const cl_kernel kernel);
103 cl_int dbg_ocl_kernel_name_address(void* kernel);
104 #endif
105
106
107 /*! \brief Launches asynchronous host to device memory copy. */
108 int ocl_copy_H2D_async(cl_mem d_dest, void * h_src,
109                        size_t offset, size_t bytes,
110                        cl_command_queue command_queue,
111                        cl_event *copy_event);
112
113 /*! \brief Launches asynchronous device to host memory copy. */
114 int ocl_copy_D2H_async(void * h_dest, cl_mem d_src,
115                        size_t offset, size_t bytes,
116                        cl_command_queue command_queue,
117                        cl_event *copy_event);
118
119 /*! \brief Launches synchronous host to device memory copy. */
120 int ocl_copy_H2D(cl_mem d_dest, void * h_src,
121                  size_t offset, size_t bytes,
122                  cl_command_queue command_queue);
123
124 /*! \brief Allocate host memory in malloc style */
125 void ocl_pmalloc(void **h_ptr, size_t nbytes);
126
127 /*! \brief Free host memory in malloc style */
128 void ocl_pfree(void *h_ptr);
129
130
131 #ifdef __cplusplus
132 }
133 #endif
134
135 #endif