LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_ccu_context/reduce - ccu_context_reduce_mesh1d.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 149 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_reduce_mesh1d.h"
      12              : #include "ccu_instruction_reduce_mesh1d.h"
      13              : 
      14              : namespace Hccl {
      15              : 
      16              : constexpr int INPUT_XN_ID  = 0;
      17              : constexpr int OUTPUT_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              : 
      24              : using CurrentCtxArg  = CcuCtxArgReduceMesh1D;
      25              : using CurrentTaskArg = CcuTaskArgReduceMesh1D;
      26              : 
      27            0 : CcuContextReduceMesh1D::CcuContextReduceMesh1D(const CcuCtxArg &arg, const std::vector<CcuTransport *> &transports,
      28            0 :                                                             const CcuTransportGroup &group)
      29            0 :     : CcuContextAlgBase(arg, transports, group)
      30              : {
      31            0 :     HCCL_DEBUG("[CcuContextReduceMesh1D] Enter Constructor");
      32            0 :     const CurrentCtxArg *ctxArg = dynamic_cast<const CurrentCtxArg *>(&arg);
      33            0 :     if (ctxArg == nullptr) {
      34            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMesh1D::ctxArg ptr is null"));
      35              :     }
      36            0 :     rankId_                     = ctxArg->rankId_;
      37            0 :     rankSize_                   = ctxArg->dimSize_[0];
      38            0 :     dataType_                   = ctxArg->op_.dataType;
      39            0 :     outputDataType_             = ctxArg->op_.outputDataType;
      40              :     
      41            0 :     if (outputDataType_ == DataType::INVALID) {
      42            0 :         outputDataType_ = dataType_;
      43            0 :         HCCL_INFO("[CcuContextReduceMesh1D] outputDataType is [INVALID], set outputDataType to[%s]",
      44              :             outputDataType_.Describe().c_str());
      45              :     }
      46              : 
      47            0 :     if (ctxArg->dimSize_.size() > 0) {
      48            0 :         rankSize_ = ctxArg->dimSize_[0];
      49              :     }
      50              : 
      51            0 :     HCCL_INFO("[CcuContextReduceMesh1D] CtxArg: rankId[%u] rankSize[%u]",
      52              :         rankId_, rankSize_);
      53              : 
      54            0 :     reduceOp_ = ctxArg->op_.reduceOp;
      55            0 :     rootId_ = ctxArg->rootId_;
      56            0 :     HCCL_INFO("[CcuContextReduceMesh1D] init end, ctxArg->dimSize size[%u] rankSize[%llu]", ctxArg->dimSize_.size(), rankSize_);
      57            0 : }
      58              : 
      59            0 : void CcuContextReduceMesh1D::InitResource()
      60              : {
      61            0 :     if (transports.size() == 0) {
      62            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMesh1D transports is empty"));
      63              :     }
      64            0 :     HCCL_INFO("[CcuContextReduceMesh1D]transports.size: [%u]", transports.size());
      65            0 :     uint16_t transportIdx = 0;
      66              :     // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
      67            0 :     for (uint16_t peerId = 0; peerId < rankSize_; peerId++) {
      68            0 :         if (peerId == rankId_) {
      69            0 :             input_.push_back(CreateVariable());
      70            0 :             output_.push_back(CreateVariable());
      71            0 :             token_.push_back(CreateVariable());
      72              :         } else {
      73            0 :             HCCL_DEBUG("[CcuContextReduceMesh1D] MyRank[%u], PeerId[%hu], TransportId[%hu]",
      74              :                 rankId_, peerId, transportIdx);
      75              :             // 判断transport是否为空,为空直接报错
      76            0 :             CHK_PRT_THROW(transports[transportIdx] == nullptr,
      77              :                 HCCL_ERROR("[CcuContextReduceMesh1D] [InitResource] transports[%u] is nullptr", transportIdx), 
      78              :                 NullPtrException, "transport is null");
      79            0 :             input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID));  // 获取transport中id=1的Var来传递input
      80            0 :             output_.push_back(CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID));  // 获取transport中id=2的Var来传递output
      81            0 :             token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
      82            0 :             transportIdx++;
      83              :         }
      84              :     }
      85              : 
      86            0 :     groupOpSize_ = CreateGroupOpSize();
      87              : 
      88            0 :     currentRankSliceInputOffset_  = CreateVariable();
      89            0 :     currentRankSliceOutputOffset_ = CreateVariable();
      90            0 :     repeatNum_                    = CreateVariable();
      91            0 :     inputRepeatStride_            = CreateVariable();
      92            0 :     outputRepeatStride_           = CreateVariable();
      93              : 
      94            0 :     normalSliceSize_ = CreateVariable();
      95            0 :     lastSliceSize_   = CreateVariable();
      96            0 :     repeatNumVar_    = CreateVariable();
      97            0 :     flag_            = CreateVariable();
      98              : 
      99            0 :     selfBit_ = 1 << rankId_;
     100            0 :     allBit_  = ((1 << rankSize_) - 1) & (~(1 << rankId_));
     101              : 
     102            0 :     localMem_ = CreateMemory();
     103            0 :     reomteMem_.reserve(rankSize_);
     104            0 :     for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     105            0 :         reomteMem_.push_back(CreateMemory());
     106              :     }
     107              : 
     108            0 :     localSignal_ = CreateMaskSignal();
     109            0 :     return;
     110              : }
     111              : 
     112            0 : void CcuContextReduceMesh1D::LoadArgs()
     113              : {
     114            0 :     Load(input_[rankId_]);
     115            0 :     Load(output_[rankId_]);
     116            0 :     Load(token_[rankId_]);
     117            0 :     Load(currentRankSliceInputOffset_);
     118            0 :     Load(currentRankSliceOutputOffset_);
     119            0 :     Load(repeatNum_);
     120            0 :     Load(inputRepeatStride_);
     121            0 :     Load(outputRepeatStride_);
     122            0 :     Load(normalSliceSize_);
     123            0 :     Load(lastSliceSize_);
     124            0 :     Load(repeatNumVar_);
     125            0 :     Load(groupOpSize_);
     126            0 :     return;
     127              : }
     128              : 
     129            0 : void CcuContextReduceMesh1D::PreSync()
     130              : {
     131            0 :     for (auto t : transports) {
     132            0 :         HCCL_INFO("[CcuContextReduceMesh1D] ReduceMesh1D LocalPost begin");
     133            0 :         WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递input信息
     134            0 :         WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit_); // index = 0,传递output信息
     135            0 :         WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit_);  // index = 2,传递token信息
     136              :     }
     137            0 :     GroupWait(*transportGroup, CKE_IDX_1, allBit_);
     138            0 :     GroupWait(*transportGroup, CKE_IDX_2, allBit_);
     139            0 :     GroupWait(*transportGroup, CKE_IDX_3, allBit_);
     140            0 :     HCCL_INFO("[CcuContextReduceMesh1D] ReduceMesh1D wait all end");
     141            0 :     return;
     142              : }
     143              : 
     144            0 : void CcuContextReduceMesh1D::PostSync()
     145              : {
     146            0 :     for (auto t : transports) {
     147            0 :         RemotePost(*t, CKE_IDX_0, selfBit_);
     148              :     }
     149            0 :     GroupWait(*transportGroup, CKE_IDX_0, allBit_);
     150            0 :     HCCL_INFO("[CcuContextReduceMesh1D] ReduceMesh1D Reduce groupwait end");
     151            0 : }
     152              : 
     153            0 : void CcuContextReduceMesh1D::DoRepeatReduce()
     154              : {
     155            0 :     std::vector<CcuRep::Memory> &src = reomteMem_;
     156            0 :     CcuRep::Memory &dst = localMem_;
     157              :     
     158            0 :     dst.addr = output_[rankId_];
     159            0 :     dst.token = token_[rankId_];
     160            0 :     uint32_t curId = 0;
     161            0 :     for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     162            0 :         if (rankIdx != rootId_) {
     163            0 :             src[curId].addr  = input_[rankIdx];
     164            0 :             src[curId].token = token_[rankIdx];
     165            0 :             curId++;
     166              :         } else {
     167            0 :             continue;
     168              :         }
     169              :     }
     170            0 :     src[rankSize_ - 1].addr = input_[rankId_];
     171            0 :     src[rankSize_ - 1].token = token_[rankId_];
     172              : 
     173            0 :     CCU_IF (flag_ != 0) {
     174              :         // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
     175            0 :         dst.addr += outputRepeatStride_;
     176            0 :         for (auto &s : src) {
     177            0 :             s.addr += inputRepeatStride_;
     178              :         }
     179            0 :     }
     180            0 :     GroupReduce(transports, dst, src, groupOpSize_, dataType_, outputDataType_, reduceOp_);
     181            0 : }
     182              : 
     183            0 : void CcuContextReduceMesh1D::Algorithm()
     184              : {
     185            0 :     HCCL_INFO("[CcuContextReduceMesh1D] ReduceMesh1D run");
     186            0 :     InitResource();
     187            0 :     LoadArgs();
     188            0 :     PreSync();
     189            0 :     if (rankId_ == rootId_) {
     190            0 :         CcuRep::Variable repeatNumAdd = CreateVariable();
     191            0 :         repeatNumAdd  = 1;
     192            0 :         flag_ = 0;
     193            0 :         CCU_WHILE(repeatNumVar_ != UINT64_MAX) { // 循环repeatNum_次
     194            0 :             DoRepeatReduce();
     195            0 :             repeatNumVar_ += repeatNumAdd;
     196            0 :             flag_ = 1;
     197            0 :         }
     198            0 :     }
     199            0 :     PostSync();
     200            0 :     HCCL_INFO("[CcuContextReduceMesh1D] ReduceMesh1D end");
     201            0 :     return;
     202              : }
     203              : 
     204            0 : std::vector<uint64_t> CcuContextReduceMesh1D::GeneArgs(const CcuTaskArg &arg)
     205              : {
     206            0 :     const CurrentTaskArg *taskArg    = dynamic_cast<const CurrentTaskArg *>(&arg);
     207              :     // 空指针校验
     208            0 :     if (taskArg == nullptr) {
     209            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMesh1D::taskArg ptr is null"));
     210              :     }
     211            0 :     uint64_t currentRankSliceInputOffset  = taskArg->inputSliceStride_ * rankId_;
     212            0 :     uint64_t currentRankSliceOutputOffset = taskArg->outputSliceStride_ * rankId_;
     213            0 :     uint64_t repeatNum                    = taskArg->repeatNum_;
     214            0 :     uint64_t inputRepeatStride            = taskArg->inputRepeatStride_;
     215            0 :     uint64_t outputRepeatStride           = taskArg->outputRepeatStride_;
     216            0 :     uint64_t normalSliceSize              = taskArg->normalSliceSize_;
     217            0 :     uint64_t lastSliceSize                = taskArg->lastSliceSize_;
     218            0 :     uint64_t repeatNumVar                 = taskArg->repeatNumVar_;
     219            0 :     uint64_t inputAddr  = taskArg->inputAddr_;
     220            0 :     uint64_t outputAddr = taskArg->outputAddr_;
     221            0 :     uint64_t tokenInfo  = taskArg->token_;
     222              : 
     223            0 :     auto     goSize     = CalGoSize(normalSliceSize);
     224              : 
     225              :     std::vector<uint64_t> taskArgs = {
     226              :         inputAddr,
     227              :         outputAddr,
     228              :         tokenInfo,
     229              :         currentRankSliceInputOffset,
     230              :         currentRankSliceOutputOffset,
     231              :         repeatNum,
     232              :         inputRepeatStride,
     233              :         outputRepeatStride,
     234              :         normalSliceSize,
     235              :         lastSliceSize,
     236              :         repeatNumVar,
     237            0 :         goSize[0],
     238            0 :         goSize[1],
     239            0 :         goSize[2],
     240            0 :         goSize[3],
     241            0 :     };
     242              : 
     243            0 :     HCCL_INFO("[CcuContextReduceMesh1D] TaskArgs: inputAddr[%llu], outputAddr[%llu], currentRankSliceInputOffset[%llu], "
     244              :         "currentRankSliceOutputOffset[%llu], repeatNum[%llu], inputRepeatStride[%llu], outputRepeatStride[%llu], "
     245              :         "normalSliceSize[%llu], lastSliceSize[%llu], repeatNumVar[%llu], goSize[0][%llu], goSize[1][%llu], goSize[2][%llu], goSize[3][%llu], ",
     246              :         inputAddr, outputAddr, currentRankSliceInputOffset, currentRankSliceOutputOffset, repeatNum, inputRepeatStride, 
     247              :         outputRepeatStride, normalSliceSize, lastSliceSize, repeatNumVar, goSize[0], goSize[1], goSize[2], goSize[3]);
     248              : 
     249            0 :     return taskArgs;
     250            0 : }
     251              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1