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 : #include "acl_rt_impl_base.h"
11 : #include <mutex>
12 : #include <string>
13 : #include <fstream>
14 : #include <sstream>
15 : #include <map>
16 : #include <unordered_set>
17 : #include <string>
18 : #include "runtime/dev.h"
19 : #include "acl_rt_impl.h"
20 : #include "acl/acl_base.h"
21 : #include "common/log_inner.h"
22 : #include "common/error_codes_inner.h"
23 : #include "platform/platform_info.h"
24 : #include "runtime/config.h"
25 :
26 : namespace {
27 : std::mutex aclSocVersionMutex;
28 : std::string aclSocVersion;
29 : constexpr size_t SOC_VERSION_LEN = 128U;
30 : std::recursive_mutex aclInitMutex;
31 : uint64_t aclInitRefCount = 0;
32 : std::string aclConfigStr;
33 : } // namespace
34 :
35 : namespace acl {
36 : constexpr int32_t MODULE_TYPE_VECTOR_CORE = 7;
37 : constexpr int32_t MODULE_TYPE_AICORE = 4;
38 : constexpr int32_t INFO_TYPE_CORE_NUM = 3;
39 : std::mutex g_platformInfoInitMutex;
40 : std::unordered_set<int32_t> g_platformInfoInitSet;
41 :
42 14 : aclError UpdatePlatformInfoWithDevice(int32_t deviceId)
43 : {
44 : #ifdef __GNUC__
45 : // init platform info
46 14 : const char* socName = aclrtGetSocNameImpl();
47 14 : if (socName == nullptr) {
48 0 : ACL_LOG_ERROR("Init SocVersion failed");
49 0 : return ACL_ERROR_INTERNAL_ERROR;
50 : }
51 14 : const std::string socVersion(socName);
52 14 : if (fe::PlatformInfoManager::GeInstance().InitRuntimePlatformInfos(socVersion) != 0U) {
53 2 : ACL_LOG_WARN(
54 : "[Init][PlatformInfo]init runtime platform info unsuccessfully, SocVersion = %s", socVersion.c_str());
55 2 : return ACL_ERROR_INTERNAL_ERROR;
56 : }
57 :
58 12 : const std::unique_lock<std::mutex> lk(g_platformInfoInitMutex);
59 12 : if (g_platformInfoInitSet.count(deviceId) > 0U) {
60 5 : return ACL_SUCCESS;
61 : }
62 :
63 7 : uint32_t aicCnt = 0U;
64 7 : auto rtErr = rtGetAiCoreCount(&aicCnt);
65 7 : if (rtErr != RT_ERROR_NONE) {
66 1 : ACL_LOG_WARN("get aicore count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
67 1 : return ACL_GET_ERRCODE_RTS(rtErr);
68 : }
69 :
70 6 : int64_t vecCoreCnt = 0U;
71 : // some chips has no vector core
72 6 : rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_VECTOR_CORE, INFO_TYPE_CORE_NUM, &vecCoreCnt);
73 6 : if (rtErr != RT_ERROR_NONE) {
74 1 : ACL_LOG_WARN("get vector core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
75 1 : return ACL_GET_ERRCODE_RTS(rtErr);
76 : }
77 :
78 5 : int64_t cubeCoreCnt = 0U;
79 5 : rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_AICORE, INFO_TYPE_CUBE_NUM, &cubeCoreCnt);
80 5 : if (rtErr != RT_ERROR_NONE) {
81 1 : ACL_LOG_WARN("get cube core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
82 1 : return ACL_GET_ERRCODE_RTS(rtErr);
83 : }
84 :
85 4 : fe::PlatFormInfos platformInfos;
86 : uint32_t platformRet =
87 4 : fe::PlatformInfoManager::GeInstance().GetRuntimePlatformInfosByDevice(deviceId, platformInfos);
88 4 : if (platformRet != 0U) {
89 1 : ACL_LOG_WARN("get runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
90 1 : return ACL_ERROR_INTERNAL_ERROR;
91 : }
92 :
93 6 : const std::string socInfoKey = "SoCInfo";
94 6 : const std::string aicCntKey = "ai_core_cnt";
95 6 : const std::string vecCoreCntKey = "vector_core_cnt";
96 3 : const std::string cubeCoreCntKey = "cube_core_cnt";
97 3 : std::map<std::string, std::string> res;
98 3 : if (!platformInfos.GetPlatformResWithLock(socInfoKey, res)) {
99 1 : ACL_LOG_WARN("unable to get platform result");
100 1 : return ACL_ERROR_INTERNAL_ERROR;
101 : }
102 :
103 2 : res[aicCntKey] = std::to_string(aicCnt);
104 2 : res[vecCoreCntKey] = std::to_string(vecCoreCnt);
105 2 : res[cubeCoreCntKey] = std::to_string(cubeCoreCnt);
106 2 : platformInfos.SetPlatformResWithLock(socInfoKey, res);
107 2 : platformRet = fe::PlatformInfoManager::GeInstance().UpdateRuntimePlatformInfosByDevice(deviceId, platformInfos);
108 2 : if (platformRet != 0U) {
109 1 : ACL_LOG_WARN("update runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
110 1 : return ACL_ERROR_INTERNAL_ERROR;
111 : }
112 1 : (void)g_platformInfoInitSet.emplace(deviceId);
113 1 : ACL_LOG_INFO(
114 : "Successfully to UpdatePlatformInfoWithDevice, deviceId = %d, aicCnt = %u, vecCoreCnt = %ld, "
115 : "cubeCoreCnt = %ld",
116 : deviceId, aicCnt, vecCoreCnt, cubeCoreCnt);
117 : #endif
118 1 : return ACL_SUCCESS;
119 14 : }
120 :
121 50 : aclError InitSocVersion()
122 : {
123 50 : const std::unique_lock<std::mutex> lk(aclSocVersionMutex);
124 50 : if (aclSocVersion.empty()) {
125 : // get socVersion
126 2 : char_t socVersion[SOC_VERSION_LEN] = {};
127 2 : const auto rtErr = rtGetSocVersion(socVersion, static_cast<uint32_t>(sizeof(socVersion)));
128 2 : if (rtErr != RT_ERROR_NONE) {
129 1 : ACL_LOG_INFO("Cannot get soc version, runtime errorCode is %d", static_cast<int32_t>(rtErr));
130 1 : return ACL_GET_ERRCODE_RTS(rtErr);
131 : }
132 2 : aclSocVersion = std::string(socVersion);
133 : }
134 49 : ACL_LOG_INFO("get SocVersion success, SocVersion = %s", aclSocVersion.c_str());
135 49 : return ACL_SUCCESS;
136 50 : }
137 :
138 8 : const std::string& GetSocVersion()
139 : {
140 8 : ACL_LOG_INFO("socVersion is %s", aclSocVersion.c_str());
141 8 : return aclSocVersion;
142 : }
143 :
144 72 : bool GetAclInitFlag() { return aclInitRefCount > 0UL; }
145 :
146 94 : uint64_t& GetAclInitRefCount() { return aclInitRefCount; }
147 :
148 99 : std::recursive_mutex& GetAclInitMutex() { return aclInitMutex; }
149 :
150 4 : std::string& GetConfigPathStr() { return aclConfigStr; }
151 :
152 44 : void SetConfigPathStr(std::string& configStr) { aclConfigStr = configStr; }
153 :
154 46 : aclError GetStrFromConfigPath(const char* configPath, std::string& configStr)
155 : {
156 : // 文件路径为空,按照空文件处理
157 46 : if (configPath != nullptr && strlen(configPath) != 0UL) {
158 28 : char_t realPath[MMPA_MAX_PATH] = {};
159 28 : if (mmRealPath(configPath, realPath, MMPA_MAX_PATH) != EN_OK) {
160 2 : const auto formatErrMsg = acl::AclGetErrorFormatMessage(mmGetErrorCode());
161 2 : acl::AclErrorLogManager::ReportInputError(
162 4 : acl::INVALID_PATH_MSG, std::vector<const char*>({"path", "reason"}),
163 4 : std::vector<const char*>({configPath, formatErrMsg.c_str()}));
164 2 : ACL_LOG_ERROR("Invalid file: %s", configPath);
165 2 : return ACL_ERROR_INVALID_FILE;
166 2 : }
167 26 : std::ifstream file(realPath, std::ios::binary);
168 26 : if (!file.is_open()) {
169 0 : acl::AclErrorLogManager::ReportInputError(
170 0 : acl::INVALID_PATH_MSG, std::vector<const char*>({"path", "reason"}),
171 0 : std::vector<const char*>({configPath, "file open failed"}));
172 0 : ACL_LOG_ERROR("Failed to open file: %s", configPath);
173 0 : return ACL_ERROR_INVALID_FILE;
174 : }
175 :
176 26 : std::stringstream buffer;
177 26 : buffer << file.rdbuf();
178 26 : configStr = buffer.str();
179 26 : file.close(); // 显式关闭文件
180 26 : }
181 44 : return ACL_SUCCESS;
182 : }
183 : } // namespace acl
184 :
185 : #ifdef __cplusplus
186 : extern "C" {
187 : #endif
188 :
189 50 : const char* aclrtGetSocNameImpl()
190 : {
191 50 : ACL_LOG_INFO("start to execute aclrtGetSocName.");
192 : // get socVersion
193 50 : const auto ret = acl::InitSocVersion();
194 50 : if (ret != ACL_SUCCESS) {
195 1 : ACL_LOG_INFO("Cannot init soc version, errorCode = %d", ret);
196 1 : return nullptr;
197 : }
198 49 : ACL_LOG_INFO("execute aclrtGetSocName successfully");
199 49 : return aclSocVersion.c_str();
200 : }
201 :
202 1 : aclError aclrtGetVersionImpl(int32_t* majorVersion, int32_t* minorVersion, int32_t* patchVersion)
203 : {
204 1 : ACL_LOG_INFO("start to execute aclrtGetVersion.");
205 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(majorVersion);
206 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(minorVersion);
207 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(patchVersion);
208 :
209 : // Acl version is (*majorVersion).(*minorVersion).(*patchVersion)
210 1 : *majorVersion = ACL_MAJOR_VERSION;
211 1 : *minorVersion = ACL_MINOR_VERSION;
212 1 : *patchVersion = ACL_PATCH_VERSION;
213 1 : ACL_LOG_INFO("acl version is %d.%d.%d", *majorVersion, *minorVersion, *patchVersion);
214 :
215 1 : return ACL_SUCCESS;
216 : }
217 : #ifdef __cplusplus
218 : }
219 : #endif
|