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