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 143 : CcuRepContext::CcuRepContext()
23 : {
24 143 : mainBlock = std::make_shared<CcuRep::CcuRepBlock>(insGenerator);
25 143 : activeBlock = mainBlock;
26 143 : }
27 :
28 143 : CcuRepContext::~CcuRepContext()
29 : {
30 :
31 143 : }
32 :
33 999 : std::shared_ptr<CcuRep::CcuRepBlock> CcuRepContext::CurrentBlock()
34 : {
35 999 : if (activeBlock == nullptr) {
36 1 : Hccl::THROW<Hccl::CcuApiException>("Invalid ActiveBlock");
37 : }
38 998 : return activeBlock;
39 : }
40 :
41 73 : void CcuRepContext::SetCurrentBlock(std::shared_ptr<CcuRep::CcuRepBlock> repBlock)
42 : {
43 73 : activeBlock = repBlock;
44 73 : }
45 :
46 : /**
47 : * @details:保存rep信息,后续有arg后配合补全profiling信息
48 : */
49 492 : void CcuRepContext::CollectProfilingReps(std::shared_ptr<CcuRep::CcuRepBase> rep)
50 : {
51 492 : if (rep->Type() == CcuRepType::ASSIGN) {
52 188 : auto assignRep = dynamic_cast<CcuRepAssign *>(rep.get());
53 188 : if (assignRep->GetSubType() == AssignSubType::VAR_TO_VAR) {
54 48 : lgProfilingInfo.assignProfilingReps.push_back(rep);
55 : }
56 608 : } else if (CurrentBlock()->Type() != CcuRep::CcuRepType::LOOP_BLOCK
57 855 : && (rep->Type() == CcuRepType::LOC_WAIT_EVENT || rep->Type() == CcuRepType::REM_WAIT_SEM
58 247 : || rep->Type() == CcuRepType::REM_WAIT_GROUP)) {
59 15 : waitCkeProfilingReps.push_back(rep);
60 289 : } else if (rep->Type() == CcuRepType::LOOPGROUP) {
61 18 : allLgProfilingReps.push_back(rep);
62 : }
63 492 : }
64 :
65 492 : void CcuRepContext::Append(std::shared_ptr<CcuRep::CcuRepBase> rep)
66 : {
67 492 : CollectProfilingReps(rep);
68 492 : CurrentBlock()->Append(rep);
69 492 : }
70 :
71 78 : const std::vector<std::shared_ptr<CcuRep::CcuRepBase>> &CcuRepContext::GetRepSequence()
72 : {
73 78 : 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 46 : void CcuRepContext::SetDieId(uint32_t dieId)
103 : {
104 46 : HCCL_INFO("set dieId[%u]", dieId);
105 46 : this->dieId = dieId;
106 46 : }
107 :
108 709 : uint32_t CcuRepContext::GetDieId() const
109 : {
110 709 : return dieId;
111 : }
112 :
113 20 : void CcuRepContext::SetMissionId(uint32_t missionId)
114 : {
115 20 : if (this->missionId == Hccl::INVALID_U32) {
116 20 : this->missionId = missionId;
117 : }
118 20 : }
119 :
120 34 : uint32_t CcuRepContext::GetMissionId() const
121 : {
122 34 : return missionId;
123 : }
124 :
125 20 : void CcuRepContext::SetMissionKey(uint32_t missionKey)
126 : {
127 20 : this->missionKey = missionKey;
128 20 : }
129 :
130 6 : uint32_t CcuRepContext::GetMissionKey() const
131 : {
132 6 : return missionKey;
133 : }
134 :
135 3 : std::vector<CcuProfilingInfo> &CcuRepContext::GetProfilingInfo()
136 : {
137 3 : return profilingInfo;
138 : }
139 :
140 12 : const std::vector<std::shared_ptr<CcuRepBase>> &CcuRepContext::GetWaiteCkeProfilingReps() const
141 : {
142 12 : return waitCkeProfilingReps;
143 : }
144 :
145 6 : LoopGroupProfilingInfo &CcuRepContext::GetLGProfilingInfo()
146 : {
147 6 : return lgProfilingInfo;
148 : }
149 :
150 26 : void CcuRepContext::AddSqeProfiling(const std::string &kernelName)
151 : {
152 26 : constexpr uint32_t defaultDieId = 0; // 首次填写profiling时dieId未确定
153 : // 生成SQE粒度profiling信息
154 26 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_TASK_PROFILING;
155 26 : ccuProfilingInfoCache.name = kernelName.c_str();
156 26 : ccuProfilingInfoCache.dieId = defaultDieId;
157 26 : HCCL_DEBUG("[%s]type[%d], name[%s], deafultDieId[0]", __func__, ccuProfilingInfoCache.type,
158 : ccuProfilingInfoCache.name.c_str(), ccuProfilingInfoCache.dieId);
159 26 : profilingInfo.push_back(ccuProfilingInfoCache);
160 26 : }
161 :
162 11 : int32_t CcuRepContext::AddProfiling(const std::string &name, uint32_t mask)
163 : {
164 11 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_WAITCKE_PROFILING;
165 11 : ccuProfilingInfoCache.name = name;
166 11 : ccuProfilingInfoCache.ckeId = INVALID_CKE_ID;
167 11 : ccuProfilingInfoCache.mask = mask;
168 11 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId), INVALID_VALUE_CHANNELID,
169 : sizeof(ccuProfilingInfoCache.channelId)));
170 :
171 11 : HCCL_INFO("[%s]name[%s], mask[%u], type[%d]", __func__, name.c_str(), mask, ccuProfilingInfoCache.type);
172 11 : profilingInfo.push_back(ccuProfilingInfoCache);
173 11 : return HCCL_SUCCESS;
174 : }
175 :
176 4 : int32_t CcuRepContext::AddProfiling(const ChannelHandle channel, const std::string &name, uint32_t signalIndex, uint32_t mask)
177 : {
178 4 : void *channelPtr{nullptr};
179 4 : CHK_RET(static_cast<HcclResult>(HcommChannelGet(channel, &channelPtr)));
180 4 : auto *channelImpl = dynamic_cast<CcuUrmaChannel *>(static_cast<Channel *>(channelPtr));
181 :
182 4 : ccuProfilingInfoCache.type = (uint8_t)CcuProfilinType::CCU_WAITCKE_PROFILING;
183 4 : ccuProfilingInfoCache.name = name;
184 4 : CHK_RET(channelImpl->GetLocCkeByIndex(signalIndex, ccuProfilingInfoCache.ckeId));
185 4 : ccuProfilingInfoCache.mask = mask;
186 4 : CHK_SAFETY_FUNC_RET(memset_s(ccuProfilingInfoCache.channelId, sizeof(ccuProfilingInfoCache.channelId),
187 : INVALID_VALUE_CHANNELID, sizeof(ccuProfilingInfoCache.channelId)));
188 4 : ccuProfilingInfoCache.channelId[0] = channelImpl->GetChannelId();
189 4 : ccuProfilingInfoCache.channelHandle[0] = channel;
190 :
191 4 : 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 4 : profilingInfo.push_back(ccuProfilingInfoCache);
195 4 : 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 35 : 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 35 : constexpr uint32_t CCU_CKE_BIT_NUM = 16; // CKE 的 bit 数最多为 16
268 35 : auto &inner = depInfo[id];
269 595 : for (uint32_t i = 0; i < CCU_CKE_BIT_NUM; i++) {
270 560 : uint32_t bit = 1u << i;
271 560 : if ((mask & bit) != 0u) {
272 44 : inner[bit].push_back(rep);
273 : }
274 : }
275 35 : }
276 :
277 30 : std::unordered_map<uint32_t, std::vector<std::shared_ptr<CcuRepBase>>> CcuRepContext::GetDependencyInfo(uint32_t id) {
278 : // 查找给定 id 是否存在于 depInfo 中
279 30 : auto it = depInfo.find(id);
280 : // 如果找到 id,返回与之关联的内层 unordered_map
281 30 : if (it != depInfo.end()) {
282 26 : 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 22 : void CcuRepContext::EraseDependencyInfo(uint32_t id) {
289 22 : depInfo.erase(id);
290 22 : }
291 :
292 1 : void CcuRepContext::ClearDependencyInfo() {
293 1 : depInfo.clear();
294 1 : }
295 :
296 : }; // namespace CcuRep
297 : }; // namespace hcomm
|