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