blob: e04ce84e496d918ac3eb0d029015dc6c650f84ab [file] [log] [blame]
Inki Daeea2ce172020-04-09 10:01:44 +09001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 Arm Limited.
Inki Daeea2ce172020-04-09 10:01:44 +09003 *
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 */
24#include "arm_compute/core/CL/CLKernelLibrary.h"
25#include "arm_compute/runtime/CL/CLScheduler.h"
26
27#include <fstream>
28#include <map>
29#include <string>
30
31namespace arm_compute
32{
33void restore_program_cache_from_file(const std::string &filename)
34{
35 std::ifstream cache_file(filename, std::ios::binary);
36 if(cache_file.is_open())
37 {
38 if(!CLScheduler::get().is_initialised())
39 {
40 arm_compute::CLScheduler::get().default_init();
41 }
42
43 while(!cache_file.eof())
44 {
45 size_t name_len = 0;
46 size_t binary_len = 0;
47 cache_file.read(reinterpret_cast<char *>(&name_len), sizeof(size_t));
48 cache_file.read(reinterpret_cast<char *>(&binary_len), sizeof(size_t));
49 if(name_len == 0 || binary_len == 0)
50 {
51 break;
52 }
53 std::vector<char> tmp(name_len);
54 std::vector<unsigned char> binary(binary_len);
55 std::string name;
56 cache_file.read(tmp.data(), name_len);
57 name.assign(tmp.data(), name_len);
58 tmp.resize(binary_len);
59 cache_file.read(reinterpret_cast<char *>(binary.data()), binary_len);
60 cl::Context context = arm_compute::CLScheduler::get().context();
61 cl::Program::Binaries binaries{ binary };
62 std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
63 cl::Program program(context, devices, binaries);
64 program.build();
65 CLKernelLibrary::get().add_built_program(name, program);
66 }
67 cache_file.close();
68 }
69}
70
71void save_program_cache_to_file(const std::string &filename)
72{
73 if(CLScheduler::get().is_initialised())
74 {
75 std::ofstream cache_file(filename, std::ios::binary);
76 if(cache_file.is_open())
77 {
78 for(const auto &it : CLKernelLibrary::get().get_built_programs())
79 {
80 std::vector<std::vector<unsigned char>> binaries = it.second.getInfo<CL_PROGRAM_BINARIES>();
81 ARM_COMPUTE_ERROR_ON(binaries.size() != 1);
82 const std::string kernel_name = it.first;
83 size_t kernel_name_size = kernel_name.length();
84 size_t binary_size = binaries[0].size();
85 cache_file.write(reinterpret_cast<char *>(&kernel_name_size), sizeof(size_t));
86 cache_file.write(reinterpret_cast<char *>(&binary_size), sizeof(size_t));
87 cache_file.write(kernel_name.c_str(), kernel_name_size);
88 cache_file.write(reinterpret_cast<const char *>(binaries[0].data()), binaries[0].size());
89 }
90 cache_file.close();
91 }
92 else
93 {
94 ARM_COMPUTE_ERROR("Cannot open cache file");
95 }
96 }
97}
98} // namespace arm_compute