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.h"
12 : #include "remote_ipc_rma_buffer_impl.h"
13 :
14 : namespace hccl {
15 4 : RemoteIpcRmaBuffer::RemoteIpcRmaBuffer(const HcclNetDevCtx netDevCtx)
16 4 : : RmaBuffer(netDevCtx, nullptr, 0, RmaMemType::TYPE_NUM, RmaType::IPC_RMA)
17 : {
18 4 : pimpl_ = std::make_unique<RemoteIpcRmaBufferImpl>(netDevCtx);
19 4 : }
20 :
21 4 : RemoteIpcRmaBuffer::~RemoteIpcRmaBuffer()
22 : {
23 4 : if (pimpl_ != nullptr) {
24 4 : pimpl_ = nullptr;
25 4 : addr = nullptr;
26 4 : size = 0;
27 4 : devAddr = nullptr;
28 : }
29 4 : }
30 :
31 4 : HcclResult RemoteIpcRmaBuffer::Deserialize(const std::string& msg)
32 : {
33 4 : std::istringstream iss(msg);
34 4 : u8 type{static_cast<u8>(RmaType::RMA_TYPE_RESERVED)};
35 4 : iss.read(reinterpret_cast<char_t*>(&type), sizeof(type));
36 4 : iss.read(reinterpret_cast<char_t*>(&addr), sizeof(addr));
37 4 : iss.read(reinterpret_cast<char_t*>(&size), sizeof(size));
38 4 : iss.read(reinterpret_cast<char_t*>(&devAddr), sizeof(devAddr));
39 4 : iss.read(reinterpret_cast<char_t*>(&memType), sizeof(memType));
40 4 : CHK_PTR_NULL(addr);
41 4 : CHK_PTR_NULL(devAddr);
42 4 : CHK_PRT_RET(
43 : type >= static_cast<u8>(RmaType::RMA_TYPE_RESERVED),
44 : HCCL_ERROR("[RemoteIpcRmaBuffer][Deserialize]rmaType[%u] is invalid.", type), HCCL_E_PARA);
45 4 : CHK_PRT_RET(
46 : (memType >= RmaMemType::TYPE_NUM),
47 : HCCL_ERROR("[RemoteIpcRmaBuffer][Deserialize]RmaMemType[%d] is invalid.", static_cast<int>(memType)),
48 : HCCL_E_PARA);
49 4 : CHK_PRT_RET(
50 : (size == 0 || (memType == RmaMemType::HOST && size >= HOST_MEM_MAX_COUNT)
51 : || (memType == RmaMemType::DEVICE && size >= DEVICE_MEM_MAX_COUNT)),
52 : HCCL_ERROR(
53 : "[RemoteIpcRmaBuffer][Deserialize]memory size[%llu] should be greater than 0 and less than [%llu].", size,
54 : (memType == RmaMemType::DEVICE ? DEVICE_MEM_MAX_COUNT : HOST_MEM_MAX_COUNT)),
55 : HCCL_E_PARA);
56 :
57 4 : HCCL_DEBUG(
58 : "[RemoteIpcRmaBuffer][Deserialize]addr[%p], size[%llu], devAddr[%p], memType[%d]", addr, size, devAddr,
59 : memType);
60 :
61 4 : CHK_SMART_PTR_NULL(pimpl_);
62 4 : if (rmaType != static_cast<RmaType>(type)) {
63 0 : HCCL_ERROR(
64 : "[RemoteIpcRmaBuffer][Deserialize]rmaType[%u] is not match to [%d].", type, static_cast<int>(rmaType));
65 0 : return HCCL_E_INTERNAL;
66 : }
67 4 : std::string remainingMsg = std::string((std::istreambuf_iterator<char>(iss)), std::istreambuf_iterator<char>());
68 4 : HcclResult ret = pimpl_->Deserialize(remainingMsg);
69 4 : if (ret != HCCL_SUCCESS) {
70 0 : pimpl_ = nullptr;
71 0 : HCCL_ERROR("[RemoteIpcRmaBuffer]Deserialize failed, ret[%d]", ret);
72 0 : return ret;
73 : }
74 4 : return HCCL_SUCCESS;
75 4 : }
76 :
77 1 : HcclResult RemoteIpcRmaBuffer::Open()
78 : {
79 1 : CHK_SMART_PTR_NULL(pimpl_);
80 1 : CHK_RET(pimpl_->Open());
81 1 : isOpened_ = true;
82 1 : this->devAddr = pimpl_->GetDevAddr();
83 1 : return HCCL_SUCCESS;
84 : }
85 :
86 1 : HcclResult RemoteIpcRmaBuffer::Close()
87 : {
88 1 : CHK_SMART_PTR_NULL(pimpl_);
89 1 : HcclResult ret = pimpl_->Close();
90 1 : isOpened_ = false;
91 1 : return ret;
92 : }
93 : } // namespace hccl
|