Compute Library
 22.02
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 920 of file ClKernelLibrary.cpp.

Referenced by 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().

921 {
922  static ClKernelLibrary _kernel_library;
923  return _kernel_library;
924 }

◆ kernel_path()

const std::string & kernel_path ( ) const

Gets the path that the kernels reside in.

Definition at line 947 of file ClKernelLibrary.cpp.

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

948 {
949  return _kernel_path;
950 }

◆ 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 952 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().

953 {
954 #ifdef EMBEDDED_KERNELS
955 #ifdef ARM_COMPUTE_COMPRESSED_KERNELS
956  const auto inflatted_program_source_it = _decompressed_source_map.find(program_name);
957  if(inflatted_program_source_it != _decompressed_source_map.end())
958  {
959  return ClProgramInfo{ inflatted_program_source_it->second, false };
960  }
961 #endif /* ARM_COMPUTE_COMPRESSED_KERNELS */
962 
963  const auto program_source_it = _program_source_map.find(program_name);
964  if(program_source_it == _program_source_map.end())
965  {
966  ARM_COMPUTE_ERROR_VAR("Embedded program for %s does not exist.", program_name.c_str());
967  }
968  std::string program_source = program_source_it->second;
969 
970 #ifdef ARM_COMPUTE_COMPRESSED_KERNELS
971  std::string decompressed_program_source = decompress_zlib(decode_base64(program_source_it->second));
972  ARM_COMPUTE_ERROR_ON_MSG(decompressed_program_source.empty(), "Cannot de-compress requested program");
973  _decompressed_source_map.insert(std::make_pair(program_name, decompressed_program_source));
974  program_source = std::move(decompressed_program_source);
975 #endif /* ARM_COMPUTE_COMPRESSED_KERNELS */
976 
977  return ClProgramInfo{ program_source, false };
978 #else /* EMBEDDED_KERNELS */
979  // Check for binary
980  std::string source_name = _kernel_path + program_name;
981  std::string binary_name = source_name + "bin";
982  std::string program_source{};
983  bool is_binary = false;
984 
985  if(std::ifstream(binary_name).is_open())
986  {
987  program_source = read_file(binary_name, true);
988  is_binary = true;
989  }
990  else if(std::ifstream(source_name).is_open())
991  {
992  program_source = read_file(source_name, false);
993  }
994  else
995  {
996  ARM_COMPUTE_ERROR_VAR("Kernel file %s does not exist.", source_name.c_str());
997  }
998 
999  return ClProgramInfo{ program_source, is_binary };
1000 #endif /* EMBEDDED_KERNELS */
1001 }
#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 926 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().

927 {
928  // Find which program contains the kernel
929  auto kernel_program_it = _kernel_program_map.find(kernel_name);
930 
931  if(_kernel_program_map.end() == kernel_program_it)
932  {
933  ARM_COMPUTE_ERROR_VAR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
934  }
935 
936  const std::string program_name = kernel_program_it->second;
937 
938  return program_name;
939 }
#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 941 of file ClKernelLibrary.cpp.

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

942 {
943  _kernel_path = std::move(kernel_path);
944  _kernel_path += "/";
945 }
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: