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