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