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