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 CCU_CHANNELCTX_POOLS_H
12 : #define CCU_CHANNELCTX_POOLS_H
13 :
14 : #include <vector>
15 : #include <unordered_map>
16 :
17 : #include "ccu_jetty_.h"
18 : #include "hash_utils.h"
19 : #include "ip_address.h"
20 : #include "virtual_topo.h"
21 :
22 : namespace hcomm {
23 :
24 : //管理着有限的硬件资源:ChannelCtx与jetty
25 : class CcuChannelCtxPool final {
26 : public:
27 : explicit CcuChannelCtxPool(int32_t devLogicId);
28 : ~CcuChannelCtxPool();
29 :
30 : HcclResult PrepareCreate(const std::vector<Hccl::LinkData> &links, uint32_t sqSize = 0);
31 : using CcuChannelCtx = std::pair<CcuChannelInfo, std::vector<CcuJetty *>>;
32 : HcclResult GetChannelCtx(const Hccl::LinkData &link, CcuChannelCtx &channelCtx) const;
33 : HcclResult GetCcuChannelCtxById(const std::pair<uint8_t, uint32_t> &key, CcuChannelCtx& ctx);
34 :
35 : private:
36 : struct ResIdHash {
37 60 : std::size_t operator()(const std::pair<uint8_t, uint32_t>& p) const
38 : {
39 60 : return Hccl::HashCombine({p.first, p.second});
40 : }
41 : };
42 :
43 : using CcuJettyPtr = CcuJetty*;
44 : using BatchKey = Hccl::IpAddress; // srcIpAddress;
45 : using ResIdkey = std::pair<uint8_t, uint32_t>;
46 : using ChannelIdKey = ResIdkey;
47 : using JettyIdKey = ResIdkey;
48 :
49 : // 平台层每次调用CcuAllocChannels可能提供多个ccu channel,且不同srcIp的jetty不能复用
50 : // 故以srcIp为粒度,多次调用接口,每次接口结果定义为一个批次资源
51 : struct ResourceBatch { // 记录该批次申请到的所有channel资源信息
52 : BatchKey key;
53 : std::vector<ChannelIdKey> channelIdKeys;
54 : std::vector<ChannelIdKey> availableChannelIdKeys;
55 : std::unordered_map<JettyIdKey, std::unique_ptr<CcuJetty>, ResIdHash> jettys;
56 :
57 12 : ResourceBatch(const BatchKey &batchKey) : key(batchKey) {};
58 : HcclResult Init(const std::vector<CcuChannelInfo> &channelInfos);
59 : };
60 :
61 : struct Allocation {
62 : Hccl::LinkData link;
63 : ChannelIdKey channelIdKey;
64 : ResourceBatch *batchPtr;
65 : };
66 :
67 : struct UnconfirmedRecord {
68 : std::vector<Allocation> allocations; // 记录从已申请的资源中的分配操作
69 : std::unordered_set<ResourceBatch *> newBatchSet; // 记录新申请资源的操作
70 :
71 : void Clear() {
72 : allocations.clear();
73 : newBatchSet.clear();
74 : }
75 : };
76 :
77 : private:
78 : HcclResult GetAvailableBatch(const BatchKey &batchKey, ResourceBatch *&batchPtr, uint32_t sqSize);
79 : bool FindAvailableBatch(const BatchKey &batchKey, ResourceBatch *&batchPtr) const;
80 : HcclResult CreateAndSaveNewBatch(const BatchKey &batchKey,
81 : const std::vector<CcuChannelInfo> channelInfos, ResourceBatch *&batchPtr);
82 : HcclResult ReleaseConfirmedChannelRes();
83 :
84 : private:
85 : int32_t devLogicId_{0};
86 : bool isReleased_{true};
87 :
88 : // 本轮下发算子新增分配记录
89 : UnconfirmedRecord unconfirmedRecord_;
90 : // 各资源申请记录,当前按SrcIpAddr粒度申请和管理
91 : std::unordered_map<BatchKey, std::vector<std::unique_ptr<ResourceBatch>>> batchMap_;
92 : // 各link已分配的channel资源Id信息
93 : std::unordered_map<Hccl::LinkData, ChannelIdKey> allocatedChannelIdMap_;
94 : // 全部已申请的channel资源信息,资源申请成功后将要记录到该map中
95 : std::unordered_map<ChannelIdKey, CcuChannelCtx, ResIdHash> channelJettyInfoMap_;
96 : // 以die粒度记录已分配channel资源, index: dieId
97 : std::unordered_map<uint8_t, uint32_t> usedChannelCntMap_;
98 : // 记录channel与对端rank的映射关系, index: (die, channelId)
99 : std::unordered_map<ChannelIdKey, Hccl::RankId, ResIdHash> channelRemoteRankIdMap_;
100 : };
101 :
102 : } // namespace hcomm
103 :
104 : #endif // CCU_CHANNELCTX_POOLS_H
|