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 "detour_service.h"
12 : #include "env_config/env_config_v2.h"
13 : #include "detour_rules.h"
14 : #include "not_support_exception.h"
15 :
16 : namespace Hccl {
17 :
18 : using namespace std;
19 :
20 : constexpr u32 DETOUR_NODE_NUM = 8;
21 : constexpr u32 DETOUR_NODE_NUM_2P = 2;
22 : constexpr u32 DETOUR_NODE_NUM_4P = 4;
23 :
24 19 : DetourService& DetourService::GetInstance()
25 : {
26 19 : static DetourService detourService(PhyTopo::GetInstance().get());
27 19 : return detourService;
28 : }
29 :
30 2 : DetourService::DetourService(const PhyTopo* phyTopo) { this->phyTopo = phyTopo; }
31 :
32 : struct DetourData {
33 : NodeId detourPhyPeerId{0};
34 : NodeId srcPhyPeerId{0};
35 : NodeId dstPhyPeerId{0};
36 : shared_ptr<NetInstance::Peer> srcNetInstPeer{nullptr};
37 : shared_ptr<NetInstance::Peer> dstNetInstPeer{nullptr};
38 : };
39 :
40 : vector<shared_ptr<PhyTopo::Link>>
41 0 : GetLinks(NodeId srcId, NodeId dstId, const shared_ptr<Graph<PhyTopo::Node, PhyTopo::Link>>& phyTopoGraph)
42 : {
43 0 : vector<shared_ptr<PhyTopo::Link>> links;
44 0 : if (phyTopoGraph == nullptr) {
45 0 : THROW<NullPtrException>(StringFormat("[GetLinks] phyTopoGraphis nullptr"));
46 : }
47 0 : phyTopoGraph->TraverseEdge(srcId, dstId, [&](shared_ptr<PhyTopo::Link> link) {
48 0 : if (link != nullptr) {
49 0 : links.emplace_back(link);
50 0 : return;
51 : }
52 : });
53 :
54 0 : return links;
55 0 : }
56 :
57 0 : void AddDetourLink(
58 : NetInstance* innerNetInst, const DetourData& data,
59 : const shared_ptr<Graph<PhyTopo::Node, PhyTopo::Link>>& phyTopoGraph, const RankTableInfo* rankTable)
60 : {
61 0 : vector<shared_ptr<PhyTopo::Link>> src2detVec = GetLinks(data.srcPhyPeerId, data.detourPhyPeerId, phyTopoGraph);
62 0 : vector<shared_ptr<PhyTopo::Link>> det2dstVec = GetLinks(data.detourPhyPeerId, data.dstPhyPeerId, phyTopoGraph);
63 0 : if (src2detVec.size() == 0 || det2dstVec.size() == 0) {
64 0 : return;
65 : }
66 0 : if (innerNetInst == nullptr) {
67 0 : THROW<NullPtrException>(StringFormat("[AddDetourLink] innerGroup is nullptr"));
68 : }
69 0 : u32 hop = 2; // 目前绕路hop一定是2
70 0 : for (const auto& src2detLink : src2detVec) {
71 0 : for (const auto& det2dstLink : det2dstVec) {
72 0 : LinkType linkType = src2detLink->GetType();
73 0 : std::set<LinkProtocol> linkProtocols = src2detLink->GetLinkProtocols();
74 0 : if (linkType != det2dstLink->GetType()) {
75 0 : HCCL_WARNING(
76 : "[DetourService][InsertDetourLinks][AddDetourLink] src2det[%s] det2dst[%s] linkType no match",
77 : linkType.Describe().c_str(), det2dstLink->GetType().Describe().c_str());
78 0 : continue;
79 0 : };
80 : // todo 是不是应该改为判断两个set是否有交集,取绕路的协议集合? 修改了一下,llt再check一下
81 0 : std::set<LinkProtocol> newLinkProtocols;
82 0 : std::set_intersection(
83 0 : linkProtocols.begin(), linkProtocols.end(), det2dstLink->GetLinkProtocols().begin(),
84 0 : det2dstLink->GetLinkProtocols().end(), std::inserter(newLinkProtocols, newLinkProtocols.begin()));
85 :
86 0 : if (newLinkProtocols.empty()) {
87 : // todo 先改编译,后面再实现日志打印
88 0 : continue;
89 : };
90 :
91 : // 从Link中获取端口集合
92 0 : if (src2detLink->GetSourceIFace() == nullptr || det2dstLink->GetTargetIFace() == nullptr) {
93 0 : THROW<InvalidParamsException>(
94 : "[DetourService][InsertDetourLinks][AddDetourLink] source ConnInterface is nullptr");
95 : }
96 0 : std::set<string> src2detPorts = src2detLink->GetSourceIFace()->GetPorts();
97 0 : std::set<string> det2dstPorts = det2dstLink->GetTargetIFace()->GetPorts();
98 0 : if (src2detPorts.size() != 1 || det2dstPorts.size() != 1) {
99 0 : THROW<InvalidParamsException>(
100 : "[DetourService][InsertDetourLinks][AddDetourLink] Peer to Peer port num error");
101 : }
102 :
103 : // 取出对应的端口,然后去ranktableInfo中查对应端口的地址信息
104 : // todo 逻辑判断一下只取第一个端口是否正确?
105 0 : IpAddress src2detAddr = data.srcNetInstPeer->GetPortAddrMapLayer0()[*src2detPorts.begin()][0];
106 0 : IpAddress det2dstAddr = data.dstNetInstPeer->GetPortAddrMapLayer0()[*det2dstPorts.begin()][0];
107 :
108 : // 构造InterFace对象用于后续生成Link
109 : shared_ptr<NetInstance::ConnInterface> sourceIface = make_shared<NetInstance::ConnInterface>(
110 0 : src2detAddr, src2detPorts, src2detLink->GetSourceIFace()->GetPos(), LinkType::PEER2PEER,
111 0 : src2detLink->GetLinkProtocols(), src2detLink->GetTopoType(), src2detLink->GetTopoInstId());
112 : shared_ptr<NetInstance::ConnInterface> targetIface = make_shared<NetInstance::ConnInterface>(
113 0 : det2dstAddr, det2dstPorts, det2dstLink->GetSourceIFace()->GetPos(), LinkType::PEER2PEER,
114 0 : det2dstLink->GetLinkProtocols(), det2dstLink->GetTopoType(), det2dstLink->GetTopoInstId());
115 :
116 : // 构造Link加入NetInstance中
117 : shared_ptr<NetInstance::Link> sendEdge = make_shared<NetInstance::Link>(
118 0 : data.srcNetInstPeer, data.dstNetInstPeer, sourceIface, targetIface, linkType, newLinkProtocols,
119 0 : LinkDirection::SEND_ONLY, hop);
120 : shared_ptr<NetInstance::Link> recvEdge = make_shared<NetInstance::Link>(
121 0 : data.dstNetInstPeer, data.srcNetInstPeer, targetIface, sourceIface, linkType, newLinkProtocols,
122 0 : LinkDirection::RECV_ONLY, hop);
123 0 : innerNetInst->AddLink(sendEdge);
124 0 : innerNetInst->AddLink(recvEdge);
125 :
126 0 : data.srcNetInstPeer->AddConnInterface(0, sourceIface);
127 0 : if (data.dstNetInstPeer == nullptr) {
128 0 : THROW<NullPtrException>(
129 0 : StringFormat("[DetourService][InsertDetourLinks][AddDetourLink] dstVirtPeer is nullptr"));
130 : }
131 0 : data.dstNetInstPeer->AddConnInterface(0, targetIface);
132 :
133 0 : HCCL_DEBUG(
134 : "[DetourService][AddDetourLink] add SEND_ONLY and RECV_ONLY two links: linkType[%s]"
135 : " sourceIfaceAddress[%s] targetIfaceAddress[%s]",
136 : linkType.Describe().c_str(), sourceIface->GetAddr().Describe().c_str(),
137 : targetIface->GetAddr().Describe().c_str());
138 0 : }
139 : }
140 :
141 0 : HCCL_DEBUG(
142 : "[DetourService][AddDetourLink] srcRankId[%llu] dstRankId[%llu] srcLocalId[%llu] detourLocalId[%llu] "
143 : "dstLocalId[%llu] src2detVec.size[%u] det2dstVec.size[%u]",
144 : data.srcNetInstPeer->GetNodeId(), data.dstNetInstPeer->GetNodeId(), data.srcPhyPeerId, data.detourPhyPeerId,
145 : data.dstPhyPeerId, src2detVec.size(), det2dstVec.size());
146 0 : }
147 :
148 18 : std::vector<LocalId> GetInnerLocalIds(const RankGraph* rankGraph)
149 : {
150 18 : if (rankGraph == nullptr) {
151 0 : THROW<NullPtrException>(StringFormat("[GetInnerLocalIds] rankGraph is nullptr"));
152 : }
153 18 : const NetInstance* innerNetInst = rankGraph->GetNetInstanceByRankId(0, rankGraph->GetMyRank());
154 18 : if (innerNetInst == nullptr) {
155 0 : THROW<NullPtrException>(StringFormat("[GetInnerLocalIds] innerNetInst is nullptr"));
156 : }
157 18 : set<RankId> innerRanks = innerNetInst->GetRankIds();
158 18 : std::vector<LocalId> localIds;
159 68 : for (auto& rankId : innerRanks) {
160 50 : localIds.emplace_back(rankGraph->GetLocalId(rankId));
161 : }
162 18 : return localIds;
163 18 : }
164 :
165 0 : bool IsInSameRow(const std::vector<LocalId>& localIds)
166 : {
167 0 : int preRowId = -1;
168 0 : for (auto& localId : localIds) {
169 0 : int curRowId = localId / DETOUR_NODE_NUM;
170 0 : if (preRowId == -1) {
171 0 : preRowId = curRowId;
172 : } else {
173 0 : if (curRowId != preRowId) {
174 0 : return false;
175 : }
176 : }
177 : }
178 0 : return true;
179 : }
180 :
181 0 : bool IsInSameCol(const std::vector<LocalId>& localIds)
182 : {
183 0 : int preColId = -1;
184 0 : for (auto& localId : localIds) {
185 0 : int curColId = localId % DETOUR_NODE_NUM;
186 0 : if (preColId == -1) {
187 0 : preColId = curColId;
188 : } else {
189 0 : if (curColId != preColId) {
190 0 : return false;
191 : }
192 : }
193 : }
194 0 : return true;
195 : }
196 :
197 0 : bool GetTableIds(
198 : const std::vector<LocalId>& localIds, std::unordered_map<LocalId, u32>& tableIds, std::set<u32>& tableIdSet)
199 : {
200 0 : if (IsInSameRow(localIds)) {
201 0 : for (auto& localId : localIds) {
202 0 : u32 tableId = localId % DETOUR_NODE_NUM;
203 0 : tableIds[localId] = tableId;
204 0 : tableIdSet.emplace(tableId);
205 : }
206 0 : } else if (IsInSameCol(localIds)) {
207 0 : for (auto& localId : localIds) {
208 0 : u32 tableId = localId / DETOUR_NODE_NUM;
209 0 : tableIds[localId] = tableId;
210 0 : tableIdSet.emplace(tableId);
211 : }
212 : } else {
213 0 : HCCL_WARNING("[DetourService][GetTableIds] The ranks localIds are not in the same row or column.");
214 0 : return false;
215 : }
216 0 : return true;
217 : }
218 :
219 4 : void SetDetourTable4P(
220 : const std::set<u32>& tableIdSet, unordered_map<LocalId, unordered_map<LocalId, vector<LocalId>>>& detourTable)
221 : {
222 12 : if (tableIdSet == std::set<u32>{0, 1, 2, 3}) { // tableId必须为 0 1 2 3,才能使用DETOUR4PTABLE_0123绕路
223 1 : detourTable = GetDetour4PTable0123();
224 1 : HCCL_DEBUG("[DetourService] selected detour table is DETOUR4PTABLE_0123");
225 9 : } else if (tableIdSet == std::set<u32>{4, 5, 6, 7}) { // tableId必须为 4 5 6 7,才能使用DETOUR4PTABLE_4567绕路
226 1 : detourTable = GetDetour4PTable4567();
227 1 : HCCL_DEBUG("[DetourService] selected detour table is DETOUR4PTABLE_4567");
228 6 : } else if (tableIdSet == std::set<u32>{0, 2, 4, 6}) { // tableId必须为 0 2 4 6,才能使用DETOUR4PTABLE_0246绕路
229 1 : detourTable = GetDetour4PTable0246();
230 1 : HCCL_DEBUG("[DetourService] selected detour table is DETOUR4PTABLE_0246");
231 3 : } else if (tableIdSet == std::set<u32>{1, 3, 5, 7}) { // tableId必须为 1 3 5 7,才能使用DETOUR4PTABLE_1357绕路
232 1 : detourTable = GetDetour4PTable1357();
233 1 : HCCL_DEBUG("[DetourService] selected detour table is DETOUR4PTABLE_1357");
234 : } else {
235 0 : HCCL_WARNING("no matched detourTable found");
236 : }
237 4 : }
238 :
239 2 : void SetDetourTable2P(
240 : const std::set<u32>& tableIdSet, unordered_map<LocalId, unordered_map<LocalId, vector<LocalId>>>& detourTable)
241 : {
242 11 : if (tableIdSet == std::set<u32>{0, 1} || tableIdSet == std::set<u32>{2, 3} || tableIdSet == std::set<u32>{4, 5}
243 7 : || tableIdSet == std::set<u32>{6, 7}) {
244 1 : detourTable = GetDetour2PTable01();
245 1 : } else if (
246 3 : tableIdSet == std::set<u32>{0, 4} || tableIdSet == std::set<u32>{1, 5} || tableIdSet == std::set<u32>{2, 6}
247 2 : || tableIdSet == std::set<u32>{3, 7}) {
248 1 : detourTable = GetDetour2PTable04();
249 : } else {
250 0 : HCCL_WARNING("[DetourService][%s]no matched detourTable found", __func__);
251 : }
252 2 : }
253 :
254 18 : void GetDetourTableAndTableIds(
255 : const std::vector<LocalId>& localIds,
256 : std::unordered_map<LocalId, unordered_map<LocalId, vector<LocalId>>>& detourTable,
257 : std::unordered_map<LocalId, u32>& tableIds, const RankTableInfo* rankTable)
258 : {
259 18 : std::set<u32> tableIdSet;
260 18 : HcclDetourType detourType = EnvConfig::GetInstance().GetDetourConfig().GetDetourType();
261 18 : if (rankTable->detour == false) {
262 4 : detourType = HcclDetourType::HCCL_DETOUR_DISABLE;
263 : }
264 18 : switch (detourType) {
265 0 : case HcclDetourType::HCCL_DETOUR_ENABLE_2P: {
266 0 : if (localIds.size() != DETOUR_NODE_NUM_2P) {
267 0 : return;
268 : }
269 0 : bool res = GetTableIds(localIds, tableIds, tableIdSet);
270 0 : if (res) {
271 0 : SetDetourTable2P(tableIdSet, detourTable);
272 0 : HCCL_DEBUG("[DetourService] selected detour type is DETOUR2PTABLE");
273 : } else {
274 0 : HCCL_WARNING("[DetourService] detourtype [%s] does not support.", detourType.Describe().c_str());
275 : }
276 0 : break;
277 : }
278 0 : case HcclDetourType::HCCL_DETOUR_ENABLE_4P: {
279 0 : if (localIds.size() != DETOUR_NODE_NUM_4P) {
280 0 : return;
281 : }
282 0 : bool res = GetTableIds(localIds, tableIds, tableIdSet);
283 0 : if (res) {
284 0 : SetDetourTable4P(tableIdSet, detourTable);
285 : } else {
286 0 : HCCL_WARNING("[DetourService] detourtype [%s] does not support.", detourType.Describe().c_str());
287 : }
288 0 : break;
289 : }
290 18 : case HcclDetourType::HCCL_DETOUR_DISABLE: {
291 46 : HCCL_DEBUG("[DetourService] detour is disable");
292 18 : break;
293 : }
294 0 : default: {
295 0 : THROW<NotSupportException>(
296 0 : StringFormat("[DetourService] detourtype [%s] does not support.", detourType.Describe().c_str()));
297 : }
298 : }
299 18 : }
300 :
301 0 : void AddDetourLinks(
302 : const PhyTopo* phyTopo, RankGraph* rankGraph,
303 : std::unordered_map<LocalId, unordered_map<LocalId, vector<LocalId>>>& detourTable,
304 : std::unordered_map<LocalId, u32>& tableIds, const RankTableInfo* rankTable)
305 : {
306 0 : auto phyTopoGraph = phyTopo->GetTopoGraph(0);
307 0 : NetInstance* innerNetInst = rankGraph->GetNetInstanceByRankId(0, rankGraph->GetMyRank());
308 0 : if (innerNetInst == nullptr) {
309 0 : THROW<NullPtrException>(StringFormat("[DetourService] innerNetInst is nullptr"));
310 : }
311 0 : set<RankId> innerRanks = innerNetInst->GetRankIds();
312 0 : for (const auto& srcRankId : innerRanks) {
313 0 : LocalId srcLocalId = rankGraph->GetLocalId(srcRankId);
314 0 : u32 srcTableId = tableIds[srcLocalId];
315 0 : for (const auto& dstRankId : innerRanks) {
316 0 : LocalId dstLocalId = rankGraph->GetLocalId(dstRankId);
317 0 : u32 dstTableId = tableIds[dstLocalId];
318 :
319 0 : if (detourTable.count(srcTableId) == 0 || detourTable[srcTableId].count(dstTableId) == 0) {
320 0 : continue;
321 : }
322 :
323 0 : auto detourTableIds = detourTable[srcTableId][dstTableId];
324 0 : for (auto& detourTableId : detourTableIds) {
325 0 : LocalId detourLocalId = detourTableId + srcLocalId - srcTableId;
326 :
327 : // 插入detourlink
328 0 : DetourData detourData;
329 0 : detourData.detourPhyPeerId = PhyTopo::Peer::GetId(detourLocalId); // 绕路可能没有绕路节点的rankid
330 0 : detourData.srcPhyPeerId = PhyTopo::Peer::GetId(srcLocalId);
331 0 : detourData.dstPhyPeerId = PhyTopo::Peer::GetId(dstLocalId);
332 0 : detourData.srcNetInstPeer = rankGraph->GetPeer(srcRankId);
333 0 : detourData.dstNetInstPeer = rankGraph->GetPeer(dstRankId);
334 :
335 0 : AddDetourLink(innerNetInst, detourData, phyTopoGraph, rankTable);
336 0 : }
337 0 : }
338 : }
339 0 : }
340 :
341 18 : void DetourService::InsertDetourLinks(RankGraph* rankGraph, const RankTableInfo* rankTable)
342 : {
343 18 : std::unordered_map<LocalId, u32> tableIds;
344 18 : std::unordered_map<LocalId, unordered_map<LocalId, vector<LocalId>>> detourTable;
345 :
346 : // 获取innerGroup的localIds
347 18 : std::vector<LocalId> localIds = GetInnerLocalIds(rankGraph);
348 : // 获取当前localIds的tableId和detourTable
349 18 : GetDetourTableAndTableIds(localIds, detourTable, tableIds, rankTable);
350 :
351 18 : if (!detourTable.empty()) {
352 : // 添加绕路links
353 0 : AddDetourLinks(phyTopo, rankGraph, detourTable, tableIds, rankTable);
354 : } else {
355 46 : HCCL_WARNING("no detour links found");
356 : }
357 18 : }
358 :
359 : } // namespace Hccl
|