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