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 "securec.h"
13 : #include "runtime/context.h"
14 : #include "common/log_inner.h"
15 : #include "common/error_codes_inner.h"
16 : #include "common/prof_reporter.h"
17 : #include "common/resource_statistics.h"
18 :
19 : namespace {
20 11 : static aclError FillAttrValue(const void *const src, const size_t srcLen, void *const dst, const size_t dstLen,
21 : size_t *const paramRetSize)
22 : {
23 11 : ACL_REQUIRES_NOT_NULL(src);
24 11 : ACL_REQUIRES_NOT_NULL(dst);
25 11 : if (srcLen > dstLen) {
26 0 : ACL_LOG_INNER_ERROR("attr real length = %zu is larger than input length = %zu", srcLen, dstLen);
27 0 : return ACL_ERROR_INVALID_PARAM;
28 : }
29 11 : const auto ret = memcpy_s(dst, dstLen, src, srcLen);
30 11 : if (ret != EOK) {
31 0 : ACL_LOG_INNER_ERROR("call memcpy_s failed, result = %d, srcLen = %zu, dstLen = %zu", ret, srcLen, dstLen);
32 0 : return ACL_ERROR_FAILURE;
33 : }
34 11 : *paramRetSize = srcLen;
35 :
36 11 : return ACL_SUCCESS;
37 : }
38 : }
39 :
40 3 : aclError aclrtSetGroupImpl(int32_t groupId)
41 : {
42 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtSetGroup);
43 3 : ACL_LOG_INFO("start to execute aclrtSetGroup, groupId is %d.", groupId);
44 3 : const rtError_t rtErr = rtSetGroup(groupId);
45 3 : if (rtErr != RT_ERROR_NONE) {
46 1 : ACL_LOG_CALL_ERROR("set group failed, runtime result = %d, groupId = %d",
47 : static_cast<int32_t>(rtErr), groupId);
48 1 : return ACL_GET_ERRCODE_RTS(rtErr);
49 : }
50 2 : ACL_LOG_INFO("successfully execute aclrtSetGroup, groupId is %d.", groupId);
51 :
52 2 : return ACL_SUCCESS;
53 : }
54 :
55 3 : aclError aclrtGetGroupCountImpl(uint32_t *count)
56 : {
57 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupCount);
58 3 : ACL_LOG_INFO("start to execute aclrtGetGroupCount");
59 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(count);
60 3 : const rtError_t rtErr = rtGetGroupCount(count);
61 3 : if (rtErr != RT_ERROR_NONE) {
62 1 : ACL_LOG_CALL_ERROR("get group number failed, runtime result = %d", static_cast<int32_t>(rtErr));
63 1 : return ACL_GET_ERRCODE_RTS(rtErr);
64 : }
65 2 : ACL_LOG_INFO("successfully execute aclrtGetGroupCount, group number is %u.", *count);
66 :
67 2 : return ACL_SUCCESS;
68 : }
69 :
70 5 : aclrtGroupInfo *aclrtCreateGroupInfoImpl()
71 : {
72 5 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
73 5 : ACL_LOG_INFO("start to execute aclrtCreateGroupInfo");
74 5 : uint32_t count = 0U;
75 5 : const rtError_t rtErr = rtGetGroupCount(&count);
76 5 : if (rtErr != RT_ERROR_NONE) {
77 1 : ACL_LOG_CALL_ERROR("get group number failed, runtime result = %d", static_cast<int32_t>(rtErr));
78 1 : return nullptr;
79 : }
80 4 : if (count == 0U) { // 0 represents that no group
81 0 : ACL_LOG_WARN("group number is 0, no memory allocation");
82 0 : return nullptr;
83 : }
84 :
85 4 : aclrtGroupInfo *const groupInfo = new(std::nothrow) aclrtGroupInfo[count];
86 4 : if (groupInfo == nullptr) {
87 0 : ACL_LOG_INNER_ERROR("fail to new group info");
88 0 : return nullptr;
89 : }
90 :
91 4 : ACL_LOG_INFO("successfully execute aclrtCreateGroupInfo, group number is %u.", count);
92 4 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
93 :
94 4 : return groupInfo;
95 : }
96 :
97 4 : aclError aclrtDestroyGroupInfoImpl(aclrtGroupInfo *groupInfo)
98 : {
99 4 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
100 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
101 4 : ACL_DELETE_ARRAY_AND_SET_NULL(groupInfo);
102 4 : ACL_LOG_INFO("successfully execute aclrtDestroyGroupInfo");
103 :
104 4 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_CREATE_DESTROY_GROUP_INFO);
105 :
106 4 : return ACL_SUCCESS;
107 : }
108 :
109 4 : aclError aclrtGetAllGroupInfoImpl(aclrtGroupInfo *groupInfo)
110 : {
111 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetAllGroupInfo);
112 4 : ACL_LOG_INFO("start to execute aclrtGetAllGroupInfo");
113 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
114 4 : uint32_t count = 0U;
115 4 : rtError_t rtErr = rtGetGroupCount(&count);
116 4 : if (rtErr != RT_ERROR_NONE) {
117 1 : ACL_LOG_CALL_ERROR("get group number failed, runtime result = %d", static_cast<int32_t>(rtErr));
118 1 : return ACL_GET_ERRCODE_RTS(rtErr);
119 : }
120 :
121 : // -1 represents that get all group information
122 3 : rtErr = rtGetGroupInfo(-1, static_cast<rtGroupInfo_t *>(groupInfo), count);
123 3 : if (rtErr != RT_ERROR_NONE) {
124 1 : ACL_LOG_CALL_ERROR("get group info failed, runtime result = %d, group number = %u",
125 : static_cast<int32_t>(rtErr), count);
126 1 : return ACL_GET_ERRCODE_RTS(rtErr);
127 : }
128 :
129 2 : ACL_LOG_INFO("successfully execute aclrtGetAllGroupInfo, group number = %u", count);
130 :
131 2 : return ACL_SUCCESS;
132 : }
133 :
134 16 : aclError aclrtGetGroupInfoDetailImpl(const aclrtGroupInfo *groupInfo, int32_t groupIndex, aclrtGroupAttr attr,
135 : void *attrValue, size_t valueLen, size_t *paramRetSize)
136 : {
137 32 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupInfoDetail);
138 16 : ACL_LOG_INFO("start to execute aclrtGetGroupInfoDetail, groupIndex = %d", groupIndex);
139 16 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(groupInfo);
140 16 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(attrValue);
141 16 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(paramRetSize);
142 16 : uint32_t count = 0U;
143 16 : const rtError_t rtErr = rtGetGroupCount(&count);
144 16 : if (rtErr != RT_ERROR_NONE) {
145 1 : ACL_LOG_CALL_ERROR("get group number failed, runtime result = %d", static_cast<int32_t>(rtErr));
146 1 : return ACL_GET_ERRCODE_RTS(rtErr);
147 : }
148 15 : if ((groupIndex < 0) || (static_cast<uint32_t>(groupIndex) >= count)) {
149 2 : ACL_LOG_INNER_ERROR("the index value of group is invalid, groupIndex = %d, not in range [0, %u)",
150 : groupIndex, count);
151 2 : return ACL_ERROR_INVALID_PARAM;
152 : }
153 :
154 : aclError aclRet;
155 13 : switch (attr) {
156 1 : case ACL_GROUP_AICORE_INT:
157 1 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].aicoreNum),
158 : sizeof(groupInfo[groupIndex].aicoreNum), attrValue, valueLen, paramRetSize);
159 1 : break;
160 2 : case ACL_GROUP_AIV_INT:
161 2 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].aivectorNum),
162 : sizeof(groupInfo[groupIndex].aivectorNum), attrValue, valueLen, paramRetSize);
163 2 : break;
164 2 : case ACL_GROUP_AIC_INT:
165 2 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].aicpuNum),
166 : sizeof(groupInfo[groupIndex].aicpuNum), attrValue, valueLen, paramRetSize);
167 2 : break;
168 2 : case ACL_GROUP_SDMANUM_INT:
169 2 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].sdmaNum),
170 : sizeof(groupInfo[groupIndex].sdmaNum), attrValue, valueLen, paramRetSize);
171 2 : break;
172 2 : case ACL_GROUP_ASQNUM_INT:
173 2 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].activeStreamNum),
174 : sizeof(groupInfo[groupIndex].activeStreamNum), attrValue, valueLen, paramRetSize);
175 2 : break;
176 2 : case ACL_GROUP_GROUPID_INT:
177 2 : aclRet = FillAttrValue(static_cast<const void *>(&groupInfo[groupIndex].groupId),
178 : sizeof(groupInfo[groupIndex].groupId), attrValue, valueLen, paramRetSize);
179 2 : break;
180 2 : default:
181 2 : ACL_LOG_INNER_ERROR("invalid group attribute, attribute = %d", static_cast<int32_t>(attr));
182 2 : return ACL_ERROR_INVALID_PARAM;
183 : }
184 :
185 11 : ACL_LOG_INFO("end to execute aclrtGetGroupInfoDetail, groupIndex = %d", groupIndex);
186 11 : return aclRet;
187 : }
|