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

Generated by: LCOV version 2.0-1