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 ENDPOINT_PAIR_H
12 : #define ENDPOINT_PAIR_H
13 :
14 : #include <memory>
15 : #include <vector>
16 : #include "channels/channel.h"
17 : #include "endpoint.h"
18 : #include "socket_mgr.h"
19 : #include "../../../../legacy/ascend950/unified_platform/resource/socket/socket.h"
20 : #include "../../../../legacy/ascend950/framework/resource_manager/socket/socket_manager.h"
21 :
22 : using EndpointDescPair = std::pair<EndpointDesc, EndpointDesc>;
23 :
24 : // 重载 == 操作符,用于 EndpointDesc 在 std::unordered_map 比较
25 299 : inline bool operator==(const EndpointDesc& a, const EndpointDesc& b) noexcept
26 : {
27 299 : return std::memcmp(&a, &b, sizeof(EndpointDesc)) == 0;
28 : }
29 :
30 : namespace std {
31 :
32 : template <>
33 : struct hash<EndpointDesc> {
34 361 : size_t operator()(const EndpointDesc& e) const noexcept
35 : {
36 : // FNV-1a(64位)对字节序列做hash
37 361 : const uint8_t* p = reinterpret_cast<const uint8_t*>(&e);
38 361 : size_t h
39 : = sizeof(size_t) == 8 ? static_cast<size_t>(14695981039346656037ull) : static_cast<size_t>(2166136261u);
40 :
41 361 : const size_t prime
42 : = sizeof(size_t) == 8 ? static_cast<size_t>(1099511628211ull) : static_cast<size_t>(16777619u);
43 :
44 58121 : for (size_t i = 0; i < sizeof(EndpointDesc); ++i) {
45 57760 : h ^= static_cast<size_t>(p[i]);
46 57760 : h *= prime;
47 : }
48 361 : return h;
49 : }
50 : };
51 :
52 : template <>
53 : struct hash<EndpointDescPair> {
54 160 : size_t operator()(const EndpointDescPair& p) const noexcept
55 : {
56 160 : size_t h1 = std::hash<EndpointDesc>{}(p.first);
57 160 : size_t h2 = std::hash<EndpointDesc>{}(p.second);
58 160 : size_t h = h1;
59 160 : h ^= h2 + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
60 160 : return h;
61 : }
62 : };
63 :
64 : } // namespace std
65 :
66 : namespace hcomm {
67 : /**
68 : * @note 职责:通信设备Endpoint对(EndpointPair,或连接)的C++类声明,
69 : * 管理该Endpoint对上的本地和远端注册内存、多个Channel、以及socket等。两个Endpoint的通信协议一致。
70 : */
71 : class EndpointPair {
72 : public:
73 19 : EndpointPair(
74 : EndpointDesc localEndpointDesc, EndpointDesc remoteEndpointDesc, const Hccl::RankIpPortMapPtr& rankIpPortMap)
75 19 : : localEndpointDesc_(localEndpointDesc),
76 19 : remoteEndpointDesc_(remoteEndpointDesc),
77 19 : rankIpPortMap_(rankIpPortMap)
78 19 : {}
79 : ~EndpointPair();
80 :
81 : HcclResult Init();
82 :
83 : // 临时方案:新增临时接口用于支持混跑
84 : HcclResult GetSocket(
85 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, u32 reuseIdx,
86 : const uint32_t listenPort, Hccl::Socket*& socket, uint32_t devicePhyId, uint32_t remoteDevicePhyId);
87 : HcclResult GetHostSocketWithRank(
88 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, const uint32_t listenPort,
89 : u32 reuseIdx, Hccl::Socket*& socket);
90 :
91 : HcclResult ServerInit(
92 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, u32 reuseIdx, uint32_t devicePhyId,
93 : uint32_t remoteDevicePhyId);
94 : HcclResult GetConnectedSocket(
95 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, u32 reuseIdx,
96 : const uint32_t listenPort, Hccl::Socket*& socket, uint32_t devicePhyId, uint32_t remoteDevicePhyId);
97 :
98 : HcclResult CreateChannel(
99 : EndpointHandle endpointHandle, CommEngine engine, u32 reuseIdx, HcommChannelDesc* channelDescs,
100 : ChannelHandle* channels);
101 :
102 : HcclResult DestroyChannel(CommEngine engine, u32 reuseIdx);
103 :
104 : bool IsChannelNotExist(CommEngine engine, u32 reuseIdx);
105 :
106 : const std::unordered_map<CommEngine, std::vector<ChannelHandle>>& GetChannelHandles();
107 :
108 : private:
109 : HcclResult EnsureSocketMgrCompat(const uint32_t myRank, const std::string& socketTag);
110 : Hccl::SocketConfig BuildSocketConfig(const Hccl::LinkData& linkData, const std::string& socketTag);
111 : HcclResult HandleHostSocketOrBuildLinkData(
112 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, u32 reuseIdx,
113 : const uint32_t listenPort, Hccl::Socket*& socket, uint32_t devicePhyId, uint32_t remoteDevicePhyId,
114 : Hccl::LinkData& linkData, bool& isHost);
115 : HcclResult GetSocketInternal(
116 : const uint32_t myRank, const uint32_t rmtRank, const std::string& socketTag, u32 reuseIdx,
117 : const uint32_t listenPort, Hccl::Socket*& socket, uint32_t devicePhyId, uint32_t remoteDevicePhyId,
118 : bool connectMode);
119 :
120 : EndpointDesc localEndpointDesc_{};
121 : EndpointDesc remoteEndpointDesc_{};
122 : std::unique_ptr<Hccl::SocketManager> socketMgrCompat_;
123 : std::unordered_map<CommEngine, std::vector<ChannelHandle>> channelHandles_{};
124 : Hccl::RankIpPortMapPtr rankIpPortMap_;
125 : uint32_t devicePhyId_{};
126 : std::unique_ptr<SocketMgr> socketMgr_;
127 : };
128 :
129 : } // namespace hcomm
130 :
131 : #endif // ENDPOINT_PAIR_H
|