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