LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_ccu_context/reduce - ccu_context_reduce_mesh1d_mem2mem.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 182 0
Test Date: 2026-08-04 10:52:23 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_mem2mem.h"
      12              : #include "ccu_instruction_reduce_mesh1d_mem2mem.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  = CcuCtxArgReduceMeshMem2Mem1D;
      25              : using CurrentTaskArg = CcuTaskArgReduceMeshMem2Mem1D;
      26              : 
      27            0 : CcuContextReduceMeshMem2Mem1D::CcuContextReduceMeshMem2Mem1D(const CcuCtxArg                   &arg,
      28              :                                                              const std::vector<CcuTransport *> &transports,
      29            0 :                                                              const CcuTransportGroup           &group)
      30            0 :     : CcuContextAlgBase(arg, transports, group)
      31              : {
      32            0 :     const CurrentCtxArg *ctxArg = dynamic_cast<const CurrentCtxArg *>(&arg);
      33            0 :     if (ctxArg == nullptr) {
      34            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D::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            0 :     if (outputDataType_ == DataType::INVALID) {
      41            0 :         outputDataType_ = dataType_;
      42            0 :         HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] outputDataType is [INVALID], set outputDataType to[%s]",
      43              :                   outputDataType_.Describe().c_str());
      44              :     }
      45            0 :     if (ctxArg->dimSize_.size() > 0) {
      46            0 :         rankSize_ = ctxArg->dimSize_[0];
      47              :     }
      48            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] CtxArg: rankId[%u] rankSize[%llu]",
      49              :         rankId_, rankSize_);
      50            0 :     reduceOp_ = ctxArg->op_.reduceOp;
      51            0 :     rootId_   = ctxArg->rootId_;
      52            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] init end, ctxArg->dimSize size[%zu] rankSize[%llu]",
      53              :               ctxArg->dimSize_.size(), rankSize_);
      54            0 : }
      55              : 
      56            0 : void CcuContextReduceMeshMem2Mem1D::InitResource()
      57              : {
      58            0 :     if (transports.size() == 0) {
      59            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D transports is empty"));
      60              :     }
      61            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D]transports.size: [%zu]", transports.size());
      62              :     // 初始化资源
      63            0 :     uint16_t transportIdx = 0;
      64              :     // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
      65            0 :     for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
      66            0 :         if (peerId == rankId_) {
      67            0 :             input_.push_back(CreateVariable());
      68            0 :             output_.push_back(CreateVariable());
      69            0 :             token_.push_back(CreateVariable());
      70              :         } else {
      71            0 :             HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] MyRank[%u], PeerId[%llu], TransportId[%hu]", rankId_, peerId,
      72              :                       transportIdx);
      73              :             // 判断transport是否为空,为空直接报错
      74            0 :             CHK_PRT_RET(transports[transportIdx] == nullptr || transportIdx >= transports.size(),
      75              :                     HCCL_ERROR("[CcuContextReduceMeshMem2Mem1D] Algorithm transport ptr is null or transportIdx is out of bounds"),);
      76            0 :             input_.push_back(
      77            0 :                 CreateVariable((*transports[transportIdx]), CKE_IDX_0)); // 获取transport中id=1的Var来传递input
      78            0 :             output_.push_back(
      79            0 :                 CreateVariable((*transports[transportIdx]), CKE_IDX_1)); // 获取transport中id=2的Var来传递output
      80            0 :             token_.push_back(CreateVariable((*transports[transportIdx]), CKE_IDX_2));
      81            0 :             transportIdx++;
      82              :         }
      83              :     }
      84            0 :     for (uint16_t roundId = 0; roundId < (rankSize_ - 1); roundId++) {
      85            0 :         chunkSize_.push_back(CreateVariable());
      86              :     }
      87            0 :     inputRepeatStride_            = CreateVariable();
      88            0 :     outputRepeatStride_           = CreateVariable();
      89            0 :     normalSliceSize_ = CreateVariable();
      90            0 :     lastSliceSize_   = CreateVariable();
      91            0 :     repeatNumVar_    = CreateVariable();
      92            0 :     flag_            = CreateVariable();
      93            0 :     isInputOutputEqual_= CreateVariable();
      94            0 :     selfBit_ = 1 << rankId_;
      95            0 :     allBit_  = ((1 << rankSize_) - 1) & (~(1 << rankId_));
      96              : 
      97            0 :     srcMem_               = CreateMemory();
      98            0 :     dstMem_               = CreateMemory();
      99            0 :     locMask_              = CreateMaskSignal();
     100            0 :     localGoSize_          = CreateGroupOpSize();
     101            0 :     chunkOffset_          = CreateVariable();
     102            0 :     AllocGoResource(CCU_MS_LOCAL_COPY_LOOP_COUNT, LOCAL_COPY_MS_PER_LOOP);
     103              : }
     104              : 
     105            0 : void CcuContextReduceMeshMem2Mem1D::LoadArgs()
     106              : {
     107            0 :     Load(input_[rankId_]);
     108            0 :     Load(output_[rankId_]);
     109            0 :     Load(token_[rankId_]);
     110            0 :     Load(isInputOutputEqual_);
     111            0 :     Load(inputRepeatStride_);
     112            0 :     Load(outputRepeatStride_);
     113            0 :     Load(normalSliceSize_);
     114            0 :     Load(lastSliceSize_);
     115            0 :     Load(repeatNumVar_);
     116            0 :     for (uint16_t i = 0; i < (rankSize_ - 1); i++) {
     117            0 :         Load(chunkSize_[i]);
     118              :     }
     119            0 :     Load(localGoSize_);
     120            0 : }
     121              : 
     122            0 : void CcuContextReduceMeshMem2Mem1D::PreSync()
     123              : {
     124              :     // 互换内存信息
     125            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D LocalPost begin");
     126            0 :     for (auto t : transports) {
     127            0 :         WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit_);  // index = 1,传递input信息
     128            0 :         WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit_); // index = 0,传递output信息
     129            0 :         WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit_);  // index = 2,传递token信息
     130              :     }
     131            0 :     GroupWait(*transportGroup, CKE_IDX_1, allBit_);
     132            0 :     GroupWait(*transportGroup, CKE_IDX_2, allBit_);
     133            0 :     GroupWait(*transportGroup, CKE_IDX_3, allBit_);
     134            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D wait all end");
     135            0 : }
     136              : 
     137            0 : void CcuContextReduceMeshMem2Mem1D::PostSync()
     138              : {
     139            0 :     for (auto t : transports) {
     140            0 :         RemotePost(*t, CKE_IDX_0, selfBit_);
     141              :     }
     142            0 :     GroupWait(*transportGroup, CKE_IDX_0, allBit_);
     143            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMesh1D Reduce groupwait end");
     144            0 : }
     145              : 
     146            0 : void CcuContextReduceMeshMem2Mem1D::DoRepeatReduce(const std::vector<CcuRep::Variable> &srcAddr,
     147              :                                                                     const CcuRep::Variable &dstAddr)
     148              : {
     149              :     // 从远程设备读取数据并逐步归约到本地设备
     150            0 :     CHK_PRT_THROW(
     151              :         srcAddr.size() != transports.size() + 1,
     152              :         HCCL_ERROR("[ReadReduceRmtToLoc] srcAddr.size[%zu] != transports size[%zu] +1", srcAddr.size(), transports.size()),
     153              :         InvalidParamsException, "Invalid srcAddr size");
     154              : 
     155            0 :     dstMem_.addr  = dstAddr;
     156            0 :     dstMem_.token = token_[rankId_];
     157              : 
     158            0 :     srcMem_.addr  = srcAddr[rankId_];
     159            0 :     srcMem_.token = token_[rankId_];
     160            0 :     CCU_IF (flag_ != 0) {
     161              :         // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
     162            0 :         dstMem_.addr += outputRepeatStride_;
     163            0 :         srcMem_.addr += inputRepeatStride_;
     164            0 :     }
     165            0 :     CCU_IF (isInputOutputEqual_ == 0) {
     166            0 :         GroupCopy(dstMem_, srcMem_, localGoSize_);
     167            0 :     }
     168            0 :     for (uint16_t i = 0; i < (rankSize_ - 1); i++) { // 外层循环控制step
     169              :         // 读不同rank的不同chunk
     170            0 :         for (uint16_t rmtId = 0; rmtId < rankSize_; ++rmtId) {
     171            0 :             if (rmtId == rootId_) {
     172            0 :                 continue;
     173              :             }
     174            0 :             chunkOffset_  = 0;
     175            0 :             dstMem_.addr  = dstAddr;
     176            0 :             srcMem_.addr  = srcAddr[rmtId];
     177            0 :             srcMem_.token = token_[rmtId];
     178              : 
     179            0 :             CCU_IF (flag_ != 0) {
     180              :                 // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
     181            0 :                 dstMem_.addr += outputRepeatStride_;
     182            0 :                 srcMem_.addr += inputRepeatStride_;
     183            0 :             }
     184            0 :             uint16_t chkId = 0;
     185            0 :             if (rmtId < rankId_) {
     186            0 :                 chkId = (i + rmtId) % (rankSize_ - 1);
     187              :             } else {
     188            0 :                 chkId = (i + rmtId - 1) % (rankSize_ - 1);
     189              :             }
     190            0 :             uint16_t transId = rmtId < rootId_ ? rmtId : rmtId - 1;
     191            0 :             HCCL_DEBUG(
     192              :                 "[ReadReduceRmtToLoc] debug rankId[%llu], root[%llu] chkId[%llu], rmtId[%llu] transId[%llu]",
     193              :                 rankId_, rootId_, chkId, rmtId, transId);
     194              : 
     195              :             // 计算一下offset 0~(chikd-1)
     196            0 :             for (uint16_t j = 0; j < chkId; ++j) {
     197            0 :                 chunkOffset_ += chunkSize_[j];
     198              :             }
     199              :             // 更新对应的addr
     200            0 :             srcMem_.addr += chunkOffset_;
     201            0 :             dstMem_.addr += chunkOffset_;
     202              : 
     203            0 :             CCU_IF(chunkSize_[chkId] == 0)
     204              :             {
     205            0 :                 LocalPost(locMask_, 1 << rmtId);
     206            0 :             }
     207              : 
     208            0 :             CCU_IF(chunkSize_[chkId] != 0)
     209              :             {
     210            0 :                 ReadReduce(*transports[transId], dstMem_, srcMem_, chunkSize_[chkId], dataType_, reduceOp_, locMask_,
     211            0 :                            1 << rmtId);
     212            0 :             }
     213              :         }
     214            0 :         LocalWait(locMask_, allBit_);
     215              :     }
     216              : 
     217            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D ReadReduce end");
     218            0 : }
     219              : 
     220            0 : void CcuContextReduceMeshMem2Mem1D::Algorithm()
     221              : {
     222            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D run");
     223            0 :     InitResource();
     224            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D load input variables, id: [%u]", rankId_);
     225            0 :     LoadArgs();
     226            0 :     PreSync();
     227            0 :     CCU_IF(normalSliceSize_ != 0) // 所有rank
     228              :     {
     229            0 :         if (rankId_ == rootId_) {
     230            0 :             CcuRep::Variable repeatNumAdd = CreateVariable();
     231            0 :             repeatNumAdd  = 1;
     232            0 :             flag_ = 0;
     233            0 :             CCU_WHILE(repeatNumVar_ != UINT64_MAX) { // 循环repeatNum_次
     234              :                 // root要去读每个rank每个chunk的数据
     235            0 :                 DoRepeatReduce(input_, output_[rankId_]);
     236            0 :                 repeatNumVar_ += repeatNumAdd;
     237            0 :                 flag_ = 1;
     238            0 :             }
     239            0 :         }
     240            0 :     }
     241            0 :     PostSync();
     242            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D end");
     243            0 :     return;
     244              : }
     245              : 
     246              : 
     247            0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem1D::GeneArgs(const CcuTaskArg &arg)
     248              : {
     249            0 :     const CcuTaskArgReduceMeshMem2Mem1D *taskArg = dynamic_cast<const CcuTaskArgReduceMeshMem2Mem1D *>(&arg);
     250            0 :     if (taskArg == nullptr) {
     251            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D::taskArg ptr is null"));
     252              :     }
     253            0 :     uint64_t inputAddr  = taskArg->inputAddr_;
     254            0 :     uint64_t outputAddr = taskArg->outputAddr_;
     255            0 :     uint64_t tokenInfo  = taskArg->token_;
     256              : 
     257            0 :     uint64_t bigDataSliceNum    = taskArg->bigDataSliceNum_;
     258            0 :     uint64_t bigDataSliceSize   = taskArg->bigDataSliceSize_;
     259            0 :     uint64_t smallDataSliceNum  = taskArg->smallDataSliceNum_;
     260            0 :     uint64_t smallDataSliceSize = taskArg->smallDataSliceSize_;
     261            0 :     uint64_t inputRepeatStride            = taskArg->inputRepeatStride_;
     262            0 :     uint64_t outputRepeatStride           = taskArg->outputRepeatStride_;
     263            0 :     uint64_t normalSliceSize              = taskArg->normalSliceSize_;
     264            0 :     uint64_t lastSliceSize                = taskArg->lastSliceSize_;
     265            0 :     uint64_t repeatNumVar                 = taskArg->repeatNumVar_;
     266            0 :     uint64_t isInputOutputEqual = (inputAddr == outputAddr) ? 1: 0;
     267              :     std::vector<uint64_t> taskArgs = {
     268              :         inputAddr,
     269              :         outputAddr,
     270              :         tokenInfo,
     271              :         isInputOutputEqual,
     272              :         inputRepeatStride,
     273              :         outputRepeatStride,
     274              :         normalSliceSize,
     275              :         lastSliceSize,
     276              :         repeatNumVar,
     277            0 :     };
     278            0 :     for (uint64_t i = 0; i < bigDataSliceNum; i++) {
     279            0 :         taskArgs.push_back(bigDataSliceSize);
     280              :     }
     281            0 :     for (uint64_t i = 0; i < smallDataSliceNum; i++) {
     282            0 :         taskArgs.push_back(smallDataSliceSize);
     283              :     }
     284              : 
     285            0 :     auto localGoSize = CalGoSize(normalSliceSize);
     286            0 :     taskArgs.push_back(localGoSize[0]);
     287            0 :     taskArgs.push_back(localGoSize[1]);
     288            0 :     taskArgs.push_back(localGoSize[2]);
     289            0 :     taskArgs.push_back(localGoSize[3]);
     290            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] TaskArgs: inputAddr[%llu], outputAddr[%llu], inputRepeatStride[%llu], "
     291              :         "outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu], repeatNumVar[%llu], "
     292              :         "bigDataSliceNum[%llu], bigDataSliceSize[%llu], smallDataSliceNum[%llu], smallDataSliceSize[%llu], ",
     293              :         inputAddr, outputAddr, inputRepeatStride, outputRepeatStride, normalSliceSize, lastSliceSize, repeatNumVar,
     294              :         bigDataSliceNum, bigDataSliceSize, smallDataSliceNum, smallDataSliceSize);
     295            0 :     return taskArgs;
     296            0 : }
     297              : 
     298              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1