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 UB_MEMORY_TRANSPORT_H
11 : #define UB_MEMORY_TRANSPORT_H
12 : #include "virtual_topo.h"
13 : #include "dev_buffer.h"
14 : #include "socket.h"
15 : #include "local_ipc_rma_buffer_v2.h"
16 : #include "remote_rma_buffer.h"
17 : #include "base_mem_transport.h"
18 :
19 : namespace Hccl {
20 : class UbMemoryTransport {
21 : public:
22 56 : MAKE_ENUM(
23 : UBTransportStatus, INIT, SOCKET_OK, SEND_MEM_INFO, RECV_MEM_INFO, RECV_MEM_INFO_PROCESS, SEND_NAME, RECV_NAME,
24 : CONNECT_FAILED, SOCKET_TIMEOUT, READY)
25 :
26 : UbMemoryTransport(
27 : const std::shared_ptr<Buffer> cclBuffer, const std::shared_ptr<Buffer> aivTagBuffer,
28 : const std::shared_ptr<Buffer> aivOffloadTagBuffer, Socket* socket, int32_t deviceLogicId);
29 :
30 : UbMemoryTransport(const UbMemoryTransport& that) = delete;
31 : UbMemoryTransport& operator=(const UbMemoryTransport& other) = delete;
32 : HcclResult Init();
33 : UBTransportStatus GetStatus();
34 :
35 : // 给算子开发提供的两个接口
36 : LocalIpcRmaBuffer* GetLocMemBuffer(const u32 bufIndex) const;
37 : RemoteIpcRmaBuffer* GetRmtMemBuffer(const u32 bufIndex) const;
38 : std::string Describe() const;
39 :
40 : vector<char> GetRmtHandshakeMsg() // 返回握手消息
41 : {
42 : return rmtHandshakeMsg;
43 : }
44 :
45 : vector<char> GetLocalHandshakeMsg() // 返回握手消息
46 : {
47 : return localHandshakeMsg;
48 : }
49 :
50 1 : void SetHandshakeMsg(const vector<char>& handshakeMsg) { localHandshakeMsg = handshakeMsg; }
51 :
52 : AcceleratorState& GetRmtOpAcceState() { return rmtOpAcceState; }
53 :
54 : AcceleratorState& GetLocalOpAcceState() { return locOpAcceState; }
55 :
56 1 : void SetLocalOpAcceState(const AcceleratorState& opAcceState) { locOpAcceState = opAcceState; }
57 :
58 : struct CclBufferInfo {
59 : uint64_t addr{0};
60 : uint32_t size{0};
61 : uint32_t tokenId{0};
62 : uint32_t tokenValue{0};
63 :
64 : void Pack(BinaryStream& binaryStream) const
65 : {
66 : binaryStream << addr << size << tokenId << tokenValue;
67 : HCCL_INFO("Pack Ccl Buffer Info: addr[%llu] size[%u]", addr, size);
68 : }
69 :
70 : void Unpack(BinaryStream& binaryStream)
71 : {
72 : binaryStream >> addr >> size >> tokenId >> tokenValue;
73 : HCCL_INFO("Unpack Ccl Buffer Info: addr[%llu] size[%u]", addr, size);
74 : }
75 : };
76 :
77 : private:
78 : UBTransportStatus ubStatus{UBTransportStatus::INIT};
79 : vector<char> rmtHandshakeMsg{0}; // 远端握手消息
80 : vector<char> localHandshakeMsg{0};
81 : vector<char> recvDataMsg{0};
82 : AcceleratorState rmtOpAcceState{AcceleratorState::AIV};
83 : AcceleratorState locOpAcceState{AcceleratorState::AIV};
84 :
85 : CclBufferInfo locCclBufInfo;
86 : CclBufferInfo rmtCclBufInfo;
87 : uint32_t exchangeDataSize{0};
88 :
89 : // 新增
90 : std::shared_ptr<Buffer> cclBuffer;
91 : std::shared_ptr<Buffer> aivTagBuffer;
92 : std::shared_ptr<Buffer> aivOffloadTagBuffer;
93 : Socket* socket{};
94 : int32_t deviceLogicId{0};
95 :
96 : std::vector<std::unique_ptr<RemoteIpcRmaBuffer>> rmtBufferVec;
97 : std::vector<std::unique_ptr<LocalIpcRmaBuffer>> localBufferVec;
98 : std::vector<RemoteRmaBuffer*> rmtRmaBufferVec;
99 :
100 : void HandshakeMsgPack(BinaryStream& binaryStream);
101 : void HandshakeMsgUnpack(BinaryStream& binaryStream);
102 : UBTransportStatus StateMachine();
103 : void ReleaseRes();
104 : void SendMemInfo();
105 : void RecvMemInfo();
106 : void RecvMemProcess();
107 : void BufferPack(BinaryStream& binaryStream);
108 : void RmtBufferUnpackProc(BinaryStream& binaryStream);
109 : void SendName();
110 : void RecvName();
111 : };
112 : } // namespace Hccl
113 :
114 : #endif
|