Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "platform/platform_info.h"
12 : #include <dirent.h>
13 : #include <string.h>
14 : #include <unordered_set>
15 : #include <algorithm>
16 : #include <cmath>
17 : #include <fstream>
18 : #include <mutex>
19 : #include "platform/platform_info_def.h"
20 : #include "platform_infos_utils.h"
21 :
22 : namespace fe {
23 : namespace {
24 : const std::string FIXPIPE_CONFIG_KEY = "Intrinsic_fix_pipe_";
25 : const std::string INI_FILE_SUFFIX = "ini";
26 : const std::string STR_SOC_VERSION = "SoC_version";
27 : const std::string STR_AIC_VERSION = "AIC_version";
28 : const std::string SHORT_SOC_VERSION = "Short_SoC_version";
29 : const std::string STR_CCEC_AIC_VERSION = "CCEC_AIC_version";
30 : const std::string STR_CCEC_AIV_VERSION = "CCEC_AIV_version";
31 : const std::string STR_IS_SUPPORT_AICPU = "Compiler_aicpu_support_os";
32 : const std::string STR_VERSION = "version";
33 : const std::string STR_SOC_INFO = "SoCInfo";
34 : const std::string STR_AI_CORE_SPEC = "AICoreSpec";
35 : const std::string STR_AI_CORE_MEMORY_RATES = "AICoreMemoryRates";
36 : const std::string STR_CPU_CACHE = "CPUCache";
37 : const std::string STR_SOFTWARE_SPEC = "SoftwareSpec";
38 :
39 : const std::string STR_AI_CORE_INTRINSIC_DTYPE_MAP = "AICoreintrinsicDtypeMap";
40 : const std::string STR_VECTOR_CORE_SPEC = "VectorCoreSpec";
41 : const std::string STR_VECTOR_CORE_MEMORY_RATES = "VectorCoreMemoryRates";
42 : const std::string STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP = "VectorCoreintrinsicDtypeMap";
43 :
44 : const std::string SOC_VERSION_ASCEND910 = "Ascend910";
45 : const std::string SOC_VERSION_ASCEND910A = "Ascend910A";
46 :
47 : const uint32_t PLATFORM_FAILED = 0xFFFFFFFF;
48 : const uint32_t PLATFORM_SUCCESS = 0;
49 :
50 : enum class PlatformInfoType {
51 : EN_VERSION = 0,
52 : EN_SOC_INFO,
53 : EN_AI_CORE_SPEC,
54 : EN_AI_CORE_MEMORY_RATES,
55 : EN_CPU_CACHE,
56 : EN_AI_CORE_INTRINSIC_DTYPE_MAP,
57 : EN_VECTOR_CORE_SPEC,
58 : EN_VECTOR_CORE_MEMORY_RATES,
59 : EN_VECTOR_CORE_INTRINSIC_DTYPE_MAP,
60 : EN_SOFTWARE_SPEC,
61 : };
62 :
63 : std::map<std::string, PlatformInfoType> g_platform_info_type_map{
64 : {STR_VERSION, PlatformInfoType::EN_VERSION},
65 : {STR_SOC_INFO, PlatformInfoType::EN_SOC_INFO},
66 : {STR_AI_CORE_SPEC, PlatformInfoType::EN_AI_CORE_SPEC},
67 : {STR_AI_CORE_MEMORY_RATES, PlatformInfoType::EN_AI_CORE_MEMORY_RATES},
68 : {STR_CPU_CACHE, PlatformInfoType::EN_CPU_CACHE},
69 : {STR_AI_CORE_INTRINSIC_DTYPE_MAP, PlatformInfoType::EN_AI_CORE_INTRINSIC_DTYPE_MAP},
70 : {STR_VECTOR_CORE_SPEC, PlatformInfoType::EN_VECTOR_CORE_SPEC},
71 : {STR_VECTOR_CORE_MEMORY_RATES, PlatformInfoType::EN_VECTOR_CORE_MEMORY_RATES},
72 : {STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP, PlatformInfoType::EN_VECTOR_CORE_INTRINSIC_DTYPE_MAP},
73 : {STR_SOFTWARE_SPEC, PlatformInfoType::EN_SOFTWARE_SPEC}};
74 : } // namespace
75 :
76 : #define CONVERT(name) #name
77 :
78 : std::mutex pc_lock_;
79 :
80 2 : PlatformInfoManager::PlatformInfoManager() : init_flag_(false), runtime_init_flag_(false), opti_compilation_info_() {}
81 :
82 2 : PlatformInfoManager::~PlatformInfoManager() {}
83 :
84 1337 : PlatformInfoManager& PlatformInfoManager::Instance()
85 : {
86 1337 : static PlatformInfoManager platform_info;
87 1337 : return platform_info;
88 : }
89 :
90 165 : PlatformInfoManager& PlatformInfoManager::GeInstance()
91 : {
92 165 : static PlatformInfoManager ge_platform_info;
93 165 : return ge_platform_info;
94 : }
95 :
96 37237 : void PlatformInfoManager::Trim(std::string& str)
97 : {
98 37237 : if (str.empty()) {
99 133 : return;
100 : }
101 37104 : size_t start_pos = str.find_first_not_of(" \t\r\n");
102 37104 : size_t end_pos = str.find_last_not_of(" \t\r\n");
103 37104 : if (start_pos == std::string::npos || start_pos > end_pos) {
104 1 : str.clear();
105 1 : return;
106 : }
107 37103 : str = str.substr(start_pos, end_pos - start_pos + 1);
108 : }
109 :
110 99 : uint32_t PlatformInfoManager::LoadIniFile(std::string ini_file_real_path)
111 : {
112 99 : std::ifstream ifs(ini_file_real_path);
113 99 : if (!ifs.is_open()) {
114 18 : PF_LOGE("Failed to open conf file, it does not exist or is already opened.");
115 18 : return PLATFORM_FAILED;
116 : }
117 :
118 81 : std::map<std::string, std::string> content_map;
119 81 : std::map<std::string, std::map<std::string, std::string>> content_info_map;
120 81 : content_map.clear();
121 81 : content_info_map.clear();
122 81 : std::string line;
123 81 : std::string map_key;
124 20024 : while (std::getline(ifs, line)) {
125 19943 : if (line.empty() || line.find('#') == 0) {
126 1714 : continue;
127 : }
128 :
129 19005 : if (line.find('[') == 0) {
130 776 : if (!map_key.empty() && !content_map.empty()) {
131 671 : content_info_map.emplace(make_pair(map_key, content_map));
132 671 : content_map.clear();
133 : }
134 776 : size_t pos = line.rfind(']');
135 776 : if (pos == std::string::npos) {
136 0 : continue;
137 : }
138 776 : map_key = line.substr(1, pos - 1);
139 776 : Trim(map_key);
140 776 : continue;
141 776 : }
142 :
143 18229 : size_t pos_of_equal = line.find('=');
144 18229 : if (pos_of_equal == std::string::npos) {
145 0 : continue;
146 : }
147 :
148 18229 : std::string key = line.substr(0, pos_of_equal);
149 18229 : Trim(key);
150 18229 : std::string value = line.substr(pos_of_equal + 1, line.length() - pos_of_equal - 1);
151 18229 : Trim(value);
152 18229 : if (!key.empty() && !value.empty()) {
153 18097 : content_map.emplace(make_pair(key, value));
154 : }
155 18229 : }
156 :
157 81 : if (!content_map.empty() && !map_key.empty()) {
158 81 : content_info_map.emplace(make_pair(map_key, content_map));
159 81 : content_map.clear();
160 : }
161 :
162 81 : ifs.close();
163 :
164 81 : if (AssemblePlatformInfoVector(content_info_map) != PLATFORM_SUCCESS) {
165 0 : PF_LOGE("Assemble platform info failed.");
166 0 : return PLATFORM_FAILED;
167 : }
168 :
169 81 : return PLATFORM_SUCCESS;
170 99 : }
171 :
172 96 : uint32_t PlatformInfoManager::EnsureSocVersionLoaded(const std::string& soc_version)
173 : {
174 96 : if (soc_version.empty()) {
175 1 : return PLATFORM_FAILED;
176 : }
177 95 : if (loaded_ini_files_.count(soc_version) > 0) {
178 5 : return PLATFORM_SUCCESS;
179 : }
180 90 : std::string ini_file_path = cfg_file_real_path_ + "/" + soc_version + ".ini";
181 90 : PF_LOGD("Begin to load ini file[%s].", ini_file_path.c_str());
182 90 : if (LoadIniFile(ini_file_path) != PLATFORM_SUCCESS) {
183 9 : std::string soc_version_ = soc_version;
184 9 : std::transform(soc_version_.begin(), soc_version_.end(), soc_version_.begin(), ::tolower);
185 9 : ini_file_path = cfg_file_real_path_ + "/" + soc_version_ + ".ini";
186 9 : if (LoadIniFile(ini_file_path) != PLATFORM_SUCCESS) {
187 9 : PF_LOGE("Failed to load ini file[%s].", ini_file_path.c_str());
188 9 : return PLATFORM_FAILED;
189 : }
190 9 : }
191 81 : loaded_ini_files_.insert(soc_version);
192 81 : return PLATFORM_SUCCESS;
193 90 : }
194 :
195 83 : void PlatformInfoManager::ParseVersion(
196 : std::map<std::string, std::string>& version_map, std::string& soc_version, PlatformInfo& platform_info_temp)
197 : {
198 83 : std::map<std::string, std::string>::const_iterator soc = version_map.find(STR_SOC_VERSION);
199 83 : if (soc != version_map.end()) {
200 81 : soc_version = soc->second;
201 : }
202 :
203 83 : std::map<std::string, std::string>::const_iterator aic_version = version_map.find(STR_AIC_VERSION);
204 83 : if (aic_version != version_map.end()) {
205 81 : platform_info_temp.str_info.aic_version = aic_version->second;
206 : }
207 :
208 83 : std::map<std::string, std::string>::const_iterator ccec_aic_version = version_map.find(STR_CCEC_AIC_VERSION);
209 83 : if (ccec_aic_version != version_map.end()) {
210 81 : platform_info_temp.str_info.ccec_aic_version = ccec_aic_version->second;
211 : }
212 :
213 83 : std::map<std::string, std::string>::const_iterator ccec_aiv_version = version_map.find(STR_CCEC_AIV_VERSION);
214 83 : if (ccec_aiv_version != version_map.end()) {
215 62 : platform_info_temp.str_info.ccec_aiv_version = ccec_aiv_version->second;
216 : }
217 :
218 83 : std::map<std::string, std::string>::const_iterator is_support_aicpu = version_map.find(STR_IS_SUPPORT_AICPU);
219 83 : if (is_support_aicpu != version_map.end()) {
220 81 : platform_info_temp.str_info.is_support_ai_cpu_compiler = is_support_aicpu->second;
221 : }
222 :
223 83 : std::map<std::string, std::string>::const_iterator short_soc_version = version_map.find(SHORT_SOC_VERSION);
224 83 : if (short_soc_version != version_map.end()) {
225 81 : platform_info_temp.str_info.short_soc_version = short_soc_version->second;
226 : }
227 :
228 : try {
229 83 : std::map<std::string, std::string>::const_iterator arch_type = version_map.find(CONVERT(Arch_type));
230 83 : if (arch_type != version_map.end()) {
231 70 : platform_info_temp.soc_info.arch_type = stoi(arch_type->second);
232 : }
233 :
234 82 : std::map<std::string, std::string>::const_iterator chip_type = version_map.find(CONVERT(Chip_type));
235 82 : if (chip_type != version_map.end()) {
236 81 : platform_info_temp.soc_info.chip_type = stoi(chip_type->second);
237 : }
238 1 : } catch (...) {
239 1 : PF_LOGE("Failed to parse version");
240 1 : }
241 166 : return;
242 : }
243 :
244 83 : void PlatformInfoManager::ParseSocInfo(
245 : std::map<std::string, std::string>& soc_info_map, PlatformInfo& platform_info_temp)
246 : {
247 : try {
248 83 : std::map<std::string, std::string>::const_iterator ai_core_cnt = soc_info_map.find(CONVERT(ai_core_cnt));
249 83 : if (ai_core_cnt != soc_info_map.end()) {
250 83 : platform_info_temp.soc_info.ai_core_cnt = static_cast<uint32_t>(stoi(ai_core_cnt->second));
251 : }
252 :
253 : std::map<std::string, std::string>::const_iterator vector_core_cnt =
254 82 : soc_info_map.find(CONVERT(vector_core_cnt));
255 82 : if (vector_core_cnt != soc_info_map.end()) {
256 82 : platform_info_temp.soc_info.vector_core_cnt = static_cast<uint32_t>(stoi(vector_core_cnt->second));
257 : }
258 :
259 82 : std::map<std::string, std::string>::const_iterator ai_cpu_cnt = soc_info_map.find(CONVERT(ai_cpu_cnt));
260 82 : if (ai_cpu_cnt != soc_info_map.end()) {
261 70 : platform_info_temp.soc_info.ai_cpu_cnt = static_cast<uint32_t>(stoi(ai_cpu_cnt->second));
262 : }
263 :
264 82 : std::map<std::string, std::string>::const_iterator memory_type = soc_info_map.find(CONVERT(memory_type));
265 82 : if (memory_type != soc_info_map.end()) {
266 17 : platform_info_temp.soc_info.memory_type = static_cast<MemoryType>(stoi(memory_type->second));
267 : }
268 :
269 82 : std::map<std::string, std::string>::const_iterator memory_size = soc_info_map.find(CONVERT(memory_size));
270 82 : if (memory_size != soc_info_map.end()) {
271 63 : platform_info_temp.soc_info.memory_size = static_cast<uint64_t>(stoll(memory_size->second));
272 : }
273 :
274 82 : std::map<std::string, std::string>::const_iterator l2_type = soc_info_map.find(CONVERT(l2_type));
275 82 : if (l2_type != soc_info_map.end()) {
276 82 : platform_info_temp.soc_info.l2_type = static_cast<L2Type>(stoi(l2_type->second));
277 : }
278 :
279 82 : std::map<std::string, std::string>::const_iterator l2_size = soc_info_map.find(CONVERT(l2_size));
280 82 : if (l2_size != soc_info_map.end()) {
281 82 : platform_info_temp.soc_info.l2_size = static_cast<uint64_t>(stoll(l2_size->second));
282 : }
283 :
284 82 : std::map<std::string, std::string>::const_iterator l2PageNum = soc_info_map.find(CONVERT(l2PageNum));
285 82 : if (l2PageNum != soc_info_map.end()) {
286 81 : platform_info_temp.soc_info.l2PageNum = static_cast<uint32_t>(stoi(l2PageNum->second));
287 : }
288 82 : std::map<std::string, std::string>::const_iterator task_num = soc_info_map.find(CONVERT(task_num));
289 82 : if (task_num != soc_info_map.end()) {
290 1 : platform_info_temp.soc_info.task_num = static_cast<uint32_t>(stoi(task_num->second));
291 : }
292 1 : } catch (...) {
293 1 : PF_LOGE("Failed to load SocInfo");
294 1 : }
295 83 : }
296 :
297 84 : void PlatformInfoManager::ParseCubeOfAICoreSpec(
298 : std::map<std::string, std::string>& ai_core_spec_map, PlatformInfo& platform_info_temp)
299 : {
300 : try {
301 84 : std::map<std::string, std::string>::const_iterator cube_freq = ai_core_spec_map.find(CONVERT(cube_freq));
302 84 : if (cube_freq != ai_core_spec_map.end()) {
303 70 : platform_info_temp.ai_core_spec.cube_freq = static_cast<double>(stod(cube_freq->second));
304 : }
305 :
306 84 : std::map<std::string, std::string>::const_iterator cube_m_size = ai_core_spec_map.find(CONVERT(cube_m_size));
307 84 : if (cube_m_size != ai_core_spec_map.end()) {
308 83 : platform_info_temp.ai_core_spec.cube_m_size = static_cast<uint64_t>(stoll(cube_m_size->second));
309 : }
310 :
311 83 : std::map<std::string, std::string>::const_iterator cube_n_size = ai_core_spec_map.find(CONVERT(cube_n_size));
312 83 : if (cube_n_size != ai_core_spec_map.end()) {
313 82 : platform_info_temp.ai_core_spec.cube_n_size = static_cast<uint64_t>(stoll(cube_n_size->second));
314 : }
315 :
316 83 : std::map<std::string, std::string>::const_iterator cube_k_size = ai_core_spec_map.find(CONVERT(cube_k_size));
317 83 : if (cube_k_size != ai_core_spec_map.end()) {
318 82 : platform_info_temp.ai_core_spec.cube_k_size = static_cast<uint64_t>(stoll(cube_k_size->second));
319 : }
320 :
321 : std::map<std::string, std::string>::const_iterator vec_calc_size =
322 83 : ai_core_spec_map.find(CONVERT(vec_calc_size));
323 83 : if (vec_calc_size != ai_core_spec_map.end()) {
324 82 : platform_info_temp.ai_core_spec.vec_calc_size = static_cast<uint64_t>(stoll(vec_calc_size->second));
325 : }
326 :
327 83 : platform_info_temp.ai_core_spec.sparsity = 0;
328 83 : std::map<std::string, std::string>::const_iterator sparse_matrix = ai_core_spec_map.find(CONVERT(sparsity));
329 83 : if (sparse_matrix != ai_core_spec_map.end()) {
330 70 : platform_info_temp.ai_core_spec.sparsity = static_cast<uint8_t>(stoll(sparse_matrix->second));
331 : }
332 1 : } catch (...) {
333 1 : PF_LOGE("Failed to load AICoreSpecs.");
334 1 : }
335 84 : }
336 :
337 84 : void PlatformInfoManager::ParseBufferOfAICoreSpec(
338 : std::map<std::string, std::string>& ai_core_spec_map, PlatformInfo& platform_info_temp)
339 : {
340 : try {
341 84 : std::map<std::string, std::string>::const_iterator l0_a_size = ai_core_spec_map.find(CONVERT(l0_a_size));
342 84 : if (l0_a_size != ai_core_spec_map.end()) {
343 82 : platform_info_temp.ai_core_spec.l0_a_size = static_cast<uint64_t>(stoll(l0_a_size->second));
344 : }
345 :
346 84 : std::map<std::string, std::string>::const_iterator l0_b_size = ai_core_spec_map.find(CONVERT(l0_b_size));
347 84 : if (l0_b_size != ai_core_spec_map.end()) {
348 82 : platform_info_temp.ai_core_spec.l0_b_size = static_cast<uint64_t>(stoll(l0_b_size->second));
349 : }
350 :
351 84 : std::map<std::string, std::string>::const_iterator l0_c_size = ai_core_spec_map.find(CONVERT(l0_c_size));
352 84 : if (l0_c_size != ai_core_spec_map.end()) {
353 82 : platform_info_temp.ai_core_spec.l0_c_size = static_cast<uint64_t>(stoll(l0_c_size->second));
354 : }
355 :
356 84 : std::map<std::string, std::string>::const_iterator l1_size = ai_core_spec_map.find(CONVERT(l1_size));
357 84 : if (l1_size != ai_core_spec_map.end()) {
358 82 : platform_info_temp.ai_core_spec.l1_size = static_cast<uint64_t>(stoll(l1_size->second));
359 : }
360 :
361 84 : std::map<std::string, std::string>::const_iterator smask_buffer = ai_core_spec_map.find(CONVERT(smask_buffer));
362 84 : if (smask_buffer != ai_core_spec_map.end()) {
363 70 : platform_info_temp.ai_core_spec.smask_buffer = static_cast<uint64_t>(stoll(smask_buffer->second));
364 : }
365 0 : } catch (...) {
366 0 : PF_LOGE("Failed to load AICoreSpecs.");
367 0 : }
368 84 : }
369 :
370 85 : void PlatformInfoManager::ParseUBOfAICoreSpec(
371 : std::map<std::string, std::string>& ai_core_spec_map, PlatformInfo& platform_info_temp)
372 : {
373 : try {
374 85 : std::map<std::string, std::string>::const_iterator ub_size = ai_core_spec_map.find(CONVERT(ub_size));
375 85 : if (ub_size != ai_core_spec_map.end()) {
376 84 : platform_info_temp.ai_core_spec.ub_size = static_cast<uint64_t>(stoll(ub_size->second));
377 : }
378 :
379 83 : std::map<std::string, std::string>::const_iterator ubblock_size = ai_core_spec_map.find(CONVERT(ubblock_size));
380 83 : if (ubblock_size != ai_core_spec_map.end()) {
381 82 : platform_info_temp.ai_core_spec.ubblock_size = static_cast<uint64_t>(stoll(ubblock_size->second));
382 : }
383 :
384 83 : std::map<std::string, std::string>::const_iterator ubbank_size = ai_core_spec_map.find(CONVERT(ubbank_size));
385 83 : if (ubbank_size != ai_core_spec_map.end()) {
386 70 : platform_info_temp.ai_core_spec.ubbank_size = static_cast<uint64_t>(stoll(ubbank_size->second));
387 : }
388 :
389 83 : std::map<std::string, std::string>::const_iterator ubbank_num = ai_core_spec_map.find(CONVERT(ubbank_num));
390 83 : if (ubbank_num != ai_core_spec_map.end()) {
391 70 : platform_info_temp.ai_core_spec.ubbank_num = static_cast<uint64_t>(stoll(ubbank_num->second));
392 : }
393 :
394 : std::map<std::string, std::string>::const_iterator ubburst_in_one_block =
395 83 : ai_core_spec_map.find(CONVERT(ubburst_in_one_block));
396 83 : if (ubburst_in_one_block != ai_core_spec_map.end()) {
397 70 : platform_info_temp.ai_core_spec.ubburst_in_one_block =
398 70 : static_cast<uint64_t>(stoll(ubburst_in_one_block->second));
399 : }
400 :
401 : std::map<std::string, std::string>::const_iterator ubbank_group_num =
402 83 : ai_core_spec_map.find(CONVERT(ubbank_group_num));
403 83 : if (ubbank_group_num != ai_core_spec_map.end()) {
404 70 : platform_info_temp.ai_core_spec.ubbank_group_num = static_cast<uint64_t>(stoll(ubbank_group_num->second));
405 : }
406 2 : } catch (...) {
407 2 : PF_LOGE("Failed to load AICoreSpecs.");
408 2 : }
409 85 : }
410 :
411 88 : void PlatformInfoManager::ParseUnzipOfAICoreSpec(
412 : std::map<std::string, std::string>& ai_core_spec_map, PlatformInfo& platform_info_temp)
413 : {
414 : try {
415 : std::map<std::string, std::string>::const_iterator unzip_engines =
416 88 : ai_core_spec_map.find(CONVERT(unzip_engines));
417 88 : if (unzip_engines != ai_core_spec_map.end()) {
418 72 : unsigned long value = std::stoul(unzip_engines->second);
419 70 : if (value > UINT32_MAX) {
420 1 : PF_LOGE("unzip_engines value [%lu] exceeds uint32 range", value);
421 4 : return;
422 : }
423 69 : platform_info_temp.ai_core_spec.unzip_engines = static_cast<uint32_t>(value);
424 : }
425 :
426 : std::map<std::string, std::string>::const_iterator unzip_max_ratios =
427 85 : ai_core_spec_map.find(CONVERT(unzip_max_ratios));
428 85 : if (unzip_max_ratios != ai_core_spec_map.end()) {
429 70 : unsigned long value = std::stoul(unzip_max_ratios->second);
430 70 : if (value > UINT32_MAX) {
431 1 : PF_LOGE("unzip_max_ratios value [%lu] exceeds uint32 range", value);
432 1 : return;
433 : }
434 69 : platform_info_temp.ai_core_spec.unzip_max_ratios = static_cast<uint32_t>(value);
435 : }
436 :
437 : std::map<std::string, std::string>::const_iterator unzip_channels =
438 84 : ai_core_spec_map.find(CONVERT(unzip_channels));
439 84 : if (unzip_channels != ai_core_spec_map.end()) {
440 70 : unsigned long value = std::stoul(unzip_channels->second);
441 70 : if (value > UINT32_MAX) {
442 1 : PF_LOGE("unzip_channels value [%lu] exceeds uint32 range", value);
443 1 : return;
444 : }
445 69 : platform_info_temp.ai_core_spec.unzip_channels = static_cast<uint32_t>(value);
446 : }
447 :
448 : std::map<std::string, std::string>::const_iterator unzip_is_tight =
449 83 : ai_core_spec_map.find(CONVERT(unzip_is_tight));
450 83 : if (unzip_is_tight != ai_core_spec_map.end()) {
451 70 : unsigned long value = std::stoul(unzip_is_tight->second);
452 70 : if (value > UINT8_MAX) {
453 1 : PF_LOGE("unzip_is_tight value [%lu] exceeds uint8 range", value);
454 1 : return;
455 : }
456 69 : platform_info_temp.ai_core_spec.unzip_is_tight = static_cast<uint8_t>(value);
457 : }
458 2 : } catch (const std::invalid_argument& e) {
459 1 : PF_LOGE("Invalid argument in ParseUnzipOfAICoreSpec: %s", e.what());
460 1 : return;
461 2 : } catch (const std::out_of_range& e) {
462 1 : PF_LOGE("Out of range in ParseUnzipOfAICoreSpec: %s", e.what());
463 1 : return;
464 1 : }
465 : }
466 :
467 82 : void PlatformInfoManager::ParseAICoreSpec(
468 : std::map<std::string, std::string>& ai_core_spec_map, PlatformInfo& platform_info_temp)
469 : {
470 82 : (void)ParseCubeOfAICoreSpec(ai_core_spec_map, platform_info_temp);
471 82 : (void)ParseBufferOfAICoreSpec(ai_core_spec_map, platform_info_temp);
472 82 : (void)ParseUBOfAICoreSpec(ai_core_spec_map, platform_info_temp);
473 82 : (void)ParseUnzipOfAICoreSpec(ai_core_spec_map, platform_info_temp);
474 :
475 82 : return;
476 : }
477 :
478 150 : static double CalculateFraction(const std::string& fraction)
479 : {
480 150 : if (fraction.empty()) {
481 1 : PF_LOGE("CalculateFraction: input string is empty.");
482 1 : return 0.0;
483 : }
484 :
485 149 : size_t pos = fraction.find('/');
486 : try {
487 149 : if (pos == std::string::npos) {
488 139 : return std::stod(fraction);
489 : }
490 :
491 10 : if (pos == 0 || pos == fraction.length() - 1) {
492 2 : PF_LOGE("CalculateFraction: invalid fraction format [%s], '/' at boundary.", fraction.c_str());
493 2 : return 0.0;
494 : }
495 :
496 8 : std::string denominator_str = fraction.substr(pos + 1);
497 :
498 : // 在转换前检查字符串是否表示零值(避免浮点数直接比较)
499 8 : std::string trimmed_denominator = denominator_str;
500 8 : size_t start = trimmed_denominator.find_first_not_of(" \t");
501 8 : size_t end = trimmed_denominator.find_last_not_of(" \t");
502 8 : if (start != std::string::npos && end != std::string::npos) {
503 8 : trimmed_denominator = trimmed_denominator.substr(start, end - start + 1);
504 : }
505 22 : if (trimmed_denominator == "0" || trimmed_denominator == "0.0" || trimmed_denominator == "0." ||
506 22 : trimmed_denominator == "-0" || trimmed_denominator == "-0.0" || trimmed_denominator == "-0.") {
507 1 : PF_LOGE("CalculateFraction: denominator is zero in [%s].", fraction.c_str());
508 1 : return 0.0;
509 : }
510 :
511 7 : double denominator = std::stod(denominator_str);
512 :
513 : // 使用 EPSILON 检查接近零的情况(避免直接比较浮点数)
514 7 : constexpr double EPSILON = 1e-10;
515 7 : if (std::abs(denominator) < EPSILON) {
516 1 : PF_LOGE("CalculateFraction: denominator is too small [%s].", fraction.c_str());
517 1 : return 0.0;
518 : }
519 :
520 7 : double numerator = std::stod(fraction.substr(0, pos));
521 5 : double result = numerator / denominator;
522 :
523 : // 检查结果是否有效
524 5 : if (std::isinf(result) || std::isnan(result)) {
525 0 : PF_LOGE("CalculateFraction: invalid result for [%s].", fraction.c_str());
526 0 : return 0.0;
527 : }
528 :
529 5 : return result;
530 11 : } catch (const std::exception& e) {
531 2 : PF_LOGE("CalculateFraction: failed to parse [%s], error: %s.", fraction.c_str(), e.what());
532 2 : return 0.0;
533 2 : } catch (...) {
534 0 : PF_LOGE("CalculateFraction: unknown exception parsing [%s].", fraction.c_str());
535 0 : return 0.0;
536 0 : }
537 : }
538 :
539 72 : void PlatformInfoManager::ParseBufferOfAICoreMemoryRates(
540 : std::map<std::string, std::string>& ai_core_memory_rates_map, PlatformInfo& platform_info_temp)
541 : {
542 : try {
543 72 : std::map<std::string, std::string>::const_iterator ddr_rate = ai_core_memory_rates_map.find(CONVERT(ddr_rate));
544 72 : if (ddr_rate != ai_core_memory_rates_map.end()) {
545 71 : platform_info_temp.ai_core_memory_rates.ddr_rate = static_cast<double>(stod(ddr_rate->second));
546 : }
547 :
548 : std::map<std::string, std::string>::const_iterator ddr_read_rate =
549 71 : ai_core_memory_rates_map.find(CONVERT(ddr_read_rate));
550 71 : if (ddr_read_rate != ai_core_memory_rates_map.end()) {
551 70 : platform_info_temp.ai_core_memory_rates.ddr_read_rate = static_cast<double>(stod(ddr_read_rate->second));
552 : }
553 :
554 : std::map<std::string, std::string>::const_iterator ddr_write_rate =
555 71 : ai_core_memory_rates_map.find(CONVERT(ddr_write_rate));
556 71 : if (ddr_write_rate != ai_core_memory_rates_map.end()) {
557 70 : platform_info_temp.ai_core_memory_rates.ddr_write_rate = static_cast<double>(stod(ddr_write_rate->second));
558 : }
559 :
560 71 : std::map<std::string, std::string>::const_iterator l2_rate = ai_core_memory_rates_map.find(CONVERT(l2_rate));
561 71 : if (l2_rate != ai_core_memory_rates_map.end()) {
562 70 : platform_info_temp.ai_core_memory_rates.l2_rate = static_cast<double>(stod(l2_rate->second));
563 : }
564 :
565 : std::map<std::string, std::string>::const_iterator l2_read_rate =
566 71 : ai_core_memory_rates_map.find(CONVERT(l2_read_rate));
567 71 : if (l2_read_rate != ai_core_memory_rates_map.end()) {
568 70 : platform_info_temp.ai_core_memory_rates.l2_read_rate = static_cast<double>(stod(l2_read_rate->second));
569 : }
570 :
571 : std::map<std::string, std::string>::const_iterator l2_write_rate =
572 71 : ai_core_memory_rates_map.find(CONVERT(l2_write_rate));
573 71 : if (l2_write_rate != ai_core_memory_rates_map.end()) {
574 70 : platform_info_temp.ai_core_memory_rates.l2_write_rate = static_cast<double>(stod(l2_write_rate->second));
575 : }
576 :
577 : std::map<std::string, std::string>::const_iterator l1_to_l0_a_rate =
578 71 : ai_core_memory_rates_map.find(CONVERT(l1_to_l0_a_rate));
579 71 : if (l1_to_l0_a_rate != ai_core_memory_rates_map.end()) {
580 69 : platform_info_temp.ai_core_memory_rates.l1_to_l0_a_rate =
581 69 : static_cast<double>(stod(l1_to_l0_a_rate->second));
582 : }
583 :
584 : std::map<std::string, std::string>::const_iterator l1_to_l0_b_rate =
585 71 : ai_core_memory_rates_map.find(CONVERT(l1_to_l0_b_rate));
586 71 : if (l1_to_l0_b_rate != ai_core_memory_rates_map.end()) {
587 69 : platform_info_temp.ai_core_memory_rates.l1_to_l0_b_rate =
588 69 : static_cast<double>(stod(l1_to_l0_b_rate->second));
589 : }
590 :
591 : std::map<std::string, std::string>::const_iterator l1_to_ub_rate =
592 71 : ai_core_memory_rates_map.find(CONVERT(l1_to_ub_rate));
593 71 : if (l1_to_ub_rate != ai_core_memory_rates_map.end()) {
594 69 : platform_info_temp.ai_core_memory_rates.l1_to_ub_rate = static_cast<double>(stod(l1_to_ub_rate->second));
595 : }
596 :
597 : std::map<std::string, std::string>::const_iterator l0_c_to_ub_rate =
598 71 : ai_core_memory_rates_map.find(CONVERT(l0_c_to_ub_rate));
599 71 : if (l0_c_to_ub_rate != ai_core_memory_rates_map.end()) {
600 69 : platform_info_temp.ai_core_memory_rates.l0_c_to_ub_rate =
601 69 : static_cast<double>(stod(l0_c_to_ub_rate->second));
602 : }
603 1 : } catch (...) {
604 1 : PF_LOGE("Failed to load AICoreMemoryRates.");
605 1 : }
606 72 : }
607 :
608 82 : void PlatformInfoManager::ParseUBOfAICoreMemoryRates(
609 : std::map<std::string, std::string>& ai_core_memory_rates_map, PlatformInfo& platform_info_temp)
610 : {
611 : std::map<std::string, std::string>::const_iterator ub_to_l2_rate =
612 82 : ai_core_memory_rates_map.find(CONVERT(ub_to_l2_rate));
613 82 : if (ub_to_l2_rate != ai_core_memory_rates_map.end()) {
614 81 : platform_info_temp.ai_core_memory_rates.ub_to_l2_rate = CalculateFraction(ub_to_l2_rate->second);
615 : }
616 :
617 : std::map<std::string, std::string>::const_iterator ub_to_ddr_rate =
618 82 : ai_core_memory_rates_map.find(CONVERT(ub_to_ddr_rate));
619 82 : if (ub_to_ddr_rate != ai_core_memory_rates_map.end()) {
620 69 : platform_info_temp.ai_core_memory_rates.ub_to_ddr_rate = CalculateFraction(ub_to_ddr_rate->second);
621 : }
622 :
623 : std::map<std::string, std::string>::const_iterator ub_to_l1_rate =
624 82 : ai_core_memory_rates_map.find(CONVERT(ub_to_l1_rate));
625 82 : if (ub_to_l1_rate != ai_core_memory_rates_map.end()) {
626 : try {
627 70 : platform_info_temp.ai_core_memory_rates.ub_to_l1_rate = static_cast<double>(stod(ub_to_l1_rate->second));
628 1 : } catch (...) {
629 1 : PF_LOGE("Failed to load ub_to_l1_rate [%s].", ub_to_l1_rate->second.c_str());
630 1 : }
631 : }
632 82 : }
633 :
634 69 : void PlatformInfoManager::ParseAICoreMemoryRates(
635 : std::map<std::string, std::string>& ai_core_memory_rates_map, PlatformInfo& platform_info_temp)
636 : {
637 69 : (void)ParseBufferOfAICoreMemoryRates(ai_core_memory_rates_map, platform_info_temp);
638 69 : (void)ParseUBOfAICoreMemoryRates(ai_core_memory_rates_map, platform_info_temp);
639 69 : }
640 :
641 23730 : static std::vector<std::string> Split(const std::string& str, char pattern)
642 : {
643 23730 : std::vector<std::string> res_vec;
644 23730 : if (str.empty()) {
645 0 : return res_vec;
646 : }
647 :
648 23730 : std::string str_and_pattern = str + pattern;
649 23730 : size_t pos = str_and_pattern.find(pattern);
650 23730 : size_t size = str_and_pattern.size();
651 125390 : while (pos != std::string::npos) {
652 101660 : std::string sub_str = str_and_pattern.substr(0, pos);
653 101660 : res_vec.push_back(sub_str);
654 101660 : str_and_pattern = str_and_pattern.substr(pos + 1, size);
655 101660 : pos = str_and_pattern.find(pattern);
656 101660 : }
657 23730 : return res_vec;
658 23730 : }
659 :
660 69 : void PlatformInfoManager::ParseAICoreintrinsicDtypeMap(
661 : std::map<std::string, std::string>& ai_coreintrinsic_dtype_map, PlatformInfo& platform_info_temp)
662 : {
663 69 : std::map<std::string, std::string>::const_iterator iter;
664 7202 : for (iter = ai_coreintrinsic_dtype_map.begin(); iter != ai_coreintrinsic_dtype_map.end(); iter++) {
665 7133 : size_t pos = iter->second.find('|');
666 7133 : if (pos == std::string::npos) {
667 0 : return;
668 : }
669 7133 : std::string key = iter->second.substr(0, pos);
670 7133 : std::string value = iter->second.substr(pos + 1);
671 7133 : std::vector<std::string> dtype_vector = Split(value, ',');
672 7133 : platform_info_temp.ai_core_intrinsic_dtype_map.emplace(make_pair(key, dtype_vector));
673 7133 : }
674 :
675 69 : return;
676 : }
677 :
678 6 : void PlatformInfoManager::ParseVectorCoreSpec(
679 : std::map<std::string, std::string>& vector_core_spec_map, PlatformInfo& platform_info_temp)
680 : {
681 : try {
682 6 : std::map<std::string, std::string>::const_iterator vec_freq = vector_core_spec_map.find(CONVERT(vec_freq));
683 6 : if (vec_freq != vector_core_spec_map.end()) {
684 5 : platform_info_temp.vector_core_spec.vec_freq = static_cast<double>(stod(vec_freq->second));
685 : }
686 :
687 : std::map<std::string, std::string>::const_iterator vec_calc_size =
688 6 : vector_core_spec_map.find(CONVERT(vec_calc_size));
689 6 : if (vec_calc_size != vector_core_spec_map.end()) {
690 5 : platform_info_temp.vector_core_spec.vec_calc_size = static_cast<uint64_t>(stoll(vec_calc_size->second));
691 : }
692 :
693 : std::map<std::string, std::string>::const_iterator smask_buffer =
694 6 : vector_core_spec_map.find(CONVERT(smask_buffer));
695 6 : if (smask_buffer != vector_core_spec_map.end()) {
696 5 : platform_info_temp.vector_core_spec.smask_buffer = static_cast<uint64_t>(stoll(smask_buffer->second));
697 : }
698 :
699 6 : std::map<std::string, std::string>::const_iterator ub_size = vector_core_spec_map.find(CONVERT(ub_size));
700 6 : if (ub_size != vector_core_spec_map.end()) {
701 5 : platform_info_temp.vector_core_spec.ub_size = static_cast<uint64_t>(stoll(ub_size->second));
702 : }
703 :
704 : std::map<std::string, std::string>::const_iterator ubblock_size =
705 6 : vector_core_spec_map.find(CONVERT(ubblock_size));
706 6 : if (ubblock_size != vector_core_spec_map.end()) {
707 5 : platform_info_temp.vector_core_spec.ubblock_size = static_cast<uint64_t>(stoll(ubblock_size->second));
708 : }
709 :
710 : std::map<std::string, std::string>::const_iterator ubbank_size =
711 6 : vector_core_spec_map.find(CONVERT(ubbank_size));
712 6 : if (ubbank_size != vector_core_spec_map.end()) {
713 5 : platform_info_temp.vector_core_spec.ubbank_size = static_cast<uint64_t>(stoll(ubbank_size->second));
714 : }
715 :
716 6 : std::map<std::string, std::string>::const_iterator ubbank_num = vector_core_spec_map.find(CONVERT(ubbank_num));
717 6 : if (ubbank_num != vector_core_spec_map.end()) {
718 5 : platform_info_temp.vector_core_spec.ubbank_num = static_cast<uint64_t>(stoll(ubbank_num->second));
719 : }
720 :
721 : std::map<std::string, std::string>::const_iterator ubburst_in_one_block =
722 6 : vector_core_spec_map.find(CONVERT(ubburst_in_one_block));
723 6 : if (ubburst_in_one_block != vector_core_spec_map.end()) {
724 5 : platform_info_temp.vector_core_spec.ubburst_in_one_block =
725 5 : static_cast<uint64_t>(stoll(ubburst_in_one_block->second));
726 : }
727 :
728 : std::map<std::string, std::string>::const_iterator ubbank_group_num =
729 6 : vector_core_spec_map.find(CONVERT(ubbank_group_num));
730 6 : if (ubbank_group_num != vector_core_spec_map.end()) {
731 5 : platform_info_temp.vector_core_spec.ubbank_group_num =
732 5 : static_cast<uint64_t>(stoll(ubbank_group_num->second));
733 : }
734 :
735 : std::map<std::string, std::string>::const_iterator vector_reg_width =
736 6 : vector_core_spec_map.find(CONVERT(vector_reg_width));
737 6 : if (vector_reg_width != vector_core_spec_map.end()) {
738 0 : platform_info_temp.vector_core_spec.vector_reg_size =
739 0 : static_cast<uint64_t>(stoll(vector_reg_width->second));
740 : }
741 :
742 : std::map<std::string, std::string>::const_iterator predicate_reg_width =
743 6 : vector_core_spec_map.find(CONVERT(predicate_reg_width));
744 6 : if (predicate_reg_width != vector_core_spec_map.end()) {
745 0 : platform_info_temp.vector_core_spec.predicate_reg_size =
746 0 : static_cast<uint64_t>(stoll(predicate_reg_width->second));
747 : }
748 0 : } catch (...) {
749 0 : PF_LOGE("Failed to load VectorCoreSpecs.");
750 0 : }
751 6 : }
752 :
753 6 : void PlatformInfoManager::ParseVectorCoreMemoryRates(
754 : std::map<std::string, std::string>& vector_core_memory_rates_map, PlatformInfo& platform_info_temp)
755 : {
756 : try {
757 : std::map<std::string, std::string>::const_iterator ddr_rate =
758 6 : vector_core_memory_rates_map.find(CONVERT(ddr_rate));
759 6 : if (ddr_rate != vector_core_memory_rates_map.end()) {
760 6 : platform_info_temp.vector_core_memory_rates.ddr_rate = static_cast<double>(stod(ddr_rate->second));
761 : }
762 :
763 : std::map<std::string, std::string>::const_iterator ddr_read_rate =
764 5 : vector_core_memory_rates_map.find(CONVERT(ddr_read_rate));
765 5 : if (ddr_read_rate != vector_core_memory_rates_map.end()) {
766 4 : platform_info_temp.vector_core_memory_rates.ddr_read_rate =
767 4 : static_cast<double>(stod(ddr_read_rate->second));
768 : }
769 :
770 : std::map<std::string, std::string>::const_iterator ddr_write_rate =
771 5 : vector_core_memory_rates_map.find(CONVERT(ddr_write_rate));
772 5 : if (ddr_write_rate != vector_core_memory_rates_map.end()) {
773 4 : platform_info_temp.vector_core_memory_rates.ddr_write_rate =
774 4 : static_cast<double>(stod(ddr_write_rate->second));
775 : }
776 :
777 : std::map<std::string, std::string>::const_iterator l2_rate =
778 5 : vector_core_memory_rates_map.find(CONVERT(l2_rate));
779 5 : if (l2_rate != vector_core_memory_rates_map.end()) {
780 5 : platform_info_temp.vector_core_memory_rates.l2_rate = static_cast<double>(stod(l2_rate->second));
781 : }
782 :
783 : std::map<std::string, std::string>::const_iterator l2_read_rate =
784 5 : vector_core_memory_rates_map.find(CONVERT(l2_read_rate));
785 5 : if (l2_read_rate != vector_core_memory_rates_map.end()) {
786 4 : platform_info_temp.vector_core_memory_rates.l2_read_rate = static_cast<double>(stod(l2_read_rate->second));
787 : }
788 :
789 : std::map<std::string, std::string>::const_iterator l2_write_rate =
790 5 : vector_core_memory_rates_map.find(CONVERT(l2_write_rate));
791 5 : if (l2_write_rate != vector_core_memory_rates_map.end()) {
792 4 : platform_info_temp.vector_core_memory_rates.l2_write_rate =
793 4 : static_cast<double>(stod(l2_write_rate->second));
794 : }
795 :
796 : std::map<std::string, std::string>::const_iterator ub_to_l2_rate =
797 5 : vector_core_memory_rates_map.find(CONVERT(ub_to_l2_rate));
798 5 : if (ub_to_l2_rate != vector_core_memory_rates_map.end()) {
799 5 : platform_info_temp.vector_core_memory_rates.ub_to_l2_rate =
800 5 : static_cast<double>(stod(ub_to_l2_rate->second));
801 : }
802 :
803 : std::map<std::string, std::string>::const_iterator ub_to_ddr_rate =
804 5 : vector_core_memory_rates_map.find(CONVERT(ub_to_ddr_rate));
805 5 : if (ub_to_ddr_rate != vector_core_memory_rates_map.end()) {
806 4 : platform_info_temp.vector_core_memory_rates.ub_to_ddr_rate =
807 4 : static_cast<double>(stod(ub_to_ddr_rate->second));
808 : }
809 1 : } catch (...) {
810 1 : PF_LOGE("Failed to load VectorCoreMemoryRates.");
811 1 : }
812 6 : }
813 :
814 82 : void PlatformInfoManager::ParseCPUCache(
815 : std::map<std::string, std::string>& CPUCacheMap, PlatformInfo& platform_info_temp)
816 : {
817 : try {
818 82 : std::map<std::string, std::string>::const_iterator AICPUSyncBySW = CPUCacheMap.find(CONVERT(AICPUSyncBySW));
819 82 : if (AICPUSyncBySW != CPUCacheMap.end()) {
820 81 : unsigned long value = std::stoul(AICPUSyncBySW->second);
821 81 : if (value > UINT32_MAX) {
822 0 : PF_LOGE("AICPUSyncBySW value [%lu] exceeds uint32 range", value);
823 0 : return;
824 : }
825 81 : platform_info_temp.cpucache.AICPUSyncBySW = static_cast<uint32_t>(value);
826 : }
827 :
828 82 : std::map<std::string, std::string>::const_iterator TSCPUSyncBySW = CPUCacheMap.find(CONVERT(TSCPUSyncBySW));
829 82 : if (TSCPUSyncBySW != CPUCacheMap.end()) {
830 81 : unsigned long value = std::stoul(TSCPUSyncBySW->second);
831 81 : if (value > UINT32_MAX) {
832 0 : PF_LOGE("TSCPUSyncBySW value [%lu] exceeds uint32 range", value);
833 0 : return;
834 : }
835 81 : platform_info_temp.cpucache.TSCPUSyncBySW = static_cast<uint32_t>(value);
836 : }
837 0 : } catch (const std::invalid_argument& e) {
838 0 : PF_LOGE("Invalid argument in ParseCPUCache: %s", e.what());
839 0 : return;
840 0 : } catch (const std::out_of_range& e) {
841 0 : PF_LOGE("Out of range in ParseCPUCache: %s", e.what());
842 0 : return;
843 0 : }
844 : }
845 :
846 62 : void PlatformInfoManager::ParseVectorCoreintrinsicDtypeMap(
847 : std::map<std::string, std::string>& vector_coreintrinsic_dtype_map, PlatformInfo& platform_info_temp)
848 : {
849 62 : std::map<std::string, std::string>::const_iterator iter;
850 4794 : for (iter = vector_coreintrinsic_dtype_map.begin(); iter != vector_coreintrinsic_dtype_map.end(); iter++) {
851 4732 : size_t pos = iter->second.find('|');
852 4732 : if (pos == std::string::npos) {
853 0 : PF_LOGE("The intrinsic_dtype_map string does not contain '|'.");
854 0 : return;
855 : }
856 4732 : std::string key = iter->second.substr(0, pos);
857 4732 : std::string value = iter->second.substr(pos + 1);
858 4732 : std::vector<std::string> dtype_vector = Split(value, ',');
859 4732 : platform_info_temp.vector_core_intrinsic_dtype_map.emplace(make_pair(key, dtype_vector));
860 4732 : }
861 : }
862 :
863 24 : void PlatformInfoManager::ParseSoftwareSpec(
864 : std::map<std::string, std::string>& software_spec_map, PlatformInfo& platform_info_temp)
865 : {
866 : try {
867 : std::map<std::string, std::string>::const_iterator jit_compile_default_value =
868 24 : software_spec_map.find(CONVERT(jit_compile_default_value));
869 24 : if (jit_compile_default_value != software_spec_map.end() && jit_compile_default_value->second == "1") {
870 23 : platform_info_temp.software_spec.jit_compile_default_value = true;
871 23 : platform_info_temp.software_spec.jit_compile_mode = JitCompileMode::COMPILE_ONLINE;
872 : } else {
873 1 : platform_info_temp.software_spec.jit_compile_default_value = false;
874 : }
875 0 : } catch (...) {
876 0 : PF_LOGE("Failed to load software specification.");
877 0 : }
878 24 : }
879 :
880 81 : uint32_t PlatformInfoManager::ParsePlatformInfoFromStrToStruct(
881 : std::map<std::string, std::map<std::string, std::string>>& content_info_map, std::string& soc_version,
882 : PlatformInfo& platform_info_temp)
883 : {
884 81 : std::map<std::string, std::map<std::string, std::string>>::iterator it;
885 833 : for (it = content_info_map.begin(); it != content_info_map.end(); it++) {
886 752 : auto iter = g_platform_info_type_map.find(it->first);
887 752 : if (iter == g_platform_info_type_map.end()) {
888 197 : continue;
889 : }
890 :
891 555 : switch (iter->second) {
892 81 : case PlatformInfoType::EN_VERSION: {
893 81 : ParseVersion(it->second, soc_version, platform_info_temp);
894 81 : break;
895 : }
896 81 : case PlatformInfoType::EN_SOC_INFO: {
897 81 : ParseSocInfo(it->second, platform_info_temp);
898 81 : break;
899 : }
900 81 : case PlatformInfoType::EN_AI_CORE_SPEC: {
901 81 : ParseAICoreSpec(it->second, platform_info_temp);
902 81 : break;
903 : }
904 69 : case PlatformInfoType::EN_AI_CORE_MEMORY_RATES: {
905 69 : ParseAICoreMemoryRates(it->second, platform_info_temp);
906 69 : break;
907 : }
908 81 : case PlatformInfoType::EN_CPU_CACHE: {
909 81 : ParseCPUCache(it->second, platform_info_temp);
910 81 : break;
911 : }
912 69 : case PlatformInfoType::EN_AI_CORE_INTRINSIC_DTYPE_MAP: {
913 69 : ParseAICoreintrinsicDtypeMap(it->second, platform_info_temp);
914 69 : break;
915 : }
916 4 : case PlatformInfoType::EN_VECTOR_CORE_SPEC: {
917 4 : ParseVectorCoreSpec(it->second, platform_info_temp);
918 4 : break;
919 : }
920 4 : case PlatformInfoType::EN_VECTOR_CORE_MEMORY_RATES: {
921 4 : ParseVectorCoreMemoryRates(it->second, platform_info_temp);
922 4 : break;
923 : }
924 62 : case PlatformInfoType::EN_VECTOR_CORE_INTRINSIC_DTYPE_MAP: {
925 62 : ParseVectorCoreintrinsicDtypeMap(it->second, platform_info_temp);
926 62 : break;
927 : }
928 23 : case PlatformInfoType::EN_SOFTWARE_SPEC: {
929 23 : ParseSoftwareSpec(it->second, platform_info_temp);
930 23 : break;
931 : }
932 0 : default: {
933 0 : return PLATFORM_FAILED;
934 : }
935 : }
936 : }
937 :
938 81 : return PLATFORM_SUCCESS;
939 : }
940 :
941 81 : uint32_t PlatformInfoManager::AssemblePlatformInfoVector(
942 : std::map<std::string, std::map<std::string, std::string>>& content_info_map)
943 : {
944 81 : std::string soc_version;
945 81 : PlatformInfo platform_info_temp;
946 81 : uint32_t ret = ParsePlatformInfoFromStrToStruct(content_info_map, soc_version, platform_info_temp);
947 81 : if (ret != PLATFORM_SUCCESS) {
948 0 : PF_LOGE("Parse platform info from content failed.");
949 0 : return PLATFORM_FAILED;
950 : }
951 :
952 81 : if (!soc_version.empty()) {
953 81 : auto iter = platform_info_map_.find(soc_version);
954 81 : if (iter == platform_info_map_.end()) {
955 81 : platform_info_map_.emplace(make_pair(soc_version, platform_info_temp));
956 : }
957 : }
958 :
959 81 : PlatFormInfos platform_infos;
960 81 : if (!platform_infos.Init()) {
961 0 : return PLATFORM_FAILED;
962 : }
963 :
964 81 : if (ParsePlatformInfo(content_info_map, soc_version, platform_infos) != PLATFORM_SUCCESS) {
965 0 : PF_LOGE("Parse platform info from content failed.");
966 0 : return PLATFORM_FAILED;
967 : }
968 81 : FillupFixPipeInfo(platform_infos);
969 81 : if (!soc_version.empty()) {
970 81 : auto iter = platform_infos_map_.find(soc_version);
971 81 : if (iter == platform_infos_map_.end()) {
972 81 : platform_infos_map_.emplace(soc_version, platform_infos);
973 81 : PF_LOGD("The platform info's os soc version[%s] has been initialized.", soc_version.c_str());
974 : }
975 : }
976 81 : return PLATFORM_SUCCESS;
977 81 : }
978 :
979 81 : void PlatformInfoManager::FillupFixPipeInfo(PlatFormInfos& platform_infos)
980 : {
981 81 : std::string is_support_fixpipe_str;
982 475 : if (!platform_infos.GetPlatformResWithLock("AICoreSpec", "support_fixpipe", is_support_fixpipe_str) ||
983 70 : !static_cast<bool>(std::atoi(is_support_fixpipe_str.c_str()))) {
984 11 : return;
985 : }
986 70 : std::map<std::string, std::vector<std::string>> aicore_map = platform_infos.GetAICoreIntrinsicDtype();
987 70 : std::map<std::string, std::vector<std::string>> fixpipe_map_;
988 5986 : for (auto iter = aicore_map.begin(); iter != aicore_map.end(); iter++) {
989 5916 : if (iter->first.find(FIXPIPE_CONFIG_KEY) != iter->first.npos) {
990 928 : fixpipe_map_.emplace(make_pair(iter->first, iter->second));
991 : }
992 : }
993 70 : if (fixpipe_map_.empty()) {
994 12 : return;
995 : }
996 58 : platform_infos.SetFixPipeDtypeMap(fixpipe_map_);
997 105 : }
998 :
999 69 : void PlatformInfoManager::ParseAICoreintrinsicDtypeMap(
1000 : std::map<std::string, std::string>& ai_coreintrinsic_dtype_map, PlatFormInfos& platform_info_temp)
1001 : {
1002 69 : std::map<std::string, std::string>::const_iterator iter;
1003 69 : std::map<std::string, std::string> platform_res_map;
1004 : std::map<std::string, std::vector<std::string>> aicore_intrinsic_dtypes =
1005 69 : platform_info_temp.GetAICoreIntrinsicDtype();
1006 7202 : for (iter = ai_coreintrinsic_dtype_map.begin(); iter != ai_coreintrinsic_dtype_map.end(); iter++) {
1007 7133 : size_t pos = iter->second.find('|');
1008 7133 : if (pos == std::string::npos) {
1009 0 : return;
1010 : }
1011 7133 : std::string key = iter->second.substr(0, pos);
1012 7133 : std::string value = iter->second.substr(pos + 1);
1013 7133 : std::vector<std::string> dtype_vector = Split(value, ',');
1014 7133 : platform_res_map.emplace(make_pair(key, value));
1015 7133 : aicore_intrinsic_dtypes.emplace(make_pair(key, dtype_vector));
1016 7133 : }
1017 :
1018 69 : platform_info_temp.SetPlatformRes(STR_AI_CORE_INTRINSIC_DTYPE_MAP, platform_res_map);
1019 69 : platform_info_temp.SetAICoreIntrinsicDtype(aicore_intrinsic_dtypes);
1020 69 : }
1021 :
1022 62 : void PlatformInfoManager::ParseVectorCoreintrinsicDtypeMap(
1023 : std::map<std::string, std::string>& vector_coreintrinsic_dtype_map, PlatFormInfos& platform_info_temp)
1024 : {
1025 62 : std::map<std::string, std::string>::const_iterator iter;
1026 62 : std::map<std::string, std::string> platform_res_map;
1027 : std::map<std::string, std::vector<std::string>> vector_core_intrinsic_dtype_map =
1028 62 : platform_info_temp.GetVectorCoreIntrinsicDtype();
1029 4794 : for (iter = vector_coreintrinsic_dtype_map.begin(); iter != vector_coreintrinsic_dtype_map.end(); iter++) {
1030 4732 : size_t pos = iter->second.find('|');
1031 4732 : if (pos == std::string::npos) {
1032 0 : PF_LOGE("The intrinsic_dtype_map string does not contain '|'.");
1033 0 : return;
1034 : }
1035 4732 : std::string key = iter->second.substr(0, pos);
1036 4732 : std::string value = iter->second.substr(pos + 1);
1037 4732 : std::vector<std::string> dtype_vector = Split(value, ',');
1038 4732 : platform_res_map.emplace(make_pair(key, value));
1039 4732 : vector_core_intrinsic_dtype_map.emplace(make_pair(key, dtype_vector));
1040 4732 : }
1041 :
1042 62 : platform_info_temp.SetPlatformRes(STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP, platform_res_map);
1043 62 : platform_info_temp.SetVectorCoreIntrinsicDtype(vector_core_intrinsic_dtype_map);
1044 62 : }
1045 :
1046 621 : void PlatformInfoManager::ParsePlatformRes(
1047 : const std::string& label, std::map<std::string, std::string>& platform_res_map, PlatFormInfos& platform_info_temp)
1048 : {
1049 621 : platform_info_temp.SetPlatformRes(label, platform_res_map);
1050 621 : }
1051 :
1052 81 : uint32_t PlatformInfoManager::ParsePlatformInfo(
1053 : std::map<std::string, std::map<std::string, std::string>>& content_info_map, std::string& soc_version,
1054 : PlatFormInfos& platform_info_temp)
1055 : {
1056 : (void)soc_version;
1057 81 : std::map<std::string, std::map<std::string, std::string>>::iterator it;
1058 833 : for (it = content_info_map.begin(); it != content_info_map.end(); it++) {
1059 752 : if (it->first == STR_AI_CORE_INTRINSIC_DTYPE_MAP) {
1060 69 : ParseAICoreintrinsicDtypeMap(it->second, platform_info_temp);
1061 683 : } else if (it->first == STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP) {
1062 62 : ParseVectorCoreintrinsicDtypeMap(it->second, platform_info_temp);
1063 : } else {
1064 621 : ParsePlatformRes(it->first, it->second, platform_info_temp);
1065 : }
1066 : }
1067 :
1068 81 : return PLATFORM_SUCCESS;
1069 : }
1070 :
1071 142 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::InitializePlatformInfo()
1072 : {
1073 : // add lock
1074 142 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1075 142 : if (init_flag_) {
1076 1 : return PLATFORM_SUCCESS;
1077 : }
1078 141 : cfg_file_real_path_ = fe::GetConfigFilePath<PlatformInfoManager>();
1079 141 : if (cfg_file_real_path_.empty()) {
1080 0 : PF_LOGE("File path[%s] is not valid.", cfg_file_real_path_.c_str());
1081 0 : return PLATFORM_FAILED;
1082 : }
1083 :
1084 141 : if (!opti_compilation_infos_.Init()) {
1085 0 : PF_LOGE("Failed to initialize optional information.");
1086 0 : return PLATFORM_FAILED;
1087 : }
1088 :
1089 141 : init_flag_ = true;
1090 :
1091 141 : return PLATFORM_SUCCESS;
1092 142 : }
1093 :
1094 2 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::InitializePlatformInfo(std::string socVersion)
1095 : {
1096 : // add lock
1097 2 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1098 2 : if (init_flag_) {
1099 0 : return PLATFORM_SUCCESS;
1100 : }
1101 2 : cfg_file_real_path_ = fe::GetConfigFilePath<PlatformInfoManager>(socVersion);
1102 2 : if (cfg_file_real_path_.empty()) {
1103 1 : PF_LOGE("File path[%s] is not valid.", cfg_file_real_path_.c_str());
1104 1 : return PLATFORM_FAILED;
1105 : }
1106 :
1107 1 : if (!opti_compilation_infos_.Init()) {
1108 0 : PF_LOGE("Failed to initialize optional information.");
1109 0 : return PLATFORM_FAILED;
1110 : }
1111 :
1112 1 : init_flag_ = true;
1113 :
1114 1 : return PLATFORM_SUCCESS;
1115 2 : }
1116 :
1117 6 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::GetPlatformInfo(
1118 : const std::string SoCVersion, PlatformInfo& platform_info, OptionalInfo& opti_compilation_info)
1119 : {
1120 6 : std::string realSocVersion = SoCVersion;
1121 6 : if (realSocVersion == SOC_VERSION_ASCEND910) {
1122 1 : realSocVersion = SOC_VERSION_ASCEND910A;
1123 : }
1124 6 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1125 6 : auto iter = platform_info_map_.find(realSocVersion);
1126 6 : if (iter == platform_info_map_.end()) {
1127 5 : if (EnsureSocVersionLoaded(realSocVersion) != PLATFORM_SUCCESS) {
1128 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", realSocVersion.c_str());
1129 1 : return PLATFORM_FAILED;
1130 : }
1131 4 : iter = platform_info_map_.find(realSocVersion);
1132 4 : if (iter == platform_info_map_.end()) {
1133 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", realSocVersion.c_str());
1134 1 : return PLATFORM_FAILED;
1135 : }
1136 : }
1137 4 : platform_info = iter->second;
1138 4 : opti_compilation_info = opti_compilation_info_;
1139 4 : return PLATFORM_SUCCESS;
1140 6 : }
1141 :
1142 4 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::GetPlatformInfoWithOutSocVersion(
1143 : PlatformInfo& platform_info, OptionalInfo& opti_compilation_info)
1144 : {
1145 4 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1146 4 : if (opti_compilation_info_.soc_version.empty()) {
1147 1 : PF_LOGW("Cannot find platform_info.");
1148 1 : return PLATFORM_FAILED;
1149 : }
1150 3 : auto iter = platform_info_map_.find(opti_compilation_info_.soc_version);
1151 3 : if (iter == platform_info_map_.end()) {
1152 2 : if (EnsureSocVersionLoaded(opti_compilation_info_.soc_version) != PLATFORM_SUCCESS) {
1153 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", opti_compilation_info_.soc_version.c_str());
1154 1 : return PLATFORM_FAILED;
1155 : }
1156 1 : iter = platform_info_map_.find(opti_compilation_info_.soc_version);
1157 1 : if (iter == platform_info_map_.end()) {
1158 0 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", opti_compilation_info_.soc_version.c_str());
1159 0 : return PLATFORM_FAILED;
1160 : }
1161 : }
1162 2 : platform_info = iter->second;
1163 2 : opti_compilation_info = opti_compilation_info_;
1164 2 : return PLATFORM_SUCCESS;
1165 4 : }
1166 :
1167 4 : void PlatformInfoManager::SetOptionalCompilationInfo(OptionalInfo& opti_compilation_info)
1168 : {
1169 : // add lock
1170 4 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1171 4 : opti_compilation_info_ = opti_compilation_info;
1172 4 : if (opti_compilation_info_.soc_version == SOC_VERSION_ASCEND910) {
1173 1 : opti_compilation_info_.soc_version = SOC_VERSION_ASCEND910A;
1174 : }
1175 4 : }
1176 :
1177 67 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::GetPlatformInfos(
1178 : const std::string SoCVersion, PlatFormInfos& platform_info, OptionalInfos& opti_compilation_info)
1179 : {
1180 67 : std::string realSocVersion = SoCVersion;
1181 67 : if (realSocVersion == SOC_VERSION_ASCEND910) {
1182 1 : realSocVersion = SOC_VERSION_ASCEND910A;
1183 : }
1184 67 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1185 67 : auto iter = platform_infos_map_.find(realSocVersion);
1186 67 : if (iter == platform_infos_map_.end()) {
1187 58 : if (EnsureSocVersionLoaded(realSocVersion) != PLATFORM_SUCCESS) {
1188 2 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", realSocVersion.c_str());
1189 2 : return PLATFORM_FAILED;
1190 : }
1191 56 : iter = platform_infos_map_.find(realSocVersion);
1192 56 : if (iter == platform_infos_map_.end()) {
1193 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", realSocVersion.c_str());
1194 1 : return PLATFORM_FAILED;
1195 : }
1196 : }
1197 64 : platform_info = iter->second;
1198 64 : opti_compilation_info = opti_compilation_infos_;
1199 64 : opti_compilation_info.SetFixPipeDtypeMap(platform_info.GetFixPipeDtypeMap());
1200 64 : return PLATFORM_SUCCESS;
1201 67 : }
1202 :
1203 40 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::GetPlatformInfoWithOutSocVersion(
1204 : PlatFormInfos& platform_info, OptionalInfos& opti_compilation_info)
1205 : {
1206 40 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1207 40 : std::string real_soc = opti_compilation_infos_.GetSocVersion();
1208 40 : if (real_soc.empty()) {
1209 10 : PF_LOGW("Cannot find platform_info.");
1210 10 : return PLATFORM_FAILED;
1211 : }
1212 30 : auto iter = platform_infos_map_.find(real_soc);
1213 30 : if (iter == platform_infos_map_.end()) {
1214 3 : if (EnsureSocVersionLoaded(real_soc) != PLATFORM_SUCCESS) {
1215 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", real_soc.c_str());
1216 1 : return PLATFORM_FAILED;
1217 : }
1218 2 : iter = platform_infos_map_.find(real_soc);
1219 2 : if (iter == platform_infos_map_.end()) {
1220 1 : PF_LOGE("Cannot find platform_info by SoCVersion %s.", real_soc.c_str());
1221 1 : return PLATFORM_FAILED;
1222 : }
1223 : }
1224 28 : platform_info = iter->second;
1225 28 : if (!platform_info.GetFixPipeDtypeMap().empty()) {
1226 28 : opti_compilation_infos_.SetFixPipeDtypeMap(platform_info.GetFixPipeDtypeMap());
1227 : }
1228 28 : opti_compilation_info = opti_compilation_infos_;
1229 28 : return PLATFORM_SUCCESS;
1230 40 : }
1231 :
1232 9 : __attribute__((visibility("default"))) uint32_t PlatformInfoManager::GetPlatformInstanceByDevice(
1233 : const uint32_t& device_id, PlatFormInfos& platform_info)
1234 : {
1235 9 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1236 9 : std::string real_soc = opti_compilation_infos_.GetSocVersion();
1237 9 : auto iter = device_platform_infos_map_.find(device_id);
1238 9 : if (iter == device_platform_infos_map_.end()) {
1239 9 : if (real_soc.empty()) {
1240 2 : PF_LOGW("Not initialize soc_version of optional_infos.");
1241 2 : return PLATFORM_FAILED;
1242 : }
1243 7 : if (platform_infos_map_.find(real_soc) == platform_infos_map_.end()) {
1244 6 : if (EnsureSocVersionLoaded(real_soc) != PLATFORM_SUCCESS) {
1245 1 : PF_LOGW("Failed to initialize platform info map.");
1246 1 : return PLATFORM_FAILED;
1247 : }
1248 5 : if (platform_infos_map_.find(real_soc) == platform_infos_map_.end()) {
1249 1 : PF_LOGW("Failed to initialize platform info map.");
1250 1 : return PLATFORM_FAILED;
1251 : }
1252 : }
1253 5 : device_platform_infos_map_[device_id] = platform_infos_map_[real_soc];
1254 : }
1255 5 : platform_info = device_platform_infos_map_[device_id];
1256 5 : return PLATFORM_SUCCESS;
1257 9 : }
1258 :
1259 49 : void PlatformInfoManager::SetOptionalCompilationInfo(OptionalInfos& opti_compilation_info)
1260 : {
1261 : // add lock
1262 49 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1263 49 : opti_compilation_infos_ = opti_compilation_info;
1264 49 : if (opti_compilation_infos_.GetSocVersion() == SOC_VERSION_ASCEND910) {
1265 1 : opti_compilation_infos_.SetSocVersion(SOC_VERSION_ASCEND910A);
1266 : }
1267 49 : }
1268 :
1269 5 : uint32_t PlatformInfoManager::UpdatePlatformInfos(fe::PlatFormInfos& platform_info)
1270 : {
1271 : // add lock
1272 5 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1273 5 : if (opti_compilation_infos_.GetSocVersion().empty()) {
1274 2 : PF_LOGE("Soc_version in the optional information is empty.");
1275 2 : return PLATFORM_FAILED;
1276 : }
1277 3 : auto iter = platform_infos_map_.find(opti_compilation_infos_.GetSocVersion());
1278 3 : if (iter == platform_infos_map_.end()) {
1279 2 : if (EnsureSocVersionLoaded(opti_compilation_infos_.GetSocVersion()) != PLATFORM_SUCCESS) {
1280 1 : PF_LOGE(
1281 : "Platform info for soc_version [%s] is not initialized.",
1282 : opti_compilation_infos_.GetSocVersion().c_str());
1283 1 : return PLATFORM_FAILED;
1284 : }
1285 1 : iter = platform_infos_map_.find(opti_compilation_infos_.GetSocVersion());
1286 1 : if (iter == platform_infos_map_.end()) {
1287 0 : PF_LOGE(
1288 : "Platform info for soc_version [%s] is not initialized.",
1289 : opti_compilation_infos_.GetSocVersion().c_str());
1290 0 : return PLATFORM_FAILED;
1291 : }
1292 : }
1293 2 : platform_infos_map_[opti_compilation_infos_.GetSocVersion()] = platform_info;
1294 2 : return PLATFORM_SUCCESS;
1295 5 : }
1296 :
1297 6 : uint32_t PlatformInfoManager::UpdatePlatformInfos(const std::string& soc_version, fe::PlatFormInfos& platform_info)
1298 : {
1299 : // add lock
1300 6 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1301 6 : if (soc_version.empty()) {
1302 1 : PF_LOGE("Soc_version is empty.");
1303 1 : return PLATFORM_FAILED;
1304 : }
1305 5 : std::string real_soc = soc_version;
1306 5 : if (real_soc == SOC_VERSION_ASCEND910) {
1307 1 : real_soc = SOC_VERSION_ASCEND910A;
1308 : }
1309 5 : auto iter = platform_infos_map_.find(real_soc);
1310 5 : if (iter == platform_infos_map_.end()) {
1311 4 : if (EnsureSocVersionLoaded(real_soc) != PLATFORM_SUCCESS) {
1312 1 : PF_LOGE("Platform info for soc_version [%s] is not initialized.", real_soc.c_str());
1313 1 : return PLATFORM_FAILED;
1314 : }
1315 3 : iter = platform_infos_map_.find(real_soc);
1316 3 : if (iter == platform_infos_map_.end()) {
1317 0 : PF_LOGE("Platform info for soc_version [%s] is not initialized.", real_soc.c_str());
1318 0 : return PLATFORM_FAILED;
1319 : }
1320 : }
1321 4 : opti_compilation_infos_.SetSocVersionWithLock(real_soc);
1322 4 : platform_infos_map_[real_soc] = platform_info;
1323 4 : return PLATFORM_SUCCESS;
1324 6 : }
1325 :
1326 162 : uint32_t PlatformInfoManager::Finalize()
1327 : {
1328 : // add lock
1329 162 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1330 162 : if (!init_flag_) {
1331 20 : return PLATFORM_SUCCESS;
1332 : }
1333 :
1334 142 : platform_info_map_.clear();
1335 :
1336 142 : platform_infos_map_.clear();
1337 :
1338 142 : device_platform_infos_map_.clear();
1339 :
1340 142 : runtime_device_platform_infos_map_.clear();
1341 :
1342 142 : loaded_ini_files_.clear();
1343 :
1344 142 : cfg_file_real_path_.clear();
1345 :
1346 142 : init_flag_ = false;
1347 :
1348 142 : return PLATFORM_SUCCESS;
1349 162 : }
1350 :
1351 16 : uint32_t PlatformInfoManager::GetRuntimePlatformInfosByDevice(
1352 : const uint32_t& device_id, PlatFormInfos& platform_infos, bool need_deep_copy)
1353 : {
1354 16 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1355 16 : const auto it = runtime_device_platform_infos_map_.find(device_id);
1356 16 : PlatFormInfos device_platform_infos;
1357 16 : if (it == runtime_device_platform_infos_map_.end()) {
1358 14 : if (runtime_init_flag_) {
1359 8 : PF_LOGD("Add new platform info with device_id %u.", device_id);
1360 8 : PlatformInfosUtils::GetInstance().Clone(device_platform_infos, runtime_platform_infos_);
1361 8 : runtime_device_platform_infos_map_[device_id] = device_platform_infos;
1362 : } else {
1363 6 : PF_LOGW("Failed to update cache: runtime_platform has not been initialized.");
1364 : }
1365 : } else {
1366 2 : PF_LOGD("Return the platform infos with device_id %u.", device_id);
1367 2 : device_platform_infos = it->second;
1368 : }
1369 16 : if (need_deep_copy) {
1370 0 : PlatformInfosUtils::GetInstance().Clone(platform_infos, device_platform_infos);
1371 : } else {
1372 16 : platform_infos = device_platform_infos;
1373 : }
1374 16 : return PLATFORM_SUCCESS;
1375 16 : }
1376 :
1377 8 : uint32_t PlatformInfoManager::UpdateRuntimePlatformInfosByDevice(
1378 : const uint32_t& device_id, PlatFormInfos& platform_infos)
1379 : {
1380 8 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1381 8 : PF_LOGD("Update the platform info with device_id %u.", device_id);
1382 8 : PlatFormInfos device_platform_infos;
1383 8 : PlatformInfosUtils::GetInstance().Clone(device_platform_infos, platform_infos);
1384 8 : runtime_device_platform_infos_map_[device_id] = device_platform_infos;
1385 8 : return PLATFORM_SUCCESS;
1386 8 : }
1387 :
1388 13 : uint32_t PlatformInfoManager::InitRuntimePlatformInfos(const std::string& SoCVersion)
1389 : {
1390 13 : std::lock_guard<std::mutex> lock_guard(pc_lock_);
1391 13 : if (runtime_init_flag_) {
1392 1 : return PLATFORM_SUCCESS;
1393 : }
1394 :
1395 12 : if (cfg_file_real_path_.empty()) {
1396 11 : cfg_file_real_path_ = fe::GetConfigFilePath<PlatformInfoManager>();
1397 11 : if (cfg_file_real_path_.empty()) {
1398 0 : PF_LOGE("File path[%s] is not valid.", cfg_file_real_path_.c_str());
1399 0 : return PLATFORM_FAILED;
1400 : }
1401 : }
1402 :
1403 12 : std::string real_soc = SoCVersion;
1404 12 : if (real_soc == SOC_VERSION_ASCEND910) {
1405 1 : real_soc = SOC_VERSION_ASCEND910A;
1406 : }
1407 :
1408 12 : if (EnsureSocVersionLoaded(real_soc) != PLATFORM_SUCCESS) {
1409 1 : PF_LOGE("Failed to load ini file for soc[%s].", real_soc.c_str());
1410 1 : return PLATFORM_FAILED;
1411 : }
1412 :
1413 11 : const auto it = platform_infos_map_.find(real_soc);
1414 11 : if (it == platform_infos_map_.end()) {
1415 0 : std::string errLevel = "E20101";
1416 0 : std::vector<const char*> args_keys;
1417 0 : const char* SocVersion = SoCVersion.c_str();
1418 0 : std::vector<const char*> args_values;
1419 0 : args_keys.push_back("value");
1420 0 : args_keys.push_back("parameter");
1421 0 : args_keys.push_back("reason");
1422 0 : args_values.push_back(SocVersion);
1423 0 : args_values.push_back("SoCVersion");
1424 0 : args_values.push_back("SocVersion may be incorrect");
1425 0 : REPORT_PREDEFINED_ERRMSG_3PARAMS(errLevel.c_str(), args_keys, args_values);
1426 0 : PF_LOGE("Not find platformInfos, SocVersion: [%s]", SoCVersion.c_str());
1427 0 : return PLATFORM_FAILED;
1428 0 : }
1429 11 : PlatformInfosUtils::GetInstance().Clone(runtime_platform_infos_, it->second);
1430 11 : runtime_init_flag_ = true;
1431 11 : return PLATFORM_SUCCESS;
1432 13 : }
1433 :
1434 : } // namespace fe
|