blob: 294396c28a0ed75c568309fd884448ce09d2951a [file] [log] [blame]
Inki Daeea2ce172020-04-09 10:01:44 +09001/*
SiCong Lidb4a6c12021-02-05 09:30:57 +00002 * Copyright (c) 2020-2021 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 */
SiCong Lidb4a6c12021-02-05 09:30:57 +000024#include "arm_compute/runtime/CL/Utils.h"
25
Inki Daeea2ce172020-04-09 10:01:44 +090026#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/runtime/CL/CLScheduler.h"
28
29#include <fstream>
30#include <map>
31#include <string>
32
33namespace arm_compute
34{
35void restore_program_cache_from_file(const std::string &filename)
36{
37 std::ifstream cache_file(filename, std::ios::binary);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038 if (cache_file.is_open())
Inki Daeea2ce172020-04-09 10:01:44 +090039 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 if (!CLScheduler::get().is_initialised())
Inki Daeea2ce172020-04-09 10:01:44 +090041 {
42 arm_compute::CLScheduler::get().default_init();
43 }
44
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 while (!cache_file.eof())
Inki Daeea2ce172020-04-09 10:01:44 +090046 {
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));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 if (name_len == 0 || binary_len == 0)
Inki Daeea2ce172020-04-09 10:01:44 +090052 {
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();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 cl::Program::Binaries binaries{binary};
Inki Daeea2ce172020-04-09 10:01:44 +090064 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
73void save_program_cache_to_file(const std::string &filename)
74{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 if (CLScheduler::get().is_initialised())
Inki Daeea2ce172020-04-09 10:01:44 +090076 {
77 std::ofstream cache_file(filename, std::ios::binary);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (cache_file.is_open())
Inki Daeea2ce172020-04-09 10:01:44 +090079 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 for (const auto &it : CLKernelLibrary::get().get_built_programs())
Inki Daeea2ce172020-04-09 10:01:44 +090081 {
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