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 : #ifndef P2P_TRANSPORT_H
11 : #define P2P_TRANSPORT_H
12 :
13 : #include "base_mem_transport.h"
14 : #include "virtual_topo.h"
15 : #include "ipc_remote_notify.h"
16 : #include "../../../resource/buffer/local_ipc_rma_buffer_v2.h"
17 : #include "remote_rma_buffer.h"
18 :
19 : namespace Hccl {
20 : class P2PTransport : public BaseMemTransport {
21 : public:
22 : P2PTransport(CommonLocRes& commonLocRes, Attribution& attr, const LinkData& linkData, const Socket& socket);
23 :
24 : P2PTransport(
25 : CommonLocRes& commonLocRes, Attribution& attr, const LinkData& linkData, const Socket& socket,
26 : std::function<void(u32 streamId, u32 taskId, TaskParam taskParam)> callback);
27 :
28 29 : ~P2PTransport() override = default;
29 :
30 : std::string Describe() const override;
31 :
32 : TransportStatus GetStatus() override;
33 :
34 : std::vector<char> GetUniqueId() override;
35 :
36 : std::vector<char> GetUniqueIdV2();
37 :
38 : void Post(u32 index, const Stream& stream) override;
39 :
40 : void Read(const RmaBufferSlice& locSlice, const RmtRmaBufferSlice& rmtSlice, const Stream& stream) override;
41 :
42 : void ReadReduce(
43 : const RmaBufferSlice& locSlice, const RmtRmaBufferSlice& rmtSlice, const ReduceIn& reduceIn,
44 : const Stream& stream) override;
45 :
46 : void Write(const RmaBufferSlice& locSlice, const RmtRmaBufferSlice& rmtSlice, const Stream& stream) override;
47 :
48 : void WriteReduce(
49 : const RmaBufferSlice& locSlice, const RmtRmaBufferSlice& rmtSlice, const ReduceIn& reduceIn,
50 : const Stream& stream) override;
51 :
52 : HcclResult GetRemoteMems(uint32_t* memNum, CommMem** remoteMem, char*** memInfos);
53 :
54 : private:
55 : MemoryBuffer GetLocMemBuffer(const RmaBufferSlice& locSlice) const;
56 : MemoryBuffer GetRmtMemBuffer(const RmtRmaBufferSlice& rmtSlice) const;
57 :
58 19 : MAKE_ENUM(
59 : P2PStatus, INIT, SOCKET_OK, SEND_PID, RECV_PID, GRANT, SEND_DATA, RECV_DATA, SEND_DATA_SIZE, RECV_DATA_SIZE)
60 : P2PStatus p2pStatus{P2PStatus::INIT};
61 :
62 : u32 pidMsgSize{0};
63 : u32 myPid{0};
64 : u32 rmtPid{0};
65 :
66 : bool rmtPidValid{false};
67 :
68 : std::vector<std::unique_ptr<IpcRemoteNotify>> rmtNotifyVec;
69 : std::vector<std::unique_ptr<RemoteIpcRmaBuffer>> rmtBufferVec;
70 :
71 : bool cacheValid_ = false; // 当前缓存是否有效
72 : std::vector<CommMem> remoteUserMems_; // 内存基本信息缓存
73 : std::vector<std::string> memInfoCopies_; // 储存 Tag 字符串副本
74 : std::vector<char*> memInfoPointers_; // Tag 缓存
75 : std::vector<char> sendData;
76 : std::vector<char> recvData;
77 :
78 : bool IsRmtPidValid() const;
79 : void SendPid();
80 : void RecvPid();
81 : void Grant();
82 : void PrepareSendData();
83 : void RecvDataSize();
84 : void SendExchangeData();
85 : void RecvExchangeData();
86 : void ProcessRecvData();
87 :
88 : void BufferVecPack(BinaryStream& binaryStream);
89 :
90 : void RmtNotifyVecUnpackProc(BinaryStream& binaryStream);
91 : void RmtBufferVecUnpackProc(BinaryStream& binaryStream);
92 :
93 : std::vector<char> GetSingleRmtNotifyUniqueId(u64 addr, u64 size, u32 notifyId) const;
94 : std::vector<char> GetSingleBufferUniqueId(u64 addr, u64 size) const;
95 : std::vector<char> GetNotifyUniqueIds();
96 : std::vector<char> GetRmtNotifyUniqueIds() const;
97 : std::vector<char> GetLocBufferUniqueIds() const;
98 : std::vector<char> GetRmtBufferUniqueIds() const;
99 :
100 : std::mutex remoteMemsMutex_; // 远端内存列表互斥锁
101 : };
102 :
103 : } // namespace Hccl
104 :
105 : #endif
|