Compute Library
 22.11
ClKernelLibrary Class Referencefinal

ClKernelLibrary contains all the OpenCL kernels that are used throughout the library. More...

#include <ClKernelLibrary.h>

Data Structures

struct  ClProgramInfo
 Structure to encapsulte program related information. More...
 

Public Member Functions

void set_kernel_path (std::string kernel_path)
 Sets the path that the kernels reside in. More...
 
const std::string & kernel_path () const
 Gets the path that the kernels reside in. More...
 
ClProgramInfo program (const std::string &program_name) const
 Gets the source of the selected program. More...
 
std::string program_name (const std::string &kernel_name) const
 Returns the program name given a kernel name. More...
 

Static Public Member Functions

static ClKernelLibraryget ()
 Access the KernelLibrary singleton. More...
 

Detailed Description

ClKernelLibrary contains all the OpenCL kernels that are used throughout the library.

Note
Kernel library is a singleton to reduce memory requirements
Sole responsibility is just to provide access to the kernel string, does not perform any compilation and relevant tasks

Definition at line 41 of file ClKernelLibrary.h.

Member Function Documentation

◆ get()

ClKernelLibrary & get ( )
static

Access the KernelLibrary singleton.

Returns
The KernelLibrary instance

Definition at line 961 of file ClKernelLibrary.cpp.

Referenced by ClCompositeKernel::configure(), ClKernelRuntime::configure(), CLKernelLibrary::create_kernel(), arm_compute::create_kernel(), CLKernelLibrary::get_kernel_path(), CLKernelLibrary::get_program(), CLKernelLibrary::get_program_name(), CLKernelLibrary::init(), and CLKernelLibrary::set_kernel_path().

962 {
963  static ClKernelLibrary _kernel_library;
964  return _kernel_library;
965 }

◆ kernel_path()

const std::string & kernel_path ( ) const

Gets the path that the kernels reside in.

Definition at line 988 of file ClKernelLibrary.cpp.

Referenced by ClCompositeKernel::configure(), ClKernelRuntime::configure(), arm_compute::create_kernel(), and CLKernelLibrary::get_kernel_path().

989 {
990  return _kernel_path;
991 }

◆ program()

ClKernelLibrary::ClProgramInfo program ( const std::string &  program_name) const

Gets the source of the selected program.

Parameters
[in]program_nameProgram name
Returns
A pair with the source (false) or the binary (true), of the selected program

Definition at line 993 of file ClKernelLibrary.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, ARM_COMPUTE_ERROR_VAR, ClKernelLibrary::program_name(), and arm_compute::read_file().

Referenced by CLKernelLibrary::create_kernel(), arm_compute::create_kernel(), and CLKernelLibrary::get_program().

994 {
995 #ifdef EMBEDDED_KERNELS
996 #ifdef ARM_COMPUTE_COMPRESSED_KERNELS
997  const auto inflatted_program_source_it = _decompressed_source_map.find(program_name);
998  if(inflatted_program_source_it != _decompressed_source_map.end())
999  {
1000  return ClProgramInfo{ inflatted_program_source_it->second, false };
1001  }
1002 #endif /* ARM_COMPUTE_COMPRESSED_KERNELS */
1003 
1004  const auto program_source_it = _program_source_map.find(program_name);
1005  if(program_source_it == _program_source_map.end())
1006  {
1007  ARM_COMPUTE_ERROR_VAR("Embedded program for %s does not exist.", program_name.c_str());
1008  }
1009  std::string program_source = program_source_it->second;
1010 
1011 #ifdef ARM_COMPUTE_COMPRESSED_KERNELS
1012  std::string decompressed_program_source = decompress_zlib(decode_base64(program_source_it->second));
1013  ARM_COMPUTE_ERROR_ON_MSG(decompressed_program_source.empty(), "Cannot de-compress requested program");
1014  _decompressed_source_map.insert(std::make_pair(program_name, decompressed_program_source));
1015  program_source = std::move(decompressed_program_source);
1016 #endif /* ARM_COMPUTE_COMPRESSED_KERNELS */
1017 
1018  return ClProgramInfo{ program_source, false };
1019 #else /* EMBEDDED_KERNELS */
1020  // Check for binary
1021  std::string source_name = _kernel_path + program_name;
1022  std::string binary_name = source_name + "bin";
1023  std::string program_source{};
1024  bool is_binary = false;
1025 
1026  if(std::ifstream(binary_name).is_open())
1027  {
1028  program_source = read_file(binary_name, true);
1029  is_binary = true;
1030  }
1031  else if(std::ifstream(source_name).is_open())
1032  {
1033  program_source = read_file(source_name, false);
1034  }
1035  else
1036  {
1037  ARM_COMPUTE_ERROR_VAR("Kernel file %s does not exist.", source_name.c_str());
1038  }
1039 
1040  return ClProgramInfo{ program_source, is_binary };
1041 #endif /* EMBEDDED_KERNELS */
1042 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
std::string program_name(const std::string &kernel_name) const
Returns the program name given a kernel name.
std::string read_file(const std::string &filename, bool binary)
Load an entire file in memory.
Definition: Utils.cpp:38
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456

◆ program_name()

std::string program_name ( const std::string &  kernel_name) const

Returns the program name given a kernel name.

Returns
Program name

Definition at line 967 of file ClKernelLibrary.cpp.

References ARM_COMPUTE_ERROR_VAR.

Referenced by CLKernelLibrary::create_kernel(), arm_compute::create_kernel(), CLKernelLibrary::get_program_name(), and ClKernelLibrary::program().

968 {
969  // Find which program contains the kernel
970  auto kernel_program_it = _kernel_program_map.find(kernel_name);
971 
972  if(_kernel_program_map.end() == kernel_program_it)
973  {
974  ARM_COMPUTE_ERROR_VAR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
975  }
976 
977  const std::string program_name = kernel_program_it->second;
978 
979  return program_name;
980 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
std::string program_name(const std::string &kernel_name) const
Returns the program name given a kernel name.
std::string kernel_name

◆ set_kernel_path()

void set_kernel_path ( std::string  kernel_path)

Sets the path that the kernels reside in.

Parameters
[in]kernel_pathPath of the kernel

Definition at line 982 of file ClKernelLibrary.cpp.

Referenced by CLKernelLibrary::init(), and CLKernelLibrary::set_kernel_path().

983 {
984  _kernel_path = std::move(kernel_path);
985  _kernel_path += "/";
986 }
const std::string & kernel_path() const
Gets the path that the kernels reside in.

The documentation for this class was generated from the following files: