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