blob: 7be65c894a76a95e7f05a25530625cb0ef5aaa32 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas4d0351c2019-04-03 15:11:16 +01002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_OPENCL_H
25#define ARM_COMPUTE_OPENCL_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Moritz Pflanzer725788e2017-07-07 15:35:56 +010027#include <string>
28#include <utility>
29
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030/* Configure the Khronos C++ wrapper to target OpenCL 1.2: */
Anthony Barbier340a2722018-01-03 16:21:54 +000031#ifndef ARM_COMPUTE_NO_EXCEPTIONS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#define CL_HPP_ENABLE_EXCEPTIONS
Anthony Barbier340a2722018-01-03 16:21:54 +000033#endif // ARM_COMPUTE_NO_EXCEPTIONS
Anthony Barbier8b2fdc92018-08-09 11:42:38 +010034#define CL_TARGET_OPENCL_VERSION 200
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#define CL_HPP_TARGET_OPENCL_VERSION 110
36#define CL_HPP_MINIMUM_OPENCL_VERSION 110
Anthony Barbier8b2fdc92018-08-09 11:42:38 +010037#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Weffc++"
39#pragma GCC diagnostic ignored "-Wignored-qualifiers"
Michalis Spyrou6bff1952019-10-02 17:22:11 +010040#pragma GCC diagnostic ignored "-Wunused-parameter"
Georgios Pinitas67d94d22018-10-09 18:48:37 +010041#if defined(__GNUG__) && __GNUG__ >= 8
42#pragma GCC diagnostic ignored "-Wcatch-value"
43#endif // defined(__GNUG__) && __GNUG__ >= 8
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044#include <CL/cl2.hpp>
Anthony Barbier8b2fdc92018-08-09 11:42:38 +010045#pragma GCC diagnostic pop
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
47namespace cl
48{
49static const NDRange Range_128_1 = NDRange(128, 1);
Moritz Pflanzer725788e2017-07-07 15:35:56 +010050} // namespace cl
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051
52namespace arm_compute
53{
Alex Gildayc357c472018-03-21 13:54:09 +000054/** Check if OpenCL is available.
55 *
56 * @return True if OpenCL is available.
57 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058bool opencl_is_available();
Moritz Pflanzer725788e2017-07-07 15:35:56 +010059
Alex Gildayc357c472018-03-21 13:54:09 +000060/** Class for loading OpenCL symbols. */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010061class CLSymbols final
62{
Pablo Tellodb8485a2019-09-24 11:03:47 +010063public:
Georgios Pinitas7ae80a92019-10-25 18:25:17 +010064 /** Default Constructor */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010065 CLSymbols() = default;
Georgios Pinitas7ae80a92019-10-25 18:25:17 +010066 /** Load OpenCL symbols from handle
67 *
68 * @param[in] handle Handle to load symbols from
69 */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010070 void load_symbols(void *handle);
Alex Gildayc357c472018-03-21 13:54:09 +000071 /** Get the static instance of CLSymbols.
72 *
73 * @return The static instance of CLSymbols.
74 */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010075 static CLSymbols &get();
Alex Gildayc357c472018-03-21 13:54:09 +000076 /** Load symbols from the given OpenCL library path.
77 *
78 * @param[in] library Path to the OpenCL library.
79 *
80 * @return True if loading the library is successful.
81 */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010082 bool load(const std::string &library);
Alex Gildayc357c472018-03-21 13:54:09 +000083 /** Load symbols from any of the default OpenCL library names.
84 *
85 * @return True if loading any library is successful.
86 */
Moritz Pflanzer725788e2017-07-07 15:35:56 +010087 bool load_default();
88
Anthony Barbier58c4ff12017-11-09 09:15:32 +000089#define DECLARE_FUNCTION_PTR(func_name) \
90 std::function<decltype(func_name)> func_name##_ptr = nullptr
Moritz Pflanzer725788e2017-07-07 15:35:56 +010091
Anthony Barbierb6eb3532018-08-08 13:20:04 +010092 DECLARE_FUNCTION_PTR(clCreateContext);
Anthony Barbiera9e15332017-12-22 16:37:30 +000093 DECLARE_FUNCTION_PTR(clCreateContextFromType);
94 DECLARE_FUNCTION_PTR(clCreateCommandQueue);
95 DECLARE_FUNCTION_PTR(clGetContextInfo);
Anthony Barbier58c4ff12017-11-09 09:15:32 +000096 DECLARE_FUNCTION_PTR(clBuildProgram);
97 DECLARE_FUNCTION_PTR(clEnqueueNDRangeKernel);
98 DECLARE_FUNCTION_PTR(clSetKernelArg);
99 DECLARE_FUNCTION_PTR(clReleaseKernel);
100 DECLARE_FUNCTION_PTR(clCreateProgramWithSource);
101 DECLARE_FUNCTION_PTR(clCreateBuffer);
102 DECLARE_FUNCTION_PTR(clRetainKernel);
103 DECLARE_FUNCTION_PTR(clCreateKernel);
104 DECLARE_FUNCTION_PTR(clGetProgramInfo);
105 DECLARE_FUNCTION_PTR(clFlush);
106 DECLARE_FUNCTION_PTR(clFinish);
107 DECLARE_FUNCTION_PTR(clReleaseProgram);
108 DECLARE_FUNCTION_PTR(clRetainContext);
109 DECLARE_FUNCTION_PTR(clCreateProgramWithBinary);
110 DECLARE_FUNCTION_PTR(clReleaseCommandQueue);
111 DECLARE_FUNCTION_PTR(clEnqueueMapBuffer);
112 DECLARE_FUNCTION_PTR(clRetainProgram);
113 DECLARE_FUNCTION_PTR(clGetProgramBuildInfo);
114 DECLARE_FUNCTION_PTR(clEnqueueReadBuffer);
115 DECLARE_FUNCTION_PTR(clEnqueueWriteBuffer);
116 DECLARE_FUNCTION_PTR(clReleaseEvent);
117 DECLARE_FUNCTION_PTR(clReleaseContext);
118 DECLARE_FUNCTION_PTR(clRetainCommandQueue);
119 DECLARE_FUNCTION_PTR(clEnqueueUnmapMemObject);
120 DECLARE_FUNCTION_PTR(clRetainMemObject);
121 DECLARE_FUNCTION_PTR(clReleaseMemObject);
122 DECLARE_FUNCTION_PTR(clGetDeviceInfo);
123 DECLARE_FUNCTION_PTR(clGetDeviceIDs);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000124 DECLARE_FUNCTION_PTR(clGetMemObjectInfo);
Anthony Barbier58c4ff12017-11-09 09:15:32 +0000125 DECLARE_FUNCTION_PTR(clRetainEvent);
126 DECLARE_FUNCTION_PTR(clGetPlatformIDs);
127 DECLARE_FUNCTION_PTR(clGetKernelWorkGroupInfo);
Gian Marco85e6f512018-02-01 16:57:48 +0000128 DECLARE_FUNCTION_PTR(clGetCommandQueueInfo);
129 DECLARE_FUNCTION_PTR(clGetKernelInfo);
130 DECLARE_FUNCTION_PTR(clGetEventProfilingInfo);
Pablo Telloe86a09f2018-01-11 15:44:48 +0000131 DECLARE_FUNCTION_PTR(clSVMAlloc);
132 DECLARE_FUNCTION_PTR(clSVMFree);
133 DECLARE_FUNCTION_PTR(clEnqueueSVMMap);
134 DECLARE_FUNCTION_PTR(clEnqueueSVMUnmap);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100135 DECLARE_FUNCTION_PTR(clEnqueueMarker);
136 DECLARE_FUNCTION_PTR(clWaitForEvents);
Anthony Barbier58c4ff12017-11-09 09:15:32 +0000137
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100138 // Third-party extensions
139 DECLARE_FUNCTION_PTR(clImportMemoryARM);
140
Anthony Barbier58c4ff12017-11-09 09:15:32 +0000141#undef DECLARE_FUNCTION_PTR
Moritz Pflanzer725788e2017-07-07 15:35:56 +0100142
143private:
144 std::pair<bool, bool> _loaded{ false, false };
145};
146} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000147#endif /* ARM_COMPUTE_OPENCL_H */