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