LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/alg_template/temp_scatter - scatter_ring_direct.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 117 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 12 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 "scatter_ring_direct.h"
      12              : #include "alg_template_register.h"
      13              : 
      14              : namespace hccl {
      15            0 : ScatterRingDirect::ScatterRingDirect(const HcclDispatcher dispatcher)
      16            0 :     : AlgTemplateBase(dispatcher)
      17              : {
      18            0 : }
      19              : 
      20            0 : ScatterRingDirect::~ScatterRingDirect()
      21              : {
      22            0 : }
      23              : 
      24            0 : HcclResult ScatterRingDirect::Prepare(HcomCollOpInfo *opInfo, const u32 userRank,
      25              :     const std::vector<u32> &ringsOrders, const std::vector<Slice> &userMemInputSlices)
      26              : {
      27            0 :     opInfo_ = opInfo;
      28            0 :     userRank_ = userRank;
      29            0 :     ringsOrder_ = ringsOrders;
      30            0 :     userMemInputSlices_ = userMemInputSlices;
      31            0 :     return HCCL_SUCCESS;
      32              : }
      33              : 
      34              : // reduce scatter ring direct算法的函数入口
      35            0 : HcclResult ScatterRingDirect::RunAsync(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
      36              : {
      37              :     // 基本的检查
      38            0 :     CHK_RET(CheckParameters(rank, rankSize, links));
      39              : 
      40              :     // 判断rank_size == 1, 若inputMem_ != outputMem_,才需要搬运
      41            0 :     if (rankSize == 1) {
      42            0 :         CHK_RET(OneRankMemcpy());
      43            0 :         return HCCL_SUCCESS;
      44              :     }
      45              :     // 收集邻居信息
      46            0 :     CHK_RET(GetInitializedNeighborLinks(rank, rankSize, links));
      47              :     // 填充slice_
      48            0 :     CHK_RET(SetSlices(rank, rankSize));
      49              : 
      50              :     // 运行scatter, ring算法
      51            0 :     CHK_RET(RunScatter(rank, rankSize));
      52              : 
      53            0 :     if (barrierSwitchOn_) {
      54              :         // 执行barrier,保证数据收发完成
      55            0 :         CHK_RET(ExecuteBarrier(leftLink_, rightLink_));
      56              :     }
      57              : 
      58            0 :     HCCL_INFO("ScatterRingDirect finished: rank[%u]", rank);
      59            0 :     return HCCL_SUCCESS;
      60              : }
      61              : 
      62            0 : HcclResult ScatterRingDirect::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              :     // 判断ringsOrder数量是否正确
      68            0 :     CHK_PRT_RET(ringsOrder_.size() != rankSize,
      69              :                 HCCL_ERROR("[ScatterRingDirect] ringsOrder size[%u] is not equal to rank size[%u]",
      70              :                            ringsOrder_.size(), rankSize),
      71              :                 HCCL_E_PARA);
      72              :     // 判断userMemInputSlices数量是否正确
      73            0 :     CHK_PRT_RET(userMemInputSlices_.size() != rankSize,
      74              :                 HCCL_ERROR("[ScatterRingDirect] userMemInputSlices size[%u] is not equal to rank size[%u]",
      75              :                            userMemInputSlices_.size(), rankSize),
      76              :                 HCCL_E_PARA);
      77            0 :     HCCL_INFO("ScatterRingDirect CheckParameters success");
      78            0 :     return HCCL_SUCCESS;
      79              : }
      80              : 
      81            0 : HcclResult ScatterRingDirect::OneRankMemcpy()
      82              : {
      83            0 :     const Slice &srcSlice = userMemInputSlices_[0];
      84            0 :     const Slice &dstSlice = slices_[0];
      85            0 :     DeviceMem    src;
      86            0 :     DeviceMem    dst;
      87            0 :     if (opInfo_->inputAddr == nullptr) {
      88            0 :         src = inputMem_.range(srcSlice.offset, srcSlice.size);
      89              :     }
      90              :     else {
      91            0 :         src = DeviceMem::create(static_cast<u8 *>(opInfo_->inputAddr) + srcSlice.offset, srcSlice.size);
      92              :     }
      93            0 :     if (opInfo_->outputAddr != nullptr) {
      94              :         // opInfo_->outputAddr != nullptr指示要将输出发送至user output
      95            0 :         u64 stepOffset = slices_[ringsOrder_[0]].offset;
      96            0 :         HCCL_DEBUG("Memcpy operation: stream[main], rank[%u] starts to rcv offset[%llu], size[%llu] at userMemOut_",
      97              :                    userRank_, stepOffset, dstSlice.size);
      98            0 :         dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + stepOffset, dstSlice.size);
      99              :     } else {
     100              :         // opInfo_->outputAddr == nullptr指示要将输出发送至CCL buffer
     101            0 :         HCCL_DEBUG("Memcpy operation: stream[main], rank[%u] starts to rcv offset[%llu], size[%llu] at outputMem_",
     102              :                    userRank_, dstSlice.offset, dstSlice.size);
     103            0 :         dst = outputMem_.range(dstSlice.offset, dstSlice.size);
     104              :     }
     105            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
     106            0 :     return HCCL_SUCCESS;
     107            0 : }
     108              : 
     109            0 : HcclResult ScatterRingDirect::GetInitializedNeighborLinks(const u32 rank, const u32 rankSize,
     110              :                                                                     const std::vector<LINK> &links)
     111              : {
     112              :     // 收集左邻居信息
     113            0 :     leftLink_ = links[(rank + rankSize - 1) % rankSize];
     114            0 :     CHK_SMART_PTR_NULL(leftLink_);
     115              : 
     116              :     // 收集右邻居信息
     117            0 :     rightLink_ = links[(rank + 1) % rankSize];
     118            0 :     CHK_SMART_PTR_NULL(rightLink_);
     119            0 :     HCCL_INFO("ScatterRingDirect finished to GetInitializedNeighborLinks");
     120            0 :     return HCCL_SUCCESS;
     121              : }
     122              : 
     123            0 : HcclResult ScatterRingDirect::SetSlices(const u32 rank, const u32 rankSize)
     124              : {
     125            0 :     if (slices_.size() == 0) {
     126            0 :         slices_.resize(rankSize);
     127              : 
     128              :         // 生成std::vector<Slice> slices_
     129            0 :         u64 sliceSize = count_ * SIZE_TABLE[dataType_];
     130              :         ;
     131              : 
     132            0 :         for (u32 i = 0; i < rankSize; i++) {
     133            0 :             slices_[i].size = sliceSize;
     134              :             // 用于DMA消减过程中,消除src与dst不对位的风险
     135            0 :             slices_[i].offset = RoundUpWithDivisor(i * sliceSize, HCCL_MIN_SLICE_ALIGN);
     136              : 
     137            0 :             HCCL_DEBUG("rank[%u], slices[%u].offset=[%llu], slices[%u].size=[%llu]", rank, i, slices_[i].offset, i,
     138              :                        slices_[i].size);
     139              :         }
     140              :     }
     141            0 :     if (UNLIKELY(HcclCheckLogLevel(HCCL_LOG_DEBUG))) {
     142            0 :         for (u32 i = 0; i < slices_.size(); i++) {
     143            0 :             HCCL_DEBUG("[ScatterRingDirect][SetSlices]rank[%u], slices[%u].offset=[%llu], slices[%u].size=[%llu]",
     144              :                     rank, i, slices_[i].offset, i, slices_[i].size);
     145              :         }
     146              :     }
     147              :     // 最后一步搬到userMemOut_的offset, 不同的ring环offset不一样
     148            0 :     lastStepOffset_ = slices_[ringsOrder_[0]].offset;
     149            0 :     HCCL_INFO("ScatterRingDirect finished to SetSlices");
     150            0 :     return HCCL_SUCCESS;
     151              : }
     152              : 
     153            0 : HcclResult ScatterRingDirect::RunScatter(const u32 rank, const u32 rankSize)
     154              : {
     155            0 :     HCCL_INFO("ScatterRingDirect starts, the input param rank[%u]", rank);
     156              :     // 空拷贝用于后续操作附着
     157            0 :     CHK_RET(ExecEmptyTask(inputMem_, outputMem_, stream_, dispatcher_));
     158              :     // 例如rank[0,1,2,3]中,rank0的rxSliceIdx = 2,txSliceIdx = 3, subSliceIdx = 1
     159            0 :     u32 txSliceIdx  = (rank + rankSize - 1) % rankSize;
     160            0 :     u32 rxSliceIdx  = (rank + rankSize - DMA_REDUCE_TWO_OFFSET) % rankSize;
     161            0 :     u32 subSliceIdx = (rank + rankSize - DMA_REDUCE_TWO_OFFSET) % rankSize; // 只存在于根节点
     162            0 :     u32 stepsFromRank2Root = (rank + rankSize - root_) % rankSize;
     163            0 :     for (u32 step = 0; step < rankSize - 1; step++) {
     164            0 :         const Slice &subSlice = userMemInputSlices_[subSliceIdx];
     165            0 :         const Slice &cclSlice = slices_[subSliceIdx];
     166            0 :         const Slice &txSlice  = slices_[txSliceIdx];
     167            0 :         const Slice &rxSlice  = slices_[rxSliceIdx];
     168              : 
     169            0 :         CHK_RET(RunScatterOnRootRank(step, subSlice, cclSlice, rank, rankSize));
     170            0 :         CHK_RET(RunScatterOnOtherRank(stepsFromRank2Root, step, txSlice, rxSlice, rankSize));
     171              : 
     172              :         // 更新索引
     173            0 :         subSliceIdx = (subSliceIdx + rankSize - 1) % rankSize;
     174            0 :         txSliceIdx  = (txSliceIdx + rankSize - 1) % rankSize;
     175            0 :         rxSliceIdx  = (rxSliceIdx + rankSize - 1) % rankSize;
     176              :     }
     177            0 :     HCCL_INFO("ScatterRingDirect finished to RunScatter");
     178            0 :     return HCCL_SUCCESS;
     179              : }
     180              : 
     181            0 : HcclResult ScatterRingDirect::RunScatterOnOtherRank(const u32 stepsFromRank2Root, const u32 step,
     182              :                                                       const Slice &txSlice, const Slice &rxSlice, const u32 rankSize)
     183              : {
     184            0 :     bool needSend    = stepsFromRank2Root <= step;
     185            0 :     bool needReceive = stepsFromRank2Root > 0 && stepsFromRank2Root <= (step + 1);
     186              :     // Ack
     187            0 :     if (needReceive) {
     188            0 :         CHK_RET(leftLink_->TxAck(stream_));
     189              :     }
     190            0 :     if (needSend) {
     191            0 :         CHK_RET(rightLink_->RxAck(stream_));
     192              :     }
     193              : 
     194            0 :     DeviceMem src;
     195              :     // 不同的rank会在不同的step开始持续发送操作,距离root节点越近,越早step开始发送操作
     196            0 :     if (needSend) {
     197            0 :         src = inputMem_.range(txSlice.offset, txSlice.size);
     198            0 :         CHK_RET(rightLink_->TxAsync(UserMemType::INPUT_MEM, txSlice.offset + baseOffset_, src.ptr(), txSlice.size,
     199              :                                     stream_));
     200              :     }
     201              :     // 不同的rank会在不同的step开始持续发送操作,距离root节点越近,越早step开始发送操作
     202            0 :     DeviceMem dst;
     203            0 :     if (needReceive) {
     204            0 :         HCCL_DEBUG("MemcpyAsync operation: step[%u] stream[main], src rank[%u] starts to send offset[%llu] size[%llu] "
     205              :                    "from leftMem_",
     206              :                    step, leftLink_->GetRemoteRank(), rxSlice.offset, rxSlice.size);
     207            0 :         if (step == rankSize - DMA_REDUCE_TWO_OFFSET && opInfo_->outputAddr != nullptr) {
     208            0 :             HCCL_DEBUG("MemcpyAsync operation: step[%u] stream[main], dst rank[%u] starts to rcv offset[%llu], "
     209              :                        "size[%llu] "
     210              :                        "at userMemOut_",
     211              :                        step, userRank_, lastStepOffset_, rxSlice.size);
     212            0 :             dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + lastStepOffset_, rxSlice.size);
     213              :         } else {
     214            0 :             HCCL_DEBUG("MemcpyAsync operation: step[%u] stream[main], dst rank[%u] starts to rcv offset[%llu], "
     215              :                        "size[%llu] "
     216              :                        "at inputMem_",
     217              :                        step, userRank_, rxSlice.offset, rxSlice.size);
     218            0 :             dst = inputMem_.range(rxSlice.offset, rxSlice.size);
     219              :         }
     220            0 :         CHK_RET(
     221              :             leftLink_->RxAsync(UserMemType::INPUT_MEM, rxSlice.offset + baseOffset_, dst.ptr(), rxSlice.size, stream_));
     222              :     }
     223            0 :     return HCCL_SUCCESS;
     224            0 : }
     225              : 
     226            0 : HcclResult ScatterRingDirect::RunScatterOnRootRank(const u32 step, const Slice &subSlice, const Slice &cclSlice,
     227              :                                                      const u32 rank, const u32 rankSize)
     228              : {
     229            0 :     if (step == rankSize - DMA_REDUCE_TWO_OFFSET && opInfo_->outputAddr != nullptr && rank == root_) {
     230            0 :         HCCL_DEBUG("MemcpyAsync operation: step[%u] stream[main], dst rank[%u] starts to rcv offset[%llu], "
     231              :                     "size[%llu] at userMemOut_", step, userRank_, lastStepOffset_, subSlice.size);
     232            0 :         DeviceMem src = inputMem_.range(cclSlice.offset, cclSlice.size);
     233            0 :         DeviceMem dst = DeviceMem::create(static_cast<u8 *>(opInfo_->outputAddr) + lastStepOffset_, subSlice.size);
     234            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
     235            0 :     }
     236            0 :     return HCCL_SUCCESS;
     237              : }
     238              : REGISTER_TEMPLATE(TemplateType::TEMPLATE_SCATTER_RING_DIRECT, ScatterRingDirect);
     239              : } // namespace hccl
        

Generated by: LCOV version 2.0-1