LCOV - code coverage report
Current view: top level - base_comm/resources/comm_engine_res - hcomm_mem_alloc.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 50 50
Test Date: 2026-08-17 10:19:35 Functions: 100.0 % 2 2

            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              : #include "hcomm_mem_alloc.h"
      11              : 
      12              : #include "acl/acl_rt.h"
      13              : #include "log.h"
      14              : #include "param_check_pub.h"
      15              : 
      16              : namespace hcomm {
      17              : namespace {
      18              : #define ALIGN_SIZE(size, align) ({ (size) = (((size) + (align) - 1) / (align)) * (align); })
      19              : } // namespace
      20              : 
      21            8 : HcommResult MemAlloc(void** ptr, size_t size)
      22              : {
      23            8 :     CHK_PTR_NULL(ptr);
      24            8 :     CHK_PRT_RET(size == 0, HCCL_ERROR("[%s] size is zero", __func__), HCCL_E_PARA);
      25              : 
      26            7 :     aclError ret = ACL_SUCCESS;
      27            7 :     int32_t deviceId = 0;
      28            7 :     ret = aclrtGetDevice(&deviceId);
      29            7 :     CHK_PRT_RET(ret != ACL_SUCCESS, HCCL_ERROR("[%s] GetDevice failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
      30              : 
      31              :     aclrtPhysicalMemProp prop;
      32            6 :     prop.handleType = ACL_MEM_HANDLE_TYPE_NONE;
      33            6 :     prop.allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED;
      34            6 :     prop.memAttr = ACL_HBM_MEM_HUGE;
      35            6 :     prop.location.id = deviceId;
      36            6 :     prop.location.type = ACL_MEM_LOCATION_TYPE_DEVICE;
      37            6 :     prop.reserve = 0;
      38              : 
      39            6 :     size_t allocSize = size;
      40            6 :     size_t granularity = 0;
      41            6 :     ret = aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_RECOMMENDED, &granularity);
      42            6 :     CHK_PRT_RET(
      43              :         ret != ACL_SUCCESS || granularity == 0,
      44              :         HCCL_ERROR("[%s] GetAllocationGranularity failed, granularity[%llu], ret[%d]", __func__, granularity, ret),
      45              :         HCCL_E_RUNTIME);
      46            4 :     ALIGN_SIZE(allocSize, granularity);
      47            4 :     HCCL_INFO(
      48              :         "[%s] deviceId[%d], granularity[%llu], size[%llu], allocSize[%llu].", __func__, deviceId, granularity, size,
      49              :         allocSize);
      50              : 
      51            4 :     ret = aclrtReserveMemAddress(ptr, allocSize, 0, nullptr, 1);
      52            4 :     CHK_PRT_RET(
      53              :         ret != ACL_SUCCESS,
      54              :         HCCL_ERROR("[%s] ReserveMemAddress failed, virPtr[%p] size[%llu], ret[%d]", __func__, ptr, allocSize, ret),
      55              :         HCCL_E_RUNTIME);
      56              : 
      57            3 :     void* virPtr = *ptr;
      58              :     aclrtDrvMemHandle handle;
      59            3 :     ret = aclrtMallocPhysical(&handle, allocSize, &prop, 0);
      60            3 :     if (ret != ACL_SUCCESS) {
      61            1 :         HCCL_ERROR("[%s] MallocPhysical failed, size[%llu], ret[%d]", __func__, allocSize, ret);
      62            1 :         aclrtReleaseMemAddress(virPtr);
      63            1 :         return HCCL_E_RUNTIME;
      64              :     }
      65            2 :     HCCL_INFO("[%s] Start to MapMem virPtr[%p], handle[%p]", __func__, virPtr, handle);
      66            2 :     ret = aclrtMapMem(virPtr, allocSize, 0, handle, 0);
      67            2 :     if (ret != ACL_SUCCESS) {
      68            1 :         HCCL_ERROR(
      69              :             "[%s] MapMem virPtr[%p] size[%llu] handle[%p] failed, ret[%d]", __func__, virPtr, allocSize, handle, ret);
      70            1 :         aclrtFreePhysical(handle);
      71            1 :         aclrtReleaseMemAddress(virPtr);
      72            1 :         return HCCL_E_RUNTIME;
      73              :     }
      74              : 
      75            1 :     return HCCL_SUCCESS;
      76              : }
      77              : 
      78            7 : HcommResult MemFree(void* ptr)
      79              : {
      80            7 :     if (ptr == nullptr) {
      81            2 :         HCCL_DEBUG("[%s] virPtr is nullptr.", __func__);
      82            2 :         return HCCL_SUCCESS;
      83              :     }
      84            5 :     aclError ret = ACL_SUCCESS;
      85              :     aclrtDrvMemHandle handle;
      86            5 :     ret = aclrtMemRetainAllocationHandle(ptr, &handle);
      87            5 :     CHK_PRT_RET(
      88              :         ret != ACL_SUCCESS, HCCL_ERROR("[%s] RetainAllocationHandle virPtr[%p] failed, ret[%d]", __func__, ptr, ret),
      89              :         HCCL_E_RUNTIME);
      90            4 :     HCCL_INFO("[%s] Start to UnmapMem virPtr[%p], handle[%p]", __func__, ptr, handle);
      91            4 :     ret = aclrtUnmapMem(ptr);
      92            4 :     CHK_PRT_RET(
      93              :         ret != ACL_SUCCESS, HCCL_ERROR("[%s] UnmapMem virPtr[%p] failed, ret[%d]", __func__, ptr, ret), HCCL_E_RUNTIME);
      94            3 :     ret = aclrtFreePhysical(handle);
      95            3 :     CHK_PRT_RET(
      96              :         ret != ACL_SUCCESS, HCCL_ERROR("[%s] FreePhysical handle[%p] failed, ret[%d]", __func__, handle, ret),
      97              :         HCCL_E_RUNTIME);
      98            2 :     ret = aclrtReleaseMemAddress(ptr);
      99            2 :     CHK_PRT_RET(
     100              :         ret != ACL_SUCCESS, HCCL_ERROR("[%s] ReleaseMemAddress virPtr[%p] failed, ret[%d]", __func__, ptr, ret),
     101              :         HCCL_E_RUNTIME);
     102            1 :     return HCCL_SUCCESS;
     103              : }
     104              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1