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_MEM_MANAGER_H
12 : #define COMM_MEM_MANAGER_H
13 : #include <string>
14 : #include <unordered_map>
15 : #include <memory>
16 : #include <mutex>
17 : #include "hccl/hccl_res.h"
18 : #include "hccl_independent_common.h"
19 : #include "hccl_mem_defs.h"
20 : #include "mem_device_pub.h"
21 : #include "ccl_buffer_manager.h"
22 : #include "rma_buffer_mgr.h"
23 :
24 : namespace hccl {
25 :
26 : struct HcclMemoryHandle {
27 : void* addr{nullptr};
28 : uint64_t size{0};
29 : HcclMemType memType{HCCL_MEM_TYPE_DEVICE};
30 : HcclRegMemAttr attr{};
31 : };
32 :
33 : class CommMemMgr {
34 : public:
35 : using Handle = std::shared_ptr<HcclMemoryHandle>;
36 : using MemKey = hccl::BufferKey<uintptr_t, uint64_t>;
37 : using Table = hcomm::RmaBufferMgr<MemKey, Handle>;
38 661 : CommMemMgr() = default;
39 659 : ~CommMemMgr() = default;
40 :
41 : // cclbuffer内存
42 : void CommSetHcclBufferManager(CCLBufferManager& bufferManager);
43 : HcclResult GetHcclBuffer(CommBuffer* buffer);
44 :
45 : // 用户注册/反注册内存
46 : HcclResult CommRegMem(const std::string& memTag, const HcclMem& mem, HcclRegMemAttr attr, void** memHandle);
47 : HcclResult CommUnregMem(const std::string& memTag, const void* memHandle);
48 : HcclResult CommGetLocalRegMemByTag(const std::string& tag, std::vector<HcclMem>& memVec);
49 : HcclResult
50 : CommGetLocalRegMemByHandles(const HcclMemHandle* memHandles, uint32_t memHandleNum, std::vector<HcclMem>& memVec);
51 :
52 : private:
53 9 : static inline MemKey MakeKey(void* addr, uint64_t size)
54 : {
55 9 : return MemKey(reinterpret_cast<uintptr_t>(addr), static_cast<uint64_t>(size));
56 : }
57 : struct TagRegistry {
58 : Table table; // 区间树 + ref 语义
59 : };
60 :
61 : // cclbuffer内存
62 : std::mutex bufferMutex_;
63 : CCLBufferManager* bufferManager_{nullptr};
64 :
65 : // 用户绑定内存
66 : std::mutex memMutex_;
67 : // 每个 tag 一份 registry
68 : std::unordered_map<std::string, TagRegistry> tagRegs_;
69 : // 每个tag n个 HcclMemoryHandle
70 : std::unordered_map<std::string, std::vector<std::shared_ptr<HcclMemoryHandle>>> opBindings_;
71 : };
72 : } // namespace hccl
73 :
74 : #endif
|