LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/alg_template/temp_all_reduce - all_reduce_mesh_opbase.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 23.6 % 161 38
Test Date: 2026-07-28 12:11:00 Functions: 38.5 % 13 5

            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 "all_reduce_mesh_opbase.h"
      13              : 
      14              : namespace hccl {
      15            3 : AllReduceMeshDirect::AllReduceMeshDirect(const HcclDispatcher dispatcher) : AlgTemplateBase(dispatcher)
      16            3 : {}
      17              : 
      18            6 : AllReduceMeshDirect::~AllReduceMeshDirect()
      19            6 : {}
      20              : 
      21            3 : HcclResult AllReduceMeshDirect::Prepare(u64 reduceAttrBitMap, std::vector<Stream> &meshStreams,
      22              :     std::vector<std::shared_ptr<LocalNotify>> &meshSignal, std::vector<std::shared_ptr<LocalNotify>> &meshSignalAux,
      23              :     u32 interRank, u32 interRankSize, u32 userRank, HcomCollOpInfo *opInfo)
      24              : {
      25            3 :     reduceAttr_ = reduceAttrBitMap;
      26            3 :     localRank_ = interRank;
      27            3 :     localRankSize_ = interRankSize;
      28            3 :     userRank_ = userRank;
      29            3 :     meshStreams_ = meshStreams;
      30            3 :     meshSignal_ = &meshSignal;
      31            3 :     meshSignalAux_ = &meshSignalAux;
      32            3 :     opInfo_ = opInfo;
      33            3 :     return HCCL_SUCCESS;
      34              : }
      35              : 
      36            0 : HcclResult AllReduceMeshDirect::MainRecordSub()
      37              : {
      38            0 :     for (u32 signalIndex = 0; signalIndex < meshSignalAux_->size(); signalIndex++) {
      39            0 :         CHK_RET(LocalNotify::Post(stream_, dispatcher_, (*meshSignalAux_)[signalIndex],
      40              :             profilerInput_.stage));
      41              :     }
      42            0 :     return HCCL_SUCCESS;
      43              : }
      44              : 
      45            0 : HcclResult AllReduceMeshDirect::SubWaitMain()
      46              : {
      47            0 :     for (u32 streamIndex = 0; streamIndex < meshSignalAux_->size(); streamIndex++) {
      48            0 :         CHK_RET(LocalNotify::Wait(meshStreams_[streamIndex], dispatcher_, (*meshSignalAux_)[streamIndex],
      49              :             profilerInput_.stage));
      50              :     }
      51            0 :     return HCCL_SUCCESS;
      52              : }
      53              : 
      54            0 : HcclResult AllReduceMeshDirect::MainWaitSub()
      55              : {
      56            0 :     for (u32 signalIndex = 0; signalIndex < meshSignal_->size(); signalIndex++) {
      57            0 :         CHK_RET(LocalNotify::Wait(stream_, dispatcher_, (*meshSignal_)[signalIndex], profilerInput_.stage));
      58              :     }
      59            0 :     return HCCL_SUCCESS;
      60              : }
      61              : 
      62            0 : HcclResult AllReduceMeshDirect::SubRecordMain()
      63              : {
      64            0 :     for (u32 streamIndex = 0; streamIndex < meshSignal_->size(); streamIndex++) {
      65            0 :         CHK_RET(LocalNotify::Post(meshStreams_[streamIndex], dispatcher_, (*meshSignal_)[streamIndex],
      66              :             profilerInput_.stage));
      67              :     }
      68            0 :     return HCCL_SUCCESS;
      69              : }
      70              : 
      71              : // 将数据均分,最小单位是128
      72            3 : HcclResult AllReduceMeshDirect::PrepareSlice(u64 dataCount, u32 unitSize, u32 sliceNum, std::vector<Slice>& dataSlice)
      73              : {
      74            3 :     Slice temp;
      75            3 :     u64 totalSize = dataCount * unitSize;
      76            3 :     dataSlice.clear();
      77            3 :     dataSlice.reserve(sliceNum);
      78            3 :     if (sliceNum == 0) {
      79            0 :         HCCL_ERROR("[Prepare][SliceData]data slice prepare, sliceNum is 0");
      80            0 :         return HCCL_E_PARA;
      81              :     }
      82            3 :     u64 sizePerSlice = (totalSize + sliceNum - 1) / sliceNum; /* 1是为了向上取整 */
      83            3 :     sizePerSlice = RoundUpWithDivisor(sizePerSlice, HCCL_MIN_SLICE_ALIGN_910B);
      84            3 :     u64 residueSize = totalSize;
      85            3 :     u32 i = 0;
      86            6 :     while (residueSize > 0) {
      87            3 :         u64 sliceSize = sizePerSlice < residueSize ? sizePerSlice : residueSize;
      88            3 :         temp.size = sliceSize;
      89            3 :         temp.offset = totalSize - residueSize;
      90            3 :         i++;
      91            3 :         if (sliceSize <= 0) {
      92            0 :             HCCL_ERROR("[Prepare][SliceData]data_slice_prepare sliceSize[%llu]", sliceSize);
      93            0 :             return HCCL_E_PARA;
      94              :         }
      95            3 :         residueSize -= sliceSize;
      96            3 :         dataSlice.push_back(temp);
      97              :     }
      98           24 :     while (i < sliceNum) {
      99           21 :         temp.size = 0;
     100           21 :         temp.offset = totalSize;
     101           21 :         i++;
     102           21 :         dataSlice.push_back(temp);
     103              :     }
     104            3 :     return HCCL_SUCCESS;
     105              : }
     106              : 
     107            0 : HcclResult AllReduceMeshDirect::PrepareAllreduceSliceData()
     108              : {
     109            0 :     return PrepareSlice(count_, DataUnitSize(dataType_), localRankSize_, slices_);
     110              : }
     111              : 
     112              : // ringallreduce算法的函数入口
     113            0 : HcclResult AllReduceMeshDirect::RunAsync(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
     114              : {
     115            0 :     HcclResult ret = HCCL_SUCCESS;
     116            0 :     CHK_SMART_PTR_NULL(dispatcher_);
     117            0 :     CHK_PTR_NULL(stream_.ptr());
     118            0 :     HCCL_INFO("AllReduceMeshDirect run: rank[%u] ranksize[%u] inputMem[%p] outputMem[%p] count[%llu]", rank, rankSize,
     119              :         inputMem_.ptr(), outputMem_.ptr(), count_);
     120              : 
     121            0 :     if (links.size() < rankSize) {
     122            0 :         HCCL_ERROR("[AllReduceMeshDirect][RunAsync]rank[%u] linksize[%llu] is less than rankSize[%u]",
     123              :             rank, links.size(), rankSize);
     124            0 :         return HCCL_E_INTERNAL;
     125              :     }
     126              : 
     127              :     // 如果ranksize为1, inline reduce和普通跨片reduce操作一致,从input->output
     128            0 :     if (rankSize == 1) {
     129            0 :         if (opInfo_->inputAddr != opInfo_->outputAddr) {
     130            0 :             DeviceMem userMemIn = DeviceMem::create(opInfo_->inputAddr, count_ * DataUnitSize(dataType_));
     131            0 :             DeviceMem userMemOut = DeviceMem::create(opInfo_->outputAddr, count_ * DataUnitSize(dataType_));
     132            0 :             ret = HcclD2DMemcpyAsync(dispatcher_, userMemOut, userMemIn, stream_);
     133            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllReduceRing][RunAsync]rank[%u] memcpy async failed", rank),
     134              :                 ret);
     135            0 :         }
     136            0 :         return ret;
     137              :     }
     138              : 
     139            0 :     ret = PrepareAllreduceSliceData();
     140            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     141              :         HCCL_ERROR("[AllReduceRing][RunAsync]rank[%u] count[%llu] failed in PrepareSliceData "
     142              :         "step",
     143              :         rank, count_),
     144              :         ret);
     145              : 
     146            0 :     ret = RunReduceScatter(rank, rankSize, links);
     147            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     148              :         HCCL_ERROR("[AllReduceRing][RunAsync]rank[%u] count[%llu] failed in reducescater "
     149              :         "step",
     150              :         rank, count_),
     151              :         ret);
     152              : 
     153            0 :     ret = RunAllGather(rank, rankSize, links);
     154            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     155              :         HCCL_ERROR("[AllReduceRing][RunAsync]rank[%u] count[%llu] failed in AllGather "
     156              :         "step",
     157              :         rank, count_),
     158              :         ret);
     159              : 
     160            0 :     HCCL_INFO("AllReduceMeshDirect finished: rank[%u] ranksize[%u].", rank, rankSize);
     161            0 :     return HCCL_SUCCESS;
     162              : }
     163              : 
     164              : 
     165            0 : HcclResult AllReduceMeshDirect::RunReduceScatter(u32 rank, u32 rankSize, const std::vector<LINK> &links)
     166              : {
     167            0 :     HCCL_INFO("ReduceScatterMeshAtomicOpbase run: rank[%u] totalrank[%u] inputMem[%p] outputMem[%p] count[%llu].", rank,
     168              :         rankSize, inputMem_.ptr(), outputMem_.ptr(), count_);
     169              : 
     170              :     // 数据准备
     171            0 :     u32 unitSize = DataUnitSize(dataType_);
     172              : 
     173            0 :     DeviceMem userMemIn = DeviceMem::create(opInfo_->inputAddr, count_ * unitSize);
     174            0 :     DeviceMem commMemOut = DeviceMem::create(outputMem_.ptr(), outputMem_.size());
     175              : 
     176            0 :     DeviceMem src;
     177            0 :     DeviceMem dst;
     178              : 
     179            0 :     src = DeviceMem::create(static_cast<char *>(opInfo_->inputAddr) + slices_[rank].offset, slices_[rank].size);
     180            0 :     dst = commMemOut.range(0, slices_[rank].size);
     181            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
     182              : 
     183            0 :     CHK_RET(MainRecordSub());
     184            0 :     CHK_RET(SubWaitMain());
     185              : 
     186            0 :     for (u32 round = 1; round < rankSize; round++) {
     187            0 :         u32 dstRank = (round + rank) % rankSize;
     188            0 :         Stream &subStream = meshStreams_[round - 1];
     189            0 :         CHK_RET(links[dstRank]->TxAck(subStream));
     190            0 :         CHK_RET(links[dstRank]->RxAck(subStream));
     191              :     }
     192              : 
     193            0 :     CHK_RET(SubRecordMain());
     194            0 :     CHK_RET(MainWaitSub());
     195            0 :     DeviceMem srcTmp = DeviceMem::create(inputMem_.ptr(), 0);
     196            0 :     DeviceMem dstTmp = DeviceMem::create(outputMem_.ptr(), 0);
     197            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstTmp, srcTmp, stream_));
     198            0 :     CHK_RET(SubWaitMain());
     199            0 :     CHK_RET(MainRecordSub());
     200              : 
     201            0 :     for (u32 round = 1; round < rankSize; round++) {
     202            0 :         u32 dstRank = (round + rank) % rankSize;
     203            0 :         Stream &subStream = meshStreams_[round - 1];
     204              : 
     205            0 :         void *remMemPtr = nullptr;
     206            0 :         CHK_RET(links[dstRank]->GetRemoteMem(UserMemType::OUTPUT_MEM, &remMemPtr));
     207              : 
     208            0 :         dst = DeviceMem::create(static_cast<char *>(remMemPtr), slices_[dstRank].size);
     209            0 :         src = userMemIn.range(slices_[dstRank].offset, slices_[dstRank].size);
     210            0 :         CHK_RET(HcclReduceAsync(dispatcher_, static_cast<void *>(src.ptr()), slices_[dstRank].size / unitSize,
     211              :             dataType_, reductionOp_, subStream, static_cast<void *>(dst.ptr()),
     212              :             links[dstRank]->GetRemoteRank(), links[dstRank]->GetLinkType(), INLINE_REDUCE_BIT));
     213              : 
     214            0 :         CHK_RET(links[dstRank]->TxDataSignal(subStream));
     215            0 :         CHK_RET(links[dstRank]->RxDataSignal(subStream));
     216              :     }
     217              : 
     218            0 :     CHK_RET(SubRecordMain());
     219            0 :     CHK_RET(MainWaitSub());
     220            0 :     return HCCL_SUCCESS;
     221            0 : }
     222              : 
     223            0 : HcclResult AllReduceMeshDirect::RunAllGather(u32 rank, u32 rankSize, const std::vector<LINK> &links)
     224              : {
     225            0 :     HCCL_INFO("AllGatherMesh run: rank[%u] totalrank[%u] inputMem[%p] outputMem[%p] count[%llu].", rank, rankSize,
     226              :         inputMem_.ptr(), outputMem_.ptr(), count_);
     227            0 :     u32 unitSize = DataUnitSize(dataType_);
     228              : 
     229            0 :     DeviceMem userMemOut = DeviceMem::create(opInfo_->outputAddr, count_ * unitSize);
     230            0 :     DeviceMem commMemOut = DeviceMem::create(outputMem_.ptr(), outputMem_.size());
     231              : 
     232            0 :     DeviceMem src;
     233            0 :     DeviceMem dst;
     234              : 
     235            0 :     src = commMemOut.range(0, slices_[rank].size);
     236            0 :     dst = userMemOut.range(slices_[rank].offset, slices_[rank].size);
     237            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, stream_));
     238              : 
     239            0 :     CHK_RET(MainRecordSub());
     240            0 :     CHK_RET(SubWaitMain());
     241              : 
     242            0 :     for (u32 round = 1; round < rankSize; round++) {
     243            0 :         u32 dstRank = BackwardRank(rank, rankSize, round);
     244            0 :         Stream &subStream = meshStreams_[round - 1];
     245            0 :         CHK_RET(links[dstRank]->TxAck(subStream));
     246            0 :         CHK_RET(links[dstRank]->RxAck(subStream));
     247              :     }
     248              : 
     249            0 :     CHK_RET(SubRecordMain());
     250            0 :     CHK_RET(MainWaitSub());
     251            0 :     DeviceMem srcTmp = DeviceMem::create(inputMem_.ptr(), 0);
     252            0 :     DeviceMem dstTmp = DeviceMem::create(outputMem_.ptr(), 0);
     253            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstTmp, srcTmp, stream_));
     254            0 :     CHK_RET(SubWaitMain());
     255            0 :     CHK_RET(MainRecordSub());
     256              : 
     257            0 :     for (u32 round = 1; round < rankSize; round++) {
     258            0 :         u32 dstRank = BackwardRank(rank, rankSize, round);
     259            0 :         Stream &subStream = meshStreams_[round - 1];
     260            0 :         void *remMemPtr = nullptr;
     261            0 :         CHK_RET(links[dstRank]->GetRemoteMem(UserMemType::OUTPUT_MEM, &remMemPtr));
     262            0 :         src = DeviceMem::create(static_cast<char *>(remMemPtr), slices_[dstRank].size);
     263            0 :         dst = userMemOut.range(slices_[dstRank].offset, slices_[dstRank].size);
     264            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, subStream,
     265              :             links[dstRank]->GetRemoteRank(), links[dstRank]->GetLinkType()));
     266            0 :         CHK_RET(links[dstRank]->TxDataSignal(subStream));
     267            0 :         CHK_RET(links[dstRank]->RxDataSignal(subStream));
     268              :     }
     269              : 
     270            0 :     CHK_RET(SubRecordMain());
     271            0 :     CHK_RET(MainWaitSub());
     272              : 
     273            0 :     HCCL_INFO("AllGatherMesh finished: rank[%u]", rank);
     274            0 :     return HCCL_SUCCESS;
     275            0 : }
     276              : REGISTER_TEMPLATE(TemplateType::TEMPLATE_ALL_REDUCE_MESH_DIRECT, AllReduceMeshDirect);
     277              : } // namespace hccl
        

Generated by: LCOV version 2.0-1