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 "remote_ipc_rma_buffer_impl.h"
12 : #include "hccl_network.h"
13 :
14 : namespace hccl {
15 4 : RemoteIpcRmaBufferImpl::RemoteIpcRmaBufferImpl(const HcclNetDevCtx netDevCtx)
16 : : RmaBuffer(netDevCtx, nullptr, 0, RmaMemType::TYPE_NUM, RmaType::IPC_RMA),
17 4 : netDevCtx(netDevCtx)
18 4 : {}
19 :
20 8 : RemoteIpcRmaBufferImpl::~RemoteIpcRmaBufferImpl() {}
21 :
22 4 : HcclResult RemoteIpcRmaBufferImpl::Deserialize(const std::string& msg)
23 : {
24 4 : std::istringstream iss(msg);
25 4 : iss.read(reinterpret_cast<char_t*>(&memName.ipcName), sizeof(memName.ipcName));
26 4 : iss.read(reinterpret_cast<char_t*>(&memOffset), sizeof(memOffset));
27 4 : HCCL_DEBUG("[RemoteIpcRmaBufferImpl][Deserialize]ipcName[%s], memOffset[%lu]", memName.ipcName, memOffset);
28 4 : return HCCL_SUCCESS;
29 4 : }
30 :
31 1 : HcclResult RemoteIpcRmaBufferImpl::Open()
32 : {
33 1 : if (memType == RmaMemType::HOST) {
34 0 : HCCL_ERROR("[RemoteIpcRmaBufferImpl][Open]remote memType[%d] not support.", memType);
35 0 : return HCCL_E_PARA;
36 : }
37 :
38 1 : s32 deviceLogicId = 0;
39 1 : if (netDevCtx != nullptr) {
40 1 : deviceLogicId = (static_cast<NetDevContext*>(netDevCtx))->GetLogicId();
41 : } else {
42 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
43 : }
44 1 : bool firstOpened = false;
45 : HcclResult ret = MemNameRepository::GetInstance(deviceLogicId)
46 1 : ->OpenIpcMem(&devAddr, size, memName.ipcName, HCCL_IPC_MEM_NAME_LEN, memOffset, firstOpened);
47 1 : CHK_PRT_RET(
48 : ret != HCCL_SUCCESS,
49 : HCCL_ERROR(
50 : "[RemoteIpcRmaBufferImpl][Open]errNo[0x%016llx] Open ipc mem failed. memName[%s], offset[%llu]",
51 : HCCL_ERROR_CODE(ret), memName.ipcName, memOffset),
52 : ret);
53 1 : return HCCL_SUCCESS;
54 : }
55 :
56 1 : HcclResult RemoteIpcRmaBufferImpl::Close()
57 : {
58 1 : s32 deviceLogicId = 0;
59 1 : if (netDevCtx != nullptr) {
60 1 : deviceLogicId = (static_cast<NetDevContext*>(netDevCtx))->GetLogicId();
61 : } else {
62 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
63 : }
64 1 : MemNameRepository::GetInstance(deviceLogicId)->CloseIpcMem(static_cast<const u8*>(memName.ipcName));
65 1 : HCCL_DEBUG("[RemoteIpcRmaBufferImpl][Close]memName[%s]", memName.ipcName);
66 1 : return HCCL_SUCCESS;
67 : }
68 : } // namespace hccl
|