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