Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 COLL_COMM_H
12 : #define COLL_COMM_H
13 :
14 : #include <cstddef>
15 : #include <cstdint>
16 : #include <functional>
17 : #include <memory>
18 : #include <mutex>
19 : #include <string>
20 : #include <vector>
21 : #include "my_rank.h"
22 : #include "rank_graph.h"
23 : #include "comm_config_pub.h"
24 : #include "comm_engine_res_manager.h"
25 : #include "independent_op_context_manager.h"
26 : #include "comm_mem_manager.h"
27 : #include "channel_manager.h"
28 : #include "hcclCommDfx.h"
29 : #include "rank_graph_v2.h"
30 : #include "error_message_v2.h"
31 : #include "manager_common.h"
32 : #include "group_schedule_mgr.h"
33 : #include "include/hccl_communicator.h"
34 : #include "hccl/hccl_res.h"
35 : namespace Hccl {
36 : class DevBuffer;
37 : } // namespace Hccl
38 : namespace hccl {
39 : class SymmetricMemory;
40 : struct SymmetricMemoryResource;
41 : struct SymmetricMemoryDeleter {
42 : void operator()(SymmetricMemory* ptr) const;
43 : };
44 : /**
45 : * @note 职责:集合通信通信域上下文管理,包括RankGraph和本rank信息资源等内容。
46 : * 当前需包含原有的91092/91093的通信域、原有的91095的通信域void
47 : * *指针、及新独立算子架构的通信域(支持91092/91093/91095...)。
48 : */
49 : enum class CollCommInitMode {
50 : fullMode, // 全功能模式:给A5及后续新架构使用,完整的CollComm初始化和资源管理
51 : simpleMode // 简化模式:给A2/A3老芯片使用,由于架构限制,仅将RankGraph、MyRank等放入CollComm管理
52 : };
53 :
54 : class CollComm {
55 : public:
56 : CollComm(
57 : void* comm, uint32_t rankId, const std::string& commName, const ManagerCallbacks& callbacks,
58 : CollCommInitMode initMode = CollCommInitMode::fullMode);
59 : ~CollComm();
60 :
61 : // 初始化通信域
62 : HcclResult Init(void* rankGraph, aclrtBinHandle binHandle, HcclMem cclBuffer, uint32_t opExpansionMode = 0);
63 :
64 223 : inline CommConfig& GetCommConfig() { return config_; }
65 21 : inline RankGraph* GetRankGraph() { return rankgraph_; }
66 46 : inline CommEngineResMgr* GetCommEngineResMgr() { return commEngineResMgr_.get(); }
67 : inline ContextManager* GetContextManager() { return contextMgr_.get(); }
68 0 : inline CommMemMgr* GetCommMemMgr() { return commMemMgr_.get(); }
69 0 : inline ChannelManager* GetChannelManager() { return channelMgr_.get(); }
70 3 : void* GetCommunicatorV2() { return comm_; }
71 :
72 : // 获取MyRank
73 238 : MyRank* GetMyRank() const { return myRank_.get(); }
74 :
75 : // 获取Rank ID
76 : uint32_t GetMyRankId() const;
77 :
78 : // 获取devicelogicId
79 494 : s32 GetDeviceLogicId() const { return deviceLogicId_; }
80 :
81 : // 是否为全功能模式(fullMode),供外部 owner 在注册/注销前判断是否需要管理
82 524 : bool IsFullMode() const { return initMode_ == CollCommInitMode::fullMode; }
83 :
84 : // 获取Rank数量
85 346 : uint32_t GetRankSize() const
86 : {
87 346 : if (rankgraph_ == nullptr) {
88 9 : HCCL_ERROR("[CollComm]get ranksize failed");
89 9 : return 0;
90 : }
91 337 : uint32_t rankSize{0};
92 337 : HcclResult ret = rankgraph_->GetRankSize(&rankSize);
93 337 : if (ret != 0) {
94 0 : HCCL_ERROR("[CollComm]get ranksize failed");
95 0 : return 0;
96 : }
97 337 : return rankSize;
98 : }
99 :
100 : // 获取HcclCommDfx
101 23 : HcclCommDfx* GetHcclCommDfx() { return hcclCommDfx_.get(); }
102 14 : std::function<HcclResult(u32, u32, const Hccl::TaskParam&, u64)> GetDfxCallback()
103 : {
104 14 : if (hcclCommDfx_ == nullptr) {
105 0 : HCCL_ERROR("[CollComm]CollComm DfxCallBack failed. hcclCommDfx is nullptr");
106 0 : return nullptr;
107 : }
108 14 : return hcclCommDfx_->GetCallback();
109 : }
110 972 : const std::string& GetCommId() const { return commId_; }
111 : HcclResult
112 : GetHDCommunicate(HDCommunicateParams& kfcControlTransferH2DParams, HDCommunicateParams& kfcStatusTransferD2HParams);
113 : Hccl::ErrorMessageReport GetAicpuTaskException();
114 : HcclResult GetParentRankId(u32& parentRankId) const;
115 : uint32_t UpdateIndex();
116 :
117 : // Todo:在这里做N秒快恢
118 : HcclCommStatus GetCommStatus() const;
119 : HcclResult Suspend();
120 : HcclResult Clean();
121 : HcclResult Resume();
122 : HcclResult RegisterWindow(void* ptr, size_t size, HcclCommSymWindow* winHandle);
123 : HcclResult DeregisterWindow(HcclCommSymWindow winHandle);
124 : HcclResult GetCommSymWin(void* ptr, size_t size, HcclCommSymWindow* winHandle, size_t* offset);
125 : HcclResult RegisterPendingSymmetricMemHandles(std::vector<HcclMemHandle>& memHandles);
126 : HcclResult
127 : UpdateSymmetricRemoteMem(uint32_t remoteRank, const CommMem* remoteMems, const std::vector<std::string>& memTags);
128 : HcclResult GetHcclBinHandle(aclrtBinHandle& binHcclHandle);
129 : std::shared_ptr<class GroupScheduleMgr> groupScheduleMgr{nullptr}; // for group
130 :
131 : private:
132 : HcclResult DestroyAicpuComm();
133 : HcclResult InitHDCommunicate();
134 : HcclResult InitTaskExceptionHandler();
135 : HcclResult InitKfcAndRegisterCollComm();
136 : HcclResult GetRankIpPortMap();
137 : HcclResult InitSymmetricMemory();
138 : HcclResult RegisterSymmetricMemoryResource(void* ptr, size_t size, SymmetricMemoryResource& resource);
139 : void UnregisterSymmetricMemoryResource(const SymmetricMemoryResource& resource);
140 :
141 : /*
142 : * CollComm初始化方式:
143 : * fullMode:给A5及后续新架构使用,完整的CollComm初始化和资源管理
144 : * SimpleMode:给A2/A3老芯片使用,由于架构限制,仅将RankGraph、MyRank等放入CollComm管理,简化CollComm实现
145 : */
146 : HcclResult InitFullMode(void* rankGraph, aclrtBinHandle binHandle, HcclMem cclBuffer, uint32_t opExpansionMode);
147 : HcclResult InitSimpleMode(void* rankGraph, aclrtBinHandle binHandle, HcclMem cclBuffer, uint32_t opExpansionMode);
148 :
149 : HcclResult HcclBinaryUnLoad();
150 :
151 : void* comm_{nullptr};
152 : uint32_t rankId_{};
153 : std::string commId_;
154 : CommConfig config_{};
155 : HcclCommStatus commStatus_{HcclCommStatus::HCCL_COMM_STATUS_INVALID};
156 :
157 : ManagerCallbacks callbacks_;
158 : s32 deviceLogicId_{0};
159 : uint32_t index_{0};
160 :
161 : // 持有CommunicatorImpl中cclBuffer的shared_ptr拷贝,延长其生命周期
162 : std::shared_ptr<Hccl::DevBuffer> cclBuffer_{nullptr};
163 :
164 : RankGraph* rankgraph_{nullptr};
165 : std::unique_ptr<RankGraph> rankGraphOwner_{nullptr};
166 : std::unique_ptr<CommEngineResMgr> commEngineResMgr_{nullptr};
167 : std::unique_ptr<ContextManager> contextMgr_{nullptr};
168 : std::unique_ptr<CommMemMgr> commMemMgr_{nullptr};
169 : std::unique_ptr<ChannelManager> channelMgr_{nullptr};
170 : std::shared_ptr<MyRank> myRank_{};
171 : std::unique_ptr<HcclCommDfx> hcclCommDfx_{nullptr};
172 : uintptr_t addr_{0};
173 : std::size_t size_{0};
174 : HcclMemType memType_{HcclMemType::HCCL_MEM_TYPE_DEVICE};
175 :
176 : // NS recover
177 : bool isCleaned_{false};
178 :
179 : std::shared_ptr<HDCommunicate> kfcControlTransferH2D_{nullptr};
180 : std::shared_ptr<HDCommunicate> kfcStatusTransferD2H_{nullptr};
181 : Hccl::RankIpPortMapPtr rankIpPortMap_;
182 :
183 : CollCommInitMode initMode_{CollCommInitMode::fullMode}; // 初始化模式
184 : std::unique_ptr<SymmetricMemory, SymmetricMemoryDeleter> symmetricMemory_{nullptr};
185 : aclrtBinHandle binHcclHandle_{nullptr};
186 : std::mutex binHcclmutex_;
187 : };
188 : } // namespace hccl
189 :
190 : #endif // COLL_COMM_H
|