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 : HcclResult AllocCke(const uint8_t dieId, const uint32_t num, vector<ResInfo> &ckeInfos);
60 : HcclResult ReleaseCke(const uint8_t dieId, const vector<ResInfo> &ckeInfos);
61 : HcclResult AllocXn(const uint8_t dieId, const uint32_t num, vector<ResInfo> &xnInfos);
62 : HcclResult ReleaseXn(const uint8_t dieId, const vector<ResInfo> &xnInfos);
63 :
64 : HcclResult CleanDieCkes(const uint8_t dieId) const;
65 : HcclResult SetTaskKill();
66 : HcclResult SetTaskKillDone();
67 : HcclResult CleanTaskKillState() const;
68 :
69 : const std::array<bool, MAX_CCU_IODIE_NUM> &GetDieEnableFlags() const;
70 6 : bool IsInited() const { return ifInit; }
71 :
72 : private:
73 : static constexpr uint32_t INVALID_DEV_ID = 0xFFFFFFFF;
74 : bool ifInit{false};
75 : int32_t devLogicId{static_cast<int32_t>(INVALID_DEV_ID)};
76 : uint32_t devPhyId{INVALID_DEV_ID};
77 : CcuVersion ccuVersion{CcuVersion::CCU_INVALID};
78 : std::array<bool, MAX_CCU_IODIE_NUM> dieEnableFlags{}; // 根据资源规格的记录可用的die
79 :
80 : // 记录环回设备信息,dieId, (feId, ipAddr)
81 : std::unordered_map<uint8_t, std::pair<uint32_t, IpAddress>> loopFeIpAddrMap{};
82 : // 记录CCU资源空间Buffer,避免重复内存注册
83 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> ccuRmaBufferMap{};
84 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> localCcuRmaBufferMap{};
85 : std::vector<std::unique_ptr<LocalUbRmaBuffer>> additionalCcuRmaBufferMap{};
86 : // 资源管理器
87 : std::array<std::unique_ptr<CcuChannelMgr>, MAX_CCU_IODIE_NUM> channelMgrs{};
88 : std::array<std::unique_ptr<CcuResAllocator>, MAX_CCU_IODIE_NUM> resAllocators{};
89 : // 环回channel编号
90 : std::array<uint32_t, MAX_CCU_IODIE_NUM> loopChannelIds{};
91 : // 环回jetty资源信息
92 : std::unordered_map<uint8_t, std::vector<HrtRaUbJettyCreatedOutParam>> createdOutParamMap{};
93 : using ImportOutParamPair = std::pair<RdmaHandle, HrtRaUbJettyImportedOutParam>;
94 : std::unordered_map<uint8_t, std::vector<ImportOutParamPair>> importedOutParamMap{};
95 : std::unordered_map<IpAddress, TpInfo> tpInfoMap{};
96 : std::unordered_map<IpAddress, TpAttrInfo> tpAttrInfoMap{};
97 : std::unordered_map<IpAddress, uint32_t> psnMap{};
98 :
99 : // CCU Task Kill相关状态
100 : enum class CcuTaskKillStatus : uint8_t { INIT = 0, TASK_KILL = 1, KILL_DONE = 2, CLEAN_TIF = 3, INVALID = 4};
101 : CcuTaskKillStatus status{CcuTaskKillStatus::INVALID};
102 : std::mutex innerMutex;
103 : std::mutex taskKillMutex_;
104 :
105 82 : explicit CcuComponent() = default;
106 : ~CcuComponent();
107 :
108 : void CheckDiesEnable();
109 : void ChooseLoopEid(bool &dieDrvEnableFlag, uint8_t dieId);
110 : HcclResult GetLoopFeIpByDieId(const uint8_t dieId, uint32_t &feId, IpAddress &ipAddr);
111 : void CreateCcuRmaBuffer();
112 : void CreateResourceManagers();
113 : void CreateLoopChannels();
114 : HcclResult CreateLoopChannel(const uint8_t dieId, uint32_t &channelId);
115 : HcclResult CreateAndImportLoopJettys(const uint8_t dieId, const IpAddress &ipAddr,
116 : const vector<JettyInfo> &jettyInfos);
117 : TpInfo RequestNewTpInfo(const IpAddress &srcIpAddr, const IpAddress &dstIpAddr) const;
118 : TpInfo GetTpInfo(const IpAddress &ipAddr);
119 : TpAttrInfo GetLoopTpAttr(const IpAddress &ipAddr, const TpHandle tpHandle);
120 : uint32_t GetPsn(const IpAddress &ipAddr);
121 : HcclResult ConfigLoopChannel(const uint8_t dieId, const IpAddress &ipAddr,
122 : const ChannelInfo &channelInfo);
123 : void ConfigMsIdToken();
124 :
125 : void ReleaseJettyRes();
126 : void UnimportAllJetty();
127 : void DestroyAllJetty();
128 :
129 : void SetProcess(CcuOpcodeType opCode) const;
130 : void PrintCcuMissionStatus(int32_t devLogicId) const;
131 : };
132 :
133 : }; // namespace Hccl
134 :
135 : #endif
|