LCOV - code coverage report
Current view: top level - coll_communicator_mgr/api_c_adpt/resource - comm_mem_c_adpt.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.2 % 85 52
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 <atomic>
      12              : #include <unordered_map>
      13              : #include <mutex>
      14              : #include <memory>
      15              : #include <vector>
      16              : #include <string>
      17              : #include "hccl/hccl_res.h"
      18              : #include "hccl_mem.h"
      19              : #include "stream_pub.h"
      20              : #include "hccl_communicator.h"
      21              : #include "hccl_comm_pub.h"
      22              : #include "param_check_pub.h"
      23              : #include "op_base_v2.h"
      24              : #include "hccl_res.h"
      25              : #include "symmetric_memory/symmetric_memory.h"
      26              : 
      27              : using namespace hccl;
      28              : 
      29           11 : HcclResult HcclCommMemReg(HcclComm comm, const char *memTag, const CommMem *mem, HcclMemHandle *memHandle)
      30              :  
      31              : {
      32           11 :     CHK_PRT_RET(comm == nullptr,  HCCL_ERROR("[HcclCommMemReg]comm is null"), HCCL_E_PTR);
      33           10 :     CHK_PRT_RET(memTag == nullptr, HCCL_ERROR("[HcclCommMemReg]memTag is null"), HCCL_E_PTR);
      34            9 :     CHK_PRT_RET(strlen(memTag) == 0 || strlen(memTag) > HCCL_RES_TAG_MAX_LEN,
      35              :         HCCL_ERROR("[HcclCommMemReg]memTag length is %zu", strlen(memTag)), HCCL_E_PARA);
      36            7 :     std::string memTagStr(memTag);
      37            7 :     CHK_PRT_RET(memTagStr.compare(0, strlen(HCCL_SYMMETRIC_MEMORY_TAG_PREFIX),
      38              :         HCCL_SYMMETRIC_MEMORY_TAG_PREFIX) == 0,
      39              :         HCCL_ERROR("[HcclCommMemReg]memTag[%s] uses reserved symmetric memory prefix[%s]",
      40              :             memTag, HCCL_SYMMETRIC_MEMORY_TAG_PREFIX), HCCL_E_PARA);
      41            7 :     CHK_PRT_RET(mem == nullptr,   HCCL_ERROR("[HcclCommMemReg]mem is null"), HCCL_E_PTR);
      42            6 :     CHK_PRT_RET(memHandle == nullptr, HCCL_ERROR("[HcclCommMemReg]memHandle is null"), HCCL_E_PTR);
      43            5 :     CHK_PRT_RET((mem->type != COMM_MEM_TYPE_DEVICE) && (mem->type != COMM_MEM_TYPE_HOST),
      44              :         HCCL_ERROR("[HcclCommMemReg]memoryType[%d] must be device or host", mem->type), HCCL_E_PARA);
      45            4 :     CHK_PRT_RET(mem->addr == nullptr, HCCL_ERROR("[HcclCommMemReg]addr is null"), HCCL_E_PTR);
      46            3 :     CHK_PRT_RET(mem->size == 0, HCCL_ERROR("[HcclCommMemReg]size[%llu] invalid",
      47              :         static_cast<unsigned long long>(mem->size)), HCCL_E_PARA);
      48              : 
      49              : #if (!defined (HCCD)) && (!defined (CCL_KERNEL_AICPU))
      50            4 :     HCCLV2_FUNC_RUN(
      51              :         [&]() -> HcclResult {
      52              :             auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
      53              :             std::string commId = hcclComm->GetIdentifier();
      54              :             HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
      55              :             hccl::CollComm* collComm = hcclComm->GetCollComm();
      56              :             CHK_PTR_NULL(collComm);
      57              :             auto myRank = collComm->GetMyRank();
      58              :             CHK_PTR_NULL(myRank);
      59              :             CommMems* commMem = myRank->GetCommMems();
      60              :             HcclResult ret = HCCL_SUCCESS;
      61              :             ret = commMem->CommRegMem(memTagStr, *mem, memHandle);
      62              :             CHK_PRT_RET(ret != HCCL_SUCCESS,
      63              :                 HCCL_ERROR("[HcclCommMemReg]Bind failed. memTag[%s], ret[%d]", memTag, ret), ret);
      64              :             HCCL_INFO("[HcclCommMemReg] success: raw handle[%p]", *memHandle);
      65              :             return HCCL_SUCCESS;
      66              :         }());
      67              : #endif
      68              : 
      69            0 :     HCCL_RUN_INFO("HcclCommMemReg is not supported");
      70            0 :     return HCCL_SUCCESS;
      71            7 : }
      72              : 
      73            0 : HcclResult HcclCommDeregMem(HcclComm comm, const char *memTag, const void* memHandle)
      74              : {
      75            0 :     CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[HcclCommDeregMem]comm is null"), HCCL_E_PTR);
      76            0 :     CHK_PRT_RET(memHandle == nullptr, HCCL_ERROR("[HcclCommDeregMem]memHandle is null"), HCCL_E_PTR);
      77            0 :     CHK_PRT_RET(memTag == nullptr, HCCL_ERROR("[HcclCommDeregMem]memTag is null"), HCCL_E_PARA);
      78            0 :     CHK_PRT_RET(strlen(memTag) == 0, HCCL_ERROR("[HcclCommDeregMem]memTag length is 0"), HCCL_E_PARA);
      79              : 
      80            0 :     auto *hcclComm = static_cast<hccl::hcclComm *>(comm);
      81            0 :     std::string commId = hcclComm->GetIdentifier();
      82            0 :     HCCL_RUN_INFO("Entry-%s: comm[%s], handle[%p]", __func__, commId.c_str(), memHandle);
      83              : 
      84              :     // 解绑某算子下的该句柄
      85            0 :     HcclResult ret = HCCL_SUCCESS;
      86            0 :     if (hcclComm->IsCommunicatorV2()) {
      87            0 :         hccl::CollComm* collComm = hcclComm->GetCollComm();
      88            0 :         CHK_PTR_NULL(collComm);
      89            0 :         CommMemMgr* commMemMgr = collComm->GetCommMemMgr();
      90            0 :         CHK_PTR_NULL(commMemMgr);
      91            0 :         ret = commMemMgr->CommUnregMem(std::string(memTag), memHandle);
      92              :     }
      93              :     else {
      94            0 :         auto& commMemMgr = hcclComm->GetIndependentOp().GetCommMemMgr();
      95            0 :         ret = commMemMgr.CommUnregMem(std::string(memTag), memHandle);
      96              :     }
      97              : 
      98            0 :     CHK_PRT_RET(ret == HCCL_E_NOT_FOUND,
      99              :         HCCL_WARNING("[HcclCommDeregMem]handle not bound in this domain. raw[%p]", memHandle), HCCL_SUCCESS);
     100            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     101              :         HCCL_ERROR("[HcclCommDeregMem] unBind failed. handle[%p], ret[%d]", memHandle, ret), ret);
     102            0 :     HCCL_INFO("[HcclCommDeregMem]success: raw handle[%p]", memHandle);
     103            0 :     return HCCL_SUCCESS;
     104            0 : }
     105              : 
     106            5 : HcclResult GetHcclBufferWithClearFlag(HcclComm comm, void **buffer, uint64_t *size, bool clearFlag)
     107              : {
     108            5 :     auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
     109            5 :     const std::string &commId = hcclComm->GetIdentifier();
     110            5 :     hccl::CollComm* collComm = hcclComm->GetCollComm();
     111            5 :     CHK_PTR_NULL(collComm);
     112            4 :     auto myRank = collComm->GetMyRank();
     113            4 :     CHK_PTR_NULL(myRank);
     114            3 :     CommMems* commMem = myRank->GetCommMems();
     115            3 :     CHK_PTR_NULL(commMem);
     116            2 :     CHK_RET(commMem->GetHcclBuffer(*buffer, *size));
     117            2 :     CHK_RET(commMem->HcclBufferMemset(*buffer, *size, clearFlag));
     118              : 
     119            2 :     return HCCL_SUCCESS;
     120            5 : }
     121              : 
     122           17 : HcclResult HcclGetHcclBuffer(HcclComm comm, void ** buffer, uint64_t *size)
     123              : {
     124           17 :     CHK_PRT_RET(buffer == nullptr, HCCL_ERROR("[%s] buffer is null", __func__), HCCL_E_PTR);
     125           14 :     CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[%s] comm is null", __func__), HCCL_E_PTR);
     126           13 :     CHK_PRT_RET(size == nullptr, HCCL_ERROR("[%s] size is null", __func__), HCCL_E_PTR);
     127              : 
     128              : #if (!defined (HCCD)) && (!defined (CCL_KERNEL_AICPU))
     129           15 :     HCCLV2_FUNC_RUN(
     130              :         [&]() -> HcclResult {
     131              :             return GetHcclBufferWithClearFlag(comm, buffer, size, false);
     132              :         }());
     133              : #endif
     134              : 
     135            7 :     auto *hcclComm = static_cast<hccl::hcclComm *>(comm);
     136            7 :     CollComm* collComm = hcclComm->GetCollComm();
     137            7 :     hccl::MyRank *myRank = nullptr;
     138            7 :     if (collComm != nullptr) {
     139            0 :         myRank = collComm->GetMyRank();
     140              :     }
     141            7 :     if (collComm != nullptr && hcclComm->GetConnectMode() !=0 && myRank != nullptr) {
     142            0 :         CommMems *commMem = myRank->GetCommMems();
     143            0 :         CHK_PTR_NULL(commMem);
     144            0 :         CHK_RET(commMem->GetHcclBuffer(*buffer, *size));
     145            0 :         return HCCL_SUCCESS;
     146              :     }
     147              : 
     148            7 :     std::string commId = hcclComm->GetIdentifier();
     149            7 :     HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
     150            7 :     HcclResult ret = HCCL_SUCCESS;
     151              :     CommBuffer commBuffer;
     152              :     
     153            7 :     auto& commMemMgr = hcclComm->GetIndependentOp().GetCommMemMgr();
     154            7 :     ret = commMemMgr.GetHcclBuffer(&commBuffer);
     155            7 :     if (ret != HCCL_SUCCESS) {
     156            0 :         HCCL_ERROR("[%s] Failed to get local cclBuffer ret[%d]", __func__, ret);
     157            0 :         return ret;
     158              :     }
     159            7 :     *buffer = commBuffer.addr;
     160            7 :     *size = commBuffer.size;
     161            7 :     HCCL_RUN_INFO("Entry-%s: success: comm[%s], buffer[%p] size[%llu]", __func__, commId.c_str(), *buffer, static_cast<unsigned long long>(*size));
     162            7 :     return HCCL_SUCCESS;
     163            7 : }
     164              : 
     165            1 : HcclResult HcclGetHcclBufferCleared(HcclComm comm, void **buffer, uint64_t *size)
     166              : {
     167            1 :     CHK_PRT_RET(buffer == nullptr, HCCL_ERROR("[%s] buffer is null", __func__), HCCL_E_PTR);
     168            1 :     CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[%s] comm is null", __func__), HCCL_E_PTR);
     169            1 :     CHK_PRT_RET(size == nullptr, HCCL_ERROR("[%s] size is null", __func__), HCCL_E_PTR);
     170              : 
     171              : #if (!defined (HCCD)) && (!defined (CCL_KERNEL_AICPU))
     172            2 :     HCCLV2_FUNC_RUN(
     173              :         [&]() -> HcclResult {
     174              :             return GetHcclBufferWithClearFlag(comm, buffer, size, true);
     175              :         }());
     176              : #endif
     177              : 
     178            0 :     HCCL_ERROR("HcclGetHcclBufferCleared is not supported");
     179            0 :     return HCCL_E_NOT_SUPPORT;
     180              : }
        

Generated by: LCOV version 2.0-1