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