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 : };
38 :
39 : struct DeviceChannelKey {
40 : int32_t deviceId;
41 : ChannelHandle handle;
42 :
43 119 : bool operator==(const DeviceChannelKey& other) const
44 : {
45 119 : return deviceId == other.deviceId && handle == other.handle;
46 : }
47 : };
48 :
49 : struct DeviceChannelKeyHash {
50 167 : std::size_t operator()(const DeviceChannelKey& key) const
51 : {
52 167 : return std::hash<int32_t>()(key.deviceId) ^ (std::hash<ChannelHandle>()(key.handle) << 1);
53 : }
54 : };
55 :
56 : class ChannelProcess {
57 : public:
58 : ChannelProcess() = default;
59 : ~ChannelProcess() = default;
60 : static HcclResult CreateChannelsLoop(
61 : EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescs, uint32_t channelNum,
62 : ChannelHandle* outHandles, bool isSharedQueue = false);
63 : static HcclResult
64 : ChannelUpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHandleNum, ChannelHandle channelHandle);
65 : static HcclResult GetChannelsInfo(
66 : const ChannelHandle* channelList, uint32_t listNum, std::vector<CommEngine>& engines,
67 : std::vector<HcommChannelDesc>& channelDescs, std::vector<ChannelStatus>& statusList);
68 : static HcclResult HandleStatusByEngine(
69 : const ChannelHandle* channelList, uint32_t listNum, const std::vector<CommEngine>& engines,
70 : const std::vector<HcommChannelDesc>& channelDescFinals, const std::vector<ChannelStatus>& internalStatus,
71 : int32_t* statusList);
72 : static HcclResult PrepareUserChannels(
73 : ChannelHandle* targetChannels, ChannelHandle* userChannels, HcommChannelDesc* channelDescs, uint32_t channelNum,
74 : CommEngine engine);
75 : static HcclResult ChannelGetStatus(const ChannelHandle* channelList, uint32_t listNum, int32_t* statusList);
76 : static HcclResult ChannelKernelLaunchForComm(
77 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
78 : const std::string& commTag, aclrtBinHandle binHandle);
79 : static HcclResult ChannelGetNotifyNum(ChannelHandle channelHandle, uint32_t* notifyNum);
80 : static HcclResult
81 : ChannelGetRemoteMems(ChannelHandle channelHandle, uint32_t* memNum, CommMem** remoteMem, char*** memInfos);
82 : static HcclResult ChannelKernelDestroy(ChannelHandle* channelHandles, uint32_t listNum, aclrtBinHandle binHandle);
83 : static HcclResult
84 : ChannelDestroy(const ChannelHandle* channels, uint32_t channelNum, aclrtBinHandle binHandle = nullptr);
85 : static HcclResult ChannelGet(const ChannelHandle channelHandle, void** channel);
86 :
87 : static HcclResult ChannelClean(const ChannelHandle* channelList, uint32_t channelNum);
88 : static HcclResult ChannelResume(const ChannelHandle* channelList, uint32_t channelNum);
89 : static HcclResult ChannelUpdateKernelLaunch(
90 : ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum,
91 : const std::string& commTag, aclrtBinHandle binHandle);
92 : static HcclResult
93 : RegisterChannelD2HMap(ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum);
94 : static HcclResult
95 : FillChannelD2HMap(ChannelHandle* deviceChannelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum);
96 : static HcclResult LaunchChannelKernel(
97 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
98 : aclrtBinHandle binHandle);
99 : static HcclResult InsertPluginChannelToMap(ChannelHandle handle, std::shared_ptr<Channel> channelPtr);
100 : static HcclResult RemovePluginChannelFromMap(ChannelHandle handle);
101 :
102 : private:
103 : template <typename Func>
104 : static HcclResult WithChannelByHandleLocked(ChannelHandle inHandle, Func&& func);
105 :
106 : static HcclResult
107 : CombineHostMemory(const std::vector<std::vector<char>>& hostPackBuffers, hccl::HostMem& hostPackBuf);
108 : static HcclResult CopyUpdateKernelPackResToDevice(
109 : const std::vector<std::vector<char>>& hostPackBuffers, const std::vector<u32>& channelSizeVec,
110 : uint32_t totalListNum, hccl::DeviceMem& channelSizeAddr, hccl::DeviceMem& devicePackBuf);
111 : static HcclResult LaunchChannelKernelCommon(
112 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
113 : const std::string& commTag, aclrtBinHandle binHandle, const std::string& kernelName, bool needProfiling);
114 : static HcclResult ChannelKernelLaunchForBase(
115 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
116 : aclrtBinHandle binHandle);
117 : static HcclResult LaunchCommonChannelKernel(
118 : ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, uint32_t listNum,
119 : HcommChannelKind channelKind, aclrtBinHandle binHandle);
120 : static HcclResult ChannelResumeConcurrency(const ChannelHandle* channelList, uint32_t channelNum);
121 : static HcclResult
122 : RemoveSingleChannel(int32_t deviceId, ChannelHandle inHandle, std::vector<ChannelHandle>& deviceHandles);
123 :
124 : static std::unordered_map<ChannelHandle, std::shared_ptr<Channel>> g_ChannelMap;
125 : static std::unordered_map<DeviceChannelKey, ChannelHandle, DeviceChannelKeyHash> g_ChannelD2HMap;
126 : static std::mutex g_ChannelMapMtx;
127 : };
128 : } // namespace hcomm
129 : #endif // CHANNEL_PROCESS_H
|