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 {
27 : return std::hash<void*>()(memInfo.mem.addr);
28 : }
29 : };
30 : }
31 :
32 : namespace hccl {
33 : struct CommMemInfoEqual {
34 : bool operator()(const CommMemInfo& lhs, const CommMemInfo& rhs) const {
35 : return lhs.mem.addr == rhs.mem.addr;
36 : }
37 : };
38 :
39 : CommMemType ConvertHcclToCommMemType(HcclMemType hcclType);
40 : HcclMemType ConvertCommToHcclMemType(CommMemType commType);
41 :
42 : /**
43 : * @note 职责:集合通信域内MyRank的通信内存管理,包括HCCL Buffer和其他待注册到EndPoint内存
44 : */
45 : class CommMems {
46 : public:
47 : using Handle = std::shared_ptr<CommMemInfo>;
48 : using MemKey = hccl::BufferKey<uintptr_t, uint64_t>;
49 : using Table = hccl::RmaBufferMgr<MemKey, Handle>;
50 :
51 : explicit CommMems(uint64_t bufferSize);
52 111 : ~CommMems() = default;
53 :
54 : HcclResult Add(void *addr, uint64_t len);
55 :
56 : HcclResult GetHcclBuffer(void *&addr, uint64_t &len);
57 :
58 : HcclResult HcclBufferMemset(void *&addr, uint64_t &len, bool clearFlag) const;
59 :
60 : HcclResult Init(HcclMem cclBuffer);
61 :
62 : HcclResult GetMemoryHandles(std::vector<HcclMem> &mem);
63 :
64 : // 用户注册/反注册内存
65 : HcclResult CommRegMem(const std::string& tag, const CommMem& mem, void** rawHandle);
66 : HcclResult CommUnregMem(const std::string& tag, const void* rawHandle);
67 : HcclResult GetTagMemoryHandles(void** memHandles, uint32_t memHandleNum, std::vector<HcclMem> &mem,
68 : std::vector<std::string> &memTag);
69 :
70 : private:
71 : uint64_t bufferSize_{};
72 : CommMemInfo cclMemInfo_{};
73 :
74 11 : static inline MemKey MakeKey(void* addr, uint64_t size) {
75 11 : return MemKey(reinterpret_cast<uintptr_t>(addr), static_cast<uint64_t>(size));
76 : }
77 : struct TagRegistry {
78 : Table table; // 区间树 + ref 语义
79 : };
80 : // 用户绑定内存
81 : std::mutex memMutex_;
82 : // 每个 tag 一份 registry
83 : std::unordered_map<std::string, TagRegistry> tagRegs_;
84 : // 每个tag 1个 CommMemInfo
85 : std::unordered_map<std::string, std::shared_ptr<CommMemInfo>> opBindings_;
86 : };
87 : } // namespace hccl
88 :
89 : #endif // COMM_MEMS_H
|