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) \
19 : ({ \
20 : (size) = (((size) + (align) - 1) / (align)) * (align);\
21 : })
22 : } // namespace
23 :
24 8 : HcommResult MemAlloc(void **ptr, size_t size)
25 : {
26 8 : CHK_PTR_NULL(ptr);
27 8 : CHK_PRT_RET(size == 0, HCCL_ERROR("[%s] size is zero", __func__), HCCL_E_PARA);
28 :
29 7 : aclError ret = ACL_SUCCESS;
30 7 : int32_t deviceId = 0;
31 7 : ret = aclrtGetDevice(&deviceId);
32 7 : CHK_PRT_RET(ret != ACL_SUCCESS, HCCL_ERROR("[%s] GetDevice failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
33 :
34 : aclrtPhysicalMemProp prop;
35 6 : prop.handleType = ACL_MEM_HANDLE_TYPE_NONE;
36 6 : prop.allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED;
37 6 : prop.memAttr = ACL_HBM_MEM_HUGE;
38 6 : prop.location.id = deviceId;
39 6 : prop.location.type = ACL_MEM_LOCATION_TYPE_DEVICE;
40 6 : prop.reserve = 0;
41 :
42 6 : size_t allocSize = size;
43 6 : size_t granularity = 0;
44 6 : ret = aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_RECOMMENDED, &granularity);
45 6 : CHK_PRT_RET(ret != ACL_SUCCESS || granularity == 0,
46 : HCCL_ERROR("[%s] GetAllocationGranularity failed, granularity[%llu], ret[%d]", __func__, granularity, ret),
47 : HCCL_E_RUNTIME);
48 4 : ALIGN_SIZE(allocSize, granularity);
49 4 : HCCL_INFO("[%s] deviceId[%d], granularity[%llu], size[%llu], allocSize[%llu].",
50 : __func__, deviceId, granularity, size, allocSize);
51 :
52 4 : ret = aclrtReserveMemAddress(ptr, allocSize, 0, nullptr, 1);
53 4 : CHK_PRT_RET(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("[%s] MapMem virPtr[%p] size[%llu] handle[%p] failed, ret[%d]",
69 : __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(ret != ACL_SUCCESS,
88 : HCCL_ERROR("[%s] RetainAllocationHandle virPtr[%p] failed, ret[%d]", __func__, ptr, ret), HCCL_E_RUNTIME);
89 4 : HCCL_INFO("[%s] Start to UnmapMem virPtr[%p], handle[%p]", __func__, ptr, handle);
90 4 : ret = aclrtUnmapMem(ptr);
91 4 : CHK_PRT_RET(ret != ACL_SUCCESS,
92 : HCCL_ERROR("[%s] UnmapMem virPtr[%p] failed, ret[%d]", __func__, ptr, ret), HCCL_E_RUNTIME);
93 3 : ret = aclrtFreePhysical(handle);
94 3 : CHK_PRT_RET(ret != ACL_SUCCESS,
95 : HCCL_ERROR("[%s] FreePhysical handle[%p] failed, ret[%d]", __func__, handle, ret), HCCL_E_RUNTIME);
96 2 : ret = aclrtReleaseMemAddress(ptr);
97 2 : CHK_PRT_RET(ret != ACL_SUCCESS,
98 : HCCL_ERROR("[%s] ReleaseMemAddress virPtr[%p] failed, ret[%d]", __func__, ptr, ret), HCCL_E_RUNTIME);
99 1 : return HCCL_SUCCESS;
100 : }
101 : } // namespace hcomm
|