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_PROCESS_H
12 : #define CHANNEL_PROCESS_H
13 :
14 : #include "hcomm_c_adpt.h"
15 : #include "channel.h"
16 : #include "mem_host_pub.h"
17 : #include <cstdint>
18 : #include <functional>
19 : #include <memory>
20 : #include <mutex>
21 : #include <string>
22 : #include <unordered_map>
23 : #include <vector>
24 :
25 : namespace hccl {
26 : class DeviceMem;
27 : }
28 :
29 : namespace hcomm {
30 :
31 : // HcommChannelGetStatus 出参状态码
32 : enum HcommChannelLinkStatus : int32_t {
33 : HCOMM_CHANNEL_STATUS_READY = 0,
34 : HCOMM_CHANNEL_STATUS_CONNECTING = 1,
35 : HCOMM_CHANNEL_STATUS_FAILED = 2,
36 : HCOMM_CHANNEL_STATUS_TIMEOUT = 3,
37 : HCOMM_CHANNEL_STATUS_RES_LOC_UNAVAIL = 4, // 本端资源不足
38 : HCOMM_CHANNEL_STATUS_RES_RMT_UNAVAIL = 5, // 对端资源不足
39 : };
40 :
41 : struct DeviceChannelKey {
42 : int32_t deviceId;
43 : ChannelHandle handle;
44 :
45 151 : bool operator==(const DeviceChannelKey& other) const
46 : {
47 151 : return deviceId == other.deviceId && handle == other.handle;
48 : }
49 : };
50 :
51 : struct DeviceChannelKeyHash {
52 230 : std::size_t operator()(const DeviceChannelKey& key) const
53 : {
54 230 : return std::hash<int32_t>()(key.deviceId) ^ (std::hash<ChannelHandle>()(key.handle) << 1);
55 : }
56 : };
57 :
58 : class ChannelProcess {
59 : public:
60 : ChannelProcess() = default;
61 : ~ChannelProcess() = default;
62 : static HcclResult CreateChannelsLoop(
63 : EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescs, uint32_t channelNum,
64 : ChannelHandle* outHandles, bool isSharedQueue = false);
65 : static HcclResult ResolveUserHandleToHost(ChannelHandle userHandle, ChannelHandle& hostHandle);
66 : static HcclResult ResolveHostHandleToDevice(ChannelHandle hostHandle, ChannelHandle& deviceHandle);
67 : static HcclResult
68 : ChannelUpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHandleNum, ChannelHandle channelHandle);
69 : static HcclResult GetChannelsInfo(
70 : const ChannelHandle* channelList, uint32_t listNum, std::vector<CommEngine>& engines,
71 : std::vector<HcommChannelDesc>& channelDescs, std::vector<ChannelStatus>& statusList);
72 : static HcclResult HandleStatusByEngine(
73 : const ChannelHandle* channelList, uint32_t listNum, const std::vector<CommEngine>& engines,
74 : const std::vector<HcommChannelDesc>& channelDescFinals, const std::vector<ChannelStatus>& internalStatus,
75 : int32_t* statusList);
76 : static HcclResult PrepareUserChannels(
77 : ChannelHandle* targetChannels, ChannelHandle* userChannels, HcommChannelDesc* channelDescs, uint32_t channelNum,
78 : CommEngine engine);
79 : static HcclResult ChannelGetStatus(const ChannelHandle* channelList, uint32_t listNum, int32_t* statusList);
80 : static HcclResult ChannelKernelLaunchForComm(
81 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
82 : const std::string& commTag, aclrtBinHandle binHandle);
83 : static HcclResult ChannelGetNotifyNum(ChannelHandle channelHandle, uint32_t* notifyNum);
84 : static HcclResult
85 : ChannelGetRemoteMems(ChannelHandle channelHandle, uint32_t* memNum, CommMem** remoteMem, char*** memInfos);
86 : static HcclResult ChannelKernelDestroy(ChannelHandle* channelHandles, uint32_t listNum, aclrtBinHandle binHandle);
87 : static HcclResult
88 : ChannelDestroy(const ChannelHandle* channels, uint32_t channelNum, aclrtBinHandle binHandle = nullptr);
89 : static HcclResult ChannelGet(const ChannelHandle channelHandle, void** channel);
90 : static HcclResult ChannelClean(const ChannelHandle* channelList, uint32_t channelNum);
91 : static HcclResult ChannelResume(const ChannelHandle* channelList, uint32_t channelNum);
92 : static HcclResult ChannelUpdateKernelLaunch(
93 : ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum,
94 : const std::string& commTag, aclrtBinHandle binHandle);
95 : static HcclResult
96 : RegisterChannelD2HMap(ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum);
97 : static HcclResult
98 : FillChannelD2HMap(ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum);
99 : static HcclResult LaunchChannelKernel(
100 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
101 : aclrtBinHandle binHandle);
102 : static HcclResult InsertPluginChannelToMap(ChannelHandle handle, std::shared_ptr<Channel> channelPtr);
103 : static HcclResult RemovePluginChannelFromMap(ChannelHandle handle);
104 :
105 : private:
106 : template <typename Func>
107 : static HcclResult WithChannelByHandleLocked(ChannelHandle inHandle, Func&& func);
108 :
109 : static HcclResult
110 : CombineHostMemory(const std::vector<std::vector<char>>& hostPackBuffers, hccl::HostMem& hostPackBuf);
111 : static HcclResult CopyUpdateKernelPackResToDevice(
112 : const std::vector<std::vector<char>>& hostPackBuffers, const std::vector<u32>& channelSizeVec,
113 : uint32_t totalListNum, hccl::DeviceMem& channelSizeAddr, hccl::DeviceMem& devicePackBuf);
114 : static HcclResult LaunchChannelKernelCommon(
115 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
116 : const std::string& commTag, aclrtBinHandle binHandle, const std::string& kernelName, bool needProfiling);
117 : static HcclResult ChannelKernelLaunchForBase(
118 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
119 : aclrtBinHandle binHandle);
120 : static HcclResult LaunchCommonChannelKernel(
121 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum,
122 : HcommChannelKind channelKind, aclrtBinHandle binHandle);
123 : static HcclResult ChannelResumeConcurrency(const ChannelHandle* channelList, uint32_t channelNum);
124 : static HcclResult
125 : RemoveSingleChannel(int32_t deviceId, ChannelHandle inHandle, std::vector<ChannelHandle>& deviceHandles);
126 :
127 : static std::unordered_map<ChannelHandle, std::shared_ptr<Channel>> g_ChannelMap;
128 : static std::unordered_map<DeviceChannelKey, ChannelHandle, DeviceChannelKeyHash> g_ChannelD2HMap;
129 : static std::unordered_map<DeviceChannelKey, ChannelHandle, DeviceChannelKeyHash> g_ChannelH2DMap;
130 : static std::mutex g_ChannelMapMtx;
131 : };
132 : } // namespace hcomm
133 : #endif // CHANNEL_PROCESS_H
|