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 CHANNEL_MANAGER_H
11 : #define CHANNEL_MANAGER_H
12 :
13 : #include "hccl/hccl_res.h"
14 : #include "hccl_types.h"
15 : #include "transport_pub.h"
16 : #include "hccl_common.h"
17 : #include "hccl_mem_defs.h"
18 : #include "transport_pub.h"
19 : #include "aicpu_operator_pub.h"
20 : #include "channel_param.h"
21 : #include "manager_common.h"
22 : #include "hccl_independent_common.h"
23 :
24 : #include <unordered_map>
25 : #include <unordered_set>
26 : #include <vector>
27 : #include <string>
28 :
29 : namespace std {
30 : template <>
31 : struct hash<HcclChannelDesc> {
32 8 : size_t operator()(const HcclChannelDesc& desc) const
33 : {
34 8 : size_t hash = 0;
35 : // 仅区分remoteRank和protocol
36 8 : hash ^= std::hash<uint32_t>()(desc.remoteRank);
37 8 : hash ^= std::hash<int32_t>()(static_cast<int32_t>(desc.channelProtocol));
38 8 : return hash;
39 : }
40 : };
41 : } // namespace std
42 :
43 : namespace hccl {
44 :
45 : struct HcclChannelDescEqual {
46 1 : bool operator()(const HcclChannelDesc& lcd, const HcclChannelDesc& rcd) const
47 : {
48 : // 需要扩展增加EndpointDesc的有关内容
49 1 : return lcd.remoteRank == rcd.remoteRank && lcd.channelProtocol == rcd.channelProtocol;
50 : }
51 : };
52 :
53 : class ChannelManager {
54 : public:
55 698 : ChannelManager() = default;
56 697 : ~ChannelManager() = default;
57 : HcclResult Init(aclrtBinHandle binHandle, u32 userRank, const ManagerCallbacks& callbacks);
58 : HcclResult SetChannelCallbacks(const ChannelManagerCallbacks& channelCallbacks);
59 : HcclResult ChannelCommCreate(
60 : const std::string& commId, CommEngine engine, const HcclChannelDesc* channelDescList, uint32_t listNum,
61 : ChannelHandle* channelList);
62 : HcclResult ChannelCommGetNotifyNum(ChannelHandle channel, uint32_t* notifyNum);
63 : HcclResult ChannelCommDestroy(ChannelHandle* channelList, uint32_t channelNum);
64 : HcclResult ChannelCommGetHcclBuffer(ChannelHandle channel, CommBuffer* buffer);
65 : HcclResult ChannelCommGetRemoteMem(ChannelHandle channel, HcclMem** remoteMem, uint32_t* memNum);
66 : HcclResult ReleaseChannel();
67 : HcclResult SetHcclQos(u32 hcclQos);
68 :
69 : private:
70 : template <typename T>
71 : HcclResult CopyVectorToDeviceMem(const u64 len, DeviceMem& dstDeviceMem, const std::vector<T>& srcVec);
72 : HcclResult AllocAndClearHostMem(u64 size, std::shared_ptr<HostMem>& bufferPtr) const;
73 : HcclResult CreateWorkSpace(u64 size, DeviceMem& buffer) const;
74 : HcclResult CheckNotifyOrQPMaxNum(u64& existNum, const u64& MaxNum, const bool& isNotifyRes);
75 : HcclResult DeepCopyH2DChannelP2p(const HcclChannelP2p& hostChannelP2p, HcclChannelP2p& deviceChannelP2p);
76 : HcclResult DeepCopyH2DChannelRoce(const HcclChannelRoce& hostChannelRoce, HcclChannelRoce& deviceChannelRoce);
77 : HcclResult DeepCopyH2DChannelRemoteResV2(
78 : const HcclIndOpChannelRemoteResV2& hostRemoteResV2, HcclIndOpChannelRemoteResV2& deviceRemoteResV2);
79 : HcclResult DeepCopyH2DchannelParam(
80 : const HcclIndOpChannelRemoteResV3& hostChannelParam, HcclIndOpChannelRemoteResV3& deviceChannelParam);
81 : HcclResult ReleaseChannelParam(HcclIndOpChannelRemoteResV3& channelParam);
82 : HcclResult BuildOpRemoteChannelP2pResParam(const LINK& link, HcclIndOpChannelRemoteResV2& remoteRes);
83 : HcclResult BuildOpRemoteChannelRoceResParam(const LINK& link, HcclIndOpChannelRemoteResV2& remoteRes);
84 : HcclResult
85 : ParseChannelRemoteDataToMem(const OpCommTransport& opTransportResponse, HcclIndOpChannelRemoteResV3& channelParam);
86 : HcclResult AicpuChannelInit(
87 : const std::string& commId, const std::string& tag, CommEngine engine,
88 : const OpCommTransport& opTransportResponse, ChannelHandle* channelList, uint32_t listNum);
89 : void ClearOpTransportResponseLinks(OpCommTransport& opTransportResponse);
90 : OpCommTransport BuildChannelRequests(const std::vector<HcclChannelDesc>& descs);
91 :
92 : HcclResult CheckChannelParam(CommEngine engine, const HcclChannelDesc* channelDesc, uint32_t descNum);
93 : HcclResult RegisterHandle(
94 : const std::string& tag, CommEngine engine, const HcclChannelDesc& channelDesc, ChannelHandle channelHandle);
95 : HcclResult RegisterHandleHDPair(ChannelHandle deviceChannelHandle, ChannelHandle hostChannelHandle);
96 : HcclResult UnregisterHandle(ChannelHandle channel);
97 : HcclResult PrepareHandleArray(
98 : const std::string& tag, CommEngine engine, const HcclChannelDesc* channelDesc, uint32_t descNum,
99 : ChannelHandle* channelHandleArray, std::vector<HcclChannelDesc>& needCreateDescs,
100 : std::vector<uint32_t>& needCreateIndices);
101 : HcclResult IsChannelExist(ChannelHandle channel);
102 : HcclResult GetHostChannel(ChannelHandle channel, ChannelHandle& hostChannel);
103 :
104 : std::unordered_map<std::string, ChannelHandle> channelHandleMap_;
105 : std::unordered_map<ChannelHandle, std::string> keyMap_;
106 : std::unordered_map<ChannelHandle, CommEngine> engineMap_;
107 : std::unordered_map<ChannelHandle, ChannelHandle> channelD2HMap_;
108 : std::unordered_set<ChannelHandle*> channelHandleArraySet_;
109 : std::unordered_map<ChannelHandle, LINK> linkMap_;
110 : std::vector<LINK> channelLinks_{};
111 : u32 userRank_;
112 : std::vector<RankInfo> rankInfoList_;
113 : std::vector<std::shared_ptr<DeviceMem>> channelParamMemVector_;
114 : std::list<DeviceMem> channelParamMemList_;
115 : aclrtBinHandle binHandle_;
116 : ManagerCallbacks callbacks_; // 存储回调函数
117 : ChannelManagerCallbacks channelCallbacks_; // channelMgr的回调函数
118 : u32 hcclQos_;
119 : std::vector<HcclMemHandle> CollectMemHandles(const std::vector<HcclChannelDesc>& descs) const;
120 : };
121 :
122 : } // namespace hccl
123 :
124 : #endif // CHANNEL_MANAGER_H
|