LCOV - code coverage report
Current view: top level - coll_communicator_mgr/api_c_adpt/resource - engine_ctx_c_adpt.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 86.2 % 58 50
Test Date: 2026-08-18 17:47:01 Functions: 87.5 % 8 7

            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 <unordered_map>
      12              : #include "hccl/hccl_res.h"
      13              : #include "independent_op_context_manager.h"
      14              : #include "log.h"
      15              : #include "hccl_comm_pub.h"
      16              : #include "independent_op.h"
      17              : #include <string>
      18              : #include "param_check_pub.h"
      19              : #include "comm_engine_utils.h"
      20              : 
      21              : using namespace hccl;
      22              : 
      23              : const char* COMM_RESERVE_CTX_TAG = "";
      24              : 
      25           37 : HcclResult HcclEngineCtxCreate(HcclComm comm, const char* ctxTag, CommEngine engine, uint64_t size, void** ctx)
      26              : {
      27           37 :     CHK_PTR_NULL(comm);
      28           35 :     CHK_PTR_NULL(ctx);
      29           33 :     const char* ctxTagTmp = (ctxTag == nullptr) ? COMM_RESERVE_CTX_TAG : ctxTag;
      30           33 :     CHK_PRT_RET(
      31              :         strlen(ctxTagTmp) > HCCL_RES_TAG_MAX_LEN,
      32              :         HCCL_ERROR(
      33              :             "[%s] ctxTag length exceeds maximum length, ctxTag length[%zu], max length[%u]", __func__,
      34              :             strlen(ctxTagTmp), HCCL_RES_TAG_MAX_LEN),
      35              :         HCCL_E_PARA);
      36           31 :     CHK_PRT_RET(
      37              :         size == 0, HCCL_ERROR("[%s]Invalid CtxSize, CtxSize[%llu]", __func__, static_cast<unsigned long long>(size)),
      38              :         HCCL_E_PARA);
      39              : 
      40              : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
      41           51 :     HCCLV2_FUNC_RUN([&]() -> HcclResult {
      42              :         auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
      43              :         std::string commId = hcclComm->GetIdentifier();
      44              :         HCCL_RUN_INFO("Entry-HcclEngineCtxCreate:comm[%s]", commId.c_str());
      45              :         hccl::CollComm* collComm = hcclComm->GetCollComm();
      46              :         CHK_PTR_NULL(collComm);
      47              :         auto myRank = collComm->GetMyRank();
      48              :         CHK_PTR_NULL(myRank);
      49              :         EngineCtxs* engineCtxs = myRank->GetEngineCtxs();
      50              :         CHK_PTR_NULL(engineCtxs);
      51              :         HcclResult ret = HCCL_SUCCESS;
      52              :         ret = engineCtxs->CreateCommEngineCtx(ctxTagTmp, engine, size, ctx);
      53              :         CHK_PRT_RET(
      54              :             ret != HCCL_SUCCESS,
      55              :             HCCL_ERROR(
      56              :                 "[%s] Failed to create CommEngineCtx with ctxTag[%s], engine[%s], ctx size[%llu], ret[%d]", __func__,
      57              :                 ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(),
      58              :                 static_cast<unsigned long long>(size), ret),
      59              :             ret);
      60              :         HCCL_RUN_INFO(
      61              :             "HcclEngineCtxCreate success, ctxTag[%s], engine[%s], size[%llu], ctx[%p], group[%s]", ctxTagTmp,
      62              :             GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), static_cast<unsigned long long>(size), *ctx,
      63              :             hcclComm->GetIdentifier().c_str());
      64              :         return HCCL_SUCCESS;
      65              :     }());
      66              : #endif
      67              : 
      68           23 :     hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
      69           23 :     auto& contextMgr = hcclComm->GetIndependentOp().GetContextManager();
      70           46 :     HcclResult ret = contextMgr.CreateCommEngineCtx(ctxTagTmp, engine, size, ctx);
      71           23 :     if (ret != HCCL_SUCCESS) {
      72            4 :         HCCL_ERROR(
      73              :             "[%s] Failed to create CommEngineCtx with ctxTag[%s], engine[%s], ctx size[%llu], ret[%d]", __func__,
      74              :             ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(),
      75              :             static_cast<unsigned long long>(size), ret);
      76            4 :         return ret;
      77              :     }
      78              : 
      79           19 :     HCCL_RUN_INFO(
      80              :         "[%s] success, ctxTag[%s], engine[%s], size[%llu], ctx[%p], group[%s]", __func__, ctxTagTmp,
      81              :         GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), static_cast<unsigned long long>(size), *ctx,
      82              :         hcclComm->GetIdentifier().c_str());
      83           19 :     return HCCL_SUCCESS;
      84              : }
      85              : 
      86           15 : HcclResult HcclEngineCtxGet(HcclComm comm, const char* ctxTag, CommEngine engine, void** ctx, uint64_t* size)
      87              : {
      88              :     // 性能关键路径,禁止打印算子粒度频次的日志
      89           15 :     CHK_PTR_NULL(comm);
      90           13 :     CHK_PTR_NULL(ctx);
      91           12 :     CHK_PTR_NULL(size);
      92           11 :     const char* ctxTagTmp = (ctxTag == nullptr) ? COMM_RESERVE_CTX_TAG : ctxTag;
      93           11 :     CHK_PRT_RET(
      94              :         strlen(ctxTagTmp) > HCCL_RES_TAG_MAX_LEN,
      95              :         HCCL_ERROR(
      96              :             "[%s] ctxTag length exceeds maximum length, ctxTag length[%zu], max length[%u]", __func__,
      97              :             strlen(ctxTagTmp), HCCL_RES_TAG_MAX_LEN),
      98              :         HCCL_E_PARA);
      99              : 
     100              : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
     101            9 :     HCCLV2_FUNC_RUN([&]() -> HcclResult {
     102              :         auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
     103              :         const std::string& commId = hcclComm->GetIdentifier();
     104              :         hccl::CollComm* collComm = hcclComm->GetCollComm();
     105              :         CHK_PTR_NULL(collComm);
     106              :         auto myRank = collComm->GetMyRank();
     107              :         CHK_PTR_NULL(myRank);
     108              :         EngineCtxs* engineCtxs = myRank->GetEngineCtxs();
     109              :         CHK_PTR_NULL(engineCtxs);
     110              :         HcclResult ret = HCCL_SUCCESS;
     111              :         ret = engineCtxs->GetCommEngineCtx(ctxTagTmp, engine, ctx, size);
     112              :         CHK_PRT_RET(
     113              :             ret != HCCL_SUCCESS,
     114              :             HCCL_WARNING(
     115              :                 "[%s] Failed to get CommEngineCtx with ctxTag[%s], engine[%s], ret[%d]", __func__, ctxTagTmp,
     116              :                 GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), ret),
     117              :             ret);
     118              :         return HCCL_SUCCESS;
     119              :     }());
     120              : #endif
     121              : 
     122            9 :     hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
     123            9 :     auto& contextMgr = hcclComm->GetIndependentOp().GetContextManager();
     124           18 :     HcclResult ret = contextMgr.GetCommEngineCtx(std::string(ctxTagTmp), engine, ctx, size);
     125            9 :     if (ret != HCCL_SUCCESS) {
     126            5 :         HCCL_WARNING(
     127              :             "[%s] Failed to get CommEngineCtx with ctxTag[%s], engine[%s], ret[%d]", __func__, ctxTagTmp,
     128              :             GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), ret);
     129            5 :         return ret;
     130              :     }
     131              : 
     132            4 :     HCCL_RUN_INFO(
     133              :         "[%s] success, ctxTag[%s], engine[%s], ctx[%p], size[%llu], group[%s]", __func__, ctxTagTmp,
     134              :         GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), *ctx, static_cast<unsigned long long>(*size),
     135              :         hcclComm->GetIdentifier().c_str());
     136            4 :     return HCCL_SUCCESS;
     137              : }
     138              : 
     139            1 : HcclResult HcclEngineCtxCopy(
     140              :     HcclComm comm, CommEngine engine, const char* ctxTag, const void* srcCtx, uint64_t size, uint64_t dstCtxOffset)
     141              : {
     142            1 :     CHK_PTR_NULL(comm);
     143            1 :     CHK_PTR_NULL(srcCtx);
     144            1 :     const char* ctxTagTmp = (ctxTag == nullptr) ? COMM_RESERVE_CTX_TAG : ctxTag;
     145            1 :     CHK_PRT_RET(
     146              :         strlen(ctxTagTmp) > HCCL_RES_TAG_MAX_LEN,
     147              :         HCCL_ERROR(
     148              :             "[%s] ctxTag length exceeds maximum length, ctxTag length[%zu], max length[%u]", __func__,
     149              :             strlen(ctxTagTmp), HCCL_RES_TAG_MAX_LEN),
     150              :         HCCL_E_PARA);
     151            1 :     CHK_PRT_RET(
     152              :         size == 0, HCCL_ERROR("[%s]Invalid size, size[%llu]", __func__, static_cast<unsigned long long>(size)),
     153              :         HCCL_E_PARA);
     154              : 
     155              : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
     156            4 :     HCCLV2_FUNC_RUN([&]() -> HcclResult {
     157              :         auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
     158              :         std::string commId = hcclComm->GetIdentifier();
     159              :         HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
     160              :         hccl::CollComm* collComm = hcclComm->GetCollComm();
     161              :         CHK_PTR_NULL(collComm);
     162              :         auto myRank = collComm->GetMyRank();
     163              :         CHK_PTR_NULL(myRank);
     164              :         EngineCtxs* engineCtxs = myRank->GetEngineCtxs();
     165              :         CHK_PTR_NULL(engineCtxs);
     166              :         HcclResult ret = HCCL_SUCCESS;
     167              :         ret = engineCtxs->CopyCommEngineCtx(ctxTagTmp, engine, srcCtx, size, dstCtxOffset);
     168              :         CHK_PRT_RET(
     169              :             ret != HCCL_SUCCESS,
     170              :             HCCL_WARNING(
     171              :                 "[%s] Failed to copy CommEngineCtx with ctxTag[%s], engine[%s], size[%llu], dstCtxOffset[%llu],"
     172              :                 " ret[%d]",
     173              :                 __func__, ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(),
     174              :                 static_cast<unsigned long long>(size), static_cast<unsigned long long>(dstCtxOffset), ret),
     175              :             ret);
     176              :         HCCL_RUN_INFO(
     177              :             "[%s] success, ctxTag[%s], engine[%s], srcCtx[%p], size[%llu], dstCtxOffset[%llu], group[%s]", __func__,
     178              :             ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), srcCtx,
     179              :             static_cast<unsigned long long>(size), static_cast<unsigned long long>(dstCtxOffset),
     180              :             hcclComm->GetIdentifier().c_str());
     181              :         return HCCL_SUCCESS;
     182              :     }());
     183              : #endif
     184              : 
     185            0 :     hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
     186            0 :     auto& contextMgr = hcclComm->GetIndependentOp().GetContextManager();
     187            0 :     HcclResult ret = contextMgr.CopyCommEngineCtx(std::string(ctxTagTmp), engine, srcCtx, size, dstCtxOffset);
     188            0 :     if (ret != HCCL_SUCCESS) {
     189            0 :         HCCL_ERROR(
     190              :             "[%s] Failed to copy CommEngineCtx with ctxTag[%s], engine[%s], size[%llu], dstCtxOffset[%llu],"
     191              :             " ret[%d]",
     192              :             __func__, ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(),
     193              :             static_cast<unsigned long long>(size), static_cast<unsigned long long>(dstCtxOffset), ret);
     194            0 :         return ret;
     195              :     }
     196              : 
     197            0 :     HCCL_RUN_INFO(
     198              :         "[%s] success, ctxTag[%s], engine[%s], srcCtx[%p], size[%llu], dstCtxOffset[%llu], group[%s]", __func__,
     199              :         ctxTagTmp, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), srcCtx,
     200              :         static_cast<unsigned long long>(size), static_cast<unsigned long long>(dstCtxOffset),
     201              :         hcclComm->GetIdentifier().c_str());
     202            0 :     return HCCL_SUCCESS;
     203              : }
     204              : 
     205           32 : HcclResult HcclEngineCtxDestroy(HcclComm comm, const char* ctxTag, CommEngine engine)
     206              : {
     207           32 :     CHK_PTR_NULL(comm);
     208           29 :     const char* ctxTagTmp = (ctxTag == nullptr) ? COMM_RESERVE_CTX_TAG : ctxTag;
     209           29 :     CHK_PRT_RET(
     210              :         strlen(ctxTagTmp) > HCCL_RES_TAG_MAX_LEN,
     211              :         HCCL_ERROR(
     212              :             "[%s] ctxTag length exceeds maximum length, ctxTag length[%zu], max length[%u]", __func__,
     213              :             strlen(ctxTagTmp), HCCL_RES_TAG_MAX_LEN),
     214              :         HCCL_E_PARA);
     215              : 
     216              : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
     217           56 :     HCCLV2_FUNC_RUN([&]() -> HcclResult {
     218              :         auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
     219              :         std::string commId = hcclComm->GetIdentifier();
     220              :         HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
     221              :         hccl::CollComm* collComm = hcclComm->GetCollComm();
     222              :         CHK_PTR_NULL(collComm);
     223              :         auto myRank = collComm->GetMyRank();
     224              :         CHK_PTR_NULL(myRank);
     225              :         EngineCtxs* engineCtxs = myRank->GetEngineCtxs();
     226              :         HcclResult ret = HCCL_SUCCESS;
     227              :         ret = engineCtxs->DestroyEngineCtx(ctxTagTmp, engine);
     228              :         CHK_PRT_RET(
     229              :             ret != HCCL_SUCCESS,
     230              :             HCCL_ERROR(
     231              :                 "[%s] Failed to destroy CommEngineCtx, ctxTag[%s], engine[%s], ret[%d]", __func__, ctxTagTmp,
     232              :                 GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), ret),
     233              :             ret);
     234              :         HCCL_RUN_INFO(
     235              :             "[%s] success, ctxTag[%s], engine[%s], group[%s]", __func__, ctxTagTmp,
     236              :             GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), hcclComm->GetIdentifier().c_str());
     237              :         return HCCL_SUCCESS;
     238              :     }());
     239              : #endif
     240              : 
     241           20 :     hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
     242           20 :     auto& contextMgr = hcclComm->GetIndependentOp().GetContextManager();
     243           40 :     HcclResult ret = contextMgr.DestroyCommEngineCtx(ctxTagTmp, engine);
     244           20 :     if (ret != HCCL_SUCCESS) {
     245            3 :         HCCL_ERROR(
     246              :             "[%s] Failed to destroy CommEngineCtx, ctxTag[%s], engine[%s], ret[%d]", __func__, ctxTagTmp,
     247              :             GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), ret);
     248            3 :         return ret;
     249              :     }
     250           17 :     HCCL_RUN_INFO(
     251              :         "[%s] success, ctxTag[%s], engine[%s], group[%s]", __func__, ctxTagTmp,
     252              :         GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), hcclComm->GetIdentifier().c_str());
     253           17 :     return HCCL_SUCCESS;
     254              : }
        

Generated by: LCOV version 2.0-1