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