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 CHANNEL_H
12 : #define CHANNEL_H
13 :
14 : #include <memory>
15 : #include <vector>
16 : #include <unordered_map>
17 : #include "hccl/hccl_res.h"
18 : #include "hccl/hccl_types.h"
19 : #include "hcomm_res_defs.h"
20 : #include "hcomm_channel.h"
21 : #include "hccl_mem_defs.h"
22 : #include "mem_device_pub.h"
23 : #include <string>
24 : #include <unordered_map>
25 : #include <vector>
26 : #include "enum_factory.h"
27 :
28 : #include "hcomm_nic_plugin.h"
29 :
30 : // Orion
31 : #include "transport_status.h"
32 : #include "ip_address.h"
33 : #include "topo_common_types.h"
34 : #include "virtual_topo.h"
35 :
36 : class AicpuTsChannelHelper;
37 :
38 : namespace hcomm {
39 :
40 1404 : MAKE_ENUM(ChannelStatus, INIT, SOCKET_OK, SOCKET_TIMEOUT, READY, FAILED)
41 :
42 : /**
43 : * @brief 通道种类(与 HcommChannelRes.channelTypeList 中 u32 数值一致;由 CommEngine + CommProtocol 推导)。
44 : */
45 : enum class HcommChannelKind : uint32_t {
46 : INVALID = 0U,
47 : AICPU_TS_URMA = 1U,
48 : AICPU_TS_ROCE = 2U,
49 : AICPU_TS_HCCS = 3U,
50 : CPU_ROCE = 4U,
51 : AIV_UB_MEM = 5U,
52 : AICPU_TS_UBOE = 6U,
53 : AICPU_TS_ROCE_V2 = 7U,
54 : AIV_URMA = 8U,
55 : AICPU_TS_UB_RTP = 9U,
56 : };
57 :
58 1 : inline const char* HcommChannelKindToString(HcommChannelKind kind)
59 : {
60 1 : switch (kind) {
61 1 : case HcommChannelKind::INVALID:
62 1 : return "INVALID";
63 0 : case HcommChannelKind::AICPU_TS_URMA:
64 0 : return "AICPU_TS_URMA";
65 0 : case HcommChannelKind::AICPU_TS_ROCE:
66 0 : return "AICPU_TS_ROCE";
67 0 : case HcommChannelKind::AICPU_TS_HCCS:
68 0 : return "AICPU_TS_HCCS";
69 0 : case HcommChannelKind::CPU_ROCE:
70 0 : return "CPU_ROCE";
71 0 : case HcommChannelKind::AIV_UB_MEM:
72 0 : return "AIV_UB_MEM";
73 0 : case HcommChannelKind::AICPU_TS_UBOE:
74 0 : return "AICPU_TS_UBOE";
75 0 : case HcommChannelKind::AICPU_TS_ROCE_V2:
76 0 : return "AICPU_TS_ROCE_V2";
77 0 : case HcommChannelKind::AIV_URMA:
78 0 : return "AIV_URMA";
79 0 : case HcommChannelKind::AICPU_TS_UB_RTP:
80 0 : return "AICPU_TS_UB_RTP";
81 0 : default:
82 0 : return "UNKNOWN";
83 : }
84 : }
85 :
86 : /**
87 : * @note 职责:一个EndPointPair上的建立的通信通道的C++抽象接口类声明。
88 : * 管理该通信通道Channel对上的同步信号Notify、通信队列(如qp、jetty等)等资源管理,负责建立连接,以及注册内存、同步信号等的交换。
89 : */
90 : class Channel {
91 : public:
92 399 : Channel() {};
93 399 : virtual ~Channel() = default;
94 :
95 : // 禁拷贝(避免切片/资源重复释放等)
96 : Channel(const Channel&) = delete;
97 : Channel& operator=(const Channel&) = delete;
98 :
99 : // 视需要决定是否允许移动;很多资源类也会禁移动
100 : Channel(Channel&&) = default;
101 : Channel& operator=(Channel&&) = default;
102 :
103 : // ------------------ 控制面接口 ------------------
104 : virtual HcclResult Init() = 0;
105 : virtual HcclResult GetNotifyNum(uint32_t* notifyNum) const = 0;
106 : virtual HcclResult GetRemoteMems(uint32_t* memNum, CommMem** remoteMem, char*** memInfos) = 0;
107 : virtual ChannelStatus GetStatus() = 0;
108 : virtual HcclResult UpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHandleNum);
109 :
110 : virtual HcclResult Clean() = 0;
111 : virtual HcclResult Resume() = 0;
112 :
113 : virtual HcommChannelKind GetChannelKind() const;
114 30 : CommEngine GetEngine() const { return engine_; }
115 : virtual const HcommChannelDesc& GetChannelDesc() const = 0;
116 : virtual HcclResult Serialize(std::shared_ptr<hccl::DeviceMem>& out);
117 : virtual void AddPtrArrayDevMem(std::shared_ptr<hccl::DeviceMem> ptrArrayMem);
118 : // ------------------ 数据面接口 ------------------
119 : virtual HcclResult NotifyRecord(const uint32_t remoteNotifyIdx) = 0;
120 : virtual HcclResult NotifyWait(const uint32_t localNotifyIdx, const uint32_t timeout) = 0;
121 : virtual HcclResult WriteWithNotify(void* dst, const void* src, const uint64_t len, uint32_t remoteNotifyIdx) = 0;
122 : virtual HcclResult Write(void* dst, const void* src, uint64_t len) = 0;
123 : virtual HcclResult Read(void* dst, const void* src, uint64_t len) = 0;
124 : virtual HcclResult ChannelFence() = 0;
125 :
126 : // ------------------ NIC插件相关 ------------------
127 58 : void SetNicChannelCtx(HcommNicChannelOps* nicOps, void* nicCtx)
128 : {
129 58 : nicOps_ = nicOps;
130 58 : nicCtx_ = nicCtx;
131 58 : }
132 76 : HcommNicChannelOps* GetNicOps() const { return nicOps_; }
133 76 : void* GetNicCtx() const { return nicCtx_; }
134 :
135 : // ------------------ 工具方法 ------------------
136 : static ChannelStatus TransportStatusToChannelStatus(Hccl::TransportStatus ts);
137 :
138 : // ------------------ 共享 Jetty 模式 ------------------
139 : // 由 CreateChannelsLoop 根据本次调用是否共享(HcommChannelConfig.isSharedQueue)设置;
140 : // BuildConnection 据此决定走复用路径还是自建独立 jetty,避免 Endpoint 全局标记跨调用串扰。
141 20 : void SetSharedJetty(bool enable) { isSharedJetty_ = enable; }
142 12 : bool IsSharedJetty() const { return isSharedJetty_; }
143 :
144 : // ------------------ 工厂 ------------------
145 : static HcclResult CreateChannel(
146 : EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc channelDesc, std::shared_ptr<Channel>& out,
147 : bool isSharedQueue = false);
148 :
149 0 : virtual AicpuTsChannelHelper* GetAicpuTsHelper() { return nullptr; }
150 :
151 6 : bool IsDeviceEntityReady() const { return deviceEntityReady_; }
152 5 : void SetDeviceEntityReady() { deviceEntityReady_ = true; }
153 :
154 : protected:
155 : void ReleasePtrArrayDevMems();
156 :
157 : HcommChannelKind channelKind_{HcommChannelKind::INVALID};
158 : CommEngine engine_{COMM_ENGINE_RESERVED};
159 : std::vector<std::shared_ptr<hccl::DeviceMem>> ptrArrayDevMems_{};
160 : bool deviceEntityReady_{false};
161 : bool isSharedJetty_{false};
162 :
163 : HcommNicChannelOps* nicOps_{nullptr};
164 : void* nicCtx_{nullptr};
165 : };
166 :
167 : } // namespace hcomm
168 : #endif // CHANNEL_H
|