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 "topo_match_concurr_mesh.h"
12 :
13 : namespace Hccl {
14 2 : TopoMatchConcurrMesh::TopoMatchConcurrMesh(const RankId vRank, const u32 rankSize, const RankGraph *rankGraph,
15 2 : const DevType devType)
16 2 : : TopoMatchBase(vRank, rankSize, rankGraph, devType)
17 : {
18 2 : }
19 :
20 2 : TopoMatchConcurrMesh::~TopoMatchConcurrMesh()
21 : {
22 2 : }
23 :
24 2 : HcclResult TopoMatchConcurrMesh::MatchTopo(std::vector<std::vector<RankId>> &vTopo,
25 : std::vector<RankId> &virtRanks, std::map<RankId, u32> &virtRankMap)
26 : {
27 : // 获取并校验当前通信层数
28 2 : std::set<u32> levelSet = rankGraph_->GetLevels(myRank_);
29 :
30 2 : CHK_PRT_RET((levelSet.size() == COMM_LEVEL_SIZE_0),
31 : HCCL_ERROR("[CollAlgFactory] [TopoMatchConcurrMesh] Rank [%d], Invalid virtual topo.", myRank_),
32 : HcclResult::HCCL_E_PARA);
33 2 : const NetInstance* netInstance = rankGraph_->GetNetInstanceByRankId(0, myRank_);
34 2 : std::set<RankId> rankSet = netInstance->GetRankIds();
35 10 : for (RankId rankId : rankSet) {
36 8 : rankIds_.push_back(rankId);
37 : }
38 : // 判断level0上的拓扑是否符合 m x n 要求
39 2 : CHK_RET(CalcRankOnSamePlaneOfR0(rankOnSameBoardVector_, rankOnSameSlotVector_, numRanksPerBoard_));
40 :
41 : const auto minmxPair =
42 2 : std::minmax_element(numRanksPerBoard_.begin(), numRanksPerBoard_.end());
43 2 : u32 minNumRankPerBoard = *minmxPair.first;
44 2 : u32 maxNumRankPerBoard = *minmxPair.second;
45 2 : CHK_PRT_RET((minNumRankPerBoard != maxNumRankPerBoard),
46 : HCCL_ERROR("[CollAlgFactory] [TopoMatchConcurrMesh] Rank [%d], Invalid virtual topo for "
47 : "multi-dimensional concurrent mesh, min numRanksPerBoard_[%u], max numRanksPerBoard_[%u].",
48 : myRank_, minNumRankPerBoard, maxNumRankPerBoard),
49 : HcclResult::HCCL_E_PARA);
50 :
51 2 : CHK_PRT_RET(((rankSize_ == 1) || (numRanksPerBoard_[0] * numRanksPerBoard_.size() != rankSize_)),
52 : HCCL_ERROR("[CollAlgFactory] [TopoMatchConcurrMesh] Rank [%d], Invalid virtual topo for "
53 : "multi-dimensional concurrent mesh "
54 : "algorithm with rankSize [%u], ranksPerBoard [%u], ranksPerSlot [%u].",
55 : myRank_, rankSize_, numRanksPerBoard_[0], numRanksPerBoard_.size()),
56 : HcclResult::HCCL_E_PARA);
57 :
58 : // 计算当前rankd的virtRanks, vTopo和virtRankMap
59 2 : u32 myLocalId = rankGraph_->GetReplacedLocalId(myRank_);
60 2 : rankOnSameBoard_ = rankOnSameBoardVector_[myLocalId / RANK_SIZE_EIGHT];
61 2 : rankOnSameSlot_ = rankOnSameSlotVector_[myLocalId % RANK_SIZE_EIGHT];
62 :
63 2 : if ((rankOnSameBoard_.size() == 1) || (rankOnSameSlot_.size() == 1)) {
64 0 : HCCL_DEBUG("[CollAlgFactory] [TopoMatchConcurrMesh] Rank [%d],Virtual topo with rankSize [%u], ranksPerBoard "
65 : "[%u], ranksPerSlot [%u]. "
66 : "1-D Mesh algorithm is adopted.",
67 : myRank_, rankSize_, rankOnSameBoard_.size(), rankOnSameSlot_.size());
68 : }
69 2 : sort(rankOnSameBoard_.begin(), rankOnSameBoard_.end());
70 2 : sort(rankOnSameSlot_.begin(), rankOnSameSlot_.end());
71 2 : vTopo.push_back(rankOnSameBoard_);
72 2 : vTopo.push_back(rankOnSameSlot_);
73 :
74 2 : sort(rankIds_.begin(), rankIds_.end());
75 2 : virtRanks = rankIds_;
76 :
77 2 : CHK_PRT_RET(
78 : GenVirtRankMapping(virtRanks, virtRankMap) != HcclResult::HCCL_SUCCESS,
79 : HCCL_ERROR("[CollAlgFactory] [TopoMatchConcurrMesh] Rank [%d], Fail to generate virtRankMapping.", myRank_),
80 : HcclResult::HCCL_E_INTERNAL);
81 :
82 2 : return HcclResult::HCCL_SUCCESS;
83 2 : }
84 :
85 :
86 : } // namespace Hccl
|