LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_executor/ins_alg_executor/all_reduce - ins_all_reduce_parallel_executor_opt.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 313 0
Test Date: 2026-08-18 17:47:01 Functions: 0.0 % 20 0

            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 "ins_all_reduce_parallel_executor_opt.h"
      12              : 
      13              : #include "log.h"
      14              : 
      15              : #include "ins_coll_alg_registry.h"
      16              : 
      17              : #include "topo_match_mesh_nhr.h"
      18              : #include "alg_data_trans_wrapper.h"
      19              : 
      20              : #include "ins_temp_reduce_scatter_mesh_1D.h"
      21              : 
      22              : #include "ins_temp_all_gather_mesh.h"
      23              : 
      24              : namespace Hccl {
      25              : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
      26              : 
      27              : template <
      28              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
      29              :     typename InsAlgTemplate3>
      30            0 : InsAllReduceParallelExecutorV2<
      31              :     AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::InsAllReduceParallelExecutorV2()
      32            0 :     : InsCollAlgBase()
      33            0 : {}
      34              : 
      35              : template <
      36              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
      37              :     typename InsAlgTemplate3>
      38            0 : InsAllReduceParallelExecutorV2<
      39              :     AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::~InsAllReduceParallelExecutorV2()
      40            0 : {}
      41              : 
      42              : template <
      43              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
      44              :     typename InsAlgTemplate3>
      45              : HcclResult
      46            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
      47              :     CalcResOffload(const RankGraph* rankGraph, [[maybe_unused]] const u64& dataSize, CollOffloadOpResReq& resReq)
      48              : {
      49            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] CalcResOffload begins.");
      50            0 :     resReq.requiredScratchMemSize = MAX_OFFLOAD_SCRATCH_SIZE; // 200MB
      51              :     // Topo Match
      52            0 :     AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
      53            0 :     CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
      54            0 :     CHK_RET(CalcLocalRankSize());
      55              : 
      56            0 :     InsAlgTemplate0 intraTempAlgRS(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
      57            0 :     InsAlgTemplate1 interTempAlgRS(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
      58            0 :     InsAlgTemplate2 intraTempAlgAG(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
      59            0 :     InsAlgTemplate3 interTempAlgAG(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
      60              : 
      61              :     // 设置链路信息
      62            0 :     std::vector<map<u32, u32>> rank2PathNumMap;
      63            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] CalcResOffload SetPathNumMap");
      64            0 :     CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
      65            0 :     intraTempAlgRS.setPathNumMap(rank2PathNumMap[0]);
      66            0 :     interTempAlgRS.setPathNumMap(rank2PathNumMap[1]);
      67            0 :     intraTempAlgAG.setPathNumMap(rank2PathNumMap[0]);
      68            0 :     interTempAlgAG.setPathNumMap(rank2PathNumMap[1]);
      69              : 
      70            0 :     AlgTempResReq resReqIntraRS;
      71            0 :     AlgTempResReq resReqInterRS;
      72              : 
      73            0 :     CHK_RET(intraTempAlgRS.CalcRes(resReqIntraRS));
      74            0 :     CHK_RET(interTempAlgRS.CalcRes(resReqInterRS));
      75              : 
      76            0 :     AlgTempResReq resReqIntraAG;
      77            0 :     AlgTempResReq resReqInterAG;
      78              : 
      79            0 :     CHK_RET(intraTempAlgAG.CalcRes(resReqIntraAG));
      80            0 :     CHK_RET(interTempAlgAG.CalcRes(resReqInterAG));
      81              : 
      82              :     // 算法从流数量 = Σ(temp的que数量 + temp的从流数量 * temp调用次数) - 算法主流数量
      83              :     resReq.requiredSubQueNum
      84            0 :         = std::max((resReqIntraAG.queNum + resReqInterAG.queNum), (resReqIntraRS.queNum + resReqInterRS.queNum)) - 1;
      85            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2::CalcResOffload]requiredSubQueNum = %llu", resReq.requiredSubQueNum);
      86              : 
      87            0 :     return HcclResult::HCCL_SUCCESS;
      88            0 : }
      89              : 
      90              : template <
      91              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
      92              :     typename InsAlgTemplate3>
      93              : HcclResult
      94            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
      95              :     CalcRes(const RankGraph* rankGraph, CollAlgResReq& algResReq)
      96              : {
      97            0 :     HCCL_INFO("[InsFourTemplateAllReduceExecutor] CalcRes begins.");
      98              : 
      99              :     // 拓扑匹配
     100            0 :     AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
     101            0 :     CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
     102            0 :     algResReq.topoInfo.UpdateMultiLevelTopo(virtRanks_, virtRankMap_, vTopo_);
     103            0 :     CHK_RET(CalcLocalRankSize());
     104              : 
     105              :     // 创建四个模板实例
     106            0 :     InsAlgTemplate0 tempAlgRSIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     107            0 :     InsAlgTemplate1 tempAlgRSInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     108            0 :     InsAlgTemplate2 tempAlgAGIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     109            0 :     InsAlgTemplate3 tempAlgAGInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     110              : 
     111              :     // 设置链路信息
     112            0 :     std::vector<map<u32, u32>> rank2PathNumMap;
     113            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] CalcResOffload SetPathNumMap");
     114            0 :     CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
     115            0 :     tempAlgRSIntra.setPathNumMap(rank2PathNumMap[0]);
     116            0 :     tempAlgRSInter.setPathNumMap(rank2PathNumMap[1]);
     117            0 :     tempAlgAGIntra.setPathNumMap(rank2PathNumMap[0]);
     118            0 :     tempAlgAGInter.setPathNumMap(rank2PathNumMap[1]);
     119              : 
     120              :     // 计算各模板资源需求
     121            0 :     AlgTempResReq resReqRSIntra, resReqRSInter, resReqAGIntra, resReqAGInter;
     122              : 
     123            0 :     CHK_RET(tempAlgRSIntra.CalcRes(resReqRSIntra));
     124            0 :     CHK_RET(tempAlgRSInter.CalcRes(resReqRSInter));
     125            0 :     CHK_RET(tempAlgAGIntra.CalcRes(resReqAGIntra));
     126            0 :     CHK_RET(tempAlgAGInter.CalcRes(resReqAGInter));
     127              : 
     128              :     // 计算链接信息
     129            0 :     CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqRSIntra.links, algResReq.levelRankPairs));
     130            0 :     CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqRSInter.links, algResReq.levelRankPairs));
     131            0 :     CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqAGIntra.links, algResReq.levelRankPairs));
     132            0 :     CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqAGInter.links, algResReq.levelRankPairs));
     133              : 
     134              :     algResReq.primQueueNum
     135            0 :         = std::max(resReqRSIntra.queNum, resReqAGIntra.queNum) + std::max(resReqRSInter.queNum, resReqAGInter.queNum);
     136            0 :     HCCL_INFO("[InsFourTemplateAllReduceExecutor::CalcRes] primQueueNum = %u", algResReq.primQueueNum);
     137              : 
     138            0 :     std::vector<std::tuple<QId, QId, u32>> notifyRequests;
     139            0 :     for (QId q = 1; q < algResReq.primQueueNum; q++) {
     140            0 :         notifyRequests.emplace_back(std::make_tuple(0, q, 0));
     141            0 :         notifyRequests.emplace_back(std::make_tuple(q, 0, 0));
     142              :     }
     143              : 
     144            0 :     u32 tempMasterQId = std::max(resReqRSIntra.queNum, resReqAGIntra.queNum);
     145            0 :     for (QId q = tempMasterQId + 1; q < algResReq.primQueueNum; q++) {
     146            0 :         notifyRequests.emplace_back(std::make_tuple(tempMasterQId, q, 0));
     147            0 :         notifyRequests.emplace_back(std::make_tuple(q, tempMasterQId, 0));
     148              :     }
     149              : 
     150            0 :     algResReq.queueNotifys = notifyRequests;
     151              : 
     152              :     // 计算链接资源
     153            0 :     CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqRSIntra.links, algResReq.links));
     154            0 :     CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqRSInter.links, algResReq.links));
     155            0 :     CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqAGIntra.links, algResReq.links));
     156            0 :     CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqAGInter.links, algResReq.links));
     157              : 
     158            0 :     return HcclResult::HCCL_SUCCESS;
     159            0 : }
     160              : 
     161              : template <
     162              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     163              :     typename InsAlgTemplate3>
     164            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     165              :     GetParallelDataSplitRate(std::vector<float>& splitDataSize) const
     166              : {
     167            0 :     double splitData = 0.5;
     168            0 :     splitDataSize.push_back(splitData);
     169            0 :     splitDataSize.push_back(splitData);
     170            0 :     return;
     171              : }
     172              : 
     173              : template <
     174              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     175              :     typename InsAlgTemplate3>
     176            0 : HcclResult InsAllReduceParallelExecutorV2<
     177              :     AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::CalcLocalRankSize()
     178              : {
     179            0 :     uint64_t virtRanks_2 = 2;
     180            0 :     CHK_PRT_RET(
     181              :         virtRanks_.size() < virtRanks_2, HCCL_ERROR("[CalcLocalRankSize] virtRanks level num is smaller than 2."),
     182              :         HcclResult::HCCL_E_INTERNAL);
     183              : 
     184            0 :     rankSizeLevel0_ = virtRanks_.at(0).size();
     185            0 :     rankSizeLevel1_ = virtRanks_.at(1).size();
     186            0 :     rankSize_ = rankSizeLevel0_ * rankSizeLevel1_;
     187              : 
     188              :     // 计算当前 rank 在各层级中的索引
     189            0 :     if (virtRankMap_[0].find(myRank_) != virtRankMap_[0].end()) {
     190            0 :         rankIdxLevel0_ = virtRankMap_[0][myRank_];
     191              :     } else {
     192            0 :         HCCL_ERROR("[CalcLocalRankSize] rank [%d] is not in level 0 topo", myRank_);
     193            0 :         return HcclResult::HCCL_E_INTERNAL;
     194              :     }
     195            0 :     if (virtRankMap_[1].find(myRank_) != virtRankMap_[1].end()) {
     196            0 :         rankIdxLevel1_ = virtRankMap_[1][myRank_];
     197              :     } else {
     198            0 :         HCCL_ERROR("[CalcLocalRankSize] rank [%d] is not in level 1 topo", myRank_);
     199            0 :         return HcclResult::HCCL_E_INTERNAL;
     200              :     }
     201              : 
     202            0 :     HCCL_INFO(
     203              :         "[CalcLocalRankSize] localRankSize: myRank[%d] rankSizeLevel0_[%u] rankSizeLevel1_[%u] "
     204              :         "rankIdxLevel0_[%llu] rankIdxLevel1_[%llu]",
     205              :         myRank_, rankSizeLevel0_, rankSizeLevel1_, rankIdxLevel0_, rankIdxLevel1_);
     206            0 :     return HcclResult::HCCL_SUCCESS;
     207              : };
     208              : 
     209              : template <
     210              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     211              :     typename InsAlgTemplate3>
     212              : HcclResult
     213            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     214              :     PrepareResForTemplate(
     215              :         const RankGraph* rankGraph, InsAlgTemplate0& tempAlgIntraRS, InsAlgTemplate1& tempAlgInterRS,
     216              :         InsAlgTemplate2& tempAlgIntraAG, InsAlgTemplate3& tempAlgInterAG)
     217              : {
     218            0 :     AlgTempResReq resReqIntraRS, resReqInterRS, resReqIntraAG, resReqInterAG;
     219              : 
     220            0 :     CHK_RET(tempAlgIntraRS.CalcRes(resReqIntraRS));
     221            0 :     CHK_RET(tempAlgInterRS.CalcRes(resReqInterRS));
     222            0 :     CHK_RET(tempAlgIntraAG.CalcRes(resReqIntraAG));
     223            0 :     CHK_RET(tempAlgInterAG.CalcRes(resReqInterAG));
     224              : 
     225            0 :     CHK_RET(CalcQue(resReqIntraRS, resReqInterRS, resReqIntraAG, resReqInterAG));
     226              : 
     227            0 :     CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqIntraRS.links, intraRSLinks_));
     228            0 :     CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqInterRS.links, interRSLinks_));
     229            0 :     CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqIntraAG.links, intraAGLinks_));
     230            0 :     CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqInterAG.links, interAGLinks_));
     231            0 :     HCCL_INFO(
     232              :         "[InsAllReduceParallelExecutorV2] intraRSLinks_ size[%zu], interRSLinks_ size[%zu], intraAGLinks_ size[%zu], "
     233              :         "interAGLinks_ size[%zu]",
     234              :         intraRSLinks_.size(), interRSLinks_.size(), intraAGLinks_.size(), interAGLinks_.size());
     235            0 :     return HCCL_SUCCESS;
     236            0 : }
     237              : 
     238              : template <
     239              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     240              :     typename InsAlgTemplate3>
     241              : HcclResult
     242            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     243              :     PrepareResForTemplate(
     244              :         ConnectedLinkMgr* linkMgr, InsAlgTemplate0& tempAlgIntraRS, InsAlgTemplate1& tempAlgInterRS,
     245              :         InsAlgTemplate2& tempAlgIntraAG, InsAlgTemplate3& tempAlgInterAG)
     246              : {
     247            0 :     AlgTempResReq resReqIntraRS, resReqInterRS, resReqIntraAG, resReqInterAG;
     248              : 
     249            0 :     CHK_RET(tempAlgIntraRS.CalcRes(resReqIntraRS));
     250            0 :     CHK_RET(tempAlgInterRS.CalcRes(resReqInterRS));
     251            0 :     CHK_RET(tempAlgIntraAG.CalcRes(resReqIntraAG));
     252            0 :     CHK_RET(tempAlgInterAG.CalcRes(resReqInterAG));
     253              : 
     254            0 :     CHK_RET(CalcQue(resReqIntraRS, resReqInterRS, resReqIntraAG, resReqInterAG));
     255              : 
     256            0 :     CHK_RET(PrepResLinks(myRank_, resReqIntraRS.links, linkMgr, intraRSLinks_));
     257            0 :     CHK_RET(PrepResLinks(myRank_, resReqInterRS.links, linkMgr, interRSLinks_));
     258            0 :     CHK_RET(PrepResLinks(myRank_, resReqIntraAG.links, linkMgr, intraAGLinks_));
     259            0 :     CHK_RET(PrepResLinks(myRank_, resReqInterAG.links, linkMgr, interAGLinks_));
     260            0 :     HCCL_INFO(
     261              :         "[InsAllReduceParallelExecutorV2] intraRSLinks_ size[%zu], interRSLinks_ size[%zu], intraAGLinks_ size[%zu], "
     262              :         "interAGLinks_ size[%zu]",
     263              :         intraRSLinks_.size(), interRSLinks_.size(), intraAGLinks_.size(), interAGLinks_.size());
     264            0 :     return HCCL_SUCCESS;
     265            0 : }
     266              : 
     267              : template <
     268              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     269              :     typename InsAlgTemplate3>
     270            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     271              :     GenRSIntraParams0(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     272              : {
     273            0 :     u64 sliceCount = dataCount / rankSize_;
     274            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     275            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSize_ - 1);
     276            0 :     SetTemplateDataParams(
     277            0 :         params, BufferType::INPUT, BufferType::OUTPUT, dataOffset, dataOffset + rankIdxLevel0_ * sliceBytes, scratchOff,
     278            0 :         sliceBytes, sliceBytes, sliceBytes, rankSizeLevel1_, sliceBytes * rankSizeLevel0_, sliceBytes * rankSizeLevel0_,
     279              :         tailSize);
     280            0 : }
     281              : 
     282              : template <
     283              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     284              :     typename InsAlgTemplate3>
     285            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     286              :     GenRSInterParams0(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     287              : {
     288            0 :     u64 sliceCount = dataCount / rankSize_;
     289            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     290            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSize_ - 1);
     291            0 :     SetTemplateDataParams(
     292            0 :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset + rankIdxLevel0_ * sliceBytes,
     293            0 :         dataOffset + myRank_ * sliceBytes, scratchOff, sliceBytes, sliceBytes * rankSizeLevel0_,
     294            0 :         sliceBytes * rankSizeLevel0_, 1, 0, 0, tailSize);
     295            0 : }
     296              : 
     297              : template <
     298              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     299              :     typename InsAlgTemplate3>
     300            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     301              :     GenAGInterParams0(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     302              : {
     303            0 :     u64 sliceCount = dataCount / rankSize_;
     304            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     305            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSize_ - 1);
     306            0 :     SetTemplateDataParams(
     307            0 :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset + rankIdxLevel0_ * sliceBytes,
     308            0 :         dataOffset + rankIdxLevel0_ * sliceBytes, scratchOff, sliceBytes, sliceBytes * rankSizeLevel0_,
     309            0 :         sliceBytes * rankSizeLevel0_, 1, 0, 0, tailSize);
     310            0 : }
     311              : 
     312              : template <
     313              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     314              :     typename InsAlgTemplate3>
     315            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     316              :     GenAGIntraParams0(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     317              : {
     318            0 :     u64 sliceCount = dataCount / rankSize_;
     319            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     320            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSize_ - 1);
     321            0 :     SetTemplateDataParams(
     322              :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset, dataOffset, scratchOff, sliceBytes, sliceBytes,
     323            0 :         sliceBytes, rankSizeLevel1_, rankSizeLevel0_ * sliceBytes, rankSizeLevel0_ * sliceBytes, tailSize);
     324            0 : }
     325              : 
     326              : template <
     327              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     328              :     typename InsAlgTemplate3>
     329            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     330              :     GenRSInterParams1(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     331              : {
     332            0 :     u64 sliceCount = dataCount / rankSize_ * rankSizeLevel0_;
     333            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     334            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSizeLevel1_ - 1);
     335            0 :     SetTemplateDataParams(
     336            0 :         params, BufferType::INPUT, BufferType::OUTPUT, dataOffset, dataOffset + rankIdxLevel1_ * sliceBytes, scratchOff,
     337              :         sliceBytes, sliceBytes, sliceBytes, 1, 0, 0, tailSize);
     338            0 : }
     339              : 
     340              : template <
     341              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     342              :     typename InsAlgTemplate3>
     343            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     344              :     GenRSIntraParams1(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     345              : {
     346            0 :     HCCL_INFO("InsAllReduceParallelExecutorV2 GenRSIntraParams1 start");
     347            0 :     u64 dataCountTmp = CalcDataCountTmp1(dataCount);
     348            0 :     u64 sliceBytes = sliceCount_ * dataTypeSize_;
     349            0 :     SetTemplateDataParams(
     350            0 :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset + rankIdxLevel1_ * sliceBytes * rankSizeLevel0_,
     351            0 :         dataOffset + sliceBytes * myRank_, scratchOff, sliceBytes, sliceBytes, sliceBytes, 1, 0, 0,
     352            0 :         (dataCountTmp - sliceCount_ * (rankSizeLevel0_ - 1)) * dataTypeSize_);
     353            0 :     HCCL_INFO("InsAllReduceParallelExecutorV2 GenRSIntraParams1 end");
     354            0 : }
     355              : 
     356              : template <
     357              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     358              :     typename InsAlgTemplate3>
     359            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     360              :     GenAGIntraParams1(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     361              : {
     362            0 :     HCCL_INFO("InsAllReduceParallelExecutorV2 GenAGIntraParams1 start");
     363            0 :     u64 dataCountTmp = CalcDataCountTmp1(dataCount);
     364            0 :     u64 sliceBytes = sliceCount_ * dataTypeSize_;
     365            0 :     SetTemplateDataParams(
     366            0 :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset + rankIdxLevel1_ * sliceBytes * rankSizeLevel0_,
     367            0 :         dataOffset + rankIdxLevel1_ * sliceBytes * rankSizeLevel0_,
     368            0 :         scratchOff + rankIdxLevel1_ * rankSizeLevel0_ * sliceBytes, sliceBytes, sliceBytes, sliceBytes, 1, 0, 0,
     369            0 :         (dataCountTmp - sliceCount_ * (rankSizeLevel0_ - 1)) * dataTypeSize_);
     370            0 :     HCCL_INFO("InsAllReduceParallelExecutorV2 GenAGIntraParams1 end");
     371            0 : }
     372              : 
     373              : template <
     374              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     375              :     typename InsAlgTemplate3>
     376            0 : void InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     377              :     GenAGInterParams1(const u64 dataOffset, const u64 dataCount, const u64 scratchOff, TemplateDataParams& params) const
     378              : {
     379            0 :     u64 sliceCount = dataCount / rankSize_ * rankSizeLevel0_;
     380            0 :     u64 sliceBytes = sliceCount * dataTypeSize_;
     381            0 :     u64 tailSize = dataCount * dataTypeSize_ - sliceBytes * (rankSizeLevel1_ - 1);
     382            0 :     SetTemplateDataParams(
     383              :         params, BufferType::OUTPUT, BufferType::OUTPUT, dataOffset, dataOffset, scratchOff, sliceBytes, sliceBytes,
     384              :         sliceBytes, 1, 0, 0, tailSize);
     385            0 :     HCCL_INFO("InsAllReduceParallelExecutorV2 GenAGInterParams1 end");
     386            0 : }
     387              : 
     388              : /*
     389              :  *@Desc: HOST算法编排
     390              :  */
     391              : template <
     392              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     393              :     typename InsAlgTemplate3>
     394              : HcclResult
     395            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     396              :     Orchestrate(const RankGraph* rankGraph, const CollAlgOperator& op, const CollAlgParams& params, InsQuePtr insQue)
     397              : {
     398            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] Host Orchestrate begins.");
     399              : 
     400              :     // init and check params
     401            0 :     CHK_RET(Init(op, params, insQue));
     402              : 
     403              :     // Topo Match
     404            0 :     AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
     405            0 :     CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
     406              : 
     407            0 :     CHK_RET(CalcLocalRankSize());
     408              : 
     409              :     // 实例化算法模板类
     410            0 :     InsAlgTemplate0 tempAlgIntraRS(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     411            0 :     InsAlgTemplate1 tempAlgInterRS(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     412            0 :     InsAlgTemplate2 tempAlgIntraAG(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     413            0 :     InsAlgTemplate3 tempAlgInterAG(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     414              : 
     415            0 :     std::vector<map<u32, u32>> rank2PathNumMap;
     416            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] Orchestrate SetPathNumMap");
     417            0 :     CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
     418            0 :     tempAlgIntraRS.setPathNumMap(rank2PathNumMap[0]);
     419            0 :     tempAlgInterRS.setPathNumMap(rank2PathNumMap[1]);
     420            0 :     tempAlgIntraAG.setPathNumMap(rank2PathNumMap[0]);
     421            0 :     tempAlgInterAG.setPathNumMap(rank2PathNumMap[1]);
     422              : 
     423            0 :     InitAlgCommonParams(tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG, op);
     424              : 
     425              :     // 计算算法模板所需资源
     426            0 :     CHK_RET(PrepareResForTemplate(rankGraph, tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG));
     427            0 :     CHK_RET(GenInsQues(tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG));
     428            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] Orchestrate success.");
     429              : 
     430            0 :     return HcclResult::HCCL_SUCCESS;
     431            0 : }
     432              : 
     433              : /*
     434              :  *@Desc: AICPU算法编排
     435              :  */
     436              : template <
     437              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     438              :     typename InsAlgTemplate3>
     439              : HcclResult
     440            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     441              :     Orchestrate(
     442              :         const AlgTopoInfo& topoInfo, const CollAlgOperator& op, const CollAlgParams& params, ConnectedLinkMgr* linkMgr,
     443              :         InsQuePtr insQue)
     444              : {
     445            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] AICPU Orchestrate begins.");
     446              :     // init and check params
     447            0 :     CHK_RET(Init(op, params, insQue));
     448              :     // 获取当前通信域的信息
     449            0 :     vTopo_ = topoInfo.vTopo;
     450            0 :     virtRankMap_ = topoInfo.virtRankMap;
     451            0 :     virtRanks_ = topoInfo.virtRanks;
     452            0 :     CHK_RET(CalcLocalRankSize());
     453              : 
     454              :     // 实例化算法模板类
     455            0 :     InsAlgTemplate0 tempAlgIntraRS(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     456            0 :     InsAlgTemplate1 tempAlgInterRS(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     457            0 :     InsAlgTemplate2 tempAlgIntraAG(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
     458            0 :     InsAlgTemplate3 tempAlgInterAG(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
     459              : 
     460            0 :     std::vector<map<u32, u32>> rank2PathNumMap;
     461            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] Orchestrate SetPathNumMap");
     462            0 :     CHK_RET(SetPathNumMapByLinkMgrMultiLevel(linkMgr, virtRanks_, myRank_, rank2PathNumMap));
     463            0 :     tempAlgIntraRS.setPathNumMap(rank2PathNumMap[0]);
     464            0 :     tempAlgInterRS.setPathNumMap(rank2PathNumMap[1]);
     465            0 :     tempAlgIntraAG.setPathNumMap(rank2PathNumMap[0]);
     466            0 :     tempAlgInterAG.setPathNumMap(rank2PathNumMap[1]);
     467              : 
     468            0 :     InitAlgCommonParams(tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG, op);
     469              : 
     470              :     // 计算算法模板所需资源
     471            0 :     CHK_RET(PrepareResForTemplate(linkMgr, tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG));
     472            0 :     CHK_RET(GenInsQues(tempAlgIntraRS, tempAlgInterRS, tempAlgIntraAG, tempAlgInterAG));
     473            0 :     HCCL_INFO("[InsAllReduceParallelExecutorV2] Orchestrate success.");
     474              : 
     475            0 :     return HcclResult::HCCL_SUCCESS;
     476            0 : }
     477              : 
     478              : template <
     479              :     typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2,
     480              :     typename InsAlgTemplate3>
     481              : HcclResult
     482            0 : InsAllReduceParallelExecutorV2<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>::
     483              :     GenInsQues(
     484              :         InsAlgTemplate0& tempAlgIntraRS, InsAlgTemplate1& tempAlgInterRS, InsAlgTemplate2& tempAlgIntraAG,
     485              :         InsAlgTemplate3& tempAlgInterAG)
     486              : {
     487            0 :     std::vector<float> dataSplitSize;
     488            0 :     GetParallelDataSplitRate(dataSplitSize);
     489            0 :     u64 alignedSize = 16 * 1024; // 16K 对齐
     490            0 :     u64 UB_DATA_SIZE_LIMIT = static_cast<u64>(UB_MAX_DATA_SIZE) * rankSize_ / rankSizeLevel0_;
     491              : 
     492            0 :     u64 dataCount0 = (static_cast<u64>((dataCount_ * dataSplitSize[0])) / rankSize_) * rankSize_;
     493            0 :     u64 dataCount1 = dataCount_ - dataCount0;
     494              : 
     495            0 :     u64 scratchSize0 = (static_cast<u64>(maxTmpMemSize_ * dataSplitSize[0]) / alignedSize / dataTypeSize_) * alignedSize
     496            0 :                        * dataTypeSize_;
     497            0 :     scratchSize0 = std::min(scratchSize0, UB_DATA_SIZE_LIMIT);
     498            0 :     u64 maxCountPerLoop0 = scratchSize0 / dataTypeSize_ / rankSize_ * rankSize_;
     499            0 :     maxCountPerLoop0 = std::min(dataCount0, maxCountPerLoop0);
     500              : 
     501            0 :     u64 scratchSize1 = ((maxTmpMemSize_ - scratchSize0) / alignedSize / dataTypeSize_) * alignedSize * dataTypeSize_;
     502            0 :     scratchSize1 = std::min(scratchSize1, UB_DATA_SIZE_LIMIT);
     503            0 :     u64 maxCountPerLoop1 = scratchSize1 / dataTypeSize_ / rankSize_ * rankSize_;
     504            0 :     maxCountPerLoop1 = std::min(dataCount1, maxCountPerLoop1);
     505              : 
     506            0 :     u32 loopTimes0 = 0;
     507            0 :     if (maxCountPerLoop0 != 0) {
     508            0 :         loopTimes0 = dataCount0 / maxCountPerLoop0 + ((dataCount0 % maxCountPerLoop0 == 0) ? 0 : 1);
     509              :     }
     510              : 
     511            0 :     u32 loopTimes1 = 0;
     512            0 :     if (maxCountPerLoop1 != 0) {
     513            0 :         loopTimes1 = dataCount1 / maxCountPerLoop1 + ((dataCount1 % maxCountPerLoop1 == 0) ? 0 : 1);
     514              :     }
     515            0 :     u32 loopTimes = std::max(loopTimes0, loopTimes1);
     516              : 
     517            0 :     u64 scratchOffset0 = 0;
     518            0 :     u64 scratchOffset1 = scratchSize0;
     519              : 
     520            0 :     TempFuncs tempFuncs;
     521            0 :     tempFuncs.opMode = opMode_;
     522            0 :     tempFuncs.enableCounterNotify = false;
     523              : 
     524            0 :     TemplateDataParams rsIntra0Params, rsInter1Params;
     525            0 :     TemplateDataParams rsInter0Params, rsIntra1Params;
     526            0 :     TemplateDataParams agIntra0Params, agInter1Params;
     527            0 :     TemplateDataParams agInter0Params, agIntra1Params;
     528              : 
     529            0 :     for (u32 loopIndex = 0; loopIndex < loopTimes; loopIndex++) {
     530            0 :         u64 currCount0 = 0;
     531            0 :         u64 dataOffset0 = 0;
     532            0 :         if (loopIndex < loopTimes) {
     533            0 :             currCount0 = (loopIndex == loopTimes - 1) ? (dataCount0 - loopIndex * maxCountPerLoop0) : maxCountPerLoop0;
     534            0 :             dataOffset0 = loopIndex * maxCountPerLoop0 * dataTypeSize_;
     535              :         }
     536              : 
     537            0 :         u64 currCount1 = 0;
     538            0 :         u64 dataOffset1 = 0;
     539            0 :         if (loopIndex < loopTimes) {
     540            0 :             currCount1 = (loopIndex == loopTimes - 1) ? (dataCount1 - loopIndex * maxCountPerLoop1) : maxCountPerLoop1;
     541            0 :             dataOffset1 = loopIndex * maxCountPerLoop1 * dataTypeSize_ + dataCount0 * dataTypeSize_;
     542              :         }
     543              : 
     544              :         // 计算统一sliceCount数量
     545            0 :         u64 totalSliceCount = currCount1 / rankSize_ * rankSizeLevel0_;
     546            0 :         sliceCount_ = (currCount1 >= rankSize_) ? totalSliceCount / rankSizeLevel0_ :
     547            0 :                                                   totalSliceCount / rankSize_ * rankSizeLevel1_;
     548              : 
     549              :         // ────────────── Phase 1: RS-1 ──────────────
     550              :         // 前半: 框内 Mesh RS,  后半: 框间 NHR RS
     551            0 :         CHK_RET(PreSyncQues(syncQueues_, 0));
     552              : 
     553            0 :         GenRSIntraParams0(dataOffset0, currCount0, scratchOffset0, rsIntra0Params);
     554            0 :         CHK_RET(tempAlgIntraRS.GenExtIns(tempFuncs, rsIntra0Params, intraRSLinks_, intraQue_));
     555              : 
     556            0 :         GenRSInterParams1(dataOffset1, currCount1, scratchOffset1, rsInter1Params);
     557            0 :         CHK_RET(tempAlgInterRS.GenExtIns(tempFuncs, rsInter1Params, interRSLinks_, interQue_));
     558              : 
     559            0 :         CHK_RET(PostSyncQues(syncQueues_, 0));
     560              : 
     561              :         // ────────────── Phase 2: RS-2 ──────────────
     562              :         // 前半: 框间 NHR RS,  后半: 框内 Mesh RS
     563            0 :         CHK_RET(PreSyncQues(syncQueues_, 0));
     564              : 
     565            0 :         GenRSInterParams0(dataOffset0, currCount0, scratchOffset0, rsInter0Params);
     566            0 :         CHK_RET(tempAlgInterRS.GenExtIns(tempFuncs, rsInter0Params, interRSLinks_, interQue_));
     567              : 
     568            0 :         GenRSIntraParams1(dataOffset1, currCount1, scratchOffset1, rsIntra1Params);
     569            0 :         CHK_RET(tempAlgIntraRS.GenExtIns(tempFuncs, rsIntra1Params, intraRSLinks_, intraQue_));
     570              : 
     571            0 :         CHK_RET(PostSyncQues(syncQueues_, 0));
     572              : 
     573              :         // ────────────── Phase 3: AG-1 ──────────────
     574              :         // 前半: 框间 NHR AG,  后半: 框内 Mesh AG
     575            0 :         CHK_RET(PreSyncQues(syncQueues_, 0));
     576              : 
     577            0 :         GenAGInterParams0(dataOffset0, currCount0, scratchOffset0, agInter0Params);
     578            0 :         CHK_RET(tempAlgInterAG.GenExtIns(tempFuncs, agInter0Params, interAGLinks_, interQue_));
     579              : 
     580            0 :         GenAGIntraParams1(dataOffset1, currCount1, scratchOffset1, agIntra1Params);
     581            0 :         CHK_RET(tempAlgIntraAG.GenExtIns(tempFuncs, agIntra1Params, intraAGLinks_, intraQue_));
     582              : 
     583            0 :         CHK_RET(PostSyncQues(syncQueues_, 0));
     584              : 
     585              :         // ────────────── Phase 4: AG-2 ──────────────
     586              :         // 前半: 框内 Mesh AG,  后半: 框间 NHR AG
     587            0 :         CHK_RET(PreSyncQues(syncQueues_, 0));
     588              : 
     589            0 :         GenAGIntraParams0(dataOffset0, currCount0, scratchOffset0, agIntra0Params);
     590            0 :         CHK_RET(tempAlgIntraAG.GenExtIns(tempFuncs, agIntra0Params, intraAGLinks_, intraQue_));
     591              : 
     592            0 :         GenAGInterParams1(dataOffset1, currCount1, scratchOffset1, agInter1Params);
     593            0 :         CHK_RET(tempAlgInterAG.GenExtIns(tempFuncs, agInter1Params, interAGLinks_, interQue_));
     594              : 
     595            0 :         CHK_RET(PostSyncQues(syncQueues_, 0));
     596              :     }
     597              : 
     598            0 :     return HcclResult::HCCL_SUCCESS;
     599            0 : }
     600              : 
     601              : // 算法注册
     602              : INS_REGISTER_IMPL_BY_FOUR_TEMPS(
     603              :     OpType::ALLREDUCE, InsAllReduceFourTemplateMesh1DNHR, InsAllReduceParallelExecutorV2, TopoMatchMeshNHR,
     604              :     InsTempReduceScatterMesh1D, InsTempReduceScatterMesh1D, InsTempAllGatherMesh1D, InsTempAllGatherMesh1D);
     605              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1