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 :
21 : using namespace hcomm;
22 :
23 : HcommResult
24 55 : HcommMemReg(EndpointHandle endpointHandle, const char* memTag, const CommMem* mem, HcommMemHandle* memHandle)
25 : {
26 55 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
27 55 : CHK_PRT_RET(
28 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
29 : HCCL_E_NOT_FOUND);
30 : return static_cast<HcclResult>(
31 54 : endpoint->GetNicOps()->registerMemory(endpoint->GetNicCtx(), mem, memTag, reinterpret_cast<void**>(memHandle)));
32 : }
33 :
34 63 : HcommResult HcommMemUnreg(EndpointHandle endpointHandle, HcommMemHandle memHandle)
35 : {
36 63 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
37 63 : CHK_PRT_RET(
38 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
39 : HCCL_E_NOT_FOUND);
40 49 : return static_cast<HcclResult>(endpoint->GetNicOps()->unregisterMemory(endpoint->GetNicCtx(), memHandle));
41 : }
42 :
43 : HcommResult
44 8 : HcommMemExport(EndpointHandle endpointHandle, HcommMemHandle memHandle, void** memDesc, uint32_t* memDescLen)
45 : {
46 8 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
47 8 : CHK_PRT_RET(
48 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
49 : HCCL_E_NOT_FOUND);
50 : return static_cast<HcclResult>(
51 7 : endpoint->GetNicOps()->memoryExport(endpoint->GetNicCtx(), memHandle, memDesc, memDescLen));
52 : }
53 :
54 7 : HcommResult HcommMemImport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen, CommMem* outMem)
55 : {
56 7 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
57 7 : CHK_PRT_RET(
58 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
59 : HCCL_E_NOT_FOUND);
60 : return static_cast<HcclResult>(
61 7 : endpoint->GetNicOps()->memoryImport(endpoint->GetNicCtx(), memDesc, descLen, outMem));
62 : }
63 :
64 6 : HcommResult HcommMemUnimport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen)
65 : {
66 6 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
67 6 : CHK_PRT_RET(
68 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
69 : HCCL_E_NOT_FOUND);
70 6 : return static_cast<HcclResult>(endpoint->GetNicOps()->memoryUnimport(endpoint->GetNicCtx(), memDesc, descLen));
71 : }
72 :
73 : /* 暂未实现 */
74 0 : HcommResult HcommMemGrant(EndpointHandle endpointHandle, const HcommMemGrantInfo* remoteGrantInfo)
75 : {
76 0 : CHK_PTR_NULL(remoteGrantInfo);
77 0 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
78 :
79 0 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
80 0 : CHK_PRT_RET(
81 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),
82 : HCCL_E_NOT_FOUND);
83 0 : CHK_RET(endpoint->MemoryGrant(remoteGrantInfo));
84 0 : return HCCL_SUCCESS;
85 : }
86 :
87 : /* 暂未实现 */
88 0 : HcommResult HcommMemRemap(const EndpointHandle endpointHandle, const CommMem* memArray, uint64_t arraySize)
89 : {
90 0 : return HCCL_E_NOT_SUPPORT;
91 : }
92 :
93 1 : HcommResult HcommMemGetAllMemHandles(EndpointHandle endpointHandle, void** memHandles, uint32_t* memHandleNum)
94 : {
95 1 : CHK_PTR_NULL(memHandles);
96 0 : CHK_PTR_NULL(memHandleNum);
97 :
98 0 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
99 0 : CHK_PRT_RET(
100 : endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),
101 : HCCL_E_NOT_FOUND);
102 0 : CHK_RET(endpoint->GetAllMemHandles(memHandles, memHandleNum));
103 0 : return HCCL_SUCCESS;
104 : }
105 :
106 8 : HcommResult HcommMemAlloc(void** ptr, size_t size) { return hcomm::MemAlloc(ptr, size); }
107 :
108 7 : HcommResult HcommMemFree(void* ptr) { return hcomm::MemFree(ptr); }
|