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 HCCLV2_COLL_ALG_BASE_SELECTOR
12 : #define HCCLV2_COLL_ALG_BASE_SELECTOR
13 :
14 : #include <string>
15 : #include "coll_alg_params.h"
16 : #include "coll_operator.h"
17 : #include "virtual_topo.h"
18 : #include "log.h"
19 : #include "env_config.h"
20 :
21 : namespace Hccl {
22 : constexpr u32 CCU_MS_MODE = 2;
23 :
24 : constexpr u32 BIG_CLOS_RANGE = 8;
25 :
26 : enum class SelectorStatus { MATCH, NOT_MATCH };
27 : enum class Level0Shape {
28 : MESH_1D = 1,
29 : MESH_2D = 2,
30 : CLOS = 3,
31 : MESH_1D_CLOS = 4,
32 : };
33 :
34 :
35 : const std::map<OpType, std::string> OP_TYPE_TO_AICPU_SOLE_ALG_MAP = {
36 : {OpType::ALLGATHER, "InsAllGatherMesh"},
37 : {OpType::REDUCESCATTER, "InsReduceScatterNHR"},
38 : {OpType::ALLREDUCE, "InsAllReduceNHR"},
39 : {OpType::ALLTOALL, "InsAlltoAllMesh"},
40 : {OpType::ALLTOALLV, "InsAlltoAllvMesh"},
41 : {OpType::ALLTOALLVC, "InsAlltoAllvcMesh"},
42 : };
43 :
44 : class BaseSelector {
45 : public:
46 0 : virtual ~BaseSelector() {};
47 : BaseSelector &SetVirtualTopo(RankGraph *rankGraph);
48 : BaseSelector &SetDevType(DevType devType);
49 : BaseSelector &SetMyRank(RankId myRank);
50 : BaseSelector &SetRankSize(u32 rankSize);
51 : BaseSelector &SetSeverId(std::string severId);
52 : BaseSelector &SetDeviceNumPerSever(u32 deviceNumPerSever);
53 : BaseSelector &SetServerNum(u32 serverNum);
54 : BaseSelector &SetOpConfig(OpExecuteConfig opConfig);
55 : BaseSelector &SetIsMc2(bool isMc2);
56 :
57 : RankGraph *GetVirtualTopo();
58 : DevType GetDevType();
59 : RankId GetMyRank() const;
60 : u32 GetRankSize() const;
61 : std::string GetSeverId();
62 : u32 GetDeviceNumPerSever() const;
63 : u32 GetServerNum() const;
64 :
65 : bool IsInputOutputOverlap(const std::shared_ptr<Buffer> &inputMem, const std::shared_ptr<Buffer> &outputMem) const;
66 : virtual SelectorStatus Select(const CollAlgOperator &op, CollAlgParams ¶ms, std::string &primQueueGenName)
67 : = 0;
68 :
69 : protected:
70 : struct NetLayerDetails {
71 : u32 netLayerNum;
72 : std::set<u32> netLayers;
73 : std::vector<u32> netInstNumOfLayer;
74 : std::vector<std::vector<u32>> instSizeListOfLayer;
75 : std::vector<u32> localNetInsSizeOfLayer;
76 : };
77 : struct TopoInstDetails {
78 : u32 topoInstNum;
79 : std::vector<u32> sizeOfTopo;
80 : std::vector<TopoType> typeOfTopo;
81 : std::vector<std::vector<u32>> ranksInTopo;
82 : std::map<TopoType, std::vector<u32>> rankNumForTopoType;
83 : };
84 : struct TopoInfo {
85 : u32 levelNum;
86 : Level0Shape level0Shape;
87 : NetLayerDetails netLayerDetails;
88 : std::vector<TopoInstDetails> topoInstDetailsOfLayer;
89 :
90 : bool Level0Nhr{false};
91 : bool Level1Nhr{false};
92 :
93 : bool level0PcieMix{false};
94 : bool level0BigClosRange{false};
95 : };
96 : u32 Gcd(u32 a, u32 b) const; // 自定义实现的 gcd 函数(兼容旧版本 C++)
97 : u32 GcdOfArray(const std::vector<u32> &numbers) const; // 计算数组中所有元素的最大公约数
98 : u32 GetLevel0Gcd();
99 : void CalcTopoShape(TopoInfo &topoInfo) const;
100 : bool IsAsymmetricTopoShapeLevel1Nhr(const std::vector<std::vector<u32>> &localIdPerBoard, u32 gcdRankSizeLevel0) const;
101 : bool IsTopoShapeLevel0Regular(const std::vector<std::vector<u32>> &localIdPerBoard) const;
102 : bool IsLayerAllConnetedWithTopo(const TopoInfo &topoInfo, const u32 netLayer, const TopoType topoType) const;
103 : HcclResult CalcLevel0TopoShape(TopoInfo &topoInfo) const;
104 : HcclResult ExtractNetLayerDetails(TopoInfo &topoInfo) const;
105 : HcclResult ExtractTopoDetails(TopoInfo &topoInfo) const;
106 : bool Is2DieFullMesh() const;
107 : bool IsLevel0PcieMix() const;
108 : RankGraph *rankGraph_ = nullptr;
109 : OpExecuteConfig opConfig_;
110 : DevType devType_;
111 : RankId myRank_;
112 : u32 rankSize_;
113 : std::string severId_;
114 : u32 deviceNumPerSever_;
115 : u32 serverNum_;
116 : bool isMc2_{false};
117 : };
118 :
119 : } // namespace Hccl
120 : #endif
|