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