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 "acl_rt_impl.h"
12 : #include <sstream>
13 : #include "securec.h"
14 : #include "runtime/context.h"
15 : #include "common/log_inner.h"
16 : #include "common/error_codes_inner.h"
17 : #include "common/prof_reporter.h"
18 : #include "common/resource_statistics.h"
19 : #include "utils/data_type_utils.h"
20 :
21 : namespace {
22 :
23 14 : static aclError FillAttrValue(
24 : const void* const src, const size_t srcLen, void* const dst, const size_t dstLen, size_t* const paramRetSize,
25 : const char* functionName)
26 : {
27 14 : ACL_REQUIRES_NOT_NULL(src);
28 14 : ACL_REQUIRES_NOT_NULL(dst);
29 14 : if (srcLen > dstLen) {
30 2 : ACL_LOG_ERROR(
31 : "[Check][valueLen]valueLen[%zu] is smaller than required size[%zu] for group computing power info", dstLen,
32 : srcLen);
33 2 : const std::string dstLenVal = std::to_string(dstLen);
34 4 : std::string reason = std::to_string(dstLen) +
35 : " is shorter than the length of the group computing power information, "
36 4 : "required size is " +
37 6 : std::to_string(srcLen) + ", cannot save the computing power information";
38 2 : acl::AclErrorLogManager::ReportInputError(
39 4 : acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
40 4 : std::vector<const char*>({functionName, dstLenVal.c_str(), "valueLen", reason.c_str()}));
41 2 : return ACL_ERROR_INVALID_PARAM;
42 2 : }
43 12 : const auto ret = memcpy_s(dst, dstLen, src, srcLen);
44 12 : if (ret != EOK) {
45 1 : ACL_LOG_ERROR("call memcpy_s failed, result = %d, srcLen = %zu, dstLen = %zu", ret, srcLen, dstLen);
46 1 : const std::string retVal = std::to_string(ret);
47 1 : std::stringstream ss;
48 1 : ss << std::hex << "src=0x" << reinterpret_cast<uintptr_t>(src) << ", attrValue=0x"
49 1 : << reinterpret_cast<uintptr_t>(dst) << std::dec << ", valueLen=" << dstLen << ", count=" << srcLen << ".";
50 1 : const std::string extendInfo = ss.str();
51 1 : acl::AclErrorLogManager::ReportInputError(
52 : acl::STANDARD_FUNC_FAILED_MSG,
53 2 : std::vector<const char*>({"func1", "func2", "ret_code", "reason", "extend_info"}),
54 2 : std::vector<const char*>({functionName, "memcpy_s", retVal.c_str(), strerror(ret), extendInfo.c_str()}));
55 1 : return ACL_ERROR_FAILURE;
56 1 : }
57 11 : *paramRetSize = srcLen;
58 :
59 11 : return ACL_SUCCESS;
60 : }
61 : } // namespace
62 :
63 : #ifdef __cplusplus
64 : extern "C" {
65 : #endif
66 :
67 3 : aclError aclrtSetGroupImpl(int32_t groupId)
68 : {
69 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtSetGroup);
70 3 : ACL_LOG_INFO("start to execute aclrtSetGroup, groupId is %d.", groupId);
71 3 : ACL_REQUIRES_RTS_OK(rtSetGroup(groupId));
72 2 : ACL_LOG_INFO("successfully execute aclrtSetGroup, groupId is %d.", groupId);
73 :
74 2 : return ACL_SUCCESS;
75 3 : }
76 :
77 3 : aclError aclrtGetGroupCountImpl(uint32_t* count)
78 : {
79 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupCount);
80 3 : ACL_LOG_INFO("start to execute aclrtGetGroupCount");
81 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(count);
82 3 : ACL_REQUIRES_RTS_OK(rtGetGroupCount(count));
83 2 : ACL_LOG_INFO("successfully execute aclrtGetGroupCount, group number is %u.", *count);
84 :
85 2 : return ACL_SUCCESS;
86 3 : }
87 :
88 7 : aclrtGroupInfo* aclrtCreateGroupInfoImpl()
89 : {
90 7 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
91 7 : ACL_LOG_INFO("start to execute aclrtCreateGroupInfo");
92 7 : uint32_t count = 0U;
93 7 : const rtError_t rtErr = rtGetGroupCount(&count);
94 7 : if (rtErr != RT_ERROR_NONE) {
95 1 : return nullptr;
96 : }
97 6 : if (count == 0U) { // 0 represents that no group
98 0 : ACL_LOG_WARN("group number is 0, no memory allocation");
99 0 : return nullptr;
100 : }
101 :
102 6 : aclrtGroupInfo* const groupInfo = new (std::nothrow) aclrtGroupInfo[count];
103 6 : ACL_CHECK_MALLOC_RESULT_REPORT_RET(groupInfo, sizeof(aclrtGroupInfo) * count, "new", nullptr);
104 :
105 6 : ACL_LOG_INFO("successfully execute aclrtCreateGroupInfo, group number is %u.", count);
106 6 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
107 :
108 6 : return groupInfo;
109 : }
110 :
111 6 : aclError aclrtDestroyGroupInfoImpl(aclrtGroupInfo* groupInfo)
112 : {
113 6 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
114 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
115 6 : ACL_DELETE_ARRAY_AND_SET_NULL(groupInfo);
116 6 : ACL_LOG_INFO("successfully execute aclrtDestroyGroupInfo");
117 :
118 6 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
119 :
120 6 : return ACL_SUCCESS;
121 : }
122 :
123 4 : aclError aclrtGetAllGroupInfoImpl(aclrtGroupInfo* groupInfo)
124 : {
125 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetAllGroupInfo);
126 4 : ACL_LOG_INFO("start to execute aclrtGetAllGroupInfo");
127 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
128 4 : uint32_t count = 0U;
129 4 : ACL_REQUIRES_RTS_OK(rtGetGroupCount(&count));
130 :
131 : // -1 represents that get all group information
132 3 : ACL_REQUIRES_RTS_OK(rtGetGroupInfo(-1, static_cast<rtGroupInfo_t*>(groupInfo), count));
133 :
134 2 : ACL_LOG_INFO("successfully execute aclrtGetAllGroupInfo, group number = %u", count);
135 :
136 2 : return ACL_SUCCESS;
137 4 : }
138 :
139 19 : aclError aclrtGetGroupInfoDetailImpl(
140 : const aclrtGroupInfo* groupInfo, int32_t groupIndex, aclrtGroupAttr attr, void* attrValue, size_t valueLen,
141 : size_t* paramRetSize)
142 : {
143 19 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupInfoDetail);
144 19 : ACL_LOG_INFO("start to execute aclrtGetGroupInfoDetail, groupIndex = %d", groupIndex);
145 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
146 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(attrValue);
147 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(paramRetSize);
148 19 : uint32_t count = 0U;
149 19 : ACL_REQUIRES_RTS_OK(rtGetGroupCount(&count));
150 18 : if ((groupIndex < 0) || (static_cast<uint32_t>(groupIndex) >= count)) {
151 2 : ACL_LOG_ERROR("the index value of group is invalid, groupIndex = %d, not in range [0, %u)", groupIndex, count);
152 2 : const std::string groupIndexVal = std::to_string(groupIndex);
153 2 : std::string expect = "[0, " + std::to_string(count) + "]";
154 2 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
155 2 : acl::AclErrorLogManager::ReportInputError(
156 4 : acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
157 4 : std::vector<const char*>({funcName.c_str(), groupIndexVal.c_str(), "groupIndex", expect.c_str()}));
158 2 : return ACL_ERROR_INVALID_PARAM;
159 2 : }
160 :
161 : aclError aclRet;
162 16 : switch (attr) {
163 3 : case ACL_GROUP_AICORE_INT:
164 6 : aclRet = FillAttrValue(
165 3 : static_cast<const void*>(&groupInfo[groupIndex].aicoreNum), sizeof(groupInfo[groupIndex].aicoreNum),
166 : attrValue, valueLen, paramRetSize, __func__);
167 3 : break;
168 3 : case ACL_GROUP_AIV_INT:
169 6 : aclRet = FillAttrValue(
170 3 : static_cast<const void*>(&groupInfo[groupIndex].aivectorNum), sizeof(groupInfo[groupIndex].aivectorNum),
171 : attrValue, valueLen, paramRetSize, __func__);
172 3 : break;
173 2 : case ACL_GROUP_AIC_INT:
174 4 : aclRet = FillAttrValue(
175 2 : static_cast<const void*>(&groupInfo[groupIndex].aicpuNum), sizeof(groupInfo[groupIndex].aicpuNum),
176 : attrValue, valueLen, paramRetSize, __func__);
177 2 : break;
178 2 : case ACL_GROUP_SDMANUM_INT:
179 4 : aclRet = FillAttrValue(
180 2 : static_cast<const void*>(&groupInfo[groupIndex].sdmaNum), sizeof(groupInfo[groupIndex].sdmaNum),
181 : attrValue, valueLen, paramRetSize, __func__);
182 2 : break;
183 2 : case ACL_GROUP_ASQNUM_INT:
184 4 : aclRet = FillAttrValue(
185 2 : static_cast<const void*>(&groupInfo[groupIndex].activeStreamNum),
186 : sizeof(groupInfo[groupIndex].activeStreamNum), attrValue, valueLen, paramRetSize, __func__);
187 2 : break;
188 2 : case ACL_GROUP_GROUPID_INT:
189 4 : aclRet = FillAttrValue(
190 2 : static_cast<const void*>(&groupInfo[groupIndex].groupId), sizeof(groupInfo[groupIndex].groupId),
191 : attrValue, valueLen, paramRetSize, __func__);
192 2 : break;
193 2 : default:
194 2 : ACL_LOG_ERROR("invalid group attribute, attribute = %d", static_cast<int32_t>(attr));
195 2 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
196 2 : acl::AclErrorLogManager::ReportInputError(
197 4 : acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
198 2 : std::vector<const char*>(
199 2 : {funcName.c_str(), acl::GetGroupAttrDesc(attr), "attr",
200 4 : "[ACL_GROUP_AICORE_INT, ACL_GROUP_GROUPID_INT]"}));
201 2 : return ACL_ERROR_INVALID_PARAM;
202 : }
203 :
204 14 : ACL_LOG_INFO("end to execute aclrtGetGroupInfoDetail, groupIndex = %d", groupIndex);
205 14 : return aclRet;
206 19 : }
207 : #ifdef __cplusplus
208 : }
209 : #endif
|