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_rma_buffer.h"
12 : #include "null_ptr_exception.h"
13 : #include "exchange_ub_buffer_dto.h"
14 : #include "exchange_ipc_buffer_dto.h"
15 : #include "exchange_rdma_buffer_dto.h"
16 : #include "rdma_handle_manager.h"
17 : namespace Hccl {
18 2 : RemoteIpcRmaBuffer::RemoteIpcRmaBuffer() : RemoteRmaBuffer(RmaType::IPC), isOpened(false) {}
19 :
20 0 : RemoteIpcRmaBuffer::RemoteIpcRmaBuffer(const Serializable& rmtDto) : RemoteRmaBuffer(RmaType::IPC), isOpened(false)
21 : {
22 0 : const auto& dto = dynamic_cast<const ExchangeIpcBufferDto&>(rmtDto);
23 0 : remotePid = dto.pid;
24 0 : ipcAddr = dto.addr;
25 0 : ipcOffset = dto.offset;
26 0 : size = dto.size;
27 0 : memInfo = dto.memInfo;
28 0 : (void)memcpy_s(ipcName, RTS_IPC_MEM_NAME_LEN, dto.name, RTS_IPC_MEM_NAME_LEN);
29 0 : HCCL_INFO(
30 : "[RemoteIpcRmaBuffer][RemoteIpcRmaBuffer]ipcAddr[%llu] ipcOffset[%llu] ipcName[%s] memInfo[%s]", ipcAddr,
31 : ipcOffset, ipcName, memInfo.c_str());
32 0 : myPid = HrtDeviceGetBareTgid();
33 0 : if (myPid == remotePid) {
34 0 : HCCL_INFO("RemoteIpcRmaBuffer: myPid is equal to remotePid, do not need to open memory");
35 0 : HrtMemPrefetchToDevice(reinterpret_cast<void*>(ipcAddr + ipcOffset), size);
36 0 : addr = ipcAddr + ipcOffset;
37 : } else {
38 0 : HCCL_INFO("RemoteIpcRmaBuffer: open memory.");
39 0 : ipcPtr = HrtIpcOpenMemory(ipcName);
40 0 : addr = reinterpret_cast<uintptr_t>(ipcPtr) + ipcOffset;
41 0 : isOpened = true;
42 : }
43 0 : }
44 :
45 1 : RemoteIpcRmaBuffer::RemoteIpcRmaBuffer(const Serializable& rmtDto, const string tag)
46 : : RemoteRmaBuffer(RmaType::IPC),
47 1 : isOpened(true)
48 : {
49 1 : const auto& dto = dynamic_cast<const ExchangeIpcBufferDto&>(rmtDto);
50 3 : HCCL_INFO("[RemoteIpcRmaBuffer][RemoteIpcRmaBuffer] dtoName[%s]", dto.name);
51 1 : ipcAddr = dto.addr;
52 1 : ipcOffset = dto.offset;
53 1 : size = dto.size;
54 1 : memInfo = dto.memInfo;
55 1 : (void)memcpy_s(ipcName, RTS_IPC_MEM_NAME_LEN, dto.name, RTS_IPC_MEM_NAME_LEN);
56 3 : HCCL_INFO(
57 : "[RemoteIpcRmaBuffer][RemoteIpcRmaBuffer] tag[%s] ipcAddr[%llu] ipcOffset[%llu] ipcName[%s] memInfo[%s]",
58 : tag.c_str(), ipcAddr, ipcOffset, ipcName, memInfo.c_str());
59 1 : ipcPtr = HrtIpcOpenMemory(ipcName);
60 1 : addr = reinterpret_cast<uintptr_t>(ipcPtr) + ipcOffset;
61 1 : isOpened = true;
62 1 : }
63 :
64 3 : void RemoteIpcRmaBuffer::Close() const
65 : {
66 3 : if (isOpened) {
67 1 : HrtIpcCloseMemory(ipcName);
68 : }
69 3 : }
70 :
71 6 : RemoteIpcRmaBuffer::~RemoteIpcRmaBuffer() { DECTOR_TRY_CATCH("RemoteIpcRmaBuffer", Close()); }
72 :
73 2 : string RemoteIpcRmaBuffer::Describe() const
74 : {
75 : return StringFormat(
76 : "RemoteIpcRmaBuffer[addr=0x%llx, size=0x%llx, myPid=%u, "
77 : "remotePid=%u, ipcAddr=0x%llx, ipcOffset=0x%llx, ipcPtr=%p, ipcName=%s, "
78 : "isOpened=%d, pathMode=%u]",
79 2 : addr, size, myPid, remotePid, ipcAddr, ipcOffset, ipcPtr, ipcName, isOpened, pathMode_);
80 : }
81 :
82 2 : RemoteRdmaRmaBuffer::RemoteRdmaRmaBuffer(RdmaHandle rdmaHandle)
83 : : RemoteRmaBuffer(RmaType::RDMA),
84 2 : rdmaHandle(rdmaHandle),
85 2 : keyValidLen(RDMA_MEM_KEY_LEN_ROCE)
86 : {
87 2 : if (rdmaHandle == nullptr) { // 使用rdmaHandle调用 HCCP 新接口 import/unimport 接口,获取和销毁key
88 0 : THROW<NullPtrException>("RemoteRdmaRmaBuffer's rdmaHandle is nullptr");
89 : }
90 : // 待修改: 利用 rdmaHandle 从 HCCP 新接口获取keyValidLen, 暂定固定值 ROCE
91 2 : }
92 :
93 1 : RemoteRdmaRmaBuffer::RemoteRdmaRmaBuffer(RdmaHandle rdmaHandle, const Serializable& rmtDto)
94 : : RemoteRmaBuffer(RmaType::RDMA),
95 1 : rdmaHandle(rdmaHandle)
96 : {
97 1 : auto dto = dynamic_cast<const ExchangeRdmaBufferDto&>(rmtDto);
98 1 : addr = dto.addr;
99 1 : size = dto.size;
100 1 : rkey = dto.rkey;
101 1 : memInfo = dto.memInfo;
102 3 : HCCL_INFO("[RemoteRdmaRmaBuffer]addr = 0x%llx; size = 0x%llx; memInfo = %s", addr, size, memInfo.c_str());
103 1 : }
104 :
105 4 : RemoteRdmaRmaBuffer::~RemoteRdmaRmaBuffer()
106 : {
107 : // 待修改: 使用rdmaHandle调用 HCCP 新接口 unimport 接口,销毁key
108 4 : }
109 :
110 1 : string RemoteRdmaRmaBuffer::Describe() const
111 : {
112 1 : return StringFormat("RemoteRdmaRmaBuffer[addr=0x%llx, size=0x%llx]", addr, size);
113 : }
114 :
115 10 : RemoteUbRmaBuffer::RemoteUbRmaBuffer(RdmaHandle rdmaHandle) : RemoteRmaBuffer(RmaType::UB), rdmaHandle(rdmaHandle)
116 : {
117 10 : if (rdmaHandle == nullptr) {
118 1 : THROW<NullPtrException>("RemoteUbRmaBuffer's rdmaHandle is nullptr");
119 : }
120 10 : }
121 :
122 28 : RemoteUbRmaBuffer::~RemoteUbRmaBuffer()
123 : {
124 16 : if (memHandle != 0) {
125 4 : if (rdmaHandle == nullptr || !RdmaHandleManager::GetInstance().IsHandleValid(rdmaHandle)) {
126 12 : HCCL_WARNING(
127 : "[RemoteUbRmaBuffer][%s] skip HrtRaUbRemoteMemUnimport, "
128 : "rdmaHandle=%p invalid (DeInit/DestroyAll done), memHandle=0x%llx",
129 : __func__, rdmaHandle, static_cast<unsigned long long>(memHandle));
130 : } else {
131 0 : DECTOR_TRY_CATCH("RemoteUbRmaBuffer", HrtRaUbRemoteMemUnimport(rdmaHandle, memHandle));
132 : }
133 4 : memHandle = 0;
134 : }
135 28 : }
136 :
137 1 : RemoteUbRmaBuffer::RemoteUbRmaBuffer(
138 1 : uintptr_t addr, u64 size, u32 tokenId, u32 tokenValue, HcclMemType memType, const std::string& memInfo)
139 : : RemoteRmaBuffer(RmaType::UB),
140 1 : tokenId(tokenId),
141 1 : tokenValue(tokenValue)
142 : {
143 1 : this->addr = addr;
144 1 : this->size = size;
145 1 : this->memType = memType;
146 1 : this->memInfo = memInfo;
147 3 : HCCL_INFO("[RemoteUbRmaBuffer]addr = 0x%llx; size = 0x%llx; memInfo = %s", addr, size, memInfo.c_str());
148 1 : }
149 :
150 6 : RemoteUbRmaBuffer::RemoteUbRmaBuffer(RdmaHandle rdmaHandle1, const Serializable& rmtDto)
151 : : RemoteRmaBuffer(RmaType::UB),
152 6 : rdmaHandle(rdmaHandle1)
153 : { // 从 DTO 取得数据,然后生成 memHandle
154 6 : auto dto = dynamic_cast<const ExchangeUbBufferDto&>(rmtDto);
155 6 : memcpy_s(key, HRT_UB_MEM_KEY_MAX_LEN, dto.key, HRT_UB_MEM_KEY_MAX_LEN);
156 6 : addr = dto.addr;
157 6 : size = dto.size;
158 6 : memType = dto.memType;
159 6 : memInfo = dto.memInfo;
160 6 : tokenId = dto.tokenId;
161 6 : tokenValue = dto.tokenValue;
162 6 : keySize = dto.keySize;
163 6 : notifyId = dto.notifyId;
164 :
165 6 : if (keySize != 0) {
166 4 : auto res = HrtRaUbRemoteMemImport(rdmaHandle1, key, keySize, tokenValue);
167 4 : memHandle = res.handle;
168 4 : segVa = res.targetSegVa;
169 : } else {
170 6 : HCCL_INFO("[RemoteUbRmaBuffer] key is 0, do not need to import memory");
171 2 : memHandle = 0;
172 : }
173 18 : HCCL_INFO("Construct RemoteUbRmaBuffer:%s", Describe().c_str());
174 6 : }
175 :
176 10 : string RemoteUbRmaBuffer::Describe() const
177 : {
178 : return StringFormat(
179 10 : "RemoteUbRmaBuffer[rdmaHandle=%p, addr=0x%llx, size=0x%llx, memHandle=%p segVa=%llu, notifyId=%u]", rdmaHandle,
180 10 : addr, size, memHandle, segVa, notifyId);
181 : }
182 :
183 : } // namespace Hccl
|