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 "local_ipc_rma_buffer.h"
12 : #include "local_ipc_rma_buffer_impl.h"
13 :
14 : namespace hccl {
15 16 : LocalIpcRmaBuffer::LocalIpcRmaBuffer(const HcclNetDevCtx netDevCtx, void* addr, u64 size, const RmaMemType memType)
16 16 : : RmaBuffer(netDevCtx, addr, size, memType, RmaType::IPC_RMA)
17 : {
18 16 : pimpl_ = std::make_unique<LocalIpcRmaBufferImpl>(netDevCtx, addr, size, memType);
19 16 : }
20 :
21 7 : LocalIpcRmaBuffer::LocalIpcRmaBuffer(const HcclNetDevCtx netDevCtx, void* addr, u64 size,
22 7 : const RmaMemType memType, const LocalIpcRmaBuffer& parent)
23 7 : : RmaBuffer(netDevCtx, addr, size, memType, RmaType::IPC_RMA, true)
24 : {
25 7 : pimpl_ = std::make_unique<LocalIpcRmaBufferImpl>(netDevCtx, addr, size, memType, *parent.pimpl_);
26 7 : this->devAddr = pimpl_->GetDevAddr();
27 7 : HCCL_INFO("[LocalIpcRmaBuffer] alias constructor");
28 7 : }
29 :
30 23 : LocalIpcRmaBuffer::~LocalIpcRmaBuffer()
31 : {
32 23 : if (!isAlias_) {
33 16 : HcclResult res = Destroy();
34 16 : if (res != HCCL_SUCCESS) {
35 0 : HCCL_ERROR("[LocalIpcRmaBuffer][~LocalIpcRmaBuffer]failed, ret[%d]", res);
36 : }
37 : }
38 23 : }
39 :
40 9 : HcclResult LocalIpcRmaBuffer::Init()
41 : {
42 9 : CHK_PTR_NULL(addr);
43 9 : CHK_PRT_RET((memType >= RmaMemType::TYPE_NUM),
44 : HCCL_ERROR("[LocalIpcRmaBuffer][Init]RmaMemType[%d] is invalid.", static_cast<int>(memType)), HCCL_E_PARA);
45 9 : CHK_PRT_RET((size == 0 || (memType == RmaMemType::HOST && size >= HOST_MEM_MAX_COUNT) ||
46 : (memType == RmaMemType::DEVICE && size >= DEVICE_MEM_MAX_COUNT)),
47 : HCCL_ERROR("[LocalIpcRmaBuffer][Init]memory size[%llu] should be greater than 0 and less than [%llu].",
48 : size, (memType == RmaMemType::DEVICE ? HOST_MEM_MAX_COUNT : DEVICE_MEM_MAX_COUNT)), HCCL_E_PARA);
49 :
50 9 : CHK_SMART_PTR_NULL(pimpl_);
51 9 : HcclResult ret = pimpl_->Init();
52 9 : if (ret != HCCL_SUCCESS) {
53 0 : pimpl_ = nullptr;
54 0 : HCCL_ERROR("[LocalIpcRmaBuffer][Init]Init failed, ret[%d]", ret);
55 0 : return ret;
56 : }
57 :
58 9 : this->devAddr = pimpl_->GetDevAddr();
59 :
60 9 : return HCCL_SUCCESS;
61 : }
62 :
63 16 : HcclResult LocalIpcRmaBuffer::Destroy()
64 : {
65 16 : if (pimpl_ != nullptr) {
66 16 : HcclResult ret = pimpl_->Destroy();
67 16 : if (ret != HCCL_SUCCESS) {
68 0 : HCCL_ERROR("[LocalIpcRmaBuffer][Destroy]Destroy failed, ret[%d]", ret);
69 : }
70 16 : pimpl_ = nullptr;
71 16 : addr = nullptr;
72 16 : size = 0;
73 16 : devAddr = nullptr;
74 16 : return ret;
75 : }
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 2 : std::string &LocalIpcRmaBuffer::Serialize()
80 : {
81 2 : return pimpl_->Serialize();
82 : }
83 :
84 2 : HcclResult LocalIpcRmaBuffer::Grant(u32 remotePid, u32 remoteSdid)
85 : {
86 2 : return pimpl_->Grant(remotePid, remoteSdid);
87 : }
88 : }
|