LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/alg_template/temp_reduce - reduce_recursive_hd.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 3.1 % 257 8
Test Date: 2026-08-04 10:52:23 Functions: 36.4 % 11 4

            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 "alg_template_register.h"
      12              : #include "reduce_recursive_hd.h"
      13              : 
      14              : namespace hccl {
      15            1 : ReduceRecursiveHalvingDoubling::ReduceRecursiveHalvingDoubling(const HcclDispatcher dispatcher)
      16            1 :     : RecursiveHalvingDoublingBase(dispatcher)
      17              : {
      18            1 : }
      19              : 
      20            2 : ReduceRecursiveHalvingDoubling::~ReduceRecursiveHalvingDoubling()
      21              : {
      22            2 : }
      23              : 
      24            1 : HcclResult ReduceRecursiveHalvingDoubling::Prepare(u64 reduceAttrBitMap, HcomCollOpInfo *opInfo)
      25              : {
      26            1 :     reduceAttr = reduceAttrBitMap;
      27            1 :     return HCCL_SUCCESS;
      28              : }
      29              : 
      30              : // 算法的主入口
      31            0 : HcclResult ReduceRecursiveHalvingDoubling::RunAsync(const u32 rank, const u32 rankSize,
      32              :                                                     const std::vector<std::shared_ptr<Transport> > &links)
      33              : {
      34            0 :     CHK_SMART_PTR_NULL(dispatcher_);
      35            0 :     CHK_PTR_NULL(stream_.ptr());
      36            0 :     if (!outputMem_ || !inputMem_) {
      37            0 :         HCCL_ERROR("[ReduceRecursiveHalvingDoubling][RunAsync]rank[%u] run_async inputmem or outputmem is null",
      38              :             rank);
      39            0 :         return HCCL_E_PTR;
      40              :     }
      41            0 :     HCCL_INFO("ReduceRecursiveHalvingDoubling run: rank[%u] root[%u] totalrank[%u] inputMem[%p] outputMem[%p]" \
      42              :         "count[%llu]", rank, root_, rankSize, inputMem_.ptr(), outputMem_.ptr(), count_);
      43              : 
      44            0 :     HcclResult ret = HCCL_SUCCESS;
      45              : 
      46            0 :     if (rankSize == 1) {
      47            0 :         if (inputMem_ != outputMem_) {
      48            0 :             ret = HcclD2DMemcpyAsync(dispatcher_, outputMem_, inputMem_, stream_);
      49              :         }
      50            0 :         return ret;
      51              :     }
      52              : 
      53            0 :     senderInfo_.reset(new (std::nothrow) Sender(dataType_, reductionOp_, reduceAttr));
      54            0 :     CHK_SMART_PTR_NULL(senderInfo_);
      55              : 
      56            0 :     reducerInfo_.reset(new (std::nothrow) Reducer(dataType_, reductionOp_, reduceAttr));
      57            0 :     CHK_SMART_PTR_NULL(reducerInfo_);
      58              : 
      59            0 :     bool bRetSize = (links.size() < rankSize);
      60            0 :     CHK_PRT_RET(bRetSize,
      61              :         HCCL_ERROR("[ReduceRecursiveHalvingDoubling][RunAsync]rank[%u] linksize[%llu] is error",
      62              :         rank, links.size()), HCCL_E_INTERNAL);
      63              : 
      64            0 :     CHK_RET(CalcPartOneSizeAndBlockSize(rankSize));
      65              : 
      66            0 :     u32 bytesPerData = DataUnitSize(dataType_);
      67            0 :     u64 dataBytes = count_ * bytesPerData;
      68            0 :     CHK_RET(CalculateSlices(dataBytes));
      69              : 
      70              :     // 结果完成需要放在input
      71            0 :     CHK_RET(ReduceInPartOne(rank, links));
      72              : 
      73              :     // 此步骤完成后,结果放在ouput中
      74            0 :     CHK_RET(ReduceScatterInBlock(rank, rankSize, links));
      75              : 
      76              :     // 使用output进行gather
      77            0 :     CHK_RET(GatherInBlock(rank, rankSize, links));
      78              : 
      79            0 :     HCCL_INFO("ReduceRecursiveHalvingDoubling rank[%u] finished", rank);
      80            0 :     return HCCL_SUCCESS;
      81              : }
      82              : 
      83            0 : HcclResult ReduceRecursiveHalvingDoubling::ReduceInPartOne(u32 rank, const std::vector<LINK> &links)
      84              : {
      85            0 :     HCCL_INFO("rank[%u] part1Size_[%u] root[%u]", rank, part1Size_, root_);
      86              : 
      87            0 :     if (rank >= part1Size_) { // rank在第二部分,不参与ReduceInPartOne
      88            0 :         HCCL_INFO("rank[%u] not in part1, don't need reduce", rank);
      89            0 :         return HCCL_SUCCESS;
      90              :     }
      91              :     // root在第二部分,需要选取第一部分偶数rank接收,以0作为判断标准,否则在第一部分,与root奇偶性相同rank接收
      92            0 :     u32 rootFlag = (root_ >= part1Size_) ? 0 : root_;
      93              : 
      94            0 :     if (rank % 2 == rootFlag % 2) {  // 1.从下一个rank接收数据到output,2. reduce到本rank的input
      95            0 :         u32 peerRank = (rank % 2) == 0 ? (rank + 1) : (rank - 1);
      96            0 :         HCCL_INFO("rank[%u] outputMem receives from PeerRank[%u] inputMem, Offset[%llu], Size[%llu]", \
      97              :                   rank, peerRank, baseOffset_, outputMem_.size());
      98              : 
      99            0 :         if (peerRank < links.size()) {
     100            0 :             CHK_SMART_PTR_NULL(links[peerRank]);
     101              : 
     102            0 :             HcclResult ret = links[peerRank]->TxAck(stream_);
     103            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     104              :                 HCCL_ERROR("[Reduce][InPartOne]tx ack to peerrank[%u] failed", peerRank), ret);
     105            0 :             ret = links[peerRank]->RxAck(stream_);
     106            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     107              :                 HCCL_ERROR("[Reduce][InPartOne]rx ack from peerank[%u] failed", peerRank), ret);
     108              : 
     109              :             //  接收数据到本端的 output
     110            0 :             HCCL_DEBUG("send mem[%p] size[%llu] to peerank[%u]", outputMem_.ptr(), outputMem_.size(), peerRank);
     111            0 :             ret = links[peerRank]->TxAsync(UserMemType::INPUT_MEM, baseOffset_, outputMem_.ptr(), 0, stream_);
     112            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][InPartOneToEven]TxAsync: tx async size[%llu] "\
     113              :                 "failed", 0), ret);
     114            0 :             CHK_RET(reducerInfo_->run(dispatcher_, links[peerRank], baseOffset_,
     115              :                 outputMem_, inputMem_, outputMem_, stream_));
     116            0 :             ret = links[peerRank]->RxWaitDone(stream_);
     117            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][InPartOne]RxWaitDone failed"), ret);
     118              :         }
     119            0 :     } else if ((rank % 2) != (rootFlag % 2)) { //  向上一个rank的output发数据 2
     120            0 :         u32 peerRank = (rank % 2 == 0) ? (rank + 1) : (rank -1);
     121              : 
     122            0 :         if (peerRank < links.size()) {
     123            0 :             CHK_SMART_PTR_NULL(links[peerRank]);
     124            0 :             HcclResult ret = links[peerRank]->TxAck(stream_);
     125            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     126              :                 HCCL_ERROR("[Reduce][InPartOne]tx ack to peerrank[%u] failed", peerRank), ret);
     127            0 :             ret = links[peerRank]->RxAck(stream_);
     128            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     129              :                 HCCL_ERROR("[Reduce][InPartOne]rx ack from peerank[%u] failed", peerRank), ret);
     130              :             //  发送到对端的output
     131            0 :             HCCL_DEBUG("rank[%u] sends inputMem[%p] to PeerRank[%u] Offset[%llu], Size[%llu]", \
     132              :                 rank, inputMem_.ptr(), peerRank, baseOffset_, inputMem_.size());
     133            0 :             ret = senderInfo_->run(links[peerRank], baseOffset_, inputMem_, stream_);
     134            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     135              :                 HCCL_ERROR("[Reduce][InPartOne]tx sync to peerank[%u] failed", peerRank), ret);
     136            0 :             ret = links[peerRank]->RxAsync(UserMemType::OUTPUT_MEM, baseOffset_, inputMem_.ptr(), 0, stream_);
     137            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     138              :                 HCCL_ERROR("[AlgTemplateBase][ExecuteTxSync]ExecuteTxSync: rx async size[%llu] failed", 0), ret);
     139            0 :             ret = links[peerRank]->DataReceivedAck(stream_);
     140            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     141              :                 HCCL_ERROR("[AlgTemplateBase][ExecuteTxSync]ExecuteTxSync: data received ack failed"), ret);
     142            0 :             ret = links[peerRank]->TxWaitDone(stream_);
     143            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][InPartOne]TxWaitDone failed"), ret);
     144              :         }
     145              :     }
     146            0 :     return HCCL_SUCCESS;
     147              : }
     148              : 
     149              : 
     150            0 : HcclResult ReduceRecursiveHalvingDoubling::ReduceScatterInBlock(u32 rank, u32 rankSize,
     151              :     const std::vector<LINK> &links)
     152              : {
     153            0 :     u32 rankInBlock = 0;
     154              : 
     155            0 :     u32 rootFlag = (root_ >= part1Size_) ? 0 : root_;
     156            0 :     HCCL_DEBUG("[ReduceRecursiveHalvingDoubling][ReduceScatterInBlock]rootFlag is %u, rankInBlock is %u", rootFlag, rankInBlock);
     157              :     // 需要根据root判断,让root节点必然参加reducescatter,在第一部分的rank若与root奇偶性不同,直接返回
     158            0 :     if (rank < part1Size_ && (rank % 2) != (rootFlag % 2)) {     // 模2判断奇偶性,本rank处于第一部分,奇偶性与root不同
     159            0 :         return HCCL_SUCCESS;
     160            0 :     } else if (rank < part1Size_) {     // 模2判断奇偶性,本rank 处于第一部分,奇偶性与root相同
     161            0 :         rankInBlock = rank / 2;                            // 除2计算block内的rank值
     162              :     } else {           // 本rank不属于第一部分
     163            0 :         rankInBlock = rank - part1Size_ / 2;               // 除2计算block内的part1的范围
     164              :     }
     165              :     // 直接调用block的reducscatterhd算法
     166            0 :     std::unique_ptr<AlgTemplateBase> executor = AlgTemplateRegistry::Instance().GetAlgTemplate(
     167            0 :         TemplateType::TEMPLATE_REDUCESCATTER_HD, dispatcher_);
     168            0 :     CHK_SMART_PTR_NULL(executor);
     169            0 :     CHK_RET(executor->Prepare(inputMem_, outputMem_, outputMem_, count_, dataType_, stream_,
     170              :         reductionOp_, -1, slices_, baseOffset_, blockSize_, reduceAttr,
     171              :         UserMemType::INPUT_MEM, UserMemType::OUTPUT_MEM));
     172              : 
     173            0 :     CHK_RET(executor->RegisterProfiler(profilerInput_.planeID, profilerInput_.stage, profilerInput_.step,
     174              :         stream_));
     175              : 
     176              :     // 重新建立reducscatterscatter需要的链接
     177            0 :     std::vector<LINK> subLinks;
     178            0 :     CHK_RET(BuildRootSubLinks(links, subLinks, rankSize));
     179              : 
     180            0 :     CHK_PRT_RET(subLinks.size() == 0, HCCL_ERROR("[ReduceRecursiveHalvingDoubling][ReduceScatterInBlock]rank[%u] "\
     181              :         "BuildSubLinks failed", rank), HCCL_E_PARA);
     182              : 
     183            0 :     CHK_RET(executor->RunAsync(rankInBlock, blockSize_, subLinks));
     184              : 
     185            0 :     return HCCL_SUCCESS;
     186            0 : }
     187              : 
     188            0 : HcclResult ReduceRecursiveHalvingDoubling::CalculateStepSlices(const std::vector<Slice> &inputSlices, u32 stepNum,
     189              :                                                                u32 rank, SliceType type, std::vector<Slice> &sliceOut)
     190              : {
     191            0 :     std::vector<Slice> slice(stepNum);
     192              : 
     193            0 :     for (u32 step = 0; step < stepNum; step++) {
     194              :         // all-gather操作, halving_bitmask从低往高循环, size倍增
     195            0 :         u32 halvingBitmask = (1 << step);
     196            0 :         u32 peerRank = rank ^ halvingBitmask;
     197              : 
     198              :         // 计算tx_slice/rx_slice
     199            0 :         u32 sliceId = (type == SliceType::SLICE_TYPE_RX) ? \
     200            0 :             (peerRank & (~(halvingBitmask - 1))) : (rank & (~(halvingBitmask - 1)));
     201              : 
     202            0 :         slice[step].offset = inputSlices[sliceId].offset;
     203            0 :         CHK_RET(Sum(inputSlices, sliceId, halvingBitmask, slice[step].size));
     204              : 
     205            0 :         HCCL_DEBUG("Slice Info: rank[%u], slices[%u].offset=%llu, slices[%u].size=%llu", \
     206              :                    rank, step, slice[step].offset, step, slice[step].size);
     207              :     }
     208              : 
     209            0 :     sliceOut = std::move(slice);
     210            0 :     return HCCL_SUCCESS;
     211            0 : }
     212            0 : HcclResult ReduceRecursiveHalvingDoubling::BuildRootSubLinks(const std::vector<LINK> &links,
     213              :                                                              std::vector<LINK> &subLinks, u32 rankSize) const
     214              : {
     215            0 :     std::vector<LINK>::const_iterator iter = links.begin();
     216            0 :     subLinks.resize(blockSize_);
     217            0 :     u32 rootFlag = (root_ >= part1Size_) ? 0 : root_;
     218            0 :     for (u32 i = 0; i < rankSize; i++) {
     219            0 :         if (i < part1Size_ && (i % 2) != rootFlag % 2) {  // 模2与root模2比较代表当前rank在part1的内且与root奇偶性不同,不参与block内的建链
     220            0 :             continue;
     221            0 :         } else if (i < part1Size_) {
     222            0 :             std::vector<LINK>::const_iterator niter = std::next(iter, i);
     223            0 :             if (niter != links.end()) {
     224            0 :                 subLinks[i / 2] = *niter;              // 除2计算出在block内的rank号
     225              :             }
     226              :         } else {
     227            0 :             std::vector<LINK>::const_iterator niter = std::next(iter, i);
     228            0 :             if (niter != links.end()) {
     229            0 :                 subLinks[i - part1Size_ / 2] = *niter; // rank在part2中,用原始rank减part1除2,计算出在block内的rank号
     230              :             }
     231              :         }
     232              :     }
     233              : 
     234            0 :     return HCCL_SUCCESS;
     235              : }
     236              : // 结果在output中,直接使用oupt进行数据收发
     237            0 : HcclResult ReduceRecursiveHalvingDoubling::GatherInBlock(u32 rank, u32 rankSize,
     238              :                                                          const std::vector<LINK> &links)
     239              : {
     240            0 :     u32 rankInBlock = 0;
     241              : 
     242            0 :     u32 rootFlag = (root_ >= part1Size_) ? 0 : root_;
     243            0 :     if (rank < part1Size_ && (rank % 2) != (rootFlag % 2)) {    // 模2判断奇偶性,本rank 处于第一部分,并且和root rank奇偶不同
     244            0 :         return HCCL_SUCCESS;
     245            0 :     } else if (rank < part1Size_) { // 模2判断奇偶性,本rank 处于第一部分,并且奇偶性和root相同
     246            0 :         rankInBlock = rank / 2;                        // 在block内的rank为实际rank除以2
     247              :     } else {
     248            0 :         rankInBlock = rank - part1Size_ / 2;           // 除2计算block内的part1的范围
     249              :     }
     250            0 :     u32 rootInBlock = (root_ > part1Size_) ? (root_ - part1Size_ / 2) : (root_ / 2);
     251              :     // 重新建立gather需要的链接
     252            0 :     std::vector<LINK> subLinks;
     253              : 
     254            0 :     CHK_RET(BuildRootSubLinks(links, subLinks, rankSize));
     255              : 
     256            0 :     CHK_PRT_RET(subLinks.size() == 0,
     257              :         HCCL_ERROR("[Gather][InBlock]rank[%u] build sub links failed", rank), HCCL_E_PARA);
     258              : 
     259            0 :     CHK_RET(CalculateStepSlices(slices_, round_, rankInBlock, SliceType::SLICE_TYPE_TX, txSlices_));
     260              : 
     261            0 :     CHK_RET(CalculateStepSlices(slices_, round_, rankInBlock, SliceType::SLICE_TYPE_RX, rxSlices_));
     262              : 
     263            0 :     for (u32 step = 0; step < round_; step++) {
     264            0 :         u32 peerRankBitmask = (1 << step);
     265            0 :         u32 opBitmask = peerRankBitmask - 1 ; // 判断本轮是否进行收发
     266              :         // 断rank是否和root在同一轮次接收发送的block内,第一轮为total,第二轮为1/2,第三轮为1/4....
     267            0 :         if ((step != 0) && ((rankInBlock & opBitmask) != (rootInBlock & opBitmask))) {
     268            0 :             return HCCL_SUCCESS; // rank在本轮同root不在一个操作块内,不操作,直接返回
     269              :         }
     270            0 :         u32 peerRank = rankInBlock ^ peerRankBitmask;
     271            0 :         CHK_SMART_PTR_NULL(subLinks[peerRank]);
     272              :         // 再次判断是否和root在同一1/2,1/4,用来判断数据是收还是发
     273            0 :         if ((rankInBlock & peerRankBitmask) == (rootInBlock & peerRankBitmask)) {
     274            0 :             DeviceMem rxMem = outputMem_.range(rxSlices_[step].offset, rxSlices_[step].size);
     275            0 :                 HcclResult ret = subLinks[peerRank]->TxAck(stream_);
     276            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Gather][InBlock]rank[%u] tx ack from peerank[%u] failed",
     277              :                     rank, peerRank), ret);
     278            0 :                 ret = subLinks[peerRank]->RxAck(stream_);
     279            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Gather][InBlock]rank[%u] rx ack from peerank[%u] failed",
     280              :                     rank, peerRank), ret);
     281              : 
     282              :                 // 等待对端可以接收数据
     283            0 :                 HCCL_DEBUG("rank[%u] outputMem[%p] receive from PeerRank[%u] outputMem, Offset[%llu], "\
     284              :                            "Size[%llu]", rank, outputMem_.ptr(), peerRank,
     285              :                            baseOffset_ + rxSlices_[step].offset, rxSlices_[step].size);
     286              : 
     287            0 :                 ret = ExecuteRxSync(subLinks[peerRank], UserMemType::OUTPUT_MEM, baseOffset_ + rxSlices_[step].offset,
     288            0 :                     rxMem.ptr(), rxSlices_[step].size, stream_);
     289            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS,
     290              :                     HCCL_ERROR("[Gather][InBlock]rank[%u] rx sync from PeerRank[%u] failed", rank, peerRank), ret);
     291            0 :                 ret = subLinks[peerRank]->RxWaitDone(stream_);
     292            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][InPartOne]RxWaitDone failed"), ret);
     293            0 :         } else {
     294            0 :             DeviceMem txMem = outputMem_.range(txSlices_[step].offset, txSlices_[step].size);
     295            0 :                 HcclResult ret = subLinks[peerRank]->TxAck(stream_);
     296            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Gather][InBlock]rank[%u] tx ack from peerank[%u] failed",
     297              :                     rank, peerRank), ret);
     298            0 :                 ret = subLinks[peerRank]->RxAck(stream_);
     299            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Gather][InBlock]rank[%u] rx ack from peerank[%u] failed",
     300              :                     rank, peerRank), ret);
     301            0 :                 HCCL_DEBUG("rank[%u] outputMem[%p] sends to peerrank[%u] outputmem, offset[%llu], " \
     302              :                            "size[%llu]", rank, outputMem_.ptr(), peerRank,
     303              :                            baseOffset_ + txSlices_[step].offset, txSlices_[step].size);
     304            0 :                 ret = ExecuteTxSync(subLinks[peerRank], UserMemType::OUTPUT_MEM, baseOffset_ + txSlices_[step].offset,
     305            0 :                     txMem.ptr(), txSlices_[step].size, stream_);
     306            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Gather][InBlock]rank[%u] tx sync to PeerRank[%u] failed",
     307              :                     rank, peerRank), ret);
     308            0 :                 ret = subLinks[peerRank]->TxWaitDone(stream_);
     309            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][InPartOne]TxWaitDone failed"), ret);
     310            0 :         }
     311              :     }
     312              : 
     313            0 :     return HCCL_SUCCESS;
     314            0 : }
     315            0 : HcclResult ReduceRecursiveHalvingDoubling::GetNslbAdjInfo(const u32 rank, const u32 rankSize,
     316              :                                                           const std::vector<LINK> &links,
     317              :                                                           AdjInfo& nslbAdjInfo)
     318              : {
     319            0 :     u32 nslbRound = 0;
     320            0 :     u32 base = 1;
     321            0 :     const u32 minExponent = 1;
     322            0 :     while ((base << nslbRound) <= rankSize) {
     323            0 :         nslbRound++;
     324              :     }
     325            0 :     if (nslbRound >= minExponent) {
     326            0 :         nslbRound = nslbRound - minExponent;
     327              :     }
     328            0 :     u32 nslbBlockSize = base << nslbRound;
     329              :     // 获取第一部分:rank数减block数乘2
     330            0 :     u32 nslbPart1Size = (rankSize - nslbBlockSize) * NSLBDP_REDUCE_MOLD2;
     331              :     // 2的次幂场景下处理流程
     332            0 :     if (nslbPart1Size == 0) {
     333            0 :         u32 stepNum = 0;
     334            0 :         while ((rankSize >> (stepNum + 1)) != 0) {
     335            0 :             stepNum++;
     336              :         }
     337            0 :         HCCL_DEBUG("[ReduceRecursiveHalvingDoubling]GetNslbAdjInfo start");
     338            0 :         for (u32 step = 0; step < stepNum; step++) {
     339            0 :             u32 peerRankBitmask = 1 << (stepNum - step - 1);
     340            0 :             u32 peerRank = rank ^ peerRankBitmask;
     341            0 :             NslbDpAdjInfo adjInfoStep = {0};
     342            0 :             u32 remoteuserRank = links[peerRank]->GetRemoteRank();
     343            0 :             HCCL_DEBUG("[ReduceRecursiveHalvingDoubling]now step %u, remoteuserRank is %u", step, remoteuserRank);
     344            0 :             adjInfoStep.dstLocalRankId = remoteuserRank;
     345            0 :             adjInfoStep.phaseId = step + 1;
     346            0 :             adjInfoStep.rev = 0;
     347            0 :             nslbAdjInfo.nsAdjInfo.push_back(adjInfoStep);
     348              :         }
     349            0 :         nslbAdjInfo.dstRankNum = stepNum;
     350            0 :         return HCCL_SUCCESS;
     351              :     }
     352              :     // 非2的次幂场景下,被合并部分的奇数rank处理流程
     353            0 :     if (rank < nslbPart1Size && rank % NSLBDP_REDUCE_MOLD2 == 1) {
     354            0 :         u32 peerRank = rank - 1;
     355            0 :         if (peerRank < links.size()) {
     356            0 :             NslbDpAdjInfo adjInfoStep = {0};
     357            0 :             adjInfoStep.dstLocalRankId = links[peerRank]->GetRemoteRank();
     358            0 :             adjInfoStep.phaseId = 1;
     359            0 :             adjInfoStep.rev = 0;
     360            0 :             HCCL_INFO("AllGatherHDR-nslb: peerRank[%u]", peerRank);
     361            0 :             nslbAdjInfo.nsAdjInfo.push_back(adjInfoStep);
     362            0 :             nslbAdjInfo.dstRankNum = 1;
     363              :         }
     364            0 :         return HCCL_SUCCESS;
     365              :     }
     366              :     // 针对合并后映射成2的次幂场景处理
     367            0 :     u32 rankInBlock = 0;
     368            0 :     if (rank < nslbPart1Size && (rank % NSLBDP_REDUCE_MOLD2) == 0) {
     369            0 :         rankInBlock = rank / NSLBDP_REDUCE_MOLD2; // 直接除以2即为本rank的在block内的排序
     370              :     } else {
     371            0 :         rankInBlock = rank - nslbPart1Size / NSLBDP_REDUCE_MOLD2; // 通过rank减去part1除2的大小即不处于第一部分的block内rank号
     372              :     }
     373            0 :     std::vector<LINK> subLinks;
     374            0 :     std::vector<LINK>::const_iterator iter = links.begin();
     375            0 :     subLinks.resize(nslbBlockSize);
     376            0 :     for (u32 i = 0; i < rankSize; i++) {
     377            0 :         if (i < nslbPart1Size && (i % NSLBDP_REDUCE_MOLD2) == 1) {   // 模2余1代表当前rank在part1的奇数位置上,不参与block内的建链
     378            0 :             continue;
     379            0 :         } else if (i < nslbPart1Size && (i % NSLBDP_REDUCE_MOLD2) == 0) {  // 模2余0代表当前rank在part1的偶数位置上
     380            0 :             std::vector<LINK>::const_iterator niter = std::next(iter, i);
     381            0 :             if (niter != links.end()) {
     382            0 :                 subLinks[i / NSLBDP_REDUCE_MOLD2] = *niter;
     383              :             }
     384            0 :         } else {
     385            0 :             std::vector<LINK>::const_iterator niter = std::next(iter, i);
     386            0 :             if (niter != links.end()) {
     387            0 :                 subLinks[i - nslbPart1Size / NSLBDP_REDUCE_MOLD2] = *niter; 
     388              :             }
     389              :         }
     390              :     }
     391            0 :     u32 stepNum = 0;
     392            0 :     while ((rankSize >> (stepNum + 1)) != 0) {
     393            0 :         stepNum++;
     394              :     }
     395              :     // 映射完成后针对以新的通信域进行邻接表获取
     396            0 :     for (u32 step = 0; step < stepNum; step++) {
     397            0 :         u32 peerRankBitmask = 1 << (stepNum - step - 1);
     398            0 :         u32 peerRank = rankInBlock ^ peerRankBitmask;
     399            0 :         if (subLinks[peerRank] == nullptr) {
     400            0 :             continue;
     401              :         }
     402            0 :         NslbDpAdjInfo adjInfoStep = {0};
     403            0 :         u32 remoteuserRank = subLinks[peerRank]->GetRemoteRank();
     404            0 :         adjInfoStep.dstLocalRankId = remoteuserRank;
     405            0 :         adjInfoStep.phaseId = step + 1;
     406            0 :         adjInfoStep.rev = 0;
     407            0 :         nslbAdjInfo.nsAdjInfo.push_back(adjInfoStep);
     408              :     }
     409            0 :     nslbAdjInfo.dstRankNum = stepNum;
     410              : 
     411            0 :     if(nslbAdjInfo.nsAdjInfo.size() == 0) {
     412            0 :         return HCCL_SUCCESS;
     413              :     }
     414              :     // 上面处理完成后,紧接着处理合并部分的偶数rank同步到奇数rank增加phaseId
     415            0 :     if (rank < nslbPart1Size && rank % NSLBDP_REDUCE_MOLD2 == 0) {
     416            0 :         u32 peerRank = rank + 1;
     417            0 :         uint16_t phaseSize = nslbAdjInfo.nsAdjInfo.size();
     418            0 :         if (peerRank < links.size()) {
     419            0 :                 NslbDpAdjInfo adjInfoStep = {0};
     420            0 :                 adjInfoStep.dstLocalRankId = links[peerRank]->GetRemoteRank();
     421            0 :                 adjInfoStep.phaseId = nslbAdjInfo.nsAdjInfo[phaseSize - 1].phaseId + 1;
     422            0 :                 adjInfoStep.rev = 0;
     423            0 :                 HCCL_INFO("Scatter-nslb: peerRank[%u]", peerRank);
     424            0 :                 nslbAdjInfo.nsAdjInfo.push_back(adjInfoStep);
     425            0 :                 nslbAdjInfo.dstRankNum = nslbAdjInfo.nsAdjInfo.size();
     426              :         }
     427            0 :         return HCCL_SUCCESS;
     428              :     }
     429            0 :     return HCCL_SUCCESS;
     430            0 : }
     431              : REGISTER_TEMPLATE(TemplateType::TEMPLATE_REDUCE_RECURSIVE_HALVING_DOUBLING, ReduceRecursiveHalvingDoubling);
     432              : }  // namespace hccl
        

Generated by: LCOV version 2.0-1