LCOV - code coverage report
Current view: top level - legacy/ascend950/service/collective/alg/coll_alg_factory/alg_ccu_context/reduce - ccu_context_reduce_mesh2d_mem2mem.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 236 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_reduce_mesh2d_mem2mem.h"
      12              : #include "ccu_instruction_reduce_mesh2d_mem2mem.h"
      13              : 
      14              : namespace Hccl {
      15              : 
      16            0 : CcuContextReduceMeshMem2Mem2D::CcuContextReduceMeshMem2Mem2D(
      17            0 :     const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
      18            0 :     : CcuContextAlgBase(arg, transports, group)
      19              : {
      20            0 :     const CcuCtxArgReduceMeshMem2Mem2D* ctxArg = dynamic_cast<const CcuCtxArgReduceMeshMem2Mem2D*>(&arg);
      21            0 :     if (ctxArg == nullptr) {
      22            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem2D::ctxArg ptr is null"));
      23              :     }
      24            0 :     rankId_ = ctxArg->rankId_;
      25            0 :     dimSize_ = ctxArg->dimSize_;
      26            0 :     axisId_ = ctxArg->axisId_; // 要进行操作的是 行或列
      27              : 
      28            0 :     if (dimSize_.size() != 2 || axisId_ > 1 || dimSize_[0] == 0) { // 2D 拓扑校验
      29            0 :         THROW<NullPtrException>(StringFormat(
      30              :             "[CcuContextReduceMeshMem2Mem2D] dimSize[%u] or axisId[%u] or dimSize[0] [%u] is invalid", dimSize_.size(),
      31            0 :             axisId_, dimSize_[0]));
      32              :     }
      33            0 :     dimId_.emplace_back(rankId_ % dimSize_[0]);
      34            0 :     dimId_.emplace_back(rankId_ / dimSize_[0]);
      35            0 :     localId_ = dimId_[axisId_];     // 本rank所在的行/列
      36            0 :     localSize_ = dimSize_[axisId_]; // 本rank所在的行/列的总数
      37              : 
      38            0 :     HCCL_INFO(
      39              :         "[CcuContextReduceMeshMem2Mem2D] RankId[%u], DimSize0[%llu], DimSize1[%llu], localId[%llu], lcoalSize[%llu]",
      40              :         rankId_, dimSize_[0], dimSize_[1], localId_, localSize_);
      41              : 
      42            0 :     dataType_ = ctxArg->op_.dataType;
      43            0 :     outputDataType_ = ctxArg->op_.outputDataType;
      44            0 :     if (outputDataType_ == DataType::INVALID) {
      45            0 :         outputDataType_ = dataType_;
      46            0 :         HCCL_INFO(
      47              :             "[CcuContextReduceMeshMem2Mem2D] outputDataType is [INVALID], set outputDataType to[%s]",
      48              :             outputDataType_.Describe().c_str());
      49              :     }
      50            0 :     reduceOp_ = ctxArg->op_.reduceOp;
      51            0 :     rootId_ = ctxArg->rootId_;
      52            0 :     rootDimId_.emplace_back(rootId_ % dimSize_[0]); // root的x
      53            0 :     rootDimId_.emplace_back(rootId_ / dimSize_[0]); // root的y
      54            0 :     HCCL_INFO(
      55              :         "[CcuContextReduceMeshMem2Mem2D] init end, ctxArg->dimSize size[%zu] localSize_[%u]", ctxArg->dimSize_.size(),
      56              :         localSize_);
      57              : 
      58            0 :     localAxisSignalName_ = "CcuContextReduceMeshMem2Mem2DAxisSync_" + std::to_string(axisId_);
      59            0 :     anotherAxisSignalName_ = "CcuContextReduceMeshMem2Mem2DAxisSync_" + std::to_string(1 - axisId_);
      60            0 : }
      61              : 
      62            0 : void CcuContextReduceMeshMem2Mem2D::InitResources()
      63              : {
      64            0 :     localAxisSignal_ = CreateMaskSignal();
      65            0 :     anotherAxisSignal_ = CreateMaskSignal();
      66            0 :     ExportMaskSignal(localAxisSignal_, localAxisSignalName_);
      67            0 :     anotherAxisSignal_ = ImportMaskSignal(anotherAxisSignalName_);
      68              : 
      69            0 :     output_ = CreateVariable();
      70            0 :     if (transports.size() == 0) {
      71            0 :         THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem2D transports is empty"));
      72              :     }
      73            0 :     uint32_t transportIdx = 0;
      74            0 :     for (uint32_t peerId = 0; peerId < localSize_; peerId++) {
      75            0 :         if (peerId == localId_) {
      76            0 :             input_.push_back(CreateVariable());
      77            0 :             token_.push_back(CreateVariable());
      78              :         } else {
      79            0 :             HCCL_INFO(
      80              :                 "[CcuContextReduceMeshMem2Mem2D] MyRank[%u], PeerId[%u], TransportId[%u]", localId_, peerId,
      81              :                 transportIdx);
      82            0 :             CHK_PRT_RET(
      83              :                 transports[transportIdx] == nullptr,
      84              :                 HCCL_ERROR("[CcuContextReduceMeshMem2Mem2D] Algorithm transport ptr is null"), );
      85            0 :             input_.push_back(
      86            0 :                 CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
      87            0 :             token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
      88            0 :             transportIdx++;
      89              :         }
      90              :     }
      91            0 :     locMask_ = CreateMaskSignal();
      92            0 :     xAxisGroupOpSize_ = CreateGroupOpSize();
      93            0 :     yAxisGroupOpSize_ = CreateGroupOpSize();
      94            0 :     xAxisSize_ = CreateVariable();
      95            0 :     yAxisSize_ = CreateVariable();
      96            0 :     yAxisOffset_ = CreateVariable();
      97            0 :     curGoSize_ = CreateGroupOpSize();
      98            0 :     for (uint16_t roundId = 0; roundId < (localSize_ - 1); roundId++) {
      99            0 :         xChunkSize_.push_back(CreateVariable());
     100            0 :         yChunkSize_.push_back(CreateVariable());
     101            0 :         chunkSize_.push_back(CreateVariable());
     102              :     }
     103            0 :     chunkOffset_ = CreateVariable();
     104            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] InitResources finished");
     105              : }
     106              : 
     107            0 : void CcuContextReduceMeshMem2Mem2D::LoadArgs()
     108              : {
     109            0 :     Load(input_[localId_]);
     110            0 :     Load(output_);
     111            0 :     Load(token_[localId_]);
     112            0 :     Load(xAxisSize_);
     113            0 :     Load(yAxisSize_);
     114            0 :     Load(yAxisOffset_);
     115            0 :     for (uint16_t i = 0; i < (localSize_ - 1); i++) {
     116            0 :         Load(xChunkSize_[i]);
     117              :     }
     118            0 :     for (uint16_t i = 0; i < (localSize_ - 1); i++) {
     119            0 :         Load(yChunkSize_[i]);
     120              :     }
     121            0 :     Load(xAxisGroupOpSize_);
     122            0 :     Load(yAxisGroupOpSize_);
     123              :     // 只有step2会用到localcopy
     124            0 :     curGoSize_ = (axisId_ == X_AXIS_ID) ? yAxisGroupOpSize_ : xAxisGroupOpSize_;
     125            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] LoadArgs run finished");
     126            0 : }
     127              : 
     128            0 : void CcuContextReduceMeshMem2Mem2D::PreSync() // 前同步
     129              : {
     130            0 :     uint16_t selfBit = 1 << localId_;
     131            0 :     uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
     132              : 
     133            0 :     for (auto t : transports) {
     134            0 :         WriteVariableWithSignal(*t, input_[localId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
     135            0 :         WriteVariableWithSignal(*t, token_[localId_], TOKEN_XN_ID, 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("[CcuContextReduceMeshMem2Mem2D] PreSync run finished");
     140            0 : }
     141              : 
     142            0 : void CcuContextReduceMeshMem2Mem2D::PostSync(uint32_t signalIndex)
     143              : {
     144            0 :     uint16_t selfBit = 1 << localId_;
     145            0 :     uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
     146              : 
     147            0 :     for (auto t : transports) {
     148            0 :         RemotePost(*t, signalIndex, selfBit);
     149              :     }
     150            0 :     GroupWait(*transportGroup, signalIndex, allBit);
     151            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] PostSync run finished");
     152            0 : }
     153              : 
     154            0 : void CcuContextReduceMeshMem2Mem2D::AxisSync(uint32_t signalIndex) // 轴间同步
     155              : {
     156            0 :     const uint32_t DIE_NUM = 2;
     157            0 :     LocalCtxPost(anotherAxisSignal_, 1 << (axisId_ + signalIndex * DIE_NUM));
     158            0 :     LocalWait(localAxisSignal_, 1 << (1 - axisId_ + signalIndex * DIE_NUM));
     159            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] AxisSync run finished");
     160            0 :     return;
     161              : }
     162              : 
     163            0 : void CcuContextReduceMeshMem2Mem2D::ReduceStep1()
     164              : {
     165            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] RankId [%u], axisId [%u],Reduce Step1 starts", rankId_, axisId_);
     166            0 :     uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
     167            0 :     CcuRep::Memory dst = CreateMemory();
     168            0 :     CcuRep::Memory src = CreateMemory();
     169            0 :     dst.addr = input_[localId_]; // step1 reduce到input
     170            0 :     dst.token = token_[localId_];
     171            0 :     bool isYAxis = (axisId_ == Y_AXIS_ID);
     172            0 :     CcuRep::Memory tmpDst = CreateMemory();
     173            0 :     chunkSize_ = isYAxis ? yChunkSize_ : xChunkSize_;
     174            0 :     for (uint16_t i = 0; i < (localSize_ - 1); i++) { // 外层循环控制步数=chunk数量
     175              :         // 读不同rank的不同chunk
     176            0 :         for (uint16_t rmtId = 0; rmtId < localSize_; ++rmtId) {
     177            0 :             if (rmtId == localId_) {
     178            0 :                 continue;
     179              :             }
     180            0 :             src.addr = input_[rmtId];
     181            0 :             src.token = token_[rmtId];
     182            0 :             tmpDst.addr = dst.addr;
     183            0 :             tmpDst.token = dst.token;
     184            0 :             if (isYAxis) { // 第一步yslicesize要在y轴方向reduce
     185            0 :                 src.addr += yAxisOffset_;
     186            0 :                 tmpDst.addr += yAxisOffset_;
     187              :             }
     188            0 :             chunkOffset_ = 0;
     189            0 :             uint16_t chkId = 0;
     190            0 :             if (rmtId < localId_) {
     191            0 :                 chkId = (i + rmtId) % (localSize_ - 1);
     192              :             } else {
     193            0 :                 chkId = (i + rmtId - 1) % (localSize_ - 1);
     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 :             src.addr += chunkOffset_;
     201            0 :             tmpDst.addr += chunkOffset_;
     202            0 :             CCU_IF(chunkSize_[chkId] == 0) { LocalPost(locMask_, 1 << rmtId); }
     203              : 
     204            0 :             CCU_IF(chunkSize_[chkId] != 0)
     205              :             {
     206            0 :                 uint16_t transId = rmtId < localId_ ? rmtId : rmtId - 1;
     207            0 :                 ReadReduce(
     208            0 :                     *transports[transId], tmpDst, src, chunkSize_[chkId], dataType_, reduceOp_, locMask_, 1 << rmtId);
     209            0 :             }
     210              :         }
     211            0 :         LocalWait(locMask_, allBit);
     212              :     }
     213            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] Reduce Step1 ends");
     214            0 : }
     215              : 
     216            0 : void CcuContextReduceMeshMem2Mem2D::ReduceStep2()
     217              : {
     218            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] RankId [%u] Reduce Step2 starts", rankId_);
     219            0 :     uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
     220            0 :     CcuRep::Memory dst = CreateMemory();
     221            0 :     CcuRep::Memory src = CreateMemory();
     222            0 :     dst.addr = output_; // 第二步reduce是从input reduce到root的output
     223            0 :     dst.token = token_[localId_];
     224              : 
     225            0 :     src.addr = input_[localId_];
     226            0 :     src.token = token_[localId_];
     227            0 :     bool isXAxis = (axisId_ == X_AXIS_ID);
     228            0 :     chunkSize_ = isXAxis ? yChunkSize_ : xChunkSize_;
     229            0 :     if (isXAxis) // 第二步yslicesize要在x轴方向reduce
     230              :     {
     231            0 :         src.addr += yAxisOffset_;
     232            0 :         dst.addr += yAxisOffset_;
     233              :     }
     234            0 :     GroupCopy(dst, src, curGoSize_);
     235            0 :     for (uint16_t i = 0; i < (localSize_ - 1); i++) {
     236            0 :         for (uint16_t rmtId = 0; rmtId < localSize_; ++rmtId) {
     237            0 :             if (rmtId == localId_) {
     238            0 :                 continue;
     239              :             }
     240            0 :             dst.addr = output_;
     241            0 :             src.addr = input_[rmtId];
     242            0 :             src.token = token_[rmtId];
     243            0 :             if (isXAxis) {
     244            0 :                 src.addr += yAxisOffset_;
     245            0 :                 dst.addr += yAxisOffset_;
     246              :             }
     247            0 :             uint16_t chkId = 0;
     248            0 :             chunkOffset_ = 0;
     249            0 :             if (rmtId < localId_) {
     250            0 :                 chkId = (i + rmtId) % (localSize_ - 1);
     251              :             } else {
     252            0 :                 chkId = (i + rmtId - 1) % (localSize_ - 1);
     253              :             }
     254              :             // 计算一下offset 0~(chikd-1)
     255            0 :             for (uint16_t j = 0; j < chkId; ++j) {
     256            0 :                 chunkOffset_ += chunkSize_[j];
     257              :             }
     258              :             // 更新对应的addr
     259            0 :             src.addr += chunkOffset_;
     260            0 :             dst.addr += chunkOffset_;
     261            0 :             CCU_IF(chunkSize_[chkId] == 0) { LocalPost(locMask_, 1 << rmtId); }
     262            0 :             CCU_IF(chunkSize_[chkId] != 0)
     263              :             {
     264            0 :                 uint16_t transId = rmtId < localId_ ? rmtId : rmtId - 1;
     265            0 :                 ReadReduce(
     266            0 :                     *transports[transId], dst, src, chunkSize_[chkId], dataType_, reduceOp_, locMask_, 1 << rmtId);
     267            0 :             }
     268              :         }
     269            0 :         LocalWait(locMask_, allBit);
     270              :     }
     271            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] Reduce Step2 ends");
     272            0 : }
     273              : 
     274            0 : void CcuContextReduceMeshMem2Mem2D::Algorithm()
     275              : {
     276            0 :     HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] ReduceMeshMem2Mem2D run");
     277            0 :     InitResources();
     278            0 :     LoadArgs();
     279            0 :     PreSync(); // 前同步
     280            0 :     if (rankId_ == rootId_ || (dimId_[1] == rootDimId_[1] && axisId_ == Y_AXIS_ID)
     281            0 :         || (dimId_[0] == rootDimId_[0] && axisId_ == X_AXIS_ID)) {
     282              :         // 与root同行的元素要在Y方向规约 同列元素要在X方向规约
     283            0 :         ReduceStep1();
     284              :     }
     285            0 :     AxisSync(0);
     286            0 :     PostSync(CKE_IDX_3);
     287            0 :     AxisSync(1);
     288            0 :     if (rankId_ == rootId_) { // 第二步只有root进行readreduce
     289            0 :         ReduceStep2();
     290              :     }
     291            0 :     AxisSync(0);
     292            0 :     PostSync(CKE_IDX_0);
     293            0 :     AxisSync(1);
     294            0 : }
     295              : 
     296            0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem2D::CalMeshChunkSlice(uint64_t dataSize, uint64_t sliceNum)
     297              : {
     298            0 :     uint64_t dataCount = dataSize / DataTypeSizeGet(dataType_);
     299            0 :     uint64_t bigDataSliceNum = dataCount % sliceNum;
     300            0 :     uint64_t bigDataSliceSize = (dataCount / sliceNum + 1) * DataTypeSizeGet(dataType_);
     301            0 :     uint64_t smallDataSliceNum = sliceNum - dataCount % sliceNum;
     302            0 :     uint64_t smallDataSliceSize = dataCount / sliceNum * DataTypeSizeGet(dataType_);
     303            0 :     return {bigDataSliceNum, bigDataSliceSize, smallDataSliceNum, smallDataSliceSize};
     304              : }
     305              : 
     306            0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem2D::GeneArgs(const CcuTaskArg& arg)
     307              : {
     308            0 :     const CcuTaskArgReduceMeshMem2Mem2D* taskArg = dynamic_cast<const CcuTaskArgReduceMeshMem2Mem2D*>(&arg);
     309            0 :     if (taskArg == nullptr) {
     310            0 :         THROW<NullPtrException>(StringFormat("CcuTaskArgReduceMeshMem2Mem2D::taskArg ptr is null"));
     311              :     }
     312            0 :     uint64_t inputAddr = taskArg->inputAddr_;
     313            0 :     uint64_t outputAddr = taskArg->outputAddr_;
     314            0 :     uint64_t tokenInfo = taskArg->token_;
     315            0 :     uint64_t xAxisSize = taskArg->xAxisSize_;
     316            0 :     uint64_t yAxisSize = taskArg->yAxisSize_;
     317            0 :     uint64_t yAxisOffset = xAxisSize;
     318            0 :     auto xAxisGoSize = CalGoSize(xAxisSize);
     319            0 :     auto yAxisGoSize = CalGoSize(yAxisSize);
     320            0 :     std::vector<uint64_t> processReturn = {inputAddr, outputAddr, tokenInfo, xAxisSize, yAxisSize, yAxisOffset};
     321            0 :     HCCL_INFO(
     322              :         "[CcuContextReduceMeshMem2Mem2D] ReduceMeshMem2Mem2D inputAddr [%llu] outputAddr [%llu] "
     323              :         "xAxisSize [%llu] yAxisSize [%llu],yAxisOffset[%llu],",
     324              :         inputAddr, outputAddr, xAxisSize, yAxisSize, yAxisOffset);
     325              :     // mesh chunk for xslicesize
     326            0 :     std::vector<uint64_t> xChunkVec = CalMeshChunkSlice(xAxisSize, localSize_ - 1);
     327            0 :     for (uint64_t i = 0; i < xChunkVec[0]; i++) {
     328            0 :         processReturn.push_back(xChunkVec[1]);
     329              :     }
     330            0 :     for (uint64_t i = 0; i < xChunkVec[2]; i++) {
     331            0 :         processReturn.push_back(xChunkVec[3]);
     332              :     }
     333              :     // mesh chunk for yslicesize
     334            0 :     std::vector<uint64_t> yChunkVec = CalMeshChunkSlice(yAxisSize, localSize_ - 1);
     335            0 :     for (uint64_t i = 0; i < yChunkVec[0]; i++) {
     336            0 :         processReturn.push_back(yChunkVec[1]);
     337              :     }
     338            0 :     for (uint64_t i = 0; i < yChunkVec[2]; i++) {
     339            0 :         processReturn.push_back(yChunkVec[3]);
     340              :     }
     341              :     // for gosize
     342            0 :     processReturn.insert(processReturn.end(), xAxisGoSize.begin(), xAxisGoSize.end());
     343            0 :     processReturn.insert(processReturn.end(), yAxisGoSize.begin(), yAxisGoSize.end());
     344            0 :     return processReturn;
     345            0 : }
     346              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1