LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_ccu_context/all_reduce - ccu_context_all_reduce_mesh2d_one_shot.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 154 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 8 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 "ccu_context_all_reduce_mesh2d_one_shot.h"
      12              : #include "ccu_instruction_all_reduce_mesh2d_one_shot.h"
      13              : 
      14              : namespace Hccl {
      15              : 
      16              : constexpr int      INPUT_XN_ID   = 0;
      17              : constexpr int      SCRATCH_XN_ID = 1;
      18              : constexpr int      TOKEN_XN_ID   = 2;
      19              : constexpr int      CKE_IDX_0     = 0;
      20              : constexpr int      CKE_IDX_1     = 1;
      21              : constexpr int      CKE_IDX_2     = 2;
      22              : constexpr int      CKE_IDX_3     = 3;
      23              : constexpr int      CKE_IDX_4     = 4;
      24              : constexpr uint32_t AXIS_NUM      = 2;
      25              : 
      26            0 : CcuContextAllReduceMesh2DOneShot::CcuContextAllReduceMesh2DOneShot(const CcuCtxArg &arg,
      27            0 :     const std::vector<CcuTransport*> &transports, const CcuTransportGroup &group)
      28            0 :     : CcuContextAlgBase(arg, transports, group)
      29              : {
      30            0 :     const CcuCtxArgAllReduceMesh2DOneShot *ctxArg = dynamic_cast<const CcuCtxArgAllReduceMesh2DOneShot *>(&arg);
      31            0 :     if (ctxArg == nullptr) {
      32            0 :         THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh2DOneShot::ctxArg ptr is null"));
      33              :     }
      34            0 :     dimSize_ = ctxArg->dimSize_;
      35            0 :     axisId_ = ctxArg->axisId_;
      36            0 :     rankId_ = ctxArg->rankId_;
      37            0 :     dataType_ = ctxArg->op_.dataType;
      38            0 :     outputDataType_ = ctxArg->op_.outputDataType;
      39            0 :     reduceOp_ = ctxArg->op_.reduceOp;
      40            0 :     if (outputDataType_ == DataType::INVALID) {
      41            0 :         outputDataType_ = dataType_;
      42            0 :         HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] outputDataType is [INVALID], set outputDataType to[%s]",
      43              :             outputDataType_.Describe().c_str());
      44              :     }
      45              : 
      46            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] Init, CtxArgs are dimSize0[%llu], dimSize1[%llu], axisId[%u], "
      47              :         "rankId[%llu], dataType[%s], outputDataType[%s], reduceOp[%s]",
      48              :         dimSize_[0], dimSize_[1], axisId_, rankId_, dataType_.Describe().c_str(),
      49              :         outputDataType_.Describe().c_str(), reduceOp_.Describe().c_str());
      50            0 :     uint32_t max_dimSize = 2;
      51            0 :     if (dimSize_.size() != max_dimSize or axisId_ > 1) {
      52            0 :         THROW<NullPtrException>(StringFormat("[CcuContextAllReduceMesh2DOneShot] dimSize[%u] or axisId[%u] is invalid",
      53              :             dimSize_.size(), axisId_));
      54              :     }
      55            0 :     CHK_PRT_THROW(dimSize_[0] == 0 || dimSize_[1] == 0,
      56              :                   HCCL_ERROR("[CcuContextAllReduceMesh2DOneShot] dimSize0[%llu] or dimSize1[%llu] is zero",
      57              :                    dimSize_[0], dimSize_[1]),
      58              :                   InvalidParamsException, "dimSize[0] or dimSize[1] is invalid");
      59              : 
      60            0 :     myRankIdxInAxis_.push_back(rankId_ % dimSize_[0]); // 本 rank 在第 0 维上的 index
      61            0 :     myRankIdxInAxis_.push_back(rankId_ / dimSize_[0]); // 本 rank 在第 1 维上的 index
      62              : 
      63            0 :     myRankIdxInCurrentAxis_ = myRankIdxInAxis_[axisId_];
      64            0 :     currentAxisRankSize_ = dimSize_[axisId_];
      65              : 
      66              :     // 同步信号初始化
      67            0 :     currAxisSignalName_ = "CcuContextAllReduceMesh2DOneShotAxisSync_" + std::to_string(axisId_);
      68            0 :     otherAxisSignalName_ = "CcuContextAllReduceMesh2DOneShotAxisSync_" + std::to_string(1 - axisId_);
      69            0 :     currAxisSignal_ = CreateMaskSignal();
      70            0 :     ExportMaskSignal(currAxisSignal_, currAxisSignalName_);
      71            0 :     otherAxisSignal_ = ImportMaskSignal(otherAxisSignalName_);
      72              : 
      73            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] Init, myRankIdx0[%llu], myRankIdx1[%llu], "
      74              :         "myRankIdxInCurrentAxis[%llu], currentAxisRankSize[%llu]",
      75              :         myRankIdxInAxis_[0], myRankIdxInAxis_[1], myRankIdxInCurrentAxis_, currentAxisRankSize_);
      76            0 : }
      77              : 
      78            0 : void CcuContextAllReduceMesh2DOneShot::Algorithm()
      79              : {
      80            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] AllReduceMesh2DOneShot run");
      81            0 :     uint16_t selfBit = 1 << myRankIdxInCurrentAxis_;
      82            0 :     uint16_t allBit  = ((1 << currentAxisRankSize_) - 1) & (~(1 << myRankIdxInCurrentAxis_));
      83              : 
      84            0 :     InitVariables();
      85              : 
      86            0 :     LoadArgs();
      87              : 
      88              :     // 前同步
      89            0 :     for (auto t : transports) {
      90            0 :         WriteVariableWithSignal(*t, inputAddr_[myRankIdxInCurrentAxis_], INPUT_XN_ID, CKE_IDX_1, selfBit);
      91            0 :         WriteVariableWithSignal(*t, scratchAddr_[myRankIdxInCurrentAxis_], SCRATCH_XN_ID, CKE_IDX_2, selfBit);
      92            0 :         WriteVariableWithSignal(*t, token_[myRankIdxInCurrentAxis_], TOKEN_XN_ID, CKE_IDX_3, selfBit);
      93              :     }
      94              : 
      95            0 :     GroupWait(*transportGroup, CKE_IDX_1, allBit);
      96            0 :     GroupWait(*transportGroup, CKE_IDX_2, allBit);
      97            0 :     GroupWait(*transportGroup, CKE_IDX_3, allBit);
      98              : 
      99              :     // OneShot Step1
     100            0 :     CcuRep::Variable& Step1Offset = (axisId_ == 0) ? xSliceOffset_ : ySliceOffset_;
     101            0 :     GroupOpSize& Step1GoSize = (axisId_ == 0) ? xGoSize_ : yGoSize_;
     102              : 
     103            0 :     DoGroupReduce(inputAddr_, scratchAddr_[myRankIdxInCurrentAxis_], Step1Offset, Step1GoSize);
     104              : 
     105            0 :     DoAxisSync(0);
     106            0 :     DoGroupSync(CKE_IDX_4, selfBit, allBit);
     107            0 :     DoAxisSync(1);
     108              : 
     109              :     // OneShot Step2
     110            0 :     CcuRep::Variable& Step2Offset  = (axisId_ == 0) ? ySliceOffset_ : xSliceOffset_ ;
     111            0 :     GroupOpSize& Step2GoSize  = (axisId_ == 0) ? yGoSize_ : xGoSize_;
     112              : 
     113            0 :     DoGroupReduce(scratchAddr_, outputAddr_[myRankIdxInCurrentAxis_], Step2Offset, Step2GoSize);
     114              : 
     115            0 :     DoAxisSync(0);
     116            0 :     DoGroupSync(CKE_IDX_0, selfBit, allBit);
     117            0 :     DoAxisSync(1);
     118            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] AllReduceMesh2DOneShot end");
     119            0 :     return;
     120              : }
     121              : 
     122            0 : void CcuContextAllReduceMesh2DOneShot::DoGroupSync(int ckeIdx, uint16_t selfBit, uint16_t allBit)
     123              : {
     124            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoGroupSync Starts, ckeIdx[%d], selfBit[%u], allBit[%u]",
     125              :         ckeIdx, selfBit, allBit);
     126            0 :     for (auto t : transports) {
     127            0 :         RemotePost(*t, ckeIdx, selfBit);
     128              :     }
     129            0 :     GroupWait(*transportGroup, ckeIdx, allBit);
     130            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoGroupSync Ends");
     131            0 :     return;
     132              : }
     133              : 
     134            0 : void CcuContextAllReduceMesh2DOneShot::DoGroupReduce(std::vector<CcuRep::Variable> &srcBase,
     135              :     CcuRep::Variable &dstBase, CcuRep::Variable &offset, GroupOpSize &goSize)
     136              : {
     137            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoGroupReduce Starts");
     138              :     // 从轴上所有的对端的对应位置读取数据
     139            0 :     std::vector<CcuRep::Memory> srcAddrs;
     140            0 :     for (uint64_t rankIdx = 0; rankIdx < currentAxisRankSize_; rankIdx++) {
     141            0 :         srcAddrs.push_back(CreateMemory());
     142              :     }
     143            0 :     uint32_t rmtId = 0;
     144            0 :     uint32_t curId = 0;
     145            0 :     for (uint64_t rankIdx = 0; rankIdx < currentAxisRankSize_; rankIdx++) {
     146            0 :         if (rankIdx != myRankIdxInCurrentAxis_) {
     147            0 :             curId = rmtId;
     148            0 :             rmtId++;
     149              :         } else {
     150            0 :             curId = currentAxisRankSize_ - 1;
     151              :         }
     152            0 :         srcAddrs[curId].addr = srcBase[rankIdx];
     153            0 :         srcAddrs[curId].addr += offset;
     154            0 :         srcAddrs[curId].token = token_[rankIdx];
     155              :     }
     156              :     // Reduce 到本端
     157            0 :     CcuRep::Memory dstAddr = CreateMemory();
     158            0 :     dstAddr.addr = dstBase;
     159            0 :     dstAddr.addr += offset;
     160            0 :     dstAddr.token = token_[myRankIdxInCurrentAxis_];
     161            0 :     GroupReduce(transports, dstAddr, srcAddrs, goSize, dataType_, outputDataType_, reduceOp_);
     162            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoGroupReduce Ends");
     163            0 :     return;
     164            0 : }
     165              : 
     166            0 : void CcuContextAllReduceMesh2DOneShot::DoAxisSync(uint32_t signalIdx)
     167              : {
     168            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoAxisSync Starts, signalIdx[%u]", signalIdx);
     169            0 :     uint32_t sendBit = 1 << axisId_;
     170            0 :     uint32_t waitBit = 1 << (1 - axisId_);
     171            0 :     sendBit = sendBit << (AXIS_NUM * signalIdx);
     172            0 :     waitBit = waitBit << (AXIS_NUM * signalIdx);
     173            0 :     LocalCtxPost(otherAxisSignal_, sendBit);
     174            0 :     LocalWait(currAxisSignal_, waitBit);
     175            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] DoAxisSync Ends");
     176            0 :     return;
     177              : }
     178              : 
     179            0 : void CcuContextAllReduceMesh2DOneShot::InitVariables()
     180              : {
     181            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] InitVariables Starts");
     182              :     // 初始化资源
     183            0 :     uint16_t transportIdx = 0;
     184            0 :     if (transports.size() == 0) {
     185            0 :         THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh2DOneShot transports is empty"));
     186              :     }
     187              :     // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
     188            0 :     for (uint64_t peerId = 0; peerId < currentAxisRankSize_; peerId++) {
     189            0 :         if (peerId == myRankIdxInCurrentAxis_) {
     190            0 :             inputAddr_.push_back(CreateVariable());
     191            0 :             scratchAddr_.push_back(CreateVariable());
     192            0 :             token_.push_back(CreateVariable());
     193              :         } else {
     194            0 :             HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] MyRank[%u], PeerId[%llu], TransportId[%u]",
     195              :                 myRankIdxInCurrentAxis_, peerId, transportIdx);
     196            0 :             CHK_PRT_RET(transports[transportIdx] == nullptr || transportIdx >= transports.size(),
     197              :                     HCCL_ERROR("[CcuContextAllReduceMesh2DOneShot] Algorithm transport ptr is null or transportIdx is out of bounds"),);
     198            0 :             inputAddr_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID));
     199            0 :             scratchAddr_.push_back(CreateVariable((*transports[transportIdx]), SCRATCH_XN_ID));
     200            0 :             token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
     201            0 :             transportIdx++;
     202              :         }
     203            0 :         outputAddr_.push_back(CreateVariable());
     204              :     }
     205              : 
     206            0 :     xSliceOffset_ = CreateVariable();
     207            0 :     ySliceOffset_ = CreateVariable();
     208            0 :     xGoSize_ = CreateGroupOpSize();
     209            0 :     yGoSize_ = CreateGroupOpSize();
     210            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] InitVariables Ends");
     211            0 :     return;
     212              : }
     213              : 
     214            0 : void CcuContextAllReduceMesh2DOneShot::LoadArgs()
     215              : {
     216            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] LoadArgs Starts");
     217            0 :     Load(inputAddr_[myRankIdxInCurrentAxis_]);
     218            0 :     Load(outputAddr_[myRankIdxInCurrentAxis_]);
     219            0 :     Load(token_[myRankIdxInCurrentAxis_]);
     220            0 :     Load(scratchAddr_[myRankIdxInCurrentAxis_]);
     221            0 :     Load(xSliceOffset_);
     222            0 :     Load(ySliceOffset_);
     223            0 :     Load(xGoSize_);
     224            0 :     Load(yGoSize_);
     225            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] LoadArgs Eends");
     226            0 :     return;
     227              : }
     228              : 
     229            0 : std::vector<uint64_t> CcuContextAllReduceMesh2DOneShot::GeneArgs(const CcuTaskArg &arg)
     230              : {
     231            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] GeneArgs Starts");
     232            0 :     const CcuTaskArgAllReduceMesh2DOneShot *taskArg = dynamic_cast<const CcuTaskArgAllReduceMesh2DOneShot *>(&arg);
     233            0 :     if (taskArg == nullptr) {
     234            0 :         THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh2DOneShot::taskArg ptr is null"));
     235              :     }
     236            0 :     uint64_t tokenInfo = taskArg->token_;
     237              : 
     238            0 :     uint64_t inputAddr   = taskArg->inputAddr_;
     239            0 :     uint64_t outputAddr  = taskArg->outputAddr_;
     240            0 :     uint64_t scratchAddr = taskArg->scratchAddr_;
     241              : 
     242            0 :     uint64_t xSliceOffset = taskArg->xSliceOffset_;
     243            0 :     uint64_t ySliceOffset = taskArg->ySliceOffset_;
     244              : 
     245            0 :     auto xGoSize = CalGoSize(taskArg->xSliceSize_);
     246            0 :     auto yGoSize = CalGoSize(taskArg->ySliceSize_);
     247              : 
     248            0 :     HCCL_INFO("[CcuContextAllReduceMesh2DOneShot] GeneArgs, TaskArgs are inputAddr[%llu], "
     249              :               "outputAddr[%llu], scratchAddr[%llu], xSliceSize[%llu], ySliceSize[%llu], xSliceOffset[%llu], "
     250              :               "ySliceOffset[%llu]",
     251              :               inputAddr, outputAddr, scratchAddr, taskArg->xSliceSize_, taskArg->ySliceSize_, xSliceOffset,
     252              :               ySliceOffset);
     253              : 
     254            0 :     std::vector<uint64_t> taskArgList = {inputAddr,  outputAddr, tokenInfo, scratchAddr, xSliceOffset, ySliceOffset};
     255              :     // push goSize
     256            0 :     for (auto goSize : {xGoSize, yGoSize}) {
     257            0 :         for (auto val : goSize) {
     258            0 :             taskArgList.push_back(val);
     259              :         }
     260            0 :     }
     261            0 :     return taskArgList;
     262            0 : }
     263              : }
        

Generated by: LCOV version 2.0-1