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-08-18 17:47:01 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            6 : void AivMc2Compont::AllocCommResource(void* mc2Tiling, void** commContext)
      25              : {
      26            6 :     if (mc2Tiling == nullptr) {
      27            3 :         HCCL_ERROR("[AivMc2Compont::%s] mc2Tiling is nullptr", __func__);
      28            1 :         return;
      29              :     }
      30            5 :     auto tilingVersion = *static_cast<uint32_t*>(mc2Tiling);
      31           15 :     HCCL_INFO("[AivMc2Compont:%s] Tiling version [%u]", __func__, tilingVersion);
      32            5 :     if (tilingVersion != UNKNOWN_TILING_V1 && tilingVersion != UNKNOWN_TILING_V2) {
      33            4 :         THROW<NotSupportException>(StringFormat("Tiling version not support, version[%u]", tilingVersion));
      34              :     }
      35              : 
      36            3 :     if (comm->GetRankSize() == 1) {
      37            1 :         THROW<NotSupportException>(StringFormat("Comm[%s] rank size is 1, Mc2 not support", comm->GetId().c_str()));
      38              :     }
      39              : 
      40            2 :     if (tilingVersion == UNKNOWN_TILING_V1) {
      41            1 :         AivMC2AllocCommRes(reinterpret_cast<Mc2Tiling*>(mc2Tiling));
      42              :     } else {
      43            1 :         AivMC2AllocCommResV2(reinterpret_cast<Mc2InitTilingInner*>(mc2Tiling));
      44              :     }
      45              : 
      46            2 :     GenerateCommContext(commContext);
      47              : }
      48              : 
      49            3 : void AddCclBuffer(HcclCombinOpParam& combinOpParam, uint64_t bufferSize, uint64_t bufferAddr, RankId rankId)
      50              : {
      51            3 :     combinOpParam.winSize = bufferSize;
      52            3 :     auto winInSize = bufferSize / 2;
      53            3 :     combinOpParam.windowsIn[rankId] = bufferAddr;
      54            3 :     combinOpParam.windowsOut[rankId] = bufferAddr + winInSize;
      55              : 
      56            9 :     HCCL_RUN_INFO(
      57              :         "hcclCombinOpParam info: bufferSize = [%llu], windowsIn[%d] = [%llu], windowsOut[%d] = [%llu]",
      58              :         combinOpParam.winSize, rankId, combinOpParam.windowsIn[rankId], rankId, combinOpParam.windowsOut[rankId]);
      59            3 : }
      60              : 
      61            5 : void AivMc2Compont::GenerateCommContext(void** commContext)
      62              : {
      63            5 :     if (combinOpParamBuffer != nullptr) {
      64            1 :         *commContext = reinterpret_cast<void*>(combinOpParamBuffer->GetAddr());
      65            1 :         return;
      66              :     }
      67              : 
      68            4 :     workspaceBuffer = std::make_shared<DevBuffer>(MC2_WORKSPACE_SIZE);
      69              : 
      70            4 :     HcclCombinOpParam combinOpParam{};
      71            4 :     combinOpParam.workSpace = static_cast<uint64_t>(workspaceBuffer->GetAddr());
      72            4 :     combinOpParam.workSpaceSize = MC2_WORKSPACE_SIZE;
      73            4 :     combinOpParam.rankId = comm->GetMyRank();
      74            4 :     combinOpParam.rankDim = comm->GetRankSize();
      75              : 
      76           12 :     HCCL_RUN_INFO(
      77              :         "hcclCombinOpParam info: workSpace = [%llu], rankId = [%u], rankDim = [%u]", combinOpParam.workSpace,
      78              :         combinOpParam.rankId, combinOpParam.rankDim);
      79              : 
      80            4 :     auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
      81            4 :     auto protocol = collService->GetAivInsPreprocessor()->GetProtocol();
      82            4 :     if (protocol == 1) { // urma
      83            0 :         GenerateAivUrmaCommContext(combinOpParam);
      84            4 :     } else if (protocol == 0) { // ubmemory
      85            4 :         GenerateAivMemoryCommContext(combinOpParam);
      86              :     } else {
      87            0 :         THROW<InvalidParamsException>(StringFormat("protocol[%u] not supported", protocol));
      88              :     }
      89            3 :     auto paramSize = sizeof(HcclCombinOpParam);
      90            3 :     combinOpParamBuffer = std::make_shared<DevBuffer>(paramSize);
      91            6 :     HrtMemcpy(
      92            3 :         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(
     149              :             "[AivMc2Compont][GenerateAivUrmaCommContext]jettyId[%u], sqVA[%llx], wqeSize[%u], sqDepth[%u], "
     150              :             "headAddr[%llx], tailAddr[%llx], dbAddr[%llx], tp_id[%u]",
     151              :             wq.jettyId, wq.sqVA, wq.wqeSize, wq.sqDepth, wq.headAddr, wq.tailAddr, wq.dbAddr, wq.tp_id);
     152            0 :         for (size_t i = 0; i < 16; i++) { // 遍历rmtEid
     153            0 :             HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtEid[%llu][%u]", i, wq.rmtEid[i]);
     154              :         }
     155            0 :         HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtObjId[%u]", wq.rmtObjId);
     156              :     }
     157              : 
     158            0 :     auto cqs = collService->GetAivInsPreprocessor()->GetCqs();
     159            0 :     for (size_t i = 0; i < cqs.size(); i++) {
     160            0 :         combinOpParam.cq[links[i].GetRemoteRankId()] = cqs[i];
     161              :     }
     162            0 :     HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] cq info:");
     163            0 :     for (auto& cq : cqs) {
     164            0 :         HCCL_INFO(
     165              :             "[AivMc2Compont][GenerateAivUrmaCommContext]jfcId[%u], cqVA[%llx], cqeSize[%u], cqDepth[%u], "
     166              :             "headAddr[%llx], "
     167              :             "tailAddr[%llx], dbAddr[%llx]",
     168              :             cq.jfcId, cq.cqVA, cq.cqeSize, cq.cqDepth, cq.headAddr, cq.tailAddr, cq.dbAddr);
     169              :     }
     170            0 : }
     171              : 
     172            1 : void AivMc2Compont::AivMC2AllocCommRes(Mc2Tiling* mc2TilingPtr) const
     173              : {
     174            3 :     HCCL_INFO("AivMC2AllocCommRes start");
     175            1 :     auto insQueue = make_shared<InsQueue>();
     176              : 
     177            1 :     auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
     178            1 :     auto aivLinks = comm->GetFullMeshLinks();
     179              : 
     180            1 :     for (auto& link : aivLinks) {
     181            0 :         HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
     182              :     }
     183              : 
     184            1 :     Mc2CommConfig* commConfigPtr = reinterpret_cast<Mc2CommConfig*>(
     185              :         reinterpret_cast<uint8_t*>(mc2TilingPtr) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(Mc2ServerCfg));
     186            1 :     const auto& commConfig = *(commConfigPtr);
     187            1 :     FillCollOperator(commConfig);
     188              : 
     189            1 :     AivOpArgs aivArgs{};
     190              : 
     191            1 :     std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
     192              : 
     193            1 :     insQueue->Append(std::move(aivIns));
     194              : 
     195            1 :     comm->GetSocketManager().BatchCreateSockets(aivLinks);
     196              : 
     197              :     // 对insQueue中ccuIns进行预处理 创建transport
     198            1 :     collService->GetAivInsPreprocessor()->Preprocess(insQueue);
     199            3 :     HCCL_INFO("AivMC2AllocCommRes success");
     200            1 : }
     201              : 
     202            1 : void AivMc2Compont::AivMC2AllocCommResV2(Mc2InitTilingInner* mc2TilingPtr) const
     203              : {
     204            3 :     HCCL_INFO("AivMC2AllocCommRes start");
     205            1 :     auto insQueue = make_shared<InsQueue>();
     206              : 
     207            1 :     auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
     208            1 :     if (collService == nullptr) {
     209            0 :         HCCL_ERROR("[AivMc2Compont::%s] dynamic_cast<CollServiceDeviceMode*> failed", __func__);
     210            0 :         return;
     211              :     }
     212            1 :     auto aivLinks = comm->GetFullMeshLinks();
     213              : 
     214            1 :     for (auto& link : aivLinks) {
     215            0 :         HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
     216              :     }
     217              : 
     218            1 :     const auto offset = mc2TilingPtr->offset[0];
     219            1 :     const auto& commConfig
     220            1 :         = *(reinterpret_cast<const Mc2CcTilingInner*>(reinterpret_cast<const uint8_t*>(mc2TilingPtr) + offset));
     221            3 :     HCCL_RUN_INFO(
     222              :         "[AivMc2Compont][AivMC2AllocCommRes] Mc2CcTilingInner skipLocalRankCopy[%u], skipBufferWindowCopy[%u], "
     223              :         "stepSize[%u], version[%u], protocol[%u], communicationEngine[%u], srcDataType[%u], dstDataType[%u], "
     224              :         "algConfig[%s], opType[%u], reduceType[%u]",
     225              :         commConfig.skipLocalRankCopy, commConfig.skipBufferWindowCopy, commConfig.stepSize, commConfig.version,
     226              :         commConfig.protocol, commConfig.communicationEngine, commConfig.srcDataType, commConfig.dstDataType,
     227              :         commConfig.algConfig, commConfig.opType, commConfig.reduceType);
     228            3 :     HCCL_RUN_INFO("[AivMc2Compont][AivMC2AllocCommRes] groupName[%s]", commConfig.groupName);
     229            1 :     FillCollOperatorV2(commConfig);
     230              : 
     231            1 :     AivOpArgs aivArgs{};
     232              : 
     233            1 :     std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
     234              : 
     235            1 :     insQueue->Append(std::move(aivIns));
     236              : 
     237            1 :     comm->GetSocketManager().BatchCreateSockets(aivLinks);
     238              : 
     239              :     // 对insQueue中ccuIns进行预处理 创建transport
     240            1 :     collService->GetAivInsPreprocessor()->SetProtocol(commConfig.protocol);
     241            1 :     collService->GetAivInsPreprocessor()->Preprocess(insQueue);
     242            3 :     HCCL_INFO("AivMC2AllocCommResV2 success");
     243            1 : }
     244              : 
     245              : // 为保证后续流程统一,构造一个虚拟算子
     246            1 : void AivMc2Compont::FillCollOperator(const Mc2CommConfig& config) const
     247              : {
     248            1 :     uint32_t dataCount = 1024;
     249            1 :     CollOpParams opParams;
     250            1 :     opParams.opType = MC2OpType(static_cast<AicpuComType>(config.opType));
     251            1 :     opParams.reduceOp = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
     252            1 :     opParams.dataType = MC2DataType(static_cast<HcclDataType>(config.dataType));
     253            1 :     opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.outputDataType));
     254            1 :     opParams.count = dataCount;
     255            1 :     std::string opTag = comm->GetId();
     256            1 :     comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
     257            1 : }
     258              : 
     259            1 : void AivMc2Compont::FillCollOperatorV2(const Mc2CcTilingInner& config) const
     260              : {
     261            1 :     uint32_t dataCount = 1024;
     262            1 :     CollOpParams opParams;
     263            1 :     opParams.opType = MC2OpType(static_cast<AicpuComType>(AIV_FAKE_OP_TPYE));
     264            1 :     opParams.reduceOp = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
     265            1 :     opParams.dataType = MC2DataType(static_cast<HcclDataType>(config.srcDataType));
     266            1 :     opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.dstDataType));
     267            1 :     opParams.count = dataCount;
     268            1 :     std::string opTag = comm->GetId();
     269              : 
     270            1 :     comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
     271            1 : }
     272              : 
     273              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1