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 <string>
16 : #include <unordered_map>
17 : #include <vector>
18 : #include <mutex>
19 : #include "endpoint.h"
20 : #include "../endpoint_pairs/endpoint_pair.h"
21 : #include "hccl_mem_defs.h"
22 :
23 : namespace hcomm {
24 :
25 : /**
26 : * @brief endpoint 粒度的 tag→handle 映射。
27 : * 持有 EndpointHandle,析构时自动解注册全部 tag 对应内存。
28 : */
29 : class TaggedMemMap {
30 : public:
31 16 : explicit TaggedMemMap(EndpointHandle handle) : handle_(handle) {}
32 : ~TaggedMemMap();
33 :
34 : TaggedMemMap(const TaggedMemMap&) = delete;
35 : TaggedMemMap& operator=(const TaggedMemMap&) = delete;
36 : TaggedMemMap(TaggedMemMap&&) = default;
37 : TaggedMemMap& operator=(TaggedMemMap&&) = default;
38 :
39 25 : uint64_t GetVersion() const { return version_; }
40 17 : void SetVersion(uint64_t ver) { version_ = ver; }
41 :
42 : MemHandle FindHandle(const std::string& tag) const;
43 : bool HasTag(const std::string& tag) const;
44 : void EmplaceHandle(const std::string& tag, MemHandle handle);
45 : MemHandle RemoveTag(const std::string& tag);
46 :
47 : private:
48 : EndpointHandle handle_;
49 : uint64_t version_{0};
50 : std::unordered_map<std::string, MemHandle> tagToHandle_;
51 : };
52 :
53 : /**
54 : * @note 职责:Endpoint管理器,支持不同类型的Endpoint的创建和销毁管理。
55 : */
56 : class EndpointMgr {
57 : public:
58 218 : EndpointMgr() {};
59 : ~EndpointMgr();
60 :
61 : // 获取端点
62 : HcclResult Get(EndpointDesc epDesc, EndpointHandle& handle);
63 :
64 : // 获取端点(按 sharedQueueTag 区分):共享 jetty 场景下不同 tag 创建独立 Endpoint,
65 : // 实现不同 tag 隔离底层 jetty 资源。tag 为空时退化为 Get(兼容非共享路径)。
66 : HcclResult GetWithTag(EndpointDesc epDesc, const std::string& sharedQueueTag, EndpointHandle& handle);
67 :
68 : // 注册内存到端点,若 commMemsVersion 与上次注册时一致则跳过
69 : HcclResult RegisterMemory(
70 : EndpointHandle epHandle, const std::vector<std::string>& memTag, const std::vector<HcclMem>& memVec,
71 : uint64_t commMemsVersion);
72 :
73 : // 查询指定 endpoint 下若干 tag 对应的 MemHandle
74 : HcclResult GetMemHandlesByTags(
75 : EndpointHandle epHandle, const std::vector<std::string>& memTags, std::vector<MemHandle>& memHandleVec);
76 :
77 : // 从所有 endpoint 中删除指定 tag 并调 HcommMemUnreg
78 : HcclResult UnregMemByTag(const std::string& tag);
79 :
80 : private:
81 : bool IsDescExist(EndpointDesc epDesc);
82 :
83 : // 共享 jetty 场景按 sharedQueueTag 区分的 Endpoint 映射 key: (EndpointDesc, tag)。
84 : // 字段级 hash/compare,规避 EndpointDesc padding 字段未初始化导致的误判(与 EndpointDescPairHash 同思路)。
85 : struct EndpointDescTagKey {
86 : EndpointDesc desc;
87 : std::string tag;
88 : };
89 : struct EndpointDescTagHash {
90 7 : std::size_t operator()(const EndpointDescTagKey& k) const noexcept
91 : {
92 7 : std::string buf;
93 7 : buf.append(reinterpret_cast<const char*>(&k.desc.protocol), sizeof(k.desc.protocol));
94 7 : buf.append(reinterpret_cast<const char*>(&k.desc.commAddr.type), sizeof(k.desc.commAddr.type));
95 7 : buf.append(reinterpret_cast<const char*>(k.desc.commAddr.raws), sizeof(k.desc.commAddr.raws));
96 7 : buf.append(reinterpret_cast<const char*>(&k.desc.loc.locType), sizeof(k.desc.loc.locType));
97 7 : buf.append(reinterpret_cast<const char*>(k.desc.loc.raws), sizeof(k.desc.loc.raws));
98 7 : buf.append(k.tag);
99 7 : return std::hash<std::string>{}(buf);
100 7 : }
101 : };
102 : struct EndpointDescTagEqual {
103 5 : bool operator()(const EndpointDescTagKey& a, const EndpointDescTagKey& b) const noexcept
104 : {
105 6 : return a.tag == b.tag && a.desc.protocol == b.desc.protocol && a.desc.commAddr.type == b.desc.commAddr.type
106 1 : && std::memcmp(a.desc.commAddr.raws, b.desc.commAddr.raws, sizeof(a.desc.commAddr.raws)) == 0
107 1 : && a.desc.loc.locType == b.desc.loc.locType
108 6 : && std::memcmp(a.desc.loc.raws, b.desc.loc.raws, sizeof(a.desc.loc.raws)) == 0;
109 : }
110 : };
111 :
112 : // 共享 jetty 场景按 sharedQueueTag 区分的 Endpoint 映射。
113 : // 同一 EndpointDesc + 不同 tag → 不同 EndpointHandle → 不同底层 jetty 资源。
114 : // tag 为空时不进入此 map,退化为 endpointMap_ 行为(兼容非共享路径)。
115 : std::unordered_map<EndpointDescTagKey, EndpointHandle, EndpointDescTagHash, EndpointDescTagEqual>
116 : taggedEndpointMap_{};
117 :
118 : std::unordered_map<EndpointDesc, EndpointHandle> endpointMap_{};
119 : std::unordered_map<EndpointHandle, TaggedMemMap> endpointTagMemMap_{};
120 : std::mutex mutex_{};
121 : };
122 :
123 : } // namespace hcomm
124 :
125 : #endif // ENDPOINT_MGR_H
|