Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "hcomm_c_adpt.h"
11 : #include "hcomm_c_adpt_common.h"
12 : #include "hcomm_result_defs.h"
13 : #include "log.h"
14 : #include "endpoint.h"
15 : #include "param_check_pub.h"
16 : #include "exception_handler.h"
17 : #include "hcomm_res_defs.h"
18 : #include "hcomm_res.h"
19 : #include "hcomm_mem_alloc.h"
20 : #ifdef ENABLE_EXPERIMENTAL
21 : #include "nic_plugin_dispatcher.h"
22 : #endif
23 :
24 : using namespace hcomm;
25 :
26 47 : HcommResult HcommMemReg(
27 : EndpointHandle endpointHandle, const char *memTag, const CommMem *mem, HcommMemHandle *memHandle)
28 : {
29 47 : CHK_PTR_NULL(memHandle);
30 : EXCEPTION_HANDLE_BEGIN
31 56 : CHK_PTR_NULL(mem);
32 44 : CHK_PTR_NULL(memHandle);
33 44 : (void)HcommResMgrInit();
34 44 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
35 : #ifdef ENABLE_EXPERIMENTAL
36 44 : bool handled = false;
37 44 : CHK_RET(static_cast<HcclResult>(PluginMemReg(endpointHandle, memTag, mem, memHandle, handled)));
38 44 : if (handled) {
39 1 : return HCCL_SUCCESS;
40 : }
41 : #endif
42 :
43 43 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
44 43 : CHK_PRT_RET(endpoint == nullptr,
45 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
46 42 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
47 42 : CHK_RET(endpoint->RegisterMemory(*mem, memTag, reinterpret_cast<void **>(memHandle)));
48 0 : EXCEPTION_HANDLE_END
49 33 : return HCCL_SUCCESS;
50 : }
51 :
52 43 : HcommResult HcommMemUnreg(EndpointHandle endpointHandle, HcommMemHandle memHandle)
53 : {
54 43 : CHK_PTR_NULL(memHandle);
55 36 : (void)HcommResMgrInit();
56 : EXCEPTION_HANDLE_BEGIN
57 36 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
58 : #ifdef ENABLE_EXPERIMENTAL
59 36 : bool handled = false;
60 47 : CHK_RET(static_cast<HcclResult>(PluginMemUnreg(endpointHandle, memHandle, handled)));
61 36 : if (handled) {
62 1 : return HCCL_SUCCESS;
63 : }
64 : #endif
65 :
66 35 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
67 35 : CHK_PRT_RET(endpoint == nullptr,
68 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
69 34 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
70 34 : CHK_RET(endpoint->UnregisterMemory(memHandle));
71 0 : EXCEPTION_HANDLE_END
72 25 : return HCCL_SUCCESS;
73 : }
74 :
75 7 : HcommResult HcommMemExport(
76 : EndpointHandle endpointHandle, HcommMemHandle memHandle, void **memDesc, uint32_t *memDescLen)
77 : {
78 7 : CHK_PTR_NULL(memHandle);
79 6 : CHK_PTR_NULL(memDesc);
80 5 : CHK_PTR_NULL(memDescLen);
81 5 : (void)HcommResMgrInit();
82 5 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
83 : #ifdef ENABLE_EXPERIMENTAL
84 5 : bool handled = false;
85 5 : CHK_RET(static_cast<HcclResult>(PluginMemExport(endpointHandle, memHandle, memDesc, memDescLen, handled)));
86 4 : if (handled) {
87 1 : return HCCL_SUCCESS;
88 : }
89 : #endif
90 :
91 3 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
92 3 : CHK_PRT_RET(endpoint == nullptr,
93 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
94 2 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
95 2 : CHK_RET(endpoint->MemoryExport(memHandle, memDesc, memDescLen));
96 2 : return HCCL_SUCCESS;
97 : }
98 :
99 6 : HcommResult HcommMemImport(EndpointHandle endpointHandle, const void *memDesc, uint32_t descLen, CommMem *outMem)
100 : {
101 6 : CHK_PTR_NULL(memDesc);
102 5 : CHK_PTR_NULL(outMem);
103 5 : CHK_PRT_RET(descLen == 0, HCCL_ERROR("[%s] descLen[0] is invalid", __func__), HCCL_E_PARA);
104 4 : (void)HcommResMgrInit();
105 4 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
106 : #ifdef ENABLE_EXPERIMENTAL
107 4 : bool handled = false;
108 4 : CHK_RET(static_cast<HcclResult>(PluginMemImport(endpointHandle, memDesc, descLen, outMem, handled)));
109 3 : if (handled) {
110 1 : return HCCL_SUCCESS;
111 : }
112 : #endif
113 :
114 2 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
115 2 : CHK_PRT_RET(endpoint == nullptr,
116 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
117 2 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
118 2 : CHK_PTR_NULL(outMem);
119 2 : CommMem importedMem{};
120 2 : CHK_RET(endpoint->MemoryImport(memDesc, descLen, &importedMem));
121 2 : *outMem = importedMem;
122 2 : return HCCL_SUCCESS;
123 : }
124 :
125 5 : HcommResult HcommMemUnimport(EndpointHandle endpointHandle, const void *memDesc, uint32_t descLen)
126 : {
127 5 : CHK_PTR_NULL(memDesc);
128 4 : (void)HcommResMgrInit();
129 4 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
130 : #ifdef ENABLE_EXPERIMENTAL
131 4 : bool handled = false;
132 4 : CHK_RET(static_cast<HcclResult>(PluginMemUnimport(endpointHandle, memDesc, descLen, handled)));
133 3 : if (handled) {
134 1 : return HCCL_SUCCESS;
135 : }
136 : #endif
137 :
138 2 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
139 2 : CHK_PRT_RET(endpoint == nullptr,
140 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
141 2 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
142 2 : CHK_RET(endpoint->MemoryUnimport(memDesc, descLen));
143 2 : return HCCL_SUCCESS;
144 : }
145 :
146 : /* 暂未实现 */
147 2 : HcommResult HcommMemGrant(EndpointHandle endpointHandle, const HcommMemGrantInfo *remoteGrantInfo)
148 : {
149 2 : CHK_PTR_NULL(remoteGrantInfo);
150 2 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
151 :
152 2 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
153 2 : CHK_PRT_RET(endpoint == nullptr,
154 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
155 0 : CHK_RET(endpoint->MemoryGrant(remoteGrantInfo));
156 0 : return HCCL_SUCCESS;
157 : }
158 :
159 : /* 暂未实现 */
160 0 : HcommResult HcommMemRemap(const EndpointHandle endpointHandle, const CommMem *memArray, uint64_t arraySize)
161 : {
162 0 : return HCCL_E_NOT_SUPPORT;
163 : }
164 :
165 3 : HcommResult HcommMemGetAllMemHandles(EndpointHandle endpointHandle, void **memHandles, uint32_t *memHandleNum)
166 : {
167 3 : CHK_PTR_NULL(memHandles);
168 2 : CHK_PTR_NULL(memHandleNum);
169 :
170 2 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
171 2 : CHK_PRT_RET(endpoint == nullptr,
172 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
173 0 : CHK_RET(endpoint->GetAllMemHandles(memHandles, memHandleNum));
174 0 : return HCCL_SUCCESS;
175 : }
176 :
177 8 : HcommResult HcommMemAlloc(void **ptr, size_t size)
178 : {
179 8 : return hcomm::MemAlloc(ptr, size);
180 : }
181 :
182 7 : HcommResult HcommMemFree(void *ptr)
183 : {
184 7 : return hcomm::MemFree(ptr);
185 : }
|