Compute Library
 22.08
OpenCL.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2022 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 300
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/opencl.hpp> // include new hpp header instead of 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  * @param[in] use_loader Use symbol loader function loadOpenCLPointer.
80  *
81  * @return True if loading the library is successful.
82  */
83  bool load(const std::string &library, bool use_loader = false);
84  /** Load symbols from any of the default OpenCL library names.
85  *
86  * @return True if loading any library is successful.
87  */
88  bool load_default();
89 
90 #define DECLARE_FUNCTION_PTR(func_name) \
91  std::function<decltype(func_name)> func_name##_ptr = nullptr
92 
142 
143  // Third-party extensions
145 
146 #undef DECLARE_FUNCTION_PTR
147 
148 private:
149  std::pair<bool, bool> _loaded;
150 };
151 } // namespace arm_compute
152 #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:447
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:536
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:1012
cl_int clSetKernelExecInfo(cl_kernel kernel, cl_kernel_exec_info param_name, size_t param_value_size, const void *param_value)
Definition: OpenCL.cpp:1054
cl_command_queue clCreateCommandQueueWithProperties(cl_context context, cl_device_id device, const cl_queue_properties *properties, cl_int *errcode_ret)
Definition: OpenCL.cpp:331
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:475
cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms)
Definition: OpenCL.cpp:939
cl_int clRetainCommandQueue(cl_command_queue command_queue)
Definition: OpenCL.cpp:495
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:559
cl_int clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value)
Definition: OpenCL.cpp:429
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:1031
cl_int clRetainEvent(cl_event event)
Definition: OpenCL.cpp:907
Copyright (c) 2017-2022 Arm Limited.
cl_int clRetainProgram(cl_program program)
Definition: OpenCL.cpp:602
cl_int clRetainKernel(cl_kernel kernel)
Definition: OpenCL.cpp:779
void clSVMFree(cl_context context, void *svm_pointer)
Definition: OpenCL.cpp:286
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:921
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:658
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:871
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:739
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
cl_command_queue clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int *errcode_ret)
Definition: OpenCL.cpp:314
cl_int clFlush(cl_command_queue command_queue)
Definition: OpenCL.cpp:711
cl_int clReleaseCommandQueue(cl_command_queue command_queue)
Definition: OpenCL.cpp:644
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:616
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:257
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:406
cl_mem clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:793
cl_int clEnqueueMarker(cl_command_queue command_queue, cl_event *event)
Definition: OpenCL.cpp:212
#define DECLARE_FUNCTION_PTR(func_name)
Definition: OpenCL.h:90
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:368
cl_int clReleaseEvent(cl_event event)
Definition: OpenCL.cpp:522
cl_int clReleaseProgram(cl_program program)
Definition: OpenCL.cpp:697
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:974
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:242
cl_int clReleaseKernel(cl_kernel kernel)
Definition: OpenCL.cpp:839
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:296
void * clSVMAlloc(cl_context context, cl_svm_mem_flags_arm flags, size_t size, cl_uint alignment)
Definition: OpenCL.cpp:272
cl_program clCreateProgramWithSource(cl_context context, cl_uint count, const char **strings, const size_t *lengths, cl_int *errcode_ret)
Definition: OpenCL.cpp:816
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:889
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:954
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:348
cl_int clReleaseContext(cl_context context)
Definition: OpenCL.cpp:509
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:1072
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:993
cl_kernel clCreateKernel(cl_program program, const char *kernel_name, cl_int *errcode_ret)
Definition: OpenCL.cpp:758
cl_int clWaitForEvents(cl_uint num_events, const cl_event *event_list)
Definition: OpenCL.cpp:227
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:386
cl_int clReleaseMemObject(cl_mem memobj)
Definition: OpenCL.cpp:461
cl_int clRetainContext(cl_context context)
Definition: OpenCL.cpp:683
cl_int clFinish(cl_command_queue command_queue)
Definition: OpenCL.cpp:725
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:188
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:582
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:853