LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/aiv/aiv_mc2 - aiv_mc2_compont.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 71.8 % 156 112
Test Date: 2026-07-28 12:11:00 Functions: 90.0 % 10 9

            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 "aiv_mc2_compont.h"
      12              : #include "hccl_aiv_utils.h"
      13              : #include <algorithm>
      14              : #include <unordered_set>
      15              : #include "coll_service_device_mode.h"
      16              : #include "aiv_ins.h"
      17              : 
      18              : namespace Hccl {
      19              : 
      20              : constexpr u32 AIV_FAKE_OP_TPYE = 1;
      21              : 
      22          242 : AivMc2Compont::~AivMc2Compont()
      23              : {
      24          242 : }
      25              : 
      26            6 : void AivMc2Compont::AllocCommResource(void *mc2Tiling, void **commContext)
      27              : {
      28            6 :     if (mc2Tiling == nullptr) {
      29            3 :         HCCL_ERROR("[AivMc2Compont::%s] mc2Tiling is nullptr", __func__);
      30            1 :         return;
      31              :     }
      32            5 :     auto tilingVersion = *static_cast<uint32_t *>(mc2Tiling);
      33           15 :     HCCL_INFO("[AivMc2Compont:%s] Tiling version [%u]", __func__, tilingVersion);
      34            5 :     if (tilingVersion != UNKNOWN_TILING_V1 && tilingVersion != UNKNOWN_TILING_V2) {
      35            4 :         THROW<NotSupportException>(StringFormat("Tiling version not support, version[%u]", tilingVersion));
      36              :     }
      37              : 
      38            3 :     if (comm->GetRankSize() == 1) {
      39            1 :         THROW<NotSupportException>(StringFormat("Comm[%s] rank size is 1, Mc2 not support", comm->GetId().c_str()));
      40              :     }
      41              : 
      42            2 :     if (tilingVersion == UNKNOWN_TILING_V1) {
      43            1 :         AivMC2AllocCommRes(reinterpret_cast<Mc2Tiling *>(mc2Tiling));
      44              :     } else {
      45            1 :         AivMC2AllocCommResV2(reinterpret_cast<Mc2InitTilingInner *>(mc2Tiling));
      46              :     }
      47              :     
      48            2 :     GenerateCommContext(commContext);
      49              : }
      50              : 
      51            3 : void AddCclBuffer(HcclCombinOpParam &combinOpParam, uint64_t bufferSize, uint64_t bufferAddr, RankId rankId)
      52              : {
      53            3 :     combinOpParam.winSize            = bufferSize;
      54            3 :     auto winInSize                   = bufferSize / 2;
      55            3 :     combinOpParam.windowsIn[rankId]  = bufferAddr;
      56            3 :     combinOpParam.windowsOut[rankId] = bufferAddr + winInSize;
      57              : 
      58            9 :     HCCL_RUN_INFO("hcclCombinOpParam info: bufferSize = [%llu], windowsIn[%d] = [%llu], windowsOut[%d] = [%llu]",
      59              :                   combinOpParam.winSize, rankId, combinOpParam.windowsIn[rankId], rankId,
      60              :                   combinOpParam.windowsOut[rankId]);
      61            3 : }
      62              : 
      63            5 : void AivMc2Compont::GenerateCommContext(void **commContext)
      64              : {
      65            5 :     if (combinOpParamBuffer != nullptr) {
      66            1 :         *commContext = reinterpret_cast<void *>(combinOpParamBuffer->GetAddr());
      67            1 :         return;
      68              :     }
      69              :     
      70            4 :     workspaceBuffer                    = std::make_shared<DevBuffer>(MC2_WORKSPACE_SIZE);
      71              : 
      72            4 :     HcclCombinOpParam combinOpParam{0};
      73            4 :     combinOpParam.workSpace     = static_cast<uint64_t>(workspaceBuffer->GetAddr());
      74            4 :     combinOpParam.workSpaceSize = MC2_WORKSPACE_SIZE;
      75            4 :     combinOpParam.rankId        = comm->GetMyRank();
      76            4 :     combinOpParam.rankDim       = comm->GetRankSize();
      77              : 
      78           12 :     HCCL_RUN_INFO("hcclCombinOpParam info: workSpace = [%llu], rankId = [%u], rankDim = [%u]",
      79              :                   combinOpParam.workSpace, combinOpParam.rankId, combinOpParam.rankDim);
      80              :     
      81            4 :     auto collService = dynamic_cast<CollServiceDeviceMode *>(comm->GetCollService());
      82            4 :     auto protocol = collService->GetAivInsPreprocessor()->GetProtocol();
      83            4 :     if (protocol == 1) {    // urma
      84            0 :         GenerateAivUrmaCommContext(combinOpParam);
      85            4 :     } else if (protocol == 0) { // ubmemory
      86            4 :         GenerateAivMemoryCommContext(combinOpParam);
      87              :     } else {
      88            0 :         THROW<InvalidParamsException>(StringFormat("protocol[%u] not supported", protocol));
      89              :     }
      90            3 :     auto paramSize      = sizeof(HcclCombinOpParam);
      91            3 :     combinOpParamBuffer = std::make_shared<DevBuffer>(paramSize);
      92            3 :     HrtMemcpy(reinterpret_cast<void *>(combinOpParamBuffer->GetAddr()), paramSize, static_cast<void *>(&combinOpParam),
      93              :         paramSize, RT_MEMCPY_HOST_TO_DEVICE);
      94            3 :     *commContext = reinterpret_cast<void *>(combinOpParamBuffer->GetAddr());
      95              : }
      96              : 
      97            4 : void AivMc2Compont::GenerateAivMemoryCommContext(HcclCombinOpParam &combinOpParam)
      98              : {
      99           12 :     HCCL_INFO("[AivMc2Compont][GenerateAivMemoryCommContext] ubmemory");
     100              :     // add cclbuffer info
     101            4 :     if (comm->GetCclBuffer() == nullptr) {
     102            2 :         THROW<Hccl::InternalException>(StringFormat("Cannot get CCL Buffer to fill window!"));
     103              :     }
     104              : 
     105              :     // winIn winOut 放到 commContext
     106            3 :     uint64_t commCclBufferSize = static_cast<uint64_t>(comm->GetCclBuffer()->GetSize());
     107            3 :     uint64_t commCclBufferAddr = static_cast<uint64_t>(comm->GetCclBuffer()->GetAddr());
     108            3 :     AddCclBuffer(combinOpParam, commCclBufferSize, commCclBufferAddr, comm->GetMyRank());
     109              : 
     110              :     // 获取ubmemoryTranMgr,遍历所有ubmemoryTran,拿到CclBuffer,一分为2,放到windowsIn、windowsOut
     111            3 :     auto rankId2RmtIpcRmaBufList = comm->GetUbMemoryTransportMgr()->GetRmtRankId2RmtIpcRmaBufList();
     112            3 :     for (auto& rankId2RmtIpcRmaBuf : rankId2RmtIpcRmaBufList) {
     113            0 :         auto     rmtRank       = rankId2RmtIpcRmaBuf.first;
     114            0 :         auto     rmtMemBuffer  = rankId2RmtIpcRmaBuf.second;
     115            0 :         uint64_t rmtBufferSize = static_cast<uint64_t>(rmtMemBuffer->GetSize());
     116            0 :         uint64_t rmtBufferAddr = static_cast<uint64_t>(rmtMemBuffer->GetAddr());
     117            0 :         AddCclBuffer(combinOpParam, rmtBufferSize, rmtBufferAddr, rmtRank);
     118              :     }
     119            3 : }
     120              : 
     121            0 : void AivMc2Compont::GenerateAivUrmaCommContext(HcclCombinOpParam &combinOpParam) const
     122              : {
     123            0 :     HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] aiv urma");
     124            0 :     auto collService = dynamic_cast<CollServiceDeviceMode *>(comm->GetCollService());
     125            0 :     auto links = comm->GetFullMeshLinks();
     126            0 :     if (links.empty()) {
     127            0 :         THROW<InvalidParamsException>(StringFormat("AivMc2Compont::GenerateAivUrmaCommContext links is empty"));
     128              :     }
     129            0 :     auto localBuffer = comm->GetLocalRmaBufManager().Get(comm->GetId(), links[0].GetLocalPort(), BufferType::SCRATCH);
     130            0 :     CHECK_NULLPTR(localBuffer, "[AivMc2Compont::GenerateAivUrmaCommContext] localBuffer is nullptr!");
     131            0 :     uint64_t localBufferSize = static_cast<uint64_t>(localBuffer->GetBuf()->GetSize());
     132            0 :     uint64_t localBufferAddr = static_cast<uint64_t>(localBuffer->GetBuf()->GetAddr());
     133            0 :     AddCclBuffer(combinOpParam, localBufferSize, localBufferAddr, comm->GetMyRank());
     134            0 :     for (auto &link : links) {
     135            0 :         auto rmtBuffer = comm->GetMemTransportManager()->GetUrmaDirectTransport(link)->GetRmtRmaBuffer(2);
     136            0 :         CHECK_NULLPTR(rmtBuffer, "[AivMc2Compont::GenerateAivUrmaCommContext] rmtBuffer is nullptr!");
     137            0 :         uint64_t rmtBufferSize = static_cast<uint64_t>(rmtBuffer->GetSize());
     138            0 :         uint64_t rmtBufferAddr = static_cast<uint64_t>(rmtBuffer->GetAddr());
     139            0 :         AddCclBuffer(combinOpParam, rmtBufferSize, rmtBufferAddr, link.GetRemoteRankId());
     140              :     }
     141              : 
     142            0 :     auto wqs = collService->GetAivInsPreprocessor()->GetWqs();
     143            0 :     for (size_t i = 0; i < wqs.size(); i++) {
     144            0 :         combinOpParam.wq[links[i].GetRemoteRankId()] = wqs[i];
     145              :     }
     146            0 :     HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] wq info:");
     147            0 :     for (auto& wq : wqs) {
     148            0 :         HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]jettyId[%u], sqVA[%llx], wqeSize[%u], sqDepth[%u], "
     149              :                   "headAddr[%llx], tailAddr[%llx], dbAddr[%llx], tp_id[%u]", wq.jettyId, wq.sqVA, wq.wqeSize, 
     150              :                   wq.sqDepth, wq.headAddr, wq.tailAddr, wq.dbAddr, wq.tp_id);
     151            0 :         for (size_t i = 0; i < 16; i++) { // 遍历rmtEid
     152            0 :             HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtEid[%llu][%u]", i, wq.rmtEid[i]);
     153              :         }
     154            0 :         HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtObjId[%u]", wq.rmtObjId);
     155              :     }
     156              : 
     157            0 :     auto cqs = collService->GetAivInsPreprocessor()->GetCqs();
     158            0 :     for (size_t i = 0; i < cqs.size(); i++) {
     159            0 :         combinOpParam.cq[links[i].GetRemoteRankId()] = cqs[i];
     160              :     }
     161            0 :     HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] cq info:");
     162            0 :     for (auto& cq : cqs) {
     163            0 :         HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]jfcId[%u], cqVA[%llx], cqeSize[%u], cqDepth[%u], headAddr[%llx], "
     164              :                   "tailAddr[%llx], dbAddr[%llx]", cq.jfcId, cq.cqVA, cq.cqeSize, cq.cqDepth, cq.headAddr, 
     165              :                   cq.tailAddr, cq.dbAddr);
     166              :     }
     167            0 : }
     168              : 
     169            1 : void AivMc2Compont::AivMC2AllocCommRes(Mc2Tiling *mc2TilingPtr) const
     170              : {
     171            3 :     HCCL_INFO("AivMC2AllocCommRes start");
     172            1 :     auto insQueue = make_shared<InsQueue>();
     173              : 
     174            1 :     auto                            collService = dynamic_cast<CollServiceDeviceMode *>(comm->GetCollService());
     175            1 :     auto                            aivLinks    = comm->GetFullMeshLinks();
     176              :     
     177            1 :     for (auto &link : aivLinks) {
     178            0 :         HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
     179              :     }
     180              :     
     181            1 :     Mc2CommConfig *commConfigPtr = reinterpret_cast<Mc2CommConfig *>(
     182              :         reinterpret_cast<uint8_t *>(mc2TilingPtr) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(Mc2ServerCfg));
     183            1 :     const auto &commConfig = *(commConfigPtr);
     184            1 :     FillCollOperator(commConfig);
     185              : 
     186            1 :     AivOpArgs aivArgs {};
     187              : 
     188            1 :     std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
     189              : 
     190            1 :     insQueue->Append(std::move(aivIns));
     191              : 
     192            1 :     comm->GetSocketManager().BatchCreateSockets(aivLinks);
     193              : 
     194              :     // 对insQueue中ccuIns进行预处理 创建transport
     195            1 :     collService->GetAivInsPreprocessor()->Preprocess(insQueue);
     196            3 :     HCCL_INFO("AivMC2AllocCommRes success");
     197            1 : }
     198              : 
     199            1 : void AivMc2Compont::AivMC2AllocCommResV2(Mc2InitTilingInner *mc2TilingPtr) const
     200              : {
     201            3 :         HCCL_INFO("AivMC2AllocCommRes start");
     202            1 :     auto insQueue = make_shared<InsQueue>();
     203              : 
     204            1 :     auto                            collService = dynamic_cast<CollServiceDeviceMode *>(comm->GetCollService());
     205            1 :     if (collService == nullptr) {
     206            0 :         HCCL_ERROR("[AivMc2Compont::%s] dynamic_cast<CollServiceDeviceMode*> failed", __func__);
     207            0 :         return;
     208              :     }
     209            1 :     auto                            aivLinks    = comm->GetFullMeshLinks();
     210              :     
     211            1 :     for (auto &link : aivLinks) {
     212            0 :         HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
     213              :     }
     214              : 
     215            1 :     const auto  offset = mc2TilingPtr->offset[0];
     216            1 :     const auto &commConfig
     217            1 :         = *(reinterpret_cast<const Mc2CcTilingInner *>(reinterpret_cast<const uint8_t *>(mc2TilingPtr) + offset));
     218            3 :     HCCL_RUN_INFO("[AivMc2Compont][AivMC2AllocCommRes] Mc2CcTilingInner skipLocalRankCopy[%u], skipBufferWindowCopy[%u], "
     219              :                   "stepSize[%u], version[%u], protocol[%u], communicationEngine[%u], srcDataType[%u], dstDataType[%u], "
     220              :                   "algConfig[%s], opType[%u], reduceType[%u]",
     221              :                   commConfig.skipLocalRankCopy, commConfig.skipBufferWindowCopy, commConfig.stepSize, commConfig.version, 
     222              :                   commConfig.protocol, commConfig.communicationEngine, commConfig.srcDataType, commConfig.dstDataType,
     223              :                   commConfig.algConfig, commConfig.opType, commConfig.reduceType);
     224            3 :     HCCL_RUN_INFO("[AivMc2Compont][AivMC2AllocCommRes] groupName[%s]", commConfig.groupName);
     225            1 :     FillCollOperatorV2(commConfig);
     226              : 
     227            1 :     AivOpArgs aivArgs {};
     228              : 
     229            1 :     std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
     230              : 
     231            1 :     insQueue->Append(std::move(aivIns));
     232              : 
     233            1 :     comm->GetSocketManager().BatchCreateSockets(aivLinks);
     234              : 
     235              :     // 对insQueue中ccuIns进行预处理 创建transport
     236            1 :     collService->GetAivInsPreprocessor()->SetProtocol(commConfig.protocol);
     237            1 :     collService->GetAivInsPreprocessor()->Preprocess(insQueue);
     238            3 :     HCCL_INFO("AivMC2AllocCommResV2 success");
     239            1 : }
     240              : 
     241              : // 为保证后续流程统一,构造一个虚拟算子
     242            1 : void AivMc2Compont::FillCollOperator(const Mc2CommConfig &config) const
     243              : {
     244            1 :     uint32_t dataCount = 1024;
     245            1 :     CollOpParams opParams;
     246            1 :     opParams.opType         = MC2OpType(static_cast<AicpuComType>(config.opType));
     247            1 :     opParams.reduceOp       = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
     248            1 :     opParams.dataType       = MC2DataType(static_cast<HcclDataType>(config.dataType));
     249            1 :     opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.outputDataType));
     250            1 :     opParams.count          = dataCount;
     251            1 :     std::string opTag       = comm->GetId();
     252            1 :     comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
     253            1 : }
     254              : 
     255            1 : void AivMc2Compont::FillCollOperatorV2(const Mc2CcTilingInner &config) const
     256              : {
     257            1 :     uint32_t dataCount = 1024;
     258            1 :     CollOpParams opParams;
     259            1 :     opParams.opType         = MC2OpType(static_cast<AicpuComType>(AIV_FAKE_OP_TPYE));
     260            1 :     opParams.reduceOp       = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
     261            1 :     opParams.dataType       = MC2DataType(static_cast<HcclDataType>(config.srcDataType));
     262            1 :     opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.dstDataType));
     263            1 :     opParams.count          = dataCount;
     264            1 :     std::string opTag       = comm->GetId();
     265              : 
     266            1 :     comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
     267            1 : }
     268              : 
     269              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1