LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/alg_template/temp_all_gather - all_gather_ring_direct.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 164 0
Test Date: 2026-08-04 10:52:23 Functions: 0.0 % 13 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 "all_gather_ring_direct.h"
      12              : #include "alg_template_register.h"
      13              : 
      14              : namespace hccl {
      15            0 : AllGatherRingDirect::AllGatherRingDirect(const HcclDispatcher dispatcher)
      16            0 :     : AlgTemplateBase(dispatcher)
      17              : {
      18            0 : }
      19              : 
      20            0 : AllGatherRingDirect::~AllGatherRingDirect()
      21              : {
      22            0 : }
      23              : 
      24            0 : HcclResult AllGatherRingDirect::Prepare(HcomCollOpInfo *opInfo, u32 userRank,
      25              :     const std::vector<Slice> &userMemOutputSlices, bool isSdma)
      26              : {
      27            0 :     opInfo_ = opInfo;
      28            0 :     userRank_ = userRank;
      29            0 :     userMemOutputSlices_ = userMemOutputSlices;
      30            0 :     isSdma_ = isSdma;
      31            0 :     return HCCL_SUCCESS;
      32              : }
      33              : 
      34              : // allgather的入口函数
      35            0 : HcclResult AllGatherRingDirect::RunAsync(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
      36              : {
      37              :     // 基本的检查
      38            0 :     CHK_RET(CheckParameters(rank, rankSize, links));
      39              : 
      40            0 :     if (rankSize == 1) {
      41            0 :         CHK_RET(OneRankMemcpy());
      42            0 :         return HCCL_SUCCESS;
      43              :     }
      44              :     // 收集邻居信息
      45            0 :     CHK_RET(GetInitializedNeighborLinks(rank, rankSize, links));
      46              : 
      47              :     // 填充slice_
      48            0 :     CHK_RET(SetSlices(rank, rankSize));
      49              : 
      50              :     // 运行all-gather, ring算法
      51            0 :     CHK_RET(RunAllGather(rank, rankSize));
      52              : 
      53            0 :     if (barrierSwitchOn_) {
      54              :         // 执行barrier,保证数据收发完成
      55            0 :         CHK_RET(ExecuteBarrier(leftLink_, rightLink_));
      56              :     }
      57              : 
      58            0 :     HCCL_INFO("AllGatherRingDirect finished: rank[%u] end", rank);
      59            0 :     return HCCL_SUCCESS;
      60              : }
      61              : 
      62            0 : HcclResult AllGatherRingDirect::CheckParameters(const u32 rank, const u32 rankSize,
      63              :                                                           const std::vector<LINK> &links)
      64              : {
      65            0 :     CHK_PTR_NULL(opInfo_);
      66            0 :     CHK_RET(CheckConcurrentDirectParameters(rank, rankSize, links));
      67              :     // 判断userMemInputSlices数量是否正确
      68            0 :     CHK_PRT_RET(userMemOutputSlices_.size() % rankSize != 0,
      69              :         HCCL_ERROR("[AllGatherRingDirect] userMemOutputSlices size[%u] can not be divided by rank size[%u]",
      70              :             userMemOutputSlices_.size(), rankSize), HCCL_E_PARA);
      71              : 
      72            0 :     HCCL_INFO("AllGatherRingDirect finished to CheckParameters");
      73            0 :     return HCCL_SUCCESS;
      74              : }
      75              : 
      76              : // 单卡场景
      77            0 : HcclResult AllGatherRingDirect::OneRankMemcpy()
      78              : {
      79            0 :     for (u32 sliceIdx = 0; sliceIdx < slices_.size(); sliceIdx++) {
      80            0 :         const Slice &srcSlice = slices_[sliceIdx];
      81            0 :         const Slice &dstSlice = userMemOutputSlices_[sliceIdx];
      82            0 :         DeviceMem    src;
      83            0 :         DeviceMem    dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + dstSlice.offset, dstSlice.size);
      84            0 :         if (opInfo_->inputAddr != nullptr) {
      85              :             // opInfo_->inputAddr != nullptr指示要从user input获取输入
      86            0 :             u64 stepOffset = slices_[0].offset;
      87            0 :             HCCL_DEBUG("Memcpy operation: stream[main], rank[%u] starts to copy offset[%llu], size[%llu] at userInput",
      88              :                 userRank_, stepOffset, srcSlice.size);
      89            0 :             src = DeviceMem::create(static_cast<u8 *>(opInfo_->inputAddr) + stepOffset, srcSlice.size);
      90              :         } else {
      91              :             // opInfo_->inputAddr == nullptr指示要从CCL buffer获取输入
      92            0 :             HCCL_DEBUG("Memcpy operation: stream[main], rank[%u] starts to copy offset[%llu], size[%llu] at inputMem_",
      93              :                 userRank_, srcSlice.offset, srcSlice.size);
      94            0 :             src = inputMem_.range(srcSlice.offset, srcSlice.size);
      95              :         }
      96            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
      97            0 :     }
      98              : 
      99            0 :     return HCCL_SUCCESS;
     100              : }
     101              : 
     102            0 : HcclResult AllGatherRingDirect::GetInitializedNeighborLinks(const u32 rank, const u32 rankSize,
     103              :                                                                       const std::vector<LINK> &links)
     104              : {
     105              :     // 收集左邻居信息
     106            0 :     leftLink_ = links[(rank + rankSize - 1) % rankSize];
     107            0 :     CHK_SMART_PTR_NULL(leftLink_);
     108              : 
     109              :     // 收集右邻居信息
     110            0 :     rightLink_ = links[(rank + 1) % rankSize];
     111            0 :     CHK_SMART_PTR_NULL(rightLink_);
     112              : 
     113            0 :     HCCL_INFO("AllGatherRingDirect finished to GetInitializedNeighborLinks");
     114            0 :     return HCCL_SUCCESS;
     115              : }
     116              : 
     117            0 : HcclResult AllGatherRingDirect::SetSlices(const u32 rank, const u32 rankSize)
     118              : {
     119            0 :     inputSlices_ = slices_;
     120            0 :     if (slices_.size() == 0) {
     121            0 :         slices_.resize(rankSize);
     122            0 :         inputSlices_.resize(rankSize);
     123              : 
     124            0 :         u64 sliceSize = count_ * DataUnitSize(dataType_);
     125            0 :         for (u32 i = 0; i < rankSize; i++) {
     126            0 :             slices_[i].size        = sliceSize;
     127            0 :             slices_[i].offset      = sliceSize * i;
     128            0 :             inputSlices_[i].size   = sliceSize;
     129            0 :             inputSlices_[i].offset = (inputMem_.size() < outputMem_.size()) ? 0 : (sliceSize * i);
     130            0 :             HCCL_DEBUG("rank[%u], slices[%u].offset=%llu, slices[%u].size=[%llu]", rank, i, slices_[i].offset, i,
     131              :                        slices_[i].size);
     132              :         }
     133              :     }
     134              : 
     135            0 :     if (UNLIKELY(HcclCheckLogLevel(DLOG_DEBUG))) {
     136            0 :         for (u32 i = 0; i < slices_.size(); i++) {
     137            0 :             HCCL_DEBUG(
     138              :                 "[AllGatherRingDirect][SetSlices]rank[%u], slices[%u].offset=[%llu], slices[%u].size=[%llu]",
     139              :                 rank, i, slices_[i].offset, i, slices_[i].size);
     140              :         }
     141              :     }
     142              : 
     143            0 :     HCCL_INFO("AllGatherRingDirect finished to SetSlices");
     144            0 :     return HCCL_SUCCESS;
     145              : }
     146              : 
     147            0 : HcclResult AllGatherRingDirect::RunInitStep(const u32 rank, const u32 rankSize)
     148              : {
     149              :     // 第一步搬到userMemIn_的offset
     150            0 :     auto firstStepOffset = slices_[0].offset;
     151              : 
     152              :     // 第-1步,片内将部分数据从userIn搬到cclIn
     153            0 :     DeviceMem srcInit;
     154            0 :     DeviceMem dstInit;
     155            0 :     u32 initSliceIdx = rank;
     156            0 :     u32 sliceSize = slices_.size() / rankSize;
     157              : 
     158            0 :     for (u32 sliceIdx = 0; sliceIdx < sliceSize; sliceIdx++) {
     159            0 :         Slice initSlice = slices_[initSliceIdx * sliceSize + sliceIdx];
     160              : 
     161              :         // 需要+userMemIn_的offset
     162            0 :         if (opInfo_->inputAddr != nullptr) {
     163              :             // AllGather算子调用AllGatherRingDirect场景
     164            0 :             srcInit = DeviceMem::create(static_cast<u8 *>(opInfo_->inputAddr) + firstStepOffset, initSlice.size);
     165              :         } else {
     166              :             // AllReduce算子调用AllGatherRingDirect场景
     167            0 :             srcInit = inputMem_.range(initSlice.offset, initSlice.size);
     168              :         }
     169              : 
     170            0 :         dstInit = outputMem_.range(initSlice.offset, initSlice.size);
     171            0 :         HCCL_DEBUG("Memcpy operation: step[-1] stream[main] src rank[%u] starts to copy(rcv) offset[%llu], "
     172              :             "size[%llu] on userMemOutput to offset[%llu], size[%llu] on CCL",
     173              :             userRank_, firstStepOffset, initSlice.size, initSlice.offset, initSlice.size);
     174              : 
     175              :         // 若src与dst一样,则不需要搬运
     176            0 :         if (srcInit != dstInit) {
     177            0 :             CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstInit, srcInit, stream_));
     178              :         }
     179              :     }
     180              : 
     181            0 :     return HCCL_SUCCESS;
     182            0 : }
     183              : 
     184              : // 本端cclout -> 本端userout
     185            0 : HcclResult AllGatherRingDirect::RunAllGatherPartOne(const u32 sliceSize, const u32 step, const u32 txSliceIdx)
     186              : {
     187            0 :     std::vector<Slice> txSliceVector;
     188            0 :     std::vector<Slice> sliceVector;
     189              : 
     190            0 :     for (u32 sliceIdx = 0; sliceIdx < sliceSize; sliceIdx++) {
     191            0 :         txSliceVector.push_back(slices_[txSliceIdx * sliceSize + sliceIdx]);
     192            0 :         sliceVector.push_back(userMemOutputSlices_[txSliceIdx * sliceSize + sliceIdx]);
     193              :     }
     194              : 
     195            0 :     for (u32 sliceIdx = 0; sliceIdx < sliceSize; sliceIdx++) {
     196            0 :         DeviceMem src = outputMem_.range(txSliceVector[sliceIdx].offset, txSliceVector[sliceIdx].size);
     197            0 :         DeviceMem dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + sliceVector[sliceIdx].offset,
     198            0 :         sliceVector[sliceIdx].size);
     199              : 
     200            0 :         HCCL_DEBUG("Memcpy operation: step[%u] stream[sub], src rank[%u] starts to send offset[%llu] size[%llu], "
     201              :             "dst rank starts to rcv offset[%llu] size[%llu] at userMemOutput_",
     202              :             step, userRank_, sliceVector[sliceIdx].offset, sliceVector[sliceIdx].size,
     203              :             txSliceVector[sliceIdx].offset, txSliceVector[sliceIdx].size);
     204              : 
     205            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
     206            0 :     }
     207              : 
     208            0 :     return HCCL_SUCCESS;
     209            0 : }
     210              : 
     211              : // 对端cclout -> 本端cclout, 如果最后一步则:对端cclout -> 本端userout (DMA消减)
     212            0 : HcclResult AllGatherRingDirect::RunAllGatherPartTwo(const u32 sliceSize, const u32 step,
     213              :         const u32 txSliceIdx, const u32 rxSliceIdx, const u32 rankSize)
     214              : {
     215            0 :     std::vector<Slice> txSliceVector;
     216            0 :     std::vector<Slice> rxSliceVector;
     217            0 :     std::vector<Slice> sliceVector;
     218              : 
     219            0 :     for (u32 sliceIdx = 0; sliceIdx < sliceSize; sliceIdx++) {
     220            0 :         txSliceVector.push_back(slices_[txSliceIdx * sliceSize + sliceIdx]);
     221            0 :         rxSliceVector.push_back(slices_[rxSliceIdx * sliceSize + sliceIdx]);
     222            0 :         sliceVector.push_back(userMemOutputSlices_[rxSliceIdx * sliceSize + sliceIdx]);
     223              :     }
     224              : 
     225            0 :     CHK_RET(leftLink_->TxAck(stream_));
     226            0 :     CHK_RET(rightLink_->RxAck(stream_));
     227              : 
     228            0 :     std::vector<TxMemoryInfo> txMems;
     229            0 :     std::vector<RxMemoryInfo> rxMems;
     230              : 
     231            0 :     for (u32 sliceIdx = 0; sliceIdx < sliceSize; sliceIdx++) {
     232            0 :         DeviceMem src = outputMem_.range(txSliceVector[sliceIdx].offset, txSliceVector[sliceIdx].size);
     233            0 :         HCCL_DEBUG("tx srcMem[%p] range[%llu] size[%llu] ", src.ptr(),
     234              :             txSliceVector[sliceIdx].offset, txSliceVector[sliceIdx].size);
     235            0 :         txMems.emplace_back(TxMemoryInfo{UserMemType::OUTPUT_MEM, txSliceVector[sliceIdx].offset + baseOffset_,
     236            0 :             src.ptr(), txSliceVector[sliceIdx].size});
     237              : 
     238            0 :         DeviceMem dst;
     239            0 :         if (isSdma_ && step == rankSize - DMA_REDUCE_TWO_OFFSET) {
     240              :             // 最后一步实现DMA消减:对端cclout -> 本端userout
     241            0 :             HCCL_DEBUG(
     242              :             "DMAReduce(sdma) MemcpyAsync operation: step[%u] stream[main], dst rank[%u] starts to rcv "
     243              :             "offset[%llu] size[%llu] at userMemOutput_",
     244              :             step, userRank_, sliceVector[sliceIdx].offset, sliceVector[sliceIdx].size);
     245              : 
     246            0 :             dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + sliceVector[sliceIdx].offset,
     247            0 :             sliceVector[sliceIdx].size);
     248              :         } else {
     249            0 :             HCCL_DEBUG(
     250              :                 "MemcpyAsync operation: step[%u] stream[main], dst rank[%u] starts to rcv offset[%llu] size[%llu] "
     251              :                 "at outputMem_",
     252              :                 step, userRank_, rxSliceVector[sliceIdx].offset, rxSliceVector[sliceIdx].size);
     253              : 
     254              :             // 中间步数无DMA消减
     255            0 :             dst = outputMem_.range(rxSliceVector[sliceIdx].offset, rxSliceVector[sliceIdx].size);
     256            0 :             if (!isSdma_ && step == rankSize - DMA_REDUCE_TWO_OFFSET) {
     257              :                 // 最后一步实现DMA消减:对端cclout -> 本端userout
     258            0 :                 HCCL_DEBUG("DMAReduce(rdma) record final addr");
     259              : 
     260            0 :                 finalSrc_.push_back(outputMem_.range(rxSliceVector[sliceIdx].offset, rxSliceVector[sliceIdx].size));
     261            0 :                 finalDst_.push_back(DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + 
     262            0 :                 sliceVector[sliceIdx].offset, sliceVector[sliceIdx].size));
     263              :             }
     264              :         }
     265              : 
     266            0 :         rxMems.emplace_back(RxMemoryInfo{UserMemType::OUTPUT_MEM, rxSliceVector[sliceIdx].offset + baseOffset_,
     267            0 :             dst.ptr(), rxSliceVector[sliceIdx].size});
     268            0 :     }
     269              : 
     270            0 :     CHK_RET(rightLink_->TxAsync(txMems, stream_));
     271              : 
     272            0 :     if (!isSdma_) {
     273            0 :         CHK_RET(leftLink_->RxAsync(rxMems, stream_));
     274              :     } else {
     275            0 :         CHK_RET(leftLink_->RxDataSignal(stream_));
     276              : 
     277            0 :         for (auto& mem : rxMems) {
     278            0 :             CHK_PTR_NULL(mem.dst);
     279            0 :             void *srcMemPtr = nullptr;
     280            0 :             CHK_RET(leftLink_->GetRemoteMem(mem.srcMemType, &srcMemPtr));
     281              : 
     282            0 :             DeviceMem srcDevMem(static_cast<s8 *>(srcMemPtr) + mem.srcOffset, mem.len);
     283            0 :             DeviceMem dstDevMem(static_cast<s8 *>(mem.dst), mem.len);
     284              : 
     285            0 :             CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstDevMem, srcDevMem,
     286              :                 stream_, leftLink_->GetRemoteRank(), leftLink_->GetLinkType()));
     287            0 :         }
     288              :     }
     289              : 
     290            0 :     return HCCL_SUCCESS;
     291            0 : }
     292              : 
     293            0 : HcclResult AllGatherRingDirect::RunAllGather(const u32 rank, const u32 rankSize)
     294              : {
     295            0 :     HCCL_INFO("AllGatherRingDirect starts, the input param rank[%u]", rank);
     296            0 :     CHK_RET(RunInitStep(rank, rankSize));
     297              : 
     298            0 :     finalSrc_.clear();
     299            0 :     finalDst_.clear();
     300              : 
     301            0 :     u32 txSliceIdx = rank;
     302            0 :     u32 sliceSize = slices_.size() / rankSize;
     303            0 :     u32 rxSliceIdx = (rank + rankSize - 1) % rankSize;
     304              : 
     305            0 :     for (u32 step = 0; step < rankSize - 1; step++) {
     306              :         // 本端cclout -> 本端userout
     307            0 :         CHK_RET(RunAllGatherPartOne(sliceSize, step, txSliceIdx));
     308              :         // 对端cclout -> 本端cclout, 如果最后一步则:对端cclout -> 本端userout (DMA消减)
     309            0 :         CHK_RET(RunAllGatherPartTwo(sliceSize, step, txSliceIdx, rxSliceIdx, rankSize));
     310              :         // 更新索引
     311            0 :         txSliceIdx = (txSliceIdx + rankSize - 1) % rankSize;
     312            0 :         rxSliceIdx = (rxSliceIdx + rankSize - 1) % rankSize;
     313              :     }
     314              : 
     315            0 :     if (!isSdma_) {
     316            0 :         for (u32 vecIdx = 0; vecIdx < finalSrc_.size(); vecIdx++) {
     317            0 :             CHK_RET(HcclD2DMemcpyAsync(dispatcher_, finalDst_[vecIdx], finalSrc_[vecIdx], stream_));
     318              :         }
     319              :     }
     320              : 
     321            0 :     HCCL_INFO("AllGatherRingDirect finished to RunAllGather");
     322              : 
     323            0 :     return HCCL_SUCCESS;
     324              : }
     325              : REGISTER_TEMPLATE(TemplateType::TEMPLATE_ALL_GATHER_RING_DIRECT, AllGatherRingDirect);
     326              : } // namespace hccl
        

Generated by: LCOV version 2.0-1