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