Compute Library
 21.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 }
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 {
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);
60 }
61 void CLKernelLibrary::set_kernel_path(const std::string &kernel_path)
62 {
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 }
78 {
79  _compile_context.set_context(context);
80 }
82 {
84 }
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 }
102 {
103  return _compile_context.int64_base_atomics_supported();
104 }
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 }
119 {
120  return _compile_context.default_ndrange();
121 }
123 {
124  return _compile_context.get_device_version();
125 }
127 {
128  return _compile_context.get_num_compute_units();
129 }
131 {
132  return _compile_context;
133 }
134 } // namespace arm_compute
void set_kernel_path(const std::string &kernel_path)
Sets the path that the kernels reside in.
void set_device(cl::Device device)
Sets the CL device for which the programs are created.
const cl::Device & get_device() const
Gets the CL device for which the programs are created.
cl::Context & context()
Accessor for the associated CL context.
ClKernelLibrary contains all the OpenCL kernels that are used throughout the library.
void add_built_program(const std::string &built_program_name, const cl::Program &program)
Add a new built program to the cache.
std::string get_device_version()
Return the device version.
std::pair< std::string, bool > get_program(const std::string &program_name) const
Gets the source of the selected program.
ClProgramInfo program(const std::string &program_name) const
Gets the source of the selected program.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
std::string program_name(const std::string &kernel_name) const
Returns the program name given a kernel name.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
CLCompileContext & get_compile_context()
Gets the compile context used.
void set_context(cl::Context context)
Sets the CL context used to create programs.
std::string get_device_version() const
Return the device version.
Copyright (c) 2017-2021 Arm Limited.
cl_uint get_num_compute_units()
Return the maximum number of compute units in the device.
const std::string & kernel_path() const
Gets the path that the kernels reside in.
size_t max_local_workgroup_size(const cl::Kernel &kernel) const
Find the maximum number of local work items in a workgroup can be supported for the kernel...
cl::NDRange default_ndrange() const
Return the default NDRange for the device.
static ClKernelLibrary & get()
Access the KernelLibrary singleton.
void clear_programs_cache()
Clear the library&#39;s cache of binary programs.
void set_device(cl::Device device)
Sets the CL device for which the programs are created.
cl_uint get_num_compute_units() const
Return the maximum number of compute units in the device.
Kernel create_kernel(const std::string &kernel_name, const std::set< std::string > &build_options_set={}) const
Creates a kernel from the kernel library.
cl::NDRange default_ndrange() const
Return the default NDRange for the device.
void init(std::string kernel_path, cl::Context context, cl::Device device)
Initialises the kernel library.
Kernel create_kernel(const std::string &kernel_name, const std::string &program_name, const std::string &program_source, const std::string &kernel_path, const StringSet &build_options_set, bool is_binary) const
Creates an OpenCL kernel.
std::string get_program_name(const std::string &kernel_name) const
Returns the program name given a kernel name.
CLCompileContext class.
void clear_programs_cache()
Clear the library&#39;s cache of binary programs.
bool int64_base_atomics_supported() const
Returns true if int64_base_atomics extension is supported by the CL device.
bool fp16_supported() const
Returns true if FP16 is supported by the CL device.
bool int64_base_atomics_supported() const
Returns true if int64_base_atomics extension is supported by the CL device.
const std::map< std::string, cl::Program > & get_built_programs() const
Access the cache of built OpenCL programs.
bool fp16_supported() const
Returns true if FP16 is supported by the CL device.
size_t max_local_workgroup_size(const cl::Kernel &kernel) const
Find the maximum number of local work items in a workgroup can be supported for the kernel...
CLKernelLibrary class.
void set_kernel_path(std::string kernel_path)
Sets the path that the kernels reside in.
std::string get_kernel_path()
Gets the path that the kernels reside in.
void add_built_program(const std::string &built_program_name, const cl::Program &program) const
Add a new built program to the cache.
void set_context(cl::Context context)
Sets the CL context used to create programs.
std::string kernel_name
const std::map< std::string, cl::Program > & get_built_programs() const
Access the cache of built OpenCL programs.
const cl::Device & get_device()
Gets the CL device for which the programs are created.
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154
cl::Context & context()
Accessor for the associated CL context.