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