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_v2.h"
12 :
13 : #include "rma_buffer.h"
14 : #include "exchange_ipc_buffer_dto.h"
15 :
16 : namespace Hccl {
17 :
18 11 : LocalIpcRmaBuffer::LocalIpcRmaBuffer(std::shared_ptr<Buffer> buf) : LocalRmaBuffer(buf, RmaType::IPC)
19 : {
20 11 : HrtDevMemAlignWithPage(reinterpret_cast<void*>(buf->GetAddr()), buf->GetSize(), ipcPtr, ipcSize, ipcOffset);
21 11 : HrtIpcSetMemoryName(ipcPtr, name, ipcSize, RTS_IPC_MEM_NAME_LEN);
22 11 : }
23 :
24 0 : LocalIpcRmaBuffer::LocalIpcRmaBuffer(std::shared_ptr<Buffer> buf, const LocalIpcRmaBuffer& parent)
25 0 : : LocalRmaBuffer(buf, RmaType::IPC, true)
26 : {
27 0 : (void)memcpy_s(name, RTS_IPC_MEM_NAME_LEN, parent.name, RTS_IPC_MEM_NAME_LEN);
28 0 : ipcPtr = parent.ipcPtr;
29 0 : ipcOffset = parent.ipcOffset;
30 0 : ipcSize = parent.ipcSize;
31 0 : HCCL_INFO(
32 : "[LocalIpcRmaBuffer] alias constructor, parent name[%s] ipcPtr[%p] ipcOffset[%llu] ipcSize[%llu] buf=%s", name,
33 : ipcPtr, static_cast<unsigned long long>(ipcOffset), static_cast<unsigned long long>(ipcSize),
34 : buf->Describe().c_str());
35 0 : }
36 :
37 15 : LocalIpcRmaBuffer::~LocalIpcRmaBuffer()
38 : {
39 11 : if (!isAlias_) {
40 11 : DECTOR_TRY_CATCH("LocalIpcRmaBuffer", HrtIpcDestroyMemoryName(name));
41 : }
42 15 : }
43 :
44 1 : string LocalIpcRmaBuffer::Describe() const
45 : {
46 : return StringFormat(
47 2 : "LocalIpcRmaBuffer[buf=%s, ipcPtr=0x%llx, ipcOffset=0x%llx, ipcSize=0x%llx, name=%s]", buf->Describe().c_str(),
48 3 : reinterpret_cast<uintptr_t>(ipcPtr), ipcOffset, ipcSize, name);
49 : }
50 :
51 1 : std::unique_ptr<Serializable> LocalIpcRmaBuffer::GetExchangeDto()
52 : {
53 : std::unique_ptr<ExchangeIpcBufferDto> dto = make_unique<ExchangeIpcBufferDto>(
54 1 : buf->GetAddr(), buf->GetSize(), ipcOffset, HrtDeviceGetBareTgid(), buf->GetMemInfo().c_str());
55 1 : (void)memcpy_s(dto->name, RTS_IPC_MEM_NAME_LEN, name, RTS_IPC_MEM_NAME_LEN);
56 2 : return std::unique_ptr<Serializable>(dto.release());
57 1 : }
58 :
59 1 : void LocalIpcRmaBuffer::Grant(u32 pid)
60 : {
61 1 : u32 myPid = HrtDeviceGetBareTgid();
62 1 : if (pid == myPid) {
63 3 : HCCL_INFO("pid is equal to myPid, do need to use HrtIpcSetMemoryPid grant, pid==myPid=%u", pid);
64 1 : return;
65 : }
66 0 : HrtIpcSetMemoryPid(name, static_cast<s32>(pid));
67 : }
68 :
69 : } // namespace Hccl
|