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 HCCL_CCU_COMPONENT_H
12 : #define HCCL_CCU_COMPONENT_H
13 :
14 : #include <memory>
15 : #include <vector>
16 : #include <unordered_map>
17 :
18 : #include "hccl/hccl_types.h"
19 : #include "types.h"
20 :
21 : #include "ccu_channel_mgr.h"
22 : #include "ccu_res_allocator.h"
23 : #include "ccu_device_manager.h"
24 : #include "tp_manager.h"
25 : #include "ccu_error_handler.h"
26 :
27 : namespace Hccl {
28 :
29 : class CcuComponent {
30 : public:
31 : CcuComponent(const CcuComponent &that) = delete;
32 : CcuComponent &operator=(const CcuComponent &that) = delete;
33 :
34 : static CcuComponent &GetInstance(const int32_t deviceLogicId);
35 : void Init();
36 : void Deinit();
37 :
38 : HcclResult GetCcuResourceSpaceBufInfo(const uint8_t dieId, uint64_t &addr, uint64_t &size) const;
39 : HcclResult GetCcuResourceSpaceTokenInfo(const uint8_t dieId, uint64_t &tokenId,
40 : uint64_t &tokenValue) const;
41 : HcclResult GetCcuResourceSpaceTokenInfoForLocal(const uint8_t dieId, uint64_t &tokenId,
42 : uint64_t &tokenValue) const;
43 :
44 : HcclResult AllocChannels(const uint8_t dieId, const ChannelPara &channelPara,
45 : std::vector<ChannelInfo> &channelInfos);
46 : HcclResult ConfigChannel(const uint8_t dieId, const ChannelCfg &cfg);
47 : HcclResult ReleaseChannel(const uint8_t dieId, const uint32_t channelId);
48 :
49 : HcclResult GetLoopChannelId(const uint8_t srcDieId, const uint8_t dstDieId,
50 : uint32_t &channelId) const;
51 :
52 : HcclResult AllocRes(const uint8_t dieId, const ResType resType, const uint32_t num,
53 : const bool consecutive, vector<ResInfo> &resInfos);
54 : HcclResult ReleaseRes(const uint8_t dieId, const ResType resType, const uint32_t startId,
55 : const uint32_t num);
56 :
57 : HcclResult AllocIns(const uint8_t dieId, const uint32_t num, ResInfo &insInfo);
58 : HcclResult ReleaseIns(const uint8_t dieId, const ResInfo &insInfo);
59 : uint32_t GetInsConsecutiveRemainSize(const uint8_t dieId) const;
60 : HcclResult AllocCke(const uint8_t dieId, const uint32_t num, vector<ResInfo> &ckeInfos);
61 : HcclResult ReleaseCke(const uint8_t dieId, const vector<ResInfo> &ckeInfos);
62 : HcclResult AllocXn(const uint8_t dieId, const uint32_t num, vector<ResInfo> &xnInfos);
63 : HcclResult ReleaseXn(const uint8_t dieId, const vector<ResInfo> &xnInfos);
64 :
65 : HcclResult CleanDieCkes(const uint8_t dieId) const;
66 : HcclResult SetTaskKill();
67 : HcclResult SetTaskKillDone();
68 : HcclResult CleanTaskKillState() const;
69 :
70 : const std::array<bool, MAX_CCU_IODIE_NUM> &GetDieEnableFlags() const;
71 6 : bool IsInited() const { return ifInit; }
72 :
73 : private:
74 : static constexpr uint32_t INVALID_DEV_ID = 0xFFFFFFFF;
75 : bool ifInit{false};
76 : int32_t devLogicId{static_cast<int32_t>(INVALID_DEV_ID)};
77 : uint32_t devPhyId{INVALID_DEV_ID};
78 : CcuVersion ccuVersion{CcuVersion::CCU_INVALID};
79 : std::array<bool, MAX_CCU_IODIE_NUM> dieEnableFlags{}; // 根据资源规格的记录可用的die
80 :
81 : // 记录环回设备信息,dieId, (feId, ipAddr)
82 : std::unordered_map<uint8_t, std::pair<uint32_t, IpAddress>> loopFeIpAddrMap{};
83 : // 记录CCU资源空间Buffer,避免重复内存注册
84 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> ccuRmaBufferMap{};
85 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> localCcuRmaBufferMap{};
86 : std::vector<std::unique_ptr<LocalUbRmaBuffer>> additionalCcuRmaBufferMap{};
87 : // 资源管理器
88 : std::array<std::unique_ptr<CcuChannelMgr>, MAX_CCU_IODIE_NUM> channelMgrs{};
89 : std::array<std::unique_ptr<CcuResAllocator>, MAX_CCU_IODIE_NUM> resAllocators{};
90 : // 环回channel编号
91 : std::array<uint32_t, MAX_CCU_IODIE_NUM> loopChannelIds{};
92 : // 环回jetty资源信息
93 : std::unordered_map<uint8_t, std::vector<HrtRaUbJettyCreatedOutParam>> createdOutParamMap{};
94 : using ImportOutParamPair = std::pair<RdmaHandle, HrtRaUbJettyImportedOutParam>;
95 : std::unordered_map<uint8_t, std::vector<ImportOutParamPair>> importedOutParamMap{};
96 : std::unordered_map<IpAddress, TpInfo> tpInfoMap{};
97 : std::unordered_map<IpAddress, TpAttrInfo> tpAttrInfoMap{};
98 : std::unordered_map<IpAddress, uint32_t> psnMap{};
99 :
100 : // CCU Task Kill相关状态
101 : enum class CcuTaskKillStatus : uint8_t { INIT = 0, TASK_KILL = 1, KILL_DONE = 2, CLEAN_TIF = 3, INVALID = 4};
102 : CcuTaskKillStatus status{CcuTaskKillStatus::INVALID};
103 : std::mutex innerMutex;
104 : std::mutex taskKillMutex_;
105 :
106 83 : explicit CcuComponent() = default;
107 : ~CcuComponent();
108 :
109 : void CheckDiesEnable();
110 : void ChooseLoopEid(bool &dieDrvEnableFlag, uint8_t dieId);
111 : HcclResult GetLoopFeIpByDieId(const uint8_t dieId, uint32_t &feId, IpAddress &ipAddr);
112 : void CreateCcuRmaBuffer();
113 : void CreateResourceManagers();
114 : void CreateLoopChannels();
115 : HcclResult CreateLoopChannel(const uint8_t dieId, uint32_t &channelId);
116 : HcclResult CreateAndImportLoopJettys(const uint8_t dieId, const IpAddress &ipAddr,
117 : const vector<JettyInfo> &jettyInfos);
118 : TpInfo RequestNewTpInfo(const IpAddress &srcIpAddr, const IpAddress &dstIpAddr) const;
119 : TpInfo GetTpInfo(const IpAddress &ipAddr);
120 : TpAttrInfo GetLoopTpAttr(const IpAddress &ipAddr, const TpHandle tpHandle);
121 : uint32_t GetPsn(const IpAddress &ipAddr);
122 : HcclResult ConfigLoopChannel(const uint8_t dieId, const IpAddress &ipAddr,
123 : const ChannelInfo &channelInfo);
124 : void ConfigMsIdToken();
125 :
126 : void ReleaseJettyRes();
127 : void UnimportAllJetty();
128 : void DestroyAllJetty();
129 :
130 : void SetProcess(CcuOpcodeType opCode) const;
131 : void PrintCcuMissionStatus(int32_t devLogicId) const;
132 : };
133 :
134 : }; // namespace Hccl
135 :
136 : #endif
|