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