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 HCCL_CCU_TRANSPORT_H
12 : #define HCCL_CCU_TRANSPORT_H
13 :
14 : #include <mutex>
15 : #include <memory>
16 : #include <vector>
17 : #include <shared_mutex>
18 :
19 : #include "../../resource/socket/socket.h"
20 : #include "op_mode.h"
21 : #include "binary_stream.h"
22 : #include "ccu_connection.h"
23 :
24 : namespace Hccl {
25 :
26 : class CcuTransport {
27 : public:
28 : static constexpr uint32_t INIT_CKE_NUM = 16;
29 : static constexpr uint32_t INIT_XN_NUM = 16;
30 626 : MAKE_ENUM(
31 : TransStatus, INIT, SEND_ALL_INFO, RECV_ALL_INFO, SEND_TRANS_RES, RECV_TRANS_RES, SEND_FIN, RECV_FIN,
32 : RECVING_FIN, RECVING_TRANS_RES, READY, CONNECT_FAILED, SOCKET_TIMEOUT)
33 :
34 : struct CclBufferInfo {
35 : uint64_t addr{0};
36 : uint32_t size{0};
37 : uint32_t tokenId{0};
38 : uint32_t tokenValue{0};
39 :
40 262 : explicit CclBufferInfo() = default;
41 12 : CclBufferInfo(const uint64_t addr, const uint32_t size, const uint32_t tokenId, const uint32_t tokenValue)
42 12 : : addr(addr),
43 12 : size(size),
44 12 : tokenId(tokenId),
45 12 : tokenValue(tokenValue)
46 12 : {}
47 :
48 2 : void Pack(BinaryStream& binaryStream) const
49 : {
50 2 : binaryStream << addr << size << tokenId << tokenValue;
51 6 : HCCL_INFO("Pack Ccl Buffer Info: addr[%llu] size[%u]", addr, size);
52 2 : }
53 :
54 1 : void Unpack(BinaryStream& binaryStream)
55 : {
56 1 : binaryStream >> addr >> size >> tokenId >> tokenValue;
57 3 : HCCL_INFO("Unpack Ccl Buffer Info: addr[%llu] size[%u]", addr, size);
58 1 : }
59 : };
60 :
61 23 : MAKE_ENUM(CcuConnectionType, UBC_TP, UB_CTP, UBC_CTP = UB_CTP);
62 : struct CcuConnectionInfo {
63 : CcuConnectionType type{CcuConnectionType::UBC_TP};
64 : IpAddress locAddr{};
65 : IpAddress rmtAddr{};
66 : CcuChannelInfo channelInfo{};
67 : std::vector<CcuJetty*> ccuJettys;
68 :
69 : explicit CcuConnectionInfo() = default;
70 12 : CcuConnectionInfo(
71 : const CcuConnectionType type, const IpAddress& locAddr, const IpAddress& rmtAddr,
72 : const CcuChannelInfo& channelInfo, const std::vector<CcuJetty*>& ccuJettys)
73 12 : : type(type),
74 12 : locAddr(locAddr),
75 12 : rmtAddr(rmtAddr),
76 12 : channelInfo(channelInfo),
77 12 : ccuJettys(ccuJettys)
78 12 : {}
79 : };
80 :
81 : CcuTransport(const CcuTransport& that) = delete;
82 : CcuTransport& operator=(const CcuTransport& other) = delete;
83 : ~CcuTransport();
84 : HcclResult AppendRes(uint32_t ckesNum, uint32_t xnsNum);
85 : HcclResult Init();
86 :
87 : struct Attribution {
88 : OpMode opMode;
89 : u32 devicePhyId;
90 : std::vector<char> handshakeMsg{0};
91 : AcceleratorState opAcceState{AcceleratorState::CCU_SCHED};
92 : string Describe() const
93 : {
94 : return StringFormat(
95 : "CcuTransportAttribution[opMode=%s, devicePhyId=%u, handshakeMsg=%s]", opMode.Describe().c_str(),
96 : devicePhyId, Bytes2hex(handshakeMsg.data(), handshakeMsg.size()).c_str());
97 : }
98 : };
99 :
100 : std::vector<char>& GetRmtHandshakeMsg() // 返回握手消息
101 : {
102 : return rmtHandshakeMsg;
103 : }
104 :
105 : std::vector<char>& GetLocalHandshakeMsg() // 返回握手消息
106 : {
107 : return attr.handshakeMsg;
108 : }
109 :
110 11 : void SetHandshakeMsg(const std::vector<char>& handshakeMsg) { attr.handshakeMsg = handshakeMsg; }
111 :
112 : AcceleratorState& GetRmtOpAcceState() { return rmtOpAcceState; }
113 :
114 : AcceleratorState& GetLocalOpAcceState() { return attr.opAcceState; }
115 :
116 9 : void SetLocalOpAcceState(const AcceleratorState& opAcceState) { attr.opAcceState = opAcceState; }
117 :
118 0 : CcuConnection* GetCcuConnection() const { return ccuConnection.get(); }
119 :
120 : // 下面接口为平台层接口,不能在框架层使用
121 : CcuTransport(Socket* socket, std::unique_ptr<CcuConnection>&& connection, const CclBufferInfo& locCclBufInfo);
122 : uint32_t GetDieId() const;
123 : uint32_t GetChannelId() const;
124 : void SetCntCke(const std::vector<uint32_t>& cntCke);
125 : uint32_t GetLocCkeByIndex(uint32_t index) const;
126 : uint32_t GetLocCntCkeByIndex(uint32_t index) const;
127 : uint32_t GetLocXnByIndex(uint32_t index) const;
128 : uint32_t GetRmtCkeByIndex(uint32_t index) const;
129 : uint32_t GetRmtCntCkeByIndex(uint32_t index) const;
130 : uint32_t GetRmtXnByIndex(uint32_t index) const;
131 : HcclResult GetLocBuffer(CclBufferInfo& bufferInfo, const uint32_t& bufNum) const;
132 : HcclResult GetRmtBuffer(CclBufferInfo& bufferInfo, const uint32_t& bufNum) const;
133 : TransStatus GetStatus();
134 : std::string Describe() const;
135 : HcclResult Clean();
136 : std::vector<ConnJettyInfo> GetDeleteJettyInfo();
137 : std::vector<ConnJettyInfo> GetUnimportJettyInfo();
138 :
139 : private:
140 : // 保存transport中需要使用的cke,xn等ccu资源
141 : struct TransRes {
142 : std::vector<uint32_t> ckes;
143 : std::vector<uint32_t> cntCkes;
144 : std::vector<uint32_t> xns;
145 : };
146 : TransStatus StateMachine();
147 : HcclResult AppendCkes(uint32_t ckesNum);
148 : HcclResult AppendXns(uint32_t xnsNum);
149 : void SendFinish();
150 : void RecvFinish();
151 : void CheckFinish();
152 : void RecvDataProcess();
153 : void RecvTransInfoProcess();
154 : void ReleaseTransRes();
155 : void SendConnAndTransInfo();
156 : void RecvConnAndTransInfo();
157 : void SendTransInfo();
158 : void RecvTransInfo();
159 : void HandshakeMsgPack(BinaryStream& binaryStream);
160 : void ConnInfoPack(BinaryStream& binaryStream) const;
161 : void TransResPack(BinaryStream& binaryStream);
162 : void CclBufferInfoPack(BinaryStream& binaryStream) const;
163 : void HandshakeMsgUnpack(BinaryStream& binaryStream);
164 : void ConnInfoUnpackProc(BinaryStream& binaryStream) const;
165 : void TransResUnpackProc(BinaryStream& binaryStream);
166 : void CclBufferInfoUnpack(BinaryStream& binaryStream);
167 : uint32_t dieId{0};
168 : int32_t devLogicId{0};
169 : Attribution attr;
170 : std::vector<char> rmtHandshakeMsg{0}; // 远端握手消息
171 : AcceleratorState rmtOpAcceState{AcceleratorState::CCU_SCHED};
172 : Socket* socket;
173 : std::unique_ptr<CcuConnection> ccuConnection;
174 : TransRes locRes;
175 : TransRes rmtRes;
176 : CcuTransport::TransStatus transStatus;
177 : std::vector<std::vector<ResInfo>> ckesRes;
178 : std::vector<std::vector<ResInfo>> xnsRes;
179 : CclBufferInfo locCclBufInfo;
180 : CclBufferInfo rmtCclBufInfo;
181 : uint32_t exchangeDataSize{0};
182 : mutable std::shared_timed_mutex transMutex;
183 : std::vector<char> recvData{};
184 : std::vector<char> recvTrans{};
185 : std::vector<char> sendData{};
186 : std::vector<char> sendTrans{};
187 : std::vector<char> recvFinishMsg{};
188 : std::vector<char> sendFinishMsg{};
189 : };
190 :
191 : HcclResult CcuCreateTransport(
192 : Socket* socket, const CcuTransport::CcuConnectionInfo& ccuConnectionInfo,
193 : const CcuTransport::CclBufferInfo& cclBufferInfo, std::unique_ptr<CcuTransport>& ccuTransport);
194 :
195 : } // namespace Hccl
196 : #endif // HCCL_CCU_TRANSPORT_H
|