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 ENDPOINT_MGR_H
12 : #define ENDPOINT_MGR_H
13 :
14 : #include <memory>
15 : #include <unordered_map>
16 : #include <vector>
17 : #include <mutex>
18 : #include "endpoint.h"
19 : #include "../endpoint_pairs/endpoint_pair.h"
20 : #include "hccl_mem_defs.h"
21 :
22 : namespace hcomm {
23 :
24 : /**
25 : * @brief endpoint 粒度的 tag→handle 映射。
26 : * 持有 EndpointHandle,析构时自动解注册全部 tag 对应内存。
27 : */
28 : class TaggedMemMap {
29 : public:
30 16 : explicit TaggedMemMap(EndpointHandle handle) : handle_(handle) {}
31 : ~TaggedMemMap();
32 :
33 : TaggedMemMap(const TaggedMemMap&) = delete;
34 : TaggedMemMap& operator=(const TaggedMemMap&) = delete;
35 : TaggedMemMap(TaggedMemMap&&) = default;
36 : TaggedMemMap& operator=(TaggedMemMap&&) = default;
37 :
38 25 : uint64_t GetVersion() const { return version_; }
39 17 : void SetVersion(uint64_t ver) { version_ = ver; }
40 :
41 : MemHandle FindHandle(const std::string& tag) const;
42 : bool HasTag(const std::string& tag) const;
43 : void EmplaceHandle(const std::string& tag, MemHandle handle);
44 : MemHandle RemoveTag(const std::string& tag);
45 :
46 : private:
47 : EndpointHandle handle_;
48 : uint64_t version_{0};
49 : std::unordered_map<std::string, MemHandle> tagToHandle_;
50 : };
51 :
52 : /**
53 : * @note 职责:Endpoint管理器,支持不同类型的Endpoint的创建和销毁管理。
54 : */
55 : class EndpointMgr {
56 : public:
57 175 : EndpointMgr() {};
58 : ~EndpointMgr();
59 :
60 : // 获取端点
61 : HcclResult Get(EndpointDesc epDesc, EndpointHandle& handle);
62 :
63 : // 注册内存到端点,若 commMemsVersion 与上次注册时一致则跳过
64 : HcclResult RegisterMemory(
65 : EndpointHandle epHandle, const std::vector<std::string>& memTag, const std::vector<HcclMem>& memVec,
66 : uint64_t commMemsVersion);
67 :
68 : // 查询指定 endpoint 下若干 tag 对应的 MemHandle
69 : HcclResult GetMemHandlesByTags(
70 : EndpointHandle epHandle, const std::vector<std::string>& memTags, std::vector<MemHandle>& memHandleVec);
71 :
72 : // 从所有 endpoint 中删除指定 tag 并调 HcommMemUnreg
73 : HcclResult UnregMemByTag(const std::string& tag);
74 :
75 : private:
76 : bool IsDescExist(EndpointDesc epDesc);
77 :
78 : private:
79 : std::unordered_map<EndpointDesc, EndpointHandle> endpointMap_{};
80 : std::unordered_map<EndpointHandle, TaggedMemMap> endpointTagMemMap_{};
81 : std::mutex mutex_{};
82 : };
83 :
84 : } // namespace hcomm
85 :
86 : #endif // ENDPOINT_MGR_H
|