LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_ccu_context/broadcast - ccu_context_broadcast_mesh1d_mem2mem.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 193 0
Test Date: 2026-08-18 17:47:01 Functions: 0.0 % 11 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_broadcast_mesh1d_mem2mem.h"
      12              : #include "ccu_instruction_broadcast_mesh1d_mem2mem.h"
      13              : 
      14              : namespace Hccl {
      15              : constexpr int INPUT_XN_ID = 0;
      16              : constexpr int OUTPUT_XN_ID = 1;
      17              : constexpr int TOKEN_XN_ID = 2;
      18              : constexpr int CKE_IDX_0 = 0;
      19              : constexpr int CKE_IDX_1 = 1;
      20              : constexpr int CKE_IDX_2 = 2;
      21              : constexpr int CKE_IDX_3 = 3;
      22              : constexpr int CKE_IDX_4 = 4;
      23              : 
      24              : using CurrentCtxArg = CcuCtxArgBroadcastMesh1DMem2Mem;
      25              : using CurrentTaskArg = CcuTaskArgBroadcastMesh1DMem2Mem;
      26              : 
      27            0 : CcuContextBroadcastMesh1DMem2Mem::CcuContextBroadcastMesh1DMem2Mem(
      28            0 :     const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
      29            0 :     : CcuContextAlgBase(arg, transports, group)
      30              : {
      31            0 :     HCCL_DEBUG("[CcuContextBroadcastMesh1DMem2Mem] Enter Constructor");
      32            0 :     const CurrentCtxArg* ctxArg = dynamic_cast<const CurrentCtxArg*>(&arg);
      33            0 :     if (ctxArg == nullptr) {
      34            0 :         THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::ctxArg ptr is null"));
      35              :     }
      36            0 :     if (ctxArg->dimSize_.size() != 1) {
      37            0 :         THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::dimSize is not 1"));
      38              :     }
      39            0 :     rankId_ = ctxArg->rankId_;
      40            0 :     rootId_ = ctxArg->rootId_;
      41            0 :     rankSize_ = ctxArg->dimSize_[0];
      42            0 :     dataType_ = ctxArg->op_.dataType;
      43            0 :     outputDataType_ = ctxArg->op_.outputDataType;
      44              : 
      45            0 :     if (outputDataType_ == DataType::INVALID) {
      46            0 :         outputDataType_ = dataType_;
      47            0 :         HCCL_INFO(
      48              :             "[CcuContextBroadcastMesh1DMem2Mem] outputDataType is [INVALID], set outputDataType to[%s]",
      49              :             outputDataType_.Describe().c_str());
      50              :     }
      51              : 
      52            0 :     HCCL_INFO(
      53              :         "[CcuContextBroadcastMesh1DMem2Mem] init end, ctxArg->dimSize size[%zu] rankSize[%llu].",
      54              :         ctxArg->dimSize_.size(), rankSize_);
      55            0 : }
      56              : 
      57            0 : void CcuContextBroadcastMesh1DMem2Mem::InitResource()
      58              : {
      59            0 :     if (transports.size() == 0) {
      60            0 :         THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem transports is empty"));
      61              :     }
      62            0 :     HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem]transports.size: [%zu]", transports.size());
      63            0 :     uint16_t transportIdx = 0;
      64              :     // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
      65            0 :     for (uint16_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_DEBUG(
      72              :                 "[CcuContextBroadcastMesh1DMem2Mem] MyRank[%u], PeerId[%hu], TransportId[%hu]", rankId_, peerId,
      73              :                 transportIdx);
      74              :             // 判断transport是否为空,为空直接报错
      75            0 :             CHK_PRT_THROW(
      76              :                 transports[transportIdx] == nullptr || transportIdx >= transports.size(),
      77              :                 HCCL_ERROR(
      78              :                     "[CcuContextBroadcastMesh1DMem2Mem] [InitResource] transports[%u] is nullptr or out of bounds",
      79              :                     transportIdx),
      80              :                 NullPtrException, "transport is null");
      81            0 :             input_.push_back(
      82            0 :                 CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递input
      83            0 :             output_.push_back(
      84            0 :                 CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID)); // 获取transport中id=2的Var来传递output
      85            0 :             token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
      86            0 :             transportIdx++; // transport对应远端通信数
      87              :         }
      88              :     }
      89            0 :     currentRankSliceInputOffset_ = CreateVariable();
      90            0 :     currentRankSliceOutputOffset_ = CreateVariable();
      91            0 :     inputRepeatStride_ = CreateVariable();
      92            0 :     outputRepeatStride_ = CreateVariable();
      93            0 :     normalSliceSize_ = CreateVariable();
      94            0 :     lastSliceSize_ = CreateVariable();
      95            0 :     allgatherOffset_ = CreateVariable();
      96            0 :     repeatNumVar_ = CreateVariable();
      97            0 :     flag_ = CreateVariable();
      98            0 :     SliceOffset_ = CreateVariable();
      99              : 
     100            0 :     selfBit_ = 1 << rankId_;
     101            0 :     allBit_ = ((1 << rankSize_) - 1) & (~(1 << rankId_));
     102              : 
     103            0 :     scattersrcMem_.reserve(rankSize_);
     104            0 :     scatterdstMem_.reserve(rankSize_);
     105            0 :     allgatherdstMem_.reserve(rankSize_);
     106            0 :     for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     107            0 :         scattersrcMem_.push_back(CreateMemory());
     108            0 :         scatterdstMem_.push_back(CreateMemory());
     109            0 :         allgatherdstMem_.push_back(CreateMemory());
     110              :     }
     111            0 :     localSignal_ = CreateMaskSignal();
     112            0 :     return;
     113              : }
     114              : 
     115            0 : void CcuContextBroadcastMesh1DMem2Mem::LoadArgs()
     116              : {
     117            0 :     Load(input_[rankId_]);
     118            0 :     Load(output_[rankId_]);
     119            0 :     Load(token_[rankId_]);
     120            0 :     Load(currentRankSliceInputOffset_);
     121            0 :     Load(currentRankSliceOutputOffset_);
     122            0 :     Load(inputRepeatStride_);
     123            0 :     Load(outputRepeatStride_);
     124            0 :     Load(normalSliceSize_);
     125            0 :     Load(lastSliceSize_);
     126            0 :     Load(allgatherOffset_);
     127            0 :     Load(repeatNumVar_);
     128            0 :     return;
     129              : }
     130              : 
     131            0 : void CcuContextBroadcastMesh1DMem2Mem::PreSync() // 前同步
     132              : {
     133            0 :     for (auto t : transports) {
     134            0 :         HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D LocalPost begin");
     135            0 :         WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递input信息
     136            0 :         WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit_); // index = 0,传递output信息
     137            0 :         WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit_); // index = 2,传递token信息
     138              :     }
     139            0 :     GroupWait(*transportGroup, CKE_IDX_1, allBit_);
     140            0 :     GroupWait(*transportGroup, CKE_IDX_2, allBit_);
     141            0 :     GroupWait(*transportGroup, CKE_IDX_3, allBit_);
     142            0 :     HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D wait all end");
     143            0 :     return;
     144              : }
     145              : 
     146            0 : void CcuContextBroadcastMesh1DMem2Mem::PostSync(int CKE_id) // 后同步
     147              : {
     148            0 :     for (auto t : transports) {
     149            0 :         RemotePost(*t, CKE_id, selfBit_);
     150              :     }
     151            0 :     GroupWait(*transportGroup, CKE_id, allBit_);
     152            0 :     HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D groupwait end");
     153            0 : }
     154              : 
     155            0 : void CcuContextBroadcastMesh1DMem2Mem::DoRepeaScatterMem2Mem()
     156              : {
     157            0 :     if (rankId_ != rootId_) {
     158            0 :         return;
     159              :     }
     160            0 :     std::vector<CcuRep::Memory>& src = scattersrcMem_;
     161            0 :     std::vector<CcuRep::Memory>& dst = scatterdstMem_;
     162              : 
     163            0 :     for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     164            0 :         if (rankIdx == 0) {
     165            0 :             SliceOffset_ = 0;
     166              :         } else {
     167            0 :             SliceOffset_ += normalSliceSize_;
     168              :         }
     169              : 
     170            0 :         src[rankIdx].addr = input_[rankId_];
     171            0 :         src[rankIdx].addr += currentRankSliceInputOffset_;
     172            0 :         src[rankIdx].addr += SliceOffset_;
     173            0 :         src[rankIdx].token = token_[rankIdx];
     174              : 
     175            0 :         dst[rankIdx].addr = output_[rankIdx];
     176            0 :         dst[rankIdx].addr += currentRankSliceOutputOffset_;
     177            0 :         dst[rankIdx].addr += SliceOffset_;
     178            0 :         dst[rankIdx].token = token_[rankIdx];
     179              : 
     180            0 :         CCU_IF(flag_ != 0)
     181              :         {
     182              :             // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
     183            0 :             for (uint32_t curId = 0; curId < rankSize_; curId++) {
     184            0 :                 src[curId].addr += inputRepeatStride_;
     185            0 :                 dst[curId].addr += outputRepeatStride_;
     186              :             }
     187            0 :         }
     188              :     }
     189            0 :     DoScatter(src, dst);
     190              : }
     191            0 : void CcuContextBroadcastMesh1DMem2Mem::DoRepeatAllGatherMem2Mem()
     192              : {
     193            0 :     CcuRep::Memory& src = scatterdstMem_[rankId_];
     194            0 :     std::vector<CcuRep::Memory>& dst = allgatherdstMem_;
     195            0 :     src.addr = output_[rankId_];
     196            0 :     src.addr += currentRankSliceOutputOffset_;
     197            0 :     src.addr += allgatherOffset_;
     198            0 :     src.token = token_[rankId_];
     199              : 
     200            0 :     for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     201            0 :         dst[rankIdx].addr = output_[rankIdx];
     202            0 :         dst[rankIdx].addr += currentRankSliceOutputOffset_;
     203            0 :         dst[rankIdx].addr += allgatherOffset_;
     204            0 :         dst[rankIdx].token = token_[rankIdx];
     205              :     }
     206            0 :     CCU_IF(flag_ != 0)
     207              :     {
     208              :         //  非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
     209            0 :         src.addr += inputRepeatStride_;
     210            0 :         for (auto& d : dst) {
     211            0 :             d.addr += outputRepeatStride_;
     212              :         }
     213            0 :     }
     214              : 
     215            0 :     DoAllGather(src, dst);
     216            0 : }
     217              : 
     218            0 : void CcuContextBroadcastMesh1DMem2Mem::DoScatter(
     219              :     const std::vector<CcuRep::Memory>& src, const std::vector<CcuRep::Memory>& dst)
     220              : {
     221            0 :     uint64_t transportId = 0;
     222            0 :     for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     223            0 :         auto& sliceSize = (rankIdx + 1 == rankSize_) ? lastSliceSize_ : normalSliceSize_;
     224            0 :         CCU_IF(sliceSize != 0)
     225              :         {
     226            0 :             if (rankIdx == rankId_) {
     227            0 :                 LocalPost(localSignal_, 1 << rankIdx);
     228              :             } else {
     229            0 :                 Write(*transports[transportId], dst[rankIdx], src[rankIdx], sliceSize, localSignal_, 1 << rankIdx);
     230            0 :                 transportId++;
     231              :             }
     232            0 :         }
     233            0 :         CCU_IF(sliceSize == 0) { LocalPost(localSignal_, 1 << rankIdx); }
     234              :     }
     235              : 
     236            0 :     LocalWait(localSignal_, (1 << rankSize_) - 1);
     237            0 : }
     238              : 
     239            0 : void CcuContextBroadcastMesh1DMem2Mem::DoAllGather(const CcuRep::Memory& src, const std::vector<CcuRep::Memory>& dst)
     240              : {
     241            0 :     uint64_t transportId = 0;
     242            0 :     auto& sliceSize = (rankId_ + 1 == rankSize_) ? lastSliceSize_ : normalSliceSize_;
     243            0 :     CCU_IF(sliceSize != 0)
     244              :     {
     245            0 :         for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
     246            0 :             if (rankIdx == rankId_) {
     247            0 :                 LocalPost(localSignal_, (1 << rankIdx));
     248              :             } else {
     249            0 :                 Write(*transports[transportId], dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
     250            0 :                 transportId++;
     251              :             }
     252              :         }
     253            0 :         LocalWait(localSignal_, (1 << rankSize_) - 1);
     254            0 :     }
     255            0 : }
     256              : 
     257            0 : void CcuContextBroadcastMesh1DMem2Mem::Algorithm()
     258              : {
     259            0 :     HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D run");
     260            0 :     InitResource();
     261            0 :     LoadArgs();
     262            0 :     PreSync();
     263              : 
     264            0 :     CcuRep::Variable repeatNumAdd = CreateVariable();
     265            0 :     repeatNumAdd = 1;
     266              : 
     267            0 :     flag_ = 0;
     268            0 :     CCU_WHILE(repeatNumVar_ != UINT64_MAX)
     269              :     { // 循环repeatNum_次
     270            0 :         DoRepeaScatterMem2Mem();
     271            0 :         PostSync(CKE_IDX_4);
     272            0 :         DoRepeatAllGatherMem2Mem();
     273            0 :         PostSync(CKE_IDX_0);
     274            0 :         repeatNumVar_ += repeatNumAdd;
     275            0 :         flag_ = 1;
     276            0 :     }
     277              : 
     278            0 :     HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D end");
     279            0 :     return;
     280            0 : }
     281              : 
     282            0 : std::vector<uint64_t> CcuContextBroadcastMesh1DMem2Mem::GeneArgs(const CcuTaskArg& arg)
     283              : {
     284            0 :     const CurrentTaskArg* taskArg = dynamic_cast<const CurrentTaskArg*>(&arg);
     285              :     // 空指针校验
     286            0 :     if (taskArg == nullptr) {
     287            0 :         THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::taskArg ptr is null"));
     288              :     }
     289            0 :     uint64_t inputAddr = taskArg->inputAddr_;
     290            0 :     uint64_t outputAddr = taskArg->outputAddr_;
     291            0 :     uint64_t tokenInfo = taskArg->token_;
     292              : 
     293            0 :     uint64_t currentRankSliceInputOffset = taskArg->inputSliceStride_ * rankId_;
     294            0 :     uint64_t currentRankSliceOutputOffset = taskArg->outputSliceStride_ * rankId_;
     295            0 :     uint64_t inputRepeatStride = taskArg->inputRepeatStride_;
     296            0 :     uint64_t outputRepeatStride = taskArg->outputRepeatStride_;
     297            0 :     uint64_t normalSliceSize = taskArg->normalSliceSize_;
     298            0 :     uint64_t lastSliceSize = taskArg->lastSliceSize_;
     299            0 :     uint64_t allgatherOffset = taskArg->normalSliceSize_ * rankId_;
     300            0 :     uint64_t repeatNumVar = taskArg->repeatNumVar_;
     301              :     std::vector<uint64_t> taskArgs = {
     302              :         inputAddr,
     303              :         outputAddr,
     304              :         tokenInfo,
     305              :         currentRankSliceInputOffset,
     306              :         currentRankSliceOutputOffset,
     307              :         inputRepeatStride,
     308              :         outputRepeatStride,
     309              :         normalSliceSize,
     310              :         lastSliceSize,
     311              :         allgatherOffset,
     312              :         repeatNumVar,
     313            0 :     };
     314              : 
     315            0 :     HCCL_INFO(
     316              :         "[CcuContextBroadcastMesh1DMem2Mem] TaskArgs: inputAddr[%llx], outputAddr[%llx], "
     317              :         "currentRankSliceInputOffset[%llu], "
     318              :         "currentRankSliceOutputOffset[%llu], inputRepeatStride[%llu], outputRepeatStride[%llu], "
     319              :         "normalSliceSize[%llu], lastSliceSize[%llu], allgatherSliceSize[%llu], repeatNumVar[%llu] ",
     320              :         inputAddr, outputAddr, currentRankSliceInputOffset, currentRankSliceOutputOffset, inputRepeatStride,
     321              :         outputRepeatStride, normalSliceSize, lastSliceSize, allgatherOffset, repeatNumVar);
     322              : 
     323            0 :     return taskArgs;
     324            0 : }
     325              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1