Compute Library
 21.02
Utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-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 
28 
29 #include <fstream>
30 #include <map>
31 #include <string>
32 
33 namespace arm_compute
34 {
35 void restore_program_cache_from_file(const std::string &filename)
36 {
37  std::ifstream cache_file(filename, std::ios::binary);
38  if(cache_file.is_open())
39  {
41  {
43  }
44 
45  while(!cache_file.eof())
46  {
47  size_t name_len = 0;
48  size_t binary_len = 0;
49  cache_file.read(reinterpret_cast<char *>(&name_len), sizeof(size_t));
50  cache_file.read(reinterpret_cast<char *>(&binary_len), sizeof(size_t));
51  if(name_len == 0 || binary_len == 0)
52  {
53  break;
54  }
55  std::vector<char> tmp(name_len);
56  std::vector<unsigned char> binary(binary_len);
57  std::string name;
58  cache_file.read(tmp.data(), name_len);
59  name.assign(tmp.data(), name_len);
60  tmp.resize(binary_len);
61  cache_file.read(reinterpret_cast<char *>(binary.data()), binary_len);
62  cl::Context context = arm_compute::CLScheduler::get().context();
63  cl::Program::Binaries binaries{ binary };
64  std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
65  cl::Program program(context, devices, binaries);
66  program.build();
67  CLKernelLibrary::get().add_built_program(name, program);
68  }
69  cache_file.close();
70  }
71 }
72 
73 void save_program_cache_to_file(const std::string &filename)
74 {
75  if(CLScheduler::get().is_initialised())
76  {
77  std::ofstream cache_file(filename, std::ios::binary);
78  if(cache_file.is_open())
79  {
80  for(const auto &it : CLKernelLibrary::get().get_built_programs())
81  {
82  std::vector<std::vector<unsigned char>> binaries = it.second.getInfo<CL_PROGRAM_BINARIES>();
83  ARM_COMPUTE_ERROR_ON(binaries.size() != 1);
84  const std::string kernel_name = it.first;
85  size_t kernel_name_size = kernel_name.length();
86  size_t binary_size = binaries[0].size();
87  cache_file.write(reinterpret_cast<char *>(&kernel_name_size), sizeof(size_t));
88  cache_file.write(reinterpret_cast<char *>(&binary_size), sizeof(size_t));
89  cache_file.write(kernel_name.c_str(), kernel_name_size);
90  cache_file.write(reinterpret_cast<const char *>(binaries[0].data()), binaries[0].size());
91  }
92  cache_file.close();
93  }
94  else
95  {
96  ARM_COMPUTE_ERROR("Cannot open cache file");
97  }
98  }
99 }
100 } // namespace arm_compute
void restore_program_cache_from_file(const std::string &filename="cache.bin")
This function loads prebuilt opencl kernels from a file.
Definition: Utils.cpp:35
void add_built_program(const std::string &built_program_name, const cl::Program &program)
Add a new built program to the cache.
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
bool is_initialised() const
Definition: CLScheduler.cpp:92
Copyright (c) 2017-2021 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:34
std::string kernel_name
void save_program_cache_to_file(const std::string &filename="cache.bin")
This function saves opencl kernels library to a file.
Definition: Utils.cpp:73
const char * name
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
void default_init(ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr)
Initialises the context and command queue used by the scheduler to default values and sets a default ...
const std::map< std::string, cl::Program > & get_built_programs() const
Access the cache of built OpenCL programs.