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 "hccl_mem_defs.h"
11 : #include "aicputs_hccs_endpoint.h"
12 : #include "log.h"
13 : #include "net_dev/global_net_dev_manager.h"
14 : #include "hccs_reged_mem_mgr.h"
15 :
16 : using namespace hccl;
17 :
18 : namespace hcomm {
19 15 : AicpuTsHccsEndpoint::AicpuTsHccsEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
20 :
21 30 : AicpuTsHccsEndpoint::~AicpuTsHccsEndpoint()
22 : {
23 : try {
24 15 : (void)ServerSocketStopListenImpl(serverPort_);
25 0 : } catch (...) {
26 0 : }
27 :
28 15 : if (regedMemMgr_ != nullptr) {
29 14 : regedMemMgr_ = nullptr;
30 : }
31 :
32 : try {
33 15 : if (netDevCtx_ != nullptr) {
34 14 : (void)hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId)
35 14 : .UnRefNetDevCtx(NicType::VNIC_TYPE, devIpAddr_, serverPort_);
36 14 : netDevCtx_ = nullptr;
37 : }
38 0 : } catch (...) {
39 0 : }
40 30 : }
41 :
42 15 : HcclResult AicpuTsHccsEndpoint::Init()
43 : {
44 15 : HCCL_INFO(
45 : "[%s]localEndpoint protocol[%d], type[%d], id[%u] locType[%d], devPhyId[%u], serverIdx[%u], "
46 : "superDevId[%u], superPodIdx[%u]",
47 : __func__, endpointDesc_.protocol, endpointDesc_.commAddr.type, endpointDesc_.commAddr.id,
48 : endpointDesc_.loc.locType, endpointDesc_.loc.device.devPhyId, endpointDesc_.loc.device.serverIdx,
49 : endpointDesc_.loc.device.superDevId, endpointDesc_.loc.device.superPodIdx);
50 :
51 15 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
52 0 : HCCL_INFO("[AicpuTsHccsEndpoint][%s] AicpuTsHccsEndpoint not support host", __func__);
53 0 : return HCCL_E_NOT_SUPPORT;
54 : }
55 :
56 15 : u32 devPhyId = endpointDesc_.loc.device.devPhyId;
57 15 : uint32_t superDevId = endpointDesc_.loc.device.superDevId;
58 15 : CHK_RET(GlobalNetDevMgr::GetDeviceVnicIP(devPhyId, superDevId, devIpAddr_));
59 14 : HCCL_INFO(
60 : "[AicpuTsHccsEndpoint]devPhyId[%u] superDevId[%u] devIpAddr_[%s] ", devPhyId, superDevId,
61 : devIpAddr_.GetReadableAddress());
62 :
63 14 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId)
64 : .RefNetDevCtx(NicType::VNIC_TYPE, devIpAddr_, serverPort_, netDevCtx_));
65 14 : EXCEPTION_CATCH(regedMemMgr_ = std::make_shared<HccsRegedMemMgr>(netDevCtx_), return HCCL_E_PARA);
66 14 : return HCCL_SUCCESS;
67 : }
68 :
69 0 : HcclResult AicpuTsHccsEndpoint::ServerSocketListen(const uint32_t port)
70 : {
71 0 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).ServerInit(serverPort_));
72 0 : serverListened_ = true;
73 0 : return HCCL_SUCCESS;
74 : }
75 :
76 15 : inline HcclResult AicpuTsHccsEndpoint::ServerSocketStopListenImpl(const uint32_t port)
77 : {
78 15 : if (serverListened_) {
79 0 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).ServerDeInit(port));
80 0 : serverListened_ = false;
81 : }
82 :
83 15 : return HCCL_SUCCESS;
84 : }
85 :
86 0 : HcclResult AicpuTsHccsEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
87 :
88 13 : HcclResult AicpuTsHccsEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
89 : {
90 13 : CHK_RET(GetRegedMemMgr()->RegisterMemory(mem, memTag, memHandle));
91 10 : return HCCL_SUCCESS;
92 : }
93 :
94 13 : HcclResult AicpuTsHccsEndpoint::UnregisterMemory(void* memHandle)
95 : {
96 13 : CHK_RET(GetRegedMemMgr()->UnregisterMemory(memHandle));
97 10 : return HCCL_SUCCESS;
98 : }
99 :
100 2 : HcclResult AicpuTsHccsEndpoint::MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen)
101 : {
102 2 : CHK_RET(GetRegedMemMgr()->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
103 2 : return HCCL_SUCCESS;
104 : }
105 :
106 2 : HcclResult AicpuTsHccsEndpoint::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
107 : {
108 2 : CHK_RET(GetRegedMemMgr()->MemoryImport(memDesc, descLen, outMem));
109 2 : return HCCL_SUCCESS;
110 : }
111 :
112 2 : HcclResult AicpuTsHccsEndpoint::MemoryUnimport(const void* memDesc, uint32_t descLen)
113 : {
114 2 : CHK_RET(GetRegedMemMgr()->MemoryUnimport(memDesc, descLen));
115 2 : return HCCL_SUCCESS;
116 : }
117 :
118 0 : HcclResult AicpuTsHccsEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
119 : {
120 0 : CHK_RET(GetRegedMemMgr()->GetAllMemHandles(memHandles, memHandleNum));
121 0 : return HCCL_SUCCESS;
122 : }
123 :
124 2 : HcclResult AicpuTsHccsEndpoint::MemoryGrant(const HcommMemGrantInfo* remoteGrantInfo)
125 : {
126 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
127 2 : CHK_PTR_NULL(mgr);
128 2 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
129 2 : CHK_RET(hccsRegedMemMgr->MemoryGrant(remoteGrantInfo));
130 2 : return HCCL_SUCCESS;
131 2 : }
132 :
133 2 : HcclResult AicpuTsHccsEndpoint::MemoryEnableP2P(const EndpointDesc& remoteEndpointDesc)
134 : {
135 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
136 2 : CHK_PTR_NULL(mgr);
137 2 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
138 2 : CHK_RET(hccsRegedMemMgr->MemoryEnableP2P(GetEndpointDesc(), remoteEndpointDesc));
139 2 : return HCCL_SUCCESS;
140 2 : }
141 :
142 2 : HcclResult AicpuTsHccsEndpoint::MemoryDisableP2P(const EndpointDesc& remoteEndpointDesc)
143 : {
144 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
145 2 : CHK_PTR_NULL(mgr);
146 2 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
147 2 : CHK_RET(hccsRegedMemMgr->MemoryDisableP2P(GetEndpointDesc(), remoteEndpointDesc));
148 2 : return HCCL_SUCCESS;
149 2 : }
150 :
151 2 : HcclResult AicpuTsHccsEndpoint::MemoryOpenRemoteIpc()
152 : {
153 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
154 2 : CHK_PTR_NULL(mgr);
155 2 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
156 2 : CHK_RET(hccsRegedMemMgr->MemoryOpenRemoteIpc());
157 2 : return HCCL_SUCCESS;
158 2 : }
159 :
160 2 : HcclResult AicpuTsHccsEndpoint::MemoryCloseRemoteIpc()
161 : {
162 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
163 2 : CHK_PTR_NULL(mgr);
164 2 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
165 2 : CHK_RET(hccsRegedMemMgr->MemoryCloseRemoteIpc());
166 2 : return HCCL_SUCCESS;
167 2 : }
168 :
169 0 : HcclResult AicpuTsHccsEndpoint::GetRemoteIpcRmaBuffer(std::vector<CommMem>& remoteIpcRmaBufferVec)
170 : {
171 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
172 0 : CHK_PTR_NULL(mgr);
173 0 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
174 0 : CHK_RET(hccsRegedMemMgr->GetRemoteIpcRmaBuffer(remoteIpcRmaBufferVec));
175 0 : return HCCL_SUCCESS;
176 0 : }
177 :
178 0 : HcclResult AicpuTsHccsEndpoint::GetRemoteIpcRmaBufferEx(std::vector<HcclMemEx>& remoteIpcRmaBufferVecEx)
179 : {
180 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
181 0 : CHK_PTR_NULL(mgr);
182 0 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
183 0 : CHK_RET(hccsRegedMemMgr->GetRemoteIpcRmaBufferEx(remoteIpcRmaBufferVecEx));
184 0 : return HCCL_SUCCESS;
185 0 : }
186 :
187 0 : HcclResult AicpuTsHccsEndpoint::GetLocalIpcRmaBufferEx(std::vector<HcclMemEx>& localIpcRmaBufferVecEx)
188 : {
189 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
190 0 : CHK_PTR_NULL(mgr);
191 0 : HccsRegedMemMgr* hccsRegedMemMgr = (HccsRegedMemMgr*)mgr.get();
192 0 : CHK_RET(hccsRegedMemMgr->GetLocalIpcRmaBufferEx(localIpcRmaBufferVecEx));
193 0 : return HCCL_SUCCESS;
194 0 : }
195 : } // namespace hcomm
|