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 "typical_window_mem.h"
11 : #include "adapter_rts_common.h"
12 :
13 : namespace hccl {
14 60 : TypicalWindowMem &TypicalWindowMem::GetInstance()
15 : {
16 126 : static TypicalWindowMem typicalWindowMem[MAX_MODULE_DEVICE_NUM + 1];
17 60 : s32 deviceLogicId = INVALID_INT;
18 60 : HcclResult ret = hrtGetDevice(&deviceLogicId);
19 60 : if (ret == HCCL_SUCCESS && (static_cast<u32>(deviceLogicId) < MAX_MODULE_DEVICE_NUM)) {
20 60 : HCCL_INFO("[TypicalWindowMem::GetInstance]deviceLogicID[%d]", deviceLogicId);
21 60 : return typicalWindowMem[deviceLogicId];
22 : }
23 0 : HCCL_WARNING("[TypicalWindowMem::GetInstance]deviceLogicID[%d] is invalid, ret[%d]", deviceLogicId, ret);
24 0 : return typicalWindowMem[MAX_MODULE_DEVICE_NUM];
25 : }
26 :
27 66 : TypicalWindowMem::TypicalWindowMem()
28 : {
29 66 : }
30 :
31 66 : TypicalWindowMem::~TypicalWindowMem()
32 : {
33 66 : FreeAllWinowMem();
34 66 : }
35 :
36 30 : HcclResult TypicalWindowMem::AllocWindowMem(void **ptr, uint64_t size)
37 : {
38 30 : HCCL_DEBUG("[TypicalWindowMem][AllocWindowMem]start alloc window mem to [%p] of size[%llu].", ptr, size);
39 : // Check ptr and size validation.
40 30 : CHK_PTR_NULL(ptr);
41 30 : CHK_PRT_RET(size < 1,
42 : HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]size[%lu], cannot alloc window mem less than 1.", size),
43 : HCCL_E_PARA);
44 :
45 : // Check whether the addr already exist in window mem map.
46 30 : std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
47 30 : if (*ptr != nullptr) {
48 30 : uint64_t inputAddr = reinterpret_cast<uintptr_t>(*ptr);
49 30 : auto wmIter = windowMemMap_.find(inputAddr);
50 30 : if (wmIter != windowMemMap_.end()) {
51 0 : HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]addr[%p] already allocated.", *ptr);
52 0 : return HCCL_E_PARA;
53 : }
54 30 : HCCL_WARNING("[TypicalWindowMem][AllocWindowMem]addr[%p] is not nullptr, " \
55 : "we will overwrite it with the new allocated address.", *ptr);
56 : }
57 : // Alloc window mem.
58 30 : CHK_RET(hrtMalloc(ptr, size));
59 30 : CHK_PRT_RET((*ptr) == nullptr,
60 : HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]In typical notify src buffer, malloc failed."), HCCL_E_MEMORY);
61 :
62 : // Add allocated mem into window mem map。
63 30 : uint64_t allocatedAddr = reinterpret_cast<uintptr_t>(*ptr);
64 30 : windowMemMap_[allocatedAddr] = size;
65 30 : HCCL_INFO("[TypicalWindowMem][AllocWindowMem]alloc window memory success, addr[%p], size[%llu]. " \
66 : "please register mr before use", *ptr, size);
67 30 : return HCCL_SUCCESS;
68 30 : }
69 :
70 30 : HcclResult TypicalWindowMem::FreeWindowMem(void *ptr)
71 : {
72 30 : HCCL_DEBUG("[TypicalWindowMem][FreeWindowMem]start free window mem on [%p], please deregister mr before free.",
73 : ptr);
74 30 : CHK_PTR_NULL(ptr);
75 : // Check whether the addr exist in window mem map. Remove allocated mem from window mem map if exists.
76 30 : uint64_t addr = reinterpret_cast<uintptr_t>(ptr);
77 30 : std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
78 30 : auto wmIter = windowMemMap_.find(addr);
79 30 : if (wmIter == windowMemMap_.end()) {
80 0 : HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]addr[%p] were not allocated or already freed.", ptr);
81 0 : return HCCL_E_PARA;
82 : }
83 : // Free window mem.
84 30 : CHK_RET(hrtFree(ptr));
85 30 : windowMemMap_.erase(wmIter);
86 30 : ptr = nullptr;
87 30 : HCCL_INFO("[TypicalWindowMem][AllocWindowMem]free window memory success, addr[%p].", ptr);
88 30 : return HCCL_SUCCESS;
89 30 : }
90 :
91 66 : HcclResult TypicalWindowMem::FreeAllWinowMem()
92 : {
93 66 : std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
94 66 : if (!windowMemMap_.empty()) {
95 0 : for (auto &wmIter : windowMemMap_) {
96 0 : auto ptr = reinterpret_cast<void *>(static_cast<uintptr_t>(wmIter.first));
97 0 : if (ptr != nullptr) {
98 0 : CHK_RET(hrtFree(ptr));
99 : }
100 : }
101 0 : windowMemMap_.clear();
102 : }
103 66 : HCCL_INFO("[TypicalWindowMem][FreeAllWinowMem]free all window memory success.");
104 66 : return HCCL_SUCCESS;
105 66 : }
106 : } // namespace hccl
|