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