Compute Library
 21.05
OpenCL.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 Arm Limited.
3  *
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  */
24 #ifndef ARM_COMPUTE_OPENCL_H
25 #define ARM_COMPUTE_OPENCL_H
26 
27 #include <string>
28 #include <utility>
29 
30 /* Configure the Khronos C++ wrapper to target OpenCL 1.2: */
31 #ifndef ARM_COMPUTE_NO_EXCEPTIONS
32 #define CL_HPP_ENABLE_EXCEPTIONS
33 #endif // ARM_COMPUTE_NO_EXCEPTIONS
34 #define CL_TARGET_OPENCL_VERSION 200
35 #define CL_HPP_TARGET_OPENCL_VERSION 110
36 #define CL_HPP_MINIMUM_OPENCL_VERSION 110
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Weffc++"
39 #pragma GCC diagnostic ignored "-Wignored-qualifiers"
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 #if defined(__GNUG__) && __GNUG__ >= 8
42 #pragma GCC diagnostic ignored "-Wcatch-value"
43 #endif // defined(__GNUG__) && __GNUG__ >= 8
44 #include <CL/cl2.hpp>
45 #pragma GCC diagnostic pop
46 
47 namespace cl
48 {
49 static const NDRange Range_128_1 = NDRange(128, 1);
50 } // namespace cl
51 
52 namespace arm_compute
53 {
54 /** Check if OpenCL is available.
55  *
56  * @return True if OpenCL is available.
57  */
58 bool opencl_is_available();
59 
60 /** Class for loading OpenCL symbols. */
61 class CLSymbols final
62 {
63 public:
64  /** Default Constructor */
65  CLSymbols() noexcept(false);
66  /** Load OpenCL symbols from handle
67  *
68  * @param[in] handle Handle to load symbols from
69  */
70  void load_symbols(void *handle);
71  /** Get the static instance of CLSymbols.
72  *
73  * @return The static instance of CLSymbols.
74  */
75  static CLSymbols &get();
76  /** 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  */
82  bool load(const std::string &library);
83  /** Load symbols from any of the default OpenCL library names.
84  *
85  * @return True if loading any library is successful.
86  */
87  bool load_default();
88 
89 #define DECLARE_FUNCTION_PTR(func_name) \
90  std::function<decltype(func_name)> func_name##_ptr = nullptr
91 
141 
142  // Third-party extensions
144 
145 #undef DECLARE_FUNCTION_PTR
146 
147 private:
148  std::pair<bool, bool> _loaded;
149 };
150 } // namespace arm_compute
151 #endif /* ARM_COMPUTE_OPENCL_H */
Class for loading OpenCL symbols.
Definition: OpenCL.h:61
Definition: ICLTensor.h:33
cl_int clRetainMemObject(cl_mem memobj)
Definition: OpenCL.cpp:413
cl_int clEnqueueWriteBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, size_t size, const void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:502
cl_int clGetEventProfilingInfo(cl_event event, cl_profiling_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:978
cl_int clSetKernelExecInfo(cl_kernel kernel, cl_kernel_exec_info param_name, size_t param_value_size, const void *param_value)
Definition: OpenCL.cpp:1020
cl_command_queue clCreateCommandQueueWithProperties(cl_context context, cl_device_id device, const cl_queue_properties *properties, cl_int *errcode_ret)
Definition: OpenCL.cpp:297
cl_int clEnqueueUnmapMemObject(cl_command_queue command_queue, cl_mem memobj, void *mapped_ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:441
cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms)
Definition: OpenCL.cpp:905
cl_int clRetainCommandQueue(cl_command_queue command_queue)
Definition: OpenCL.cpp:461
cl_int clEnqueueReadBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:525
cl_int clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value)
Definition: OpenCL.cpp:395
cl_mem clCreateImage(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, const cl_image_desc *image_desc, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:997
cl_int clRetainEvent(cl_event event)
Definition: OpenCL.cpp:873
Copyright (c) 2017-2021 Arm Limited.
cl_int clRetainProgram(cl_program program)
Definition: OpenCL.cpp:568
cl_int clRetainKernel(cl_kernel kernel)
Definition: OpenCL.cpp:745
void clSVMFree(cl_context context, void *svm_pointer)
Definition: OpenCL.cpp:252
cl_int clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:887
cl_program clCreateProgramWithBinary(cl_context context, cl_uint num_devices, const cl_device_id *device_list, const size_t *lengths, const unsigned char **binaries, cl_int *binary_status, cl_int *errcode_ret)
Definition: OpenCL.cpp:624
bool load(const std::string &library)
Load symbols from the given OpenCL library path.
Definition: OpenCL.cpp:76
cl_int clGetDeviceInfo(cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:837
CLSymbols() noexcept(false)
Default Constructor.
Definition: OpenCL.cpp:37
cl_int clGetProgramInfo(cl_program program, cl_program_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:705
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:75
cl_command_queue clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int *errcode_ret)
Definition: OpenCL.cpp:280
cl_int clFlush(cl_command_queue command_queue)
Definition: OpenCL.cpp:677
cl_int clReleaseCommandQueue(cl_command_queue command_queue)
Definition: OpenCL.cpp:610
bool load_default()
Load symbols from any of the default OpenCL library names.
Definition: OpenCL.cpp:51
void * clEnqueueMapBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_map, cl_map_flags map_flags, size_t offset, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event, cl_int *errcode_ret)
Definition: OpenCL.cpp:582
cl_int clEnqueueSVMUnmap(cl_command_queue command_queue, void *svm_ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:223
cl_int clEnqueueNDRangeKernel(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *global_work_offset, const size_t *global_work_size, const size_t *local_work_size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:372
cl_mem clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:759
cl_int clEnqueueMarker(cl_command_queue command_queue, cl_event *event)
Definition: OpenCL.cpp:178
#define DECLARE_FUNCTION_PTR(func_name)
Definition: OpenCL.h:89
cl_context clCreateContextFromType(const cl_context_properties *properties, cl_device_type device_type, void(*pfn_notify)(const char *, const void *, size_t, void *), void *user_data, cl_int *errcode_ret)
Definition: OpenCL.cpp:334
cl_int clReleaseEvent(cl_event event)
Definition: OpenCL.cpp:488
cl_int clReleaseProgram(cl_program program)
Definition: OpenCL.cpp:663
cl_int clGetCommandQueueInfo(cl_command_queue command_queue, cl_command_queue_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:940
cl_int clEnqueueSVMMap(cl_command_queue command_queue, cl_bool blocking_map, cl_map_flags flags, void *svm_ptr, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:208
cl_int clReleaseKernel(cl_kernel kernel)
Definition: OpenCL.cpp:805
cl_int clGetContextInfo(cl_context context, cl_context_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:262
void * clSVMAlloc(cl_context context, cl_svm_mem_flags_arm flags, size_t size, cl_uint alignment)
Definition: OpenCL.cpp:238
cl_program clCreateProgramWithSource(cl_context context, cl_uint count, const char **strings, const size_t *lengths, cl_int *errcode_ret)
Definition: OpenCL.cpp:782
cl_int clGetMemObjectInfo(cl_mem memobj, cl_mem_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:855
cl_int clGetKernelWorkGroupInfo(cl_kernel kernel, cl_device_id device, cl_kernel_work_group_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:920
cl_context clCreateContext(const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *devices, void(*pfn_notify)(const char *, const void *, size_t, void *), void *user_data, cl_int *errcode_ret)
Definition: OpenCL.cpp:314
cl_int clReleaseContext(cl_context context)
Definition: OpenCL.cpp:475
cl_mem clImportMemoryARM(cl_context context, cl_mem_flags flags, const cl_import_properties_arm *properties, void *memory, size_t size, cl_int *errcode_ret)
Definition: OpenCL.cpp:1038
cl_int clGetKernelInfo(cl_kernel kernel, cl_kernel_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:959
void load_symbols(void *handle)
Load OpenCL symbols from handle.
cl_kernel clCreateKernel(cl_program program, const char *kernel_name, cl_int *errcode_ret)
Definition: OpenCL.cpp:724
cl_int clWaitForEvents(cl_uint num_events, const cl_event *event_list)
Definition: OpenCL.cpp:193
static CLSymbols & get()
Get the static instance of CLSymbols.
Definition: OpenCL.cpp:45
cl_int clBuildProgram(cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void(CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data)
Definition: OpenCL.cpp:352
cl_int clReleaseMemObject(cl_mem memobj)
Definition: OpenCL.cpp:427
cl_int clRetainContext(cl_context context)
Definition: OpenCL.cpp:649
cl_int clFinish(cl_command_queue command_queue)
Definition: OpenCL.cpp:691
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154
cl_int clGetProgramBuildInfo(cl_program program, cl_device_id device, cl_program_build_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: OpenCL.cpp:548
cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices)
Definition: OpenCL.cpp:819