Line data Source code
1 : /*
2 : * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
3 : * Description: ccu representation context implementation file
4 : * Author: sunzhepeng
5 : * Create: 2024-06-17
6 : */
7 :
8 : #include "ccu_rep_context_v1.h"
9 :
10 : #include "exception_util.h"
11 : #include "ccu_api_exception.h"
12 : #include "ccu_rep_assign_v1.h"
13 : #include "const_val.h"
14 :
15 : #include "hcomm_c_adpt.h" // 需优化
16 : #include "../../../endpoint_pairs/channels/ccu/ccu_urma_channel.h" // 需优化
17 : #include "ccu_dev_mgr_imp.h"
18 :
19 : namespace hcomm {
20 : namespace CcuRep {
21 :
22 176 : CcuRepContext::CcuRepContext()
23 : {
24 176 : mainBlock = std::make_shared<CcuRep::CcuRepBlock>(insGenerator);
25 176 : activeBlock = mainBlock;
26 176 : }
27 :
28 176 : CcuRepContext::~CcuRepContext()
29 : {
30 :
31 176 : }
32 :
33 1732 : std::shared_ptr<CcuRep::CcuRepBlock> CcuRepContext::CurrentBlock()
34 : {
35 1732 : if (activeBlock == nullptr) {
36 1 : Hccl::THROW<Hccl::CcuApiException>("Invalid ActiveBlock");
37 : }
38 1731 : return activeBlock;
39 : }
40 :
41 115 : void CcuRepContext::SetCurrentBlock(std::shared_ptr<CcuRep::CcuRepBlock> repBlock)
42 : {
43 115 : activeBlock = repBlock;
44 115 : }
45 :
46 : /**
47 : * @details:保存rep信息,后续有arg后配合补全profiling信息
48 : */
49 882 : void CcuRepContext::CollectProfilingReps(std::shared_ptr<CcuRep::CcuRepBase> rep)
50 : {
51 882 : if (rep->Type() == CcuRepType::ASSIGN) {
52 351 : auto assignRep = dynamic_cast<CcuRepAssign *>(rep.get());
53 351 : if (assignRep->GetSubType() == AssignSubType::VAR_TO_VAR) {
54 78 : lgProfilingInfo.assignProfilingReps.push_back(rep);
55 : }
56 1062 : } else if (CurrentBlock()->Type() != CcuRep::CcuRepType::LOOP_BLOCK
57 1493 : && (rep->Type() == CcuRepType::LOC_WAIT_EVENT || rep->Type() == CcuRepType::REM_WAIT_SEM
58 431 : || rep->Type() == CcuRepType::REM_WAIT_GROUP)) {
59 26 : waitCkeProfilingReps.push_back(rep);
60 505 : } else if (rep->Type() == CcuRepType::LOOPGROUP) {
61 28 : allLgProfilingReps.push_back(rep);
62 : }
63 882 : }
64 :
65 882 : void CcuRepContext::Append(std::shared_ptr<CcuRep::CcuRepBase> rep)
66 : {
67 882 : CollectProfilingReps(rep);
68 882 : CurrentBlock()->Append(rep);
69 882 : }
70 :
71 147 : const std::vector<std::shared_ptr<CcuRep::CcuRepBase>> &CcuRepContext::GetRepSequence()
72 : {
73 147 : return mainBlock->GetReps();
74 : }
75 :
76 0 : std::shared_ptr<CcuRep::CcuRepBase> CcuRepContext::GetRepByInstrId(uint16_t instrId)
77 : {
78 0 : for (const auto &rep : GetRepSequence()) {
79 0 : CHK_PRT_RET(rep == nullptr, HCCL_ERROR("[%s]fail, rep is nullptr", __func__), nullptr);
80 0 : const uint16_t repInstrCount = rep->InstrCount();
81 0 : if (repInstrCount == 0) {
82 0 : continue;
83 : }
84 0 : const uint16_t startId = rep->StartInstrId();
85 0 : const uint16_t endId = startId + repInstrCount - 1;
86 0 : HCCL_INFO("[%s]startId[%u], endId[%u], instrId[%u]", __func__, startId, endId, instrId);
87 0 : if (instrId >= startId && instrId <= endId) {
88 0 : return rep;
89 : }
90 : }
91 0 : return nullptr;
92 : }
93 :
94 0 : void CcuRepContext::DumpReprestation()
95 : {
96 0 : HCCL_INFO("Rep Count: %lu", GetRepSequence().size());
97 0 : for (uint32_t index = 0; index < GetRepSequence().size(); index++) {
98 0 : HCCL_INFO("index[%u]: %s", index, GetRepSequence()[index]->Describe().c_str());
99 : }
100 0 : }
101 :
102 103 : void CcuRepContext::SetDieId(uint32_t dieId)
103 : {
104 103 : HCCL_INFO("set dieId[%u]", dieId);
105 103 : this->dieId = dieId;
106 103 : }
107 :
108 1277 : uint32_t CcuRepContext::GetDieId() const
109 : {
110 1277 : return dieId;
111 : }
112 :
113 34 : void CcuRepContext::SetMissionId(uint32_t missionId)
114 : {
115 34 : if (this->missionId == Hccl::INVALID_U32) {
116 34 : this->missionId = missionId;
117 : }
118 34 : }
119 :
120 56 : uint32_t CcuRepContext::GetMissionId() const
121 : {
122 56 : return missionId;
123 : }
124 :
125 34 : void CcuRepContext::SetMissionKey(uint32_t missionKey)
126 : {
127 34 : this->missionKey = missionKey;
128 34 : }
129 :
130 9 : uint32_t CcuRepContext::GetMissionKey() const
131 : {
132 9 : return missionKey;
133 : }
134 :
135 5 : std::vector<CcuProfilingInfo> &CcuRepContext::GetProfilingInfo()
136 : {
137 5 : return profilingInfo;
138 : }
139 :
140 18 : const std::vector<std::shared_ptr<CcuRepBase>> &CcuRepContext::GetWaiteCkeProfilingReps() const
141 : {
142 18 : return waitCkeProfilingReps;
143 : }
144 :
145 10 : LoopGroupProfilingInfo &CcuRepContext::GetLGProfilingInfo()
146 : {
147 10 : return lgProfilingInfo;
148 : }
149 :
150 59 : void CcuRepContext::AddSqeProfiling(const std::string &kernelName)
151 : {
152 59 : constexpr uint32_t defaultDieId = 0; // 首次填写profiling时dieId未确定
153 : // 生成SQE粒度profiling信息
154 59 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_TASK_PROFILING;
155 59 : ccuProfilingInfoCache.name = kernelName.c_str();
156 59 : ccuProfilingInfoCache.dieId = defaultDieId;
157 59 : HCCL_DEBUG("[%s]type[%d], name[%s], deafultDieId[0]", __func__, ccuProfilingInfoCache.type,
158 : ccuProfilingInfoCache.name.c_str(), ccuProfilingInfoCache.dieId);
159 59 : profilingInfo.push_back(ccuProfilingInfoCache);
160 59 : }
161 :
162 16 : int32_t CcuRepContext::AddProfiling(const std::string &name, uint32_t mask)
163 : {
164 16 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_WAITCKE_PROFILING;
165 16 : ccuProfilingInfoCache.name = name;
166 16 : ccuProfilingInfoCache.ckeId = INVALID_CKE_ID;
167 16 : ccuProfilingInfoCache.mask = mask;
168 16 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId), INVALID_VALUE_CHANNELID,
169 : sizeof(ccuProfilingInfoCache.channelId)));
170 :
171 16 : HCCL_INFO("[%s]name[%s], mask[%u], type[%d]", __func__, name.c_str(), mask, ccuProfilingInfoCache.type);
172 16 : profilingInfo.push_back(ccuProfilingInfoCache);
173 16 : return HCCL_SUCCESS;
174 : }
175 :
176 11 : int32_t CcuRepContext::AddProfiling(const ChannelHandle channel, const std::string &name, uint32_t signalIndex, uint32_t mask)
177 : {
178 11 : void *channelPtr{nullptr};
179 11 : CHK_RET(static_cast<HcclResult>(HcommChannelGet(channel, &channelPtr)));
180 10 : auto *channelImpl = dynamic_cast<CcuUrmaChannel *>(static_cast<Channel *>(channelPtr));
181 :
182 10 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_WAITCKE_PROFILING;
183 10 : ccuProfilingInfoCache.name = name;
184 10 : CHK_RET(channelImpl->GetLocCkeByIndex(signalIndex, ccuProfilingInfoCache.ckeId));
185 10 : ccuProfilingInfoCache.mask = mask;
186 10 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId),
187 : INVALID_VALUE_CHANNELID, sizeof(ccuProfilingInfoCache.channelId)));
188 10 : ccuProfilingInfoCache.channelId[0] = channelImpl->GetChannelId();
189 10 : ccuProfilingInfoCache.channelHandle[0] = channel;
190 :
191 10 : HCCL_INFO("[%s]channelHandle[0x%llx], name[%s], signalIndex[%u], mask[%u], type[%d], ckeId[%u], channelId[%u]",
192 : __func__, channel, name.c_str(), signalIndex, mask, ccuProfilingInfoCache.type, ccuProfilingInfoCache.ckeId,
193 : ccuProfilingInfoCache.channelId[0]);
194 10 : profilingInfo.push_back(ccuProfilingInfoCache);
195 10 : return HCCL_SUCCESS;
196 : }
197 :
198 0 : int32_t CcuRepContext::AddProfiling(const ChannelHandle *channels, uint32_t channelNum)
199 : {
200 0 : CHK_PTR_NULL(channels);
201 0 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_LOOPGROUP_PROFILING;
202 0 : ccuProfilingInfoCache.name = "GroupBroadcast";
203 0 : ccuProfilingInfoCache.reduceOpType = 0xFF; // 0xFF 无效值
204 0 : ccuProfilingInfoCache.inputDataType = 0xFF; // 0xFF 无效值
205 0 : ccuProfilingInfoCache.outputDataType = 0xFF; // 0xFF 无效值
206 0 : ccuProfilingInfoCache.missionId = GetMissionId();
207 :
208 0 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId),
209 : INVALID_VALUE_CHANNELID, sizeof(ccuProfilingInfoCache.channelId)));
210 0 : for (u32 i = 0; i < channelNum; i++) {
211 0 : void *channelPtr{nullptr};
212 0 : CHK_RET(static_cast<HcclResult>(HcommChannelGet(channels[i], &channelPtr)));
213 0 : auto *channelImpl = dynamic_cast<CcuUrmaChannel *>(static_cast<Channel *>(channelPtr));
214 0 : CHK_PTR_NULL(channelImpl);
215 0 : ccuProfilingInfoCache.channelId[i] = channelImpl->GetChannelId();
216 0 : ccuProfilingInfoCache.channelHandle[i] = channels[i];
217 0 : HCCL_INFO("[%s]type[%d], name[%s], missionId[%u], channelHandle[0x%llx], channelId[%u]",
218 : __func__, ccuProfilingInfoCache.type, ccuProfilingInfoCache.name.c_str(), ccuProfilingInfoCache.missionId,
219 : ccuProfilingInfoCache.channelHandle[i], ccuProfilingInfoCache.channelId[i]);
220 : }
221 :
222 0 : lgProfilingInfo.ccuProfilingInfos.push_back(ccuProfilingInfoCache);
223 0 : if (!allLgProfilingReps.empty()) {
224 0 : lgProfilingInfo.lgProfilingReps.push_back(allLgProfilingReps.back());
225 : }
226 0 : return HCCL_SUCCESS;
227 : }
228 :
229 0 : int32_t CcuRepContext::AddProfiling(const ChannelHandle *channels, uint32_t channelNum, HcommDataType hcommDataType,
230 : HcommDataType hcommOutputDataType, HcommReduceOp hcommOpType)
231 : {
232 0 : HcclDataType dataType = static_cast<HcclDataType>(hcommDataType);
233 0 : HcclDataType outputDataType = static_cast<HcclDataType>(hcommOutputDataType);
234 0 : HcclReduceOp opType = static_cast<HcclReduceOp>(hcommOpType);
235 :
236 0 : CHK_PTR_NULL(channels);
237 0 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_LOOPGROUP_PROFILING;
238 0 : ccuProfilingInfoCache.name = "GroupReduce";
239 0 : ccuProfilingInfoCache.reduceOpType = opType;
240 0 : ccuProfilingInfoCache.inputDataType = dataType;
241 0 : ccuProfilingInfoCache.outputDataType = outputDataType;
242 0 : ccuProfilingInfoCache.missionId = GetMissionId();
243 :
244 0 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId),
245 : INVALID_VALUE_CHANNELID, sizeof(ccuProfilingInfoCache.channelId)));
246 0 : for (u32 i = 0; i < channelNum; ++i) {
247 0 : void *channelPtr{nullptr};
248 0 : CHK_RET(static_cast<HcclResult>(HcommChannelGet(channels[i], &channelPtr)));
249 0 : auto *channelImpl = dynamic_cast<CcuUrmaChannel *>(static_cast<Channel *>(channelPtr));
250 0 : CHK_PTR_NULL(channelImpl);
251 0 : ccuProfilingInfoCache.channelId[i] = channelImpl->GetChannelId();
252 0 : ccuProfilingInfoCache.channelHandle[i] = channels[i];
253 0 : HCCL_INFO("[%s]type[%d], name[%s], opType[%d], dataType[%d], outputDataType[%d], missionId[%u], "
254 : "channelHandle[0x%llx], channelId[%u]", __func__, ccuProfilingInfoCache.type,
255 : ccuProfilingInfoCache.name.c_str(), opType, dataType, outputDataType, ccuProfilingInfoCache.missionId,
256 : ccuProfilingInfoCache.channelHandle[i], ccuProfilingInfoCache.channelId[i]);
257 : }
258 :
259 0 : lgProfilingInfo.ccuProfilingInfos.push_back(ccuProfilingInfoCache);
260 0 : lgProfilingInfo.lgProfilingReps.push_back(allLgProfilingReps.back());
261 0 : return HCCL_SUCCESS;
262 : }
263 :
264 55 : void CcuRepContext::SetDependencyInfo(uint32_t id, uint32_t mask, const std::shared_ptr<CcuRepBase> &rep)
265 : {
266 : // 按 mask 各置位 bit 分别登记:异常侧按 1<<i 单 bit 查询,多 bit mask 需拆解到每个单 bit key
267 55 : constexpr uint32_t CCU_CKE_BIT_NUM = 16; // CKE 的 bit 数最多为 16
268 55 : auto &inner = depInfo[id];
269 935 : for (uint32_t i = 0; i < CCU_CKE_BIT_NUM; i++) {
270 880 : uint32_t bit = 1u << i;
271 880 : if ((mask & bit) != 0u) {
272 64 : inner[bit].push_back(rep);
273 : }
274 : }
275 55 : }
276 :
277 41 : std::unordered_map<uint32_t, std::vector<std::shared_ptr<CcuRepBase>>> CcuRepContext::GetDependencyInfo(uint32_t id) {
278 : // 查找给定 id 是否存在于 depInfo 中
279 41 : auto it = depInfo.find(id);
280 : // 如果找到 id,返回与之关联的内层 unordered_map
281 41 : if (it != depInfo.end()) {
282 37 : return it->second;
283 : }
284 : // 如果未找到 id,返回一个空的 unordered_map
285 4 : return std::unordered_map<uint32_t, std::vector<std::shared_ptr<CcuRepBase>>>();
286 : }
287 :
288 33 : void CcuRepContext::EraseDependencyInfo(uint32_t id) {
289 33 : depInfo.erase(id);
290 33 : }
291 :
292 1 : void CcuRepContext::ClearDependencyInfo() {
293 1 : depInfo.clear();
294 1 : }
295 :
296 : }; // namespace CcuRep
297 : }; // namespace hcomm
|