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 "endpoint_mgr.h"
11 : #include <algorithm>
12 : #include "hcomm_c_adpt.h"
13 :
14 : namespace hcomm {
15 :
16 109 : EndpointMgr::~EndpointMgr()
17 : {
18 109 : for (const auto &kv : endpointMemMap_) {
19 0 : const EndpointHandle &endpointHandle = kv.first;
20 0 : const std::vector<MemHandle> &memHandleVec = kv.second;
21 :
22 0 : for (auto menHandle : memHandleVec) {
23 0 : (void)HcommMemUnreg(endpointHandle, menHandle);
24 : }
25 : }
26 :
27 112 : for (const auto &kv : endpointMap_) {
28 3 : const EndpointHandle &endpointHandle = kv.second;
29 3 : (void)HcommEndpointDestroy(endpointHandle);
30 : }
31 109 : }
32 :
33 18 : HcclResult EndpointMgr::Get(EndpointDesc epDesc, EndpointHandle &handle)
34 : {
35 18 : auto iterPtr = endpointMap_.find(epDesc);
36 18 : if (iterPtr != endpointMap_.end()) {
37 15 : handle = iterPtr->second;
38 15 : return HCCL_SUCCESS;
39 : }
40 3 : HCCL_INFO("[EndpointMgr::Get] create Endpoint");
41 3 : CHK_RET(static_cast<HcclResult>(HcommEndpointCreate(&epDesc, &handle)));
42 :
43 3 : endpointMap_.emplace(epDesc, handle);
44 3 : return HCCL_SUCCESS;
45 : }
46 :
47 0 : HcclResult EndpointMgr::RegisterMemory(EndpointHandle epHandle, const std::vector<std::string>& memTag,
48 : const std::vector<HcclMem>& memVec, std::vector<MemHandle>& memHandleVec)
49 : {
50 0 : memHandleVec.clear();
51 0 : uint32_t index = 0;
52 0 : for (const auto &mem: memVec) {
53 0 : MemHandle memHandle = nullptr;
54 : CommMem commMem {
55 0 : static_cast<CommMemType>(mem.type),
56 0 : mem.addr,
57 0 : mem.size
58 0 : };
59 0 : HcclResult ret = static_cast<HcclResult>(HcommMemReg(epHandle, memTag[index].c_str(), &commMem, &memHandle));
60 0 : if(ret != HCCL_SUCCESS && ret != HCCL_E_AGAIN) {
61 0 : HCCL_ERROR("[%s]call trace: hcclRet -> %d", __FUNCTION__, ret);
62 0 : return ret;
63 : }
64 0 : CHK_PTR_NULL(memHandle);
65 0 : memHandleVec.push_back(memHandle);
66 0 : index++;
67 0 : if(ret == HCCL_E_AGAIN) {
68 0 : HCCL_WARNING("This mem has already been registered, addr=%p, size=%llu", mem.addr, mem.size);
69 : }
70 : }
71 0 : CHK_RET(AddMemHandle(epHandle, memHandleVec));
72 0 : return HCCL_SUCCESS;
73 : }
74 :
75 0 : HcclResult EndpointMgr::AddMemHandle(EndpointHandle epHandle, const std::vector<MemHandle>& memHandleVec)
76 : {
77 0 : if (memHandleVec.empty()) {
78 0 : return HCCL_SUCCESS;
79 : }
80 :
81 0 : if (IsMemExist(epHandle)) {
82 0 : auto& existMemHandleVec = endpointMemMap_.at(epHandle);
83 0 : existMemHandleVec.insert(existMemHandleVec.end(), memHandleVec.begin(), memHandleVec.end());
84 0 : return HCCL_SUCCESS;
85 : }
86 :
87 0 : endpointMemMap_.emplace(epHandle, std::move(memHandleVec));
88 0 : return HCCL_SUCCESS;
89 : }
90 :
91 0 : bool EndpointMgr::IsMemExist(EndpointHandle epHandle)
92 : {
93 0 : return endpointMemMap_.find(epHandle) != endpointMemMap_.end();
94 : }
95 :
96 0 : bool EndpointMgr::IsDescExist(EndpointDesc epDesc)
97 : {
98 0 : return endpointMap_.find(epDesc) != endpointMap_.end();
99 : }
100 :
101 0 : HcclResult EndpointMgr::GetAllRegisteredMemory(EndpointHandle epHandle, std::vector<MemHandle>& memHandleVec)
102 : {
103 0 : if (!IsMemExist(epHandle)) {
104 0 : HCCL_ERROR("EndpointMgr GetAllRegisteredMemory Fail");
105 0 : return HCCL_E_MEMORY;
106 : }
107 0 : memHandleVec = endpointMemMap_.at(epHandle);
108 0 : return HCCL_SUCCESS;
109 : }
110 :
111 : } // namespace hcomm
|