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

Generated by: LCOV version 2.0-1