forked from ROCm/MIOpen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_cache.cpp
More file actions
237 lines (218 loc) · 6.88 KB
/
Copy pathbinary_cache.cpp
File metadata and controls
237 lines (218 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2017 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#include <miopen/binary_cache.hpp>
#include <miopen/handle.hpp>
#include <miopen/md5.hpp>
#include <miopen/errors.hpp>
#include <miopen/env.hpp>
#include <miopen/stringutils.hpp>
#include <miopen/expanduser.hpp>
#include <miopen/miopen.h>
#include <miopen/version.h>
#if MIOPEN_ENABLE_SQLITE
#include <miopen/sqlite_db.hpp>
#endif
#include <miopen/kern_db.hpp>
#include <miopen/db.hpp>
#include <miopen/db_path.hpp>
#include <miopen/target_properties.hpp>
#include <miopen/filesystem.hpp>
#include <fstream>
#include <iostream>
MIOPEN_DECLARE_ENV_VAR_BOOL(MIOPEN_DISABLE_CACHE)
MIOPEN_DECLARE_ENV_VAR_STR(MIOPEN_CUSTOM_CACHE_DIR)
namespace miopen {
static fs::path ComputeSysCachePath()
{
auto p = miopen::ExpandUser(GetSystemDbPath());
if(!fs::exists(p))
return {};
else
return p;
}
static fs::path ComputeUserCachePath()
{
#ifdef MIOPEN_CACHE_DIR
fs::path p;
/// If MIOPEN_CUSTOM_CACHE_DIR is set in the environment, then
/// use exactly that path.
const auto& custom = env::value(MIOPEN_CUSTOM_CACHE_DIR);
if(!custom.empty())
{
p = ExpandUser(custom);
}
else
{
const std::string cache_dir = MIOPEN_CACHE_DIR;
const std::string version = std::to_string(MIOPEN_VERSION_MAJOR) //
+ "." + std::to_string(MIOPEN_VERSION_MINOR) //
+ "." + std::to_string(MIOPEN_VERSION_PATCH) //
+ "." + MIOPEN_STRINGIZE(MIOPEN_VERSION_TWEAK);
p = miopen::ExpandUser(cache_dir) / version;
#if !MIOPEN_BUILD_DEV
/// \ref nfs-detection
if(IsNetworkedFilesystem(p))
p = fs::temp_directory_path();
#endif
}
if(!fs::exists(p) && !MIOPEN_DISABLE_USERDB)
fs::create_directories(p);
return p;
#else
return {};
#endif
}
fs::path GetCachePath(bool is_system)
{
static const fs::path user_path = ComputeUserCachePath();
static const fs::path sys_path = ComputeSysCachePath();
if(is_system)
{
if(MIOPEN_DISABLE_SYSDB)
return {};
else
return sys_path;
}
else
{
if(MIOPEN_DISABLE_USERDB)
return {};
else
return user_path;
}
}
bool IsCacheDisabled()
{
#ifdef MIOPEN_CACHE_DIR
if(MIOPEN_DISABLE_USERDB && MIOPEN_DISABLE_SYSDB)
return true;
else
return env::enabled(MIOPEN_DISABLE_CACHE);
#else
return true;
#endif
}
#if MIOPEN_ENABLE_SQLITE_KERN_CACHE
using KDb = DbTimer<MultiFileDb<KernDb, KernDb, false>>;
KDb GetDb(const TargetProperties& target, size_t num_cu)
{
static const auto user_dir = ComputeUserCachePath();
static const auto sys_dir = ComputeSysCachePath();
fs::path user_path = user_dir / (Handle::GetDbBasename(target, num_cu) + ".ukdb");
fs::path sys_path = sys_dir / (Handle::GetDbBasename(target, num_cu) + ".kdb");
if(user_dir.empty())
user_path = user_dir;
if(!fs::exists(sys_path))
sys_path = sys_dir / (target.DbId() + ".kdb");
#if !MIOPEN_EMBED_DB
if(!fs::exists(sys_path))
sys_path = fs::path{};
#endif
return {DbKinds::KernelDb, sys_path, user_path};
}
#endif
fs::path GetCacheFile(const std::string& device, const fs::path& name, const std::string& args)
{
const auto filename = make_object_file_name(name);
return GetCachePath(false) / miopen::md5(device + ":" + args) / filename;
}
#if MIOPEN_ENABLE_SQLITE_KERN_CACHE
std::vector<char> LoadBinary(const TargetProperties& target,
const size_t num_cu,
const fs::path& name,
const std::string& args)
{
if(miopen::IsCacheDisabled())
return {};
auto db = GetDb(target, num_cu);
const auto filename = make_object_file_name(name);
const KernelConfig cfg{filename, args, {}};
MIOPEN_LOG_I2("Loading binary for: " << filename << "; args: " << args);
auto record = db.FindRecord(cfg);
if(record)
{
MIOPEN_LOG_I2("Successfully loaded binary for: " << filename << "; args: " << args);
return *record;
}
else
{
MIOPEN_LOG_I2("Unable to load binary for: " << filename << "; args: " << args);
return {};
}
}
void SaveBinary(const std::vector<char>& hsaco,
const TargetProperties& target,
const std::size_t num_cu,
const fs::path& name,
const std::string& args)
{
if(miopen::IsCacheDisabled())
return;
auto db = GetDb(target, num_cu);
const auto filename = make_object_file_name(name);
KernelConfig cfg{filename, args, hsaco};
MIOPEN_LOG_I2("Saving binary for: " << filename << "; args: " << args);
db.StoreRecord(cfg);
}
#else
fs::path LoadBinary(const TargetProperties& target,
const size_t num_cu,
const fs::path& name,
const std::string& args)
{
if(miopen::IsCacheDisabled())
return {};
(void)num_cu;
auto f = GetCacheFile(target.DbId(), name, args);
if(fs::exists(f))
{
return f;
}
else
{
return {};
}
}
fs::path SaveBinary(const fs::path& binary_path,
const TargetProperties& target,
const fs::path& name,
const std::string& args)
{
if(miopen::IsCacheDisabled())
{
fs::remove(binary_path);
return {};
}
else
{
auto p = GetCacheFile(target.DbId(), name, args);
fs::create_directories(p.parent_path());
fs::rename(binary_path, p);
return p;
}
}
#endif
} // namespace miopen