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