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