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 : #ifndef COMM_MEMS_H
11 : #define COMM_MEMS_H
12 :
13 : #include <memory>
14 : #include <vector>
15 : #include <unordered_map>
16 : #include <mutex>
17 : #include "hccl_types.h"
18 : #include "log.h"
19 : #include "hccl_mem_defs.h"
20 : #include "rma_buffer_mgr.h"
21 : #include "hcomm_c_adpt.h"
22 :
23 : namespace std {
24 : template <>
25 : struct hash<CommMemInfo> {
26 : size_t operator()(const CommMemInfo& memInfo) const { return std::hash<void*>()(memInfo.mem.addr); }
27 : };
28 : } // namespace std
29 :
30 : namespace hccl {
31 : struct CommMemInfoEqual {
32 : bool operator()(const CommMemInfo& lhs, const CommMemInfo& rhs) const { return lhs.mem.addr == rhs.mem.addr; }
33 : };
34 :
35 : CommMemType ConvertHcclToCommMemType(HcclMemType hcclType);
36 : HcclMemType ConvertCommToHcclMemType(CommMemType commType);
37 :
38 : /**
39 : * @note 职责:集合通信域内MyRank的通信内存管理,包括HCCL Buffer和其他待注册到EndPoint内存
40 : */
41 : class CommMems {
42 : public:
43 : using Handle = std::shared_ptr<CommMemInfo>;
44 : using MemKey = hccl::BufferKey<uintptr_t, uint64_t>;
45 : using Table = hcomm::RmaBufferMgr<MemKey, Handle>;
46 :
47 : explicit CommMems(uint64_t bufferSize);
48 178 : ~CommMems() = default;
49 :
50 : HcclResult Add(void* addr, uint64_t len);
51 :
52 : HcclResult GetHcclBuffer(void*& addr, uint64_t& len);
53 :
54 : HcclResult HcclBufferMemset(void*& addr, uint64_t& len, bool clearFlag) const;
55 :
56 : HcclResult Init(HcclMem cclBuffer);
57 :
58 : // 用户注册/反注册内存
59 : HcclResult CommRegMem(const std::string& tag, const CommMem& mem, void** rawHandle);
60 : HcclResult CommUnregMem(const std::string& tag, const void* rawHandle);
61 : // 从 CommMemInfo* 数组提取 tag 列表
62 : HcclResult GetTagsFromHandles(void** memHandles, uint32_t memHandleNum, std::vector<std::string>& memTags);
63 : /**
64 : * 获取域内全部待注册内存(cclBuffer + 所有用户绑定内存),用于 endpoint 粒度批量注册。
65 : * 约定:返回的 memVec/memTags 0号位固定为 cclBuffer(tag="HcclBuffer"),
66 : * 后续为 opBindings_ 全量,同下标一一对应。
67 : * version 为当前 CommMems 内存集合变更版本号。
68 : */
69 : HcclResult GetAllMemory(std::vector<HcclMem>& memVec, std::vector<std::string>& memTags, uint64_t& version);
70 :
71 : private:
72 : uint64_t bufferSize_{};
73 : CommMemInfo cclMemInfo_{};
74 : uint64_t memVersion_{0}; // opBindings_ 变更版本号,每次注册/反注册递增
75 :
76 45 : static inline MemKey MakeKey(void* addr, uint64_t size)
77 : {
78 45 : return MemKey(reinterpret_cast<uintptr_t>(addr), static_cast<uint64_t>(size));
79 : }
80 : struct TagRegistry {
81 : Table table; // 区间树 + ref 语义
82 : };
83 : // 用户绑定内存
84 : std::mutex memMutex_;
85 : // 每个 tag 一份 registry
86 : std::unordered_map<std::string, TagRegistry> tagRegs_;
87 : // 每个tag 1个 CommMemInfo
88 : std::unordered_map<std::string, std::shared_ptr<CommMemInfo>> opBindings_;
89 : };
90 : } // namespace hccl
91 :
92 : #endif // COMM_MEMS_H
|