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 : #ifndef SEARCH_PATH_H
12 : #define SEARCH_PATH_H
13 :
14 : class SearchPath {
15 : public:
16 764 : std::vector<u32> Search(const std::vector<u32>& nicList, bool isDoubleRingMap = false)
17 : {
18 764 : if (isDoubleRingMap && nicList.size() <= 1) { // 单卡不满足doubleRing
19 474 : return {};
20 : }
21 290 : if (nicList.size() == 0 || nicList.size() == 1) {
22 0 : return nicList;
23 : }
24 :
25 290 : result_.clear();
26 290 : std::vector<bool> arrived(nicList.size(), 0);
27 290 : nicSet_.clear();
28 1482 : for (auto i : nicList) {
29 1191 : nicSet_.insert(i);
30 : }
31 :
32 287 : if (dfs(nicList, arrived, 0, nicList[0], isDoubleRingMap)) {
33 285 : return result_;
34 : } else {
35 5 : return {};
36 : }
37 290 : }
38 :
39 : private:
40 1199 : bool dfs(const std::vector<u32>& nicList, std::vector<bool>& arrived, u32 idx, u32 nowNicIdx, bool isDoubleRingMap)
41 : {
42 1199 : std::map<int, std::vector<u32>> reachableMap = isDoubleRingMap ? reachableDoubleRing_ : reachableRank_;
43 : // the last nic, it must reachable to result_[0]
44 1200 : if (idx == nicList.size() - 1) {
45 1442 : for (auto i : reachableMap[nowNicIdx]) {
46 1438 : if (i == result_[0]) {
47 285 : result_.push_back(nowNicIdx);
48 285 : std::string valueStr = "";
49 1471 : for (auto j : result_) {
50 1186 : valueStr.append(std::to_string(j));
51 : }
52 285 : HCCL_INFO("find path success: %s", valueStr.c_str());
53 285 : return true;
54 285 : }
55 : }
56 :
57 4 : return false;
58 : }
59 :
60 911 : arrived[nowNicIdx] = true;
61 912 : result_.push_back(nowNicIdx);
62 :
63 1167 : for (auto i : reachableMap[nowNicIdx]) {
64 1155 : if (nicSet_.count(i) == 0 || arrived[i]) {
65 246 : continue;
66 : }
67 :
68 911 : if (dfs(nicList, arrived, idx + 1, i, isDoubleRingMap)) {
69 901 : return true;
70 : }
71 : }
72 :
73 12 : arrived[nowNicIdx] = false;
74 12 : result_.pop_back();
75 12 : return false;
76 1202 : }
77 :
78 : std::vector<u32> result_;
79 : std::set<u32> nicSet_;
80 : // 适配910_93设备双轨组网,可通过SIO串联
81 : std::map<int, std::vector<u32>> reachableRank_
82 : = {{0, {1, 2, 4, 6, 8, 10, 12, 14}}, {1, {0, 3, 5, 7, 9, 11, 13, 15}}, {2, {3, 4, 6, 8, 10, 12, 14, 0}},
83 : {3, {2, 5, 7, 9, 11, 13, 15, 1}}, {4, {5, 6, 8, 10, 12, 14, 0, 2}}, {5, {4, 7, 9, 11, 13, 15, 1, 3}},
84 : {6, {7, 8, 10, 12, 14, 0, 2, 4}}, {7, {6, 9, 11, 13, 15, 1, 3, 5}}, {8, {9, 10, 12, 14, 0, 2, 4, 6}},
85 : {9, {8, 11, 13, 15, 1, 3, 5, 7}}, {10, {11, 12, 14, 0, 2, 4, 6, 8}}, {11, {10, 13, 15, 1, 3, 5, 7, 9}},
86 : {12, {13, 14, 0, 2, 4, 6, 8, 10}}, {13, {12, 15, 1, 3, 5, 7, 9, 11}}, {14, {15, 0, 2, 4, 6, 8, 10, 12}},
87 : {15, {14, 1, 3, 5, 7, 9, 11, 13}}};
88 :
89 : std::map<int, std::vector<u32>> reachableDoubleRing_
90 : = {{0, {1}}, {1, {0, 2, 4, 6, 8, 10, 12, 14}}, {2, {3}}, {3, {4, 6, 8, 10, 12, 14, 0, 2}},
91 : {4, {5}}, {5, {6, 8, 10, 12, 14, 0, 2, 4}}, {6, {7}}, {7, {8, 10, 12, 14, 0, 2, 4, 6}},
92 : {8, {9}}, {9, {10, 12, 14, 0, 2, 4, 6, 8}}, {10, {11}}, {11, {12, 14, 0, 2, 4, 6, 8, 10}},
93 : {12, {13}}, {13, {14, 0, 2, 4, 6, 8, 10, 12}}, {14, {15}}, {15, {0, 2, 4, 6, 8, 10, 12, 14}}};
94 : };
95 : #endif
|