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

Generated by: LCOV version 2.0-1