xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/Utils.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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  */
24 #include "arm_compute/runtime/CL/Utils.h"
25 
26 #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 
33 namespace arm_compute
34 {
restore_program_cache_from_file(const std::string & filename)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     {
40         if(!CLScheduler::get().is_initialised())
41         {
42             arm_compute::CLScheduler::get().default_init();
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 
save_program_cache_to_file(const std::string & filename)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
101