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 COMM_CHANNEL_MANAGER_H
12 : #define COMM_CHANNEL_MANAGER_H
13 :
14 : #include <string>
15 : #include <unordered_map>
16 : #include "hccl/hccl_types_in.h"
17 : #include "queue_schedule/dgw_client.h"
18 : #include "fsm/state_define.h"
19 :
20 : namespace dgw {
21 : class CommChannel {
22 : public:
23 : CommChannel() = delete;
24 58 : ~CommChannel() = default;
25 :
26 52 : explicit CommChannel(
27 : const HcclComm handle = nullptr, const uint32_t localTagId = 0U, const uint32_t peerTagId = 0U,
28 : const uint32_t localRankId = 0U, const uint32_t peerRankId = 0U, const uint32_t localTagDepth = 0U,
29 : const uint32_t peerTagDepth = 0U)
30 52 : : handle_(handle),
31 52 : localTagId_(localTagId),
32 52 : peerTagId_(peerTagId),
33 52 : localRankId_(localRankId),
34 52 : peerRankId_(peerRankId),
35 52 : localTagDepth_(localTagDepth),
36 52 : peerTagDepth_(peerTagDepth)
37 : {
38 52 : (void)channelDesc_.append("handle:")
39 52 : .append(std::to_string(PtrToValue(handle_)))
40 52 : .append(", rank:")
41 104 : .append(std::to_string(localRankId_))
42 52 : .append("->")
43 104 : .append(std::to_string(peerRankId_))
44 52 : .append(", tag:")
45 104 : .append(std::to_string(localTagId_))
46 52 : .append("->")
47 52 : .append(std::to_string(peerTagId_));
48 52 : }
49 :
50 13 : bool operator==(const CommChannel& commChannel) const
51 : {
52 13 : if (handle_ != commChannel.handle_) {
53 0 : return false;
54 : }
55 13 : if (localTagId_ != commChannel.localTagId_) {
56 8 : return false;
57 : }
58 5 : if (peerTagId_ != commChannel.peerTagId_) {
59 0 : return false;
60 : }
61 5 : if (localRankId_ != commChannel.localRankId_) {
62 0 : return false;
63 : }
64 5 : if (peerRankId_ != commChannel.peerRankId_) {
65 0 : return false;
66 : }
67 5 : return true;
68 : }
69 :
70 69 : inline HcclComm GetHandle() const { return handle_; }
71 13 : inline uint32_t GetLocalTagId() const { return localTagId_; }
72 83 : inline uint32_t GetPeerTagId() const { return peerTagId_; }
73 13 : inline uint32_t GetLocalRankId() const { return localRankId_; }
74 69 : inline uint32_t GetPeerRankId() const { return peerRankId_; }
75 85 : inline uint32_t GetLocalTagDepth() const { return localTagDepth_; }
76 12 : inline uint32_t GetPeerTagDepth() const { return peerTagDepth_; }
77 109 : inline const std::string& ToString() const { return channelDesc_; }
78 :
79 : private:
80 : HcclComm handle_;
81 : uint32_t localTagId_;
82 : uint32_t peerTagId_;
83 : uint32_t localRankId_;
84 : uint32_t peerRankId_;
85 : uint32_t localTagDepth_;
86 : uint32_t peerTagDepth_;
87 : // comm channel desc
88 : std::string channelDesc_;
89 : };
90 :
91 : class CommChannelHash {
92 : public:
93 10 : size_t operator()(const CommChannel& channel) const
94 : {
95 10 : return std::hash<uint64_t>()(PtrToValue(channel.GetHandle())) ^
96 10 : std::hash<uint32_t>()(channel.GetLocalRankId()) ^ std::hash<uint32_t>()(channel.GetPeerRankId()) ^
97 10 : std::hash<uint32_t>()(channel.GetLocalTagId()) ^ std::hash<uint32_t>()(channel.GetPeerTagId());
98 : }
99 : };
100 :
101 : class CommChannelManager {
102 : public:
103 : /**
104 : * @brief Get the Instance object
105 : * @return object of CommChannelManager
106 : */
107 : static CommChannelManager& GetInstance();
108 :
109 : /**
110 : * @brief Destroy the Comm Channel Manager object
111 : */
112 1 : ~CommChannelManager() = default;
113 :
114 : /**
115 : * @brief Get comm channel id
116 : * @param channel comm channel
117 : * @param channelPtr comm channel ptr in commChannelMap_
118 : * @return comm channel id.
119 : */
120 : uint32_t GetCommChannelId(const CommChannel& channel, const CommChannel*& channelPtr);
121 :
122 : /**
123 : * @brief Delete comm channel
124 : * @param channel comm channel
125 : * @return FSM_SUCCESS: success, other: failed
126 : */
127 : FsmStatus DeleteCommChannel(const CommChannel& channel);
128 :
129 : private:
130 : /**
131 : * @brief Construct a new Comm Channel Manager object
132 : */
133 1 : CommChannelManager() = default;
134 :
135 : // comm channel map
136 : std::unordered_map<const CommChannel, uint32_t, CommChannelHash> commChannelMap_;
137 : // comm channel map mutex
138 : std::mutex commChannelMapMutex_;
139 : };
140 : } // namespace dgw
141 : #endif
|