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

Generated by: LCOV version 2.0-1