Compute Library
 23.08
CLKernelLibrary.cpp
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  */
25 #include "arm_compute/core/Error.h"
27 #include <algorithm>
28 #include <array>
29 #include <fstream>
30 #include <utility>
31 #include <vector>
32 namespace arm_compute
33 {
34 CLKernelLibrary::CLKernelLibrary()
35  : _compile_context()
36 {
37  opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built
38 }
39 CLKernelLibrary &CLKernelLibrary::get()
40 {
41  static CLKernelLibrary _kernel_library;
42  return _kernel_library;
43 }
44 Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set) const
45 {
46  const opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
47  const std::string program_name = klib.program_name(kernel_name);
48  auto program = klib.program(program_name);
49  const std::string &kernel_path = CLKernelLibrary::get().get_kernel_path();
50  return _compile_context.create_kernel(kernel_name, program_name, program.program, kernel_path, build_options_set, program.is_binary);
51 }
52 std::string CLKernelLibrary::get_program_name(const std::string &kernel_name) const
53 {
54  return opencl::ClKernelLibrary::get().program_name(kernel_name);
55 }
56 void CLKernelLibrary::init(std::string kernel_path, cl::Context context, cl::Device device)
57 {
58  _compile_context = CLCompileContext(context, device);
59  opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
60 }
61 void CLKernelLibrary::set_kernel_path(const std::string &kernel_path)
62 {
63  opencl::ClKernelLibrary::get().set_kernel_path(kernel_path);
64 }
66 {
67  return _compile_context.context();
68 }
69 const cl::Device &CLKernelLibrary::get_device()
70 {
71  return _compile_context.get_device();
72 }
73 void CLKernelLibrary::set_device(cl::Device device)
74 {
75  _compile_context.set_device(device);
76 }
77 void CLKernelLibrary::set_context(cl::Context context)
78 {
79  _compile_context.set_context(context);
80 }
81 std::string CLKernelLibrary::get_kernel_path()
82 {
83  return opencl::ClKernelLibrary::get().kernel_path();
84 }
85 void CLKernelLibrary::clear_programs_cache()
86 {
87  _compile_context.clear_programs_cache();
88 }
89 const std::map<std::string, cl::Program> &CLKernelLibrary::get_built_programs() const
90 {
91  return _compile_context.get_built_programs();
92 }
93 void CLKernelLibrary::add_built_program(const std::string &built_program_name, const cl::Program &program)
94 {
95  _compile_context.add_built_program(built_program_name, program);
96 }
98 {
99  return _compile_context.fp16_supported();
100 }
101 bool CLKernelLibrary::int64_base_atomics_supported() const
102 {
103  return _compile_context.int64_base_atomics_supported();
104 }
105 bool CLKernelLibrary::is_wbsm_supported()
106 {
107  return _compile_context.is_wbsm_supported();
108 }
109 std::pair<std::string, bool> CLKernelLibrary::get_program(const std::string &program_name) const
110 {
111  auto program_info = opencl::ClKernelLibrary::get().program(program_name);
112  return std::make_pair(std::move(program_info.program), program_info.is_binary);
113 }
114 size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
115 {
116  return _compile_context.max_local_workgroup_size(kernel);
117 }
118 cl::NDRange CLKernelLibrary::default_ndrange() const
119 {
120  return _compile_context.default_ndrange();
121 }
122 std::string CLKernelLibrary::get_device_version()
123 {
124  return _compile_context.get_device_version();
125 }
126 cl_uint CLKernelLibrary::get_num_compute_units()
127 {
128  return _compile_context.get_num_compute_units();
129 }
130 CLCompileContext &CLKernelLibrary::get_compile_context()
131 {
132  return _compile_context;
133 }
134 } // namespace arm_compute
arm_compute::opencl_is_available
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:203
arm_compute::opencl::ClKernelLibrary
ClKernelLibrary contains all the OpenCL kernels that are used throughout the library.
Definition: ClKernelLibrary.h:41
arm_compute::fp16_supported
bool fp16_supported(const cl::Device &device)
Helper function to check whether the cl_khr_fp16 extension is supported.
Definition: CLHelpers.cpp:236
arm_compute::opencl::ClKernelLibrary::program
ClProgramInfo program(const std::string &program_name) const
Gets the source of the selected program.
Definition: ClKernelLibrary.cpp:1041
Error.h
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
arm_compute::CLKernelLibrary
CLKernelLibrary class.
Definition: CLKernelLibrary.h:38
arm_compute::opencl::ClKernelLibrary::program_name
std::string program_name(const std::string &kernel_name) const
Returns the program name given a kernel name.
Definition: ClKernelLibrary.cpp:1015
ClKernelLibrary.h
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
arm_compute::create_kernel
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:404
arm_compute::Kernel
Kernel class.
Definition: CLCompileContext.h:162
arm_compute::test::validation::context
auto context
Definition: DirectConv2d.cpp:160
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:57