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 ZERO_COPY_ADDRESS_MGR_H
12 : #define ZERO_COPY_ADDRESS_MGR_H
13 :
14 : #include <set>
15 : #include <mutex>
16 : #include <hccl/hccl_types.h>
17 : #include "aicpu_operator_pub.h"
18 :
19 : namespace hccl {
20 : /*
21 : * 该类负责管理HcclCommSetMemoryRange/HcclCommUnsetMemoryRange等API注册的地址进行管理
22 : */
23 : using ZeroCopyReserveAddrMap = std::unordered_map<u32, std::unordered_map<void*, LocalIpc2RemoteAddr>>;
24 : class ZeroCopyAddressMgr {
25 : public:
26 48 : ZeroCopyAddressMgr() = default;
27 47 : ~ZeroCopyAddressMgr() = default;
28 :
29 : // Set/Unset地址
30 : HcclResult SetMemoryRange(u32 devicePhyId, void* baseAddr, u64 length);
31 : HcclResult UnsetMemoryRange(u32 devicePhyId, void* baseAddr);
32 : // 判断地址是否仍被Set
33 : bool IsAddressSet(u32 devicePhyId, void* baseAddr);
34 :
35 : // 添加与对端内存的映射关系,只保留基地址映射
36 : HcclResult AddLocalIpc2RemoteAddr(u32 devicePhyId, void* localIpcBase, void* remoteAddrBase, u64 length);
37 : HcclResult DelLocalIpc2RemoteAddr(u32 devicePhyId, void* remoteAddrBase);
38 : HcclResult GetLocalIpc2RemoteAddr(u32 devicePhyId, void* remoteAddr, LocalIpc2RemoteAddr& addr);
39 :
40 : // 添加Activate的内存段
41 : HcclResult ActivateCommMemoryAddr(void* startPtr, u64 length);
42 : HcclResult DeactivateCommMemoryAddr(void* startPtr);
43 :
44 : // 管理从远端import的内存
45 : HcclResult AddRemoteImportAddr(void* devPtr, void* handle);
46 : HcclResult GetRemoteImportAddr(void* devPtr, void*& handle);
47 : HcclResult DelRemoteImportAddr(void* devPtr);
48 :
49 : // 只有[startPtr, startPtr + length)全在前面Activate接口注册的内存段中才为有效
50 : bool IsActivateCommMemoryAddr(void* startPtr, u64 length);
51 :
52 : // 判断是否与activate有交叠,只要与active的内存范围有交集就返回真
53 : bool IsOverlapWithActivateAddr(void* startPtr, u64 length);
54 :
55 : // 判断内存区间是否在Set区间内
56 : bool IsInSetAddressRange(u32 devicePhyId, void* startPtr, u64 length);
57 :
58 0 : void GetRingBufferAddr(u64& bufferPtr, u64& headPtr, u64& tailPtr)
59 : {
60 0 : bufferPtr = reinterpret_cast<u64>(devRingBufBase_);
61 0 : headPtr = reinterpret_cast<u64>(devRingHead_);
62 0 : tailPtr = reinterpret_cast<u64>(devRingTail_);
63 0 : }
64 :
65 : // 处理RingBuffer
66 : HcclResult ProcessRingBuffer(ZeroCopyRingBufferItem* ringBuffer, u32* head, u32* tail);
67 : // 处理引用计数
68 : u32 GetCommRefCnt();
69 : HcclResult IncreCommRefCnt();
70 : HcclResult DecreCommRefCnt();
71 :
72 : private:
73 : ZeroCopyAddressMgr(const ZeroCopyAddressMgr&) = delete;
74 : ZeroCopyAddressMgr(ZeroCopyAddressMgr&&) = delete;
75 : ZeroCopyAddressMgr& operator=(const ZeroCopyAddressMgr&) = delete;
76 : ZeroCopyAddressMgr& operator=(ZeroCopyAddressMgr&&) = delete;
77 :
78 : __attribute__((weak)) HcclResult InitRingBuffer();
79 : __attribute__((weak)) HcclResult PushOne(ZeroCopyRingBufferItem& item);
80 : HcclResult ProcessOneAddrMap(const ZeroCopyRingBufferItem& item);
81 :
82 : // 表示一段内存[start, end)是个左闭右开的区间,即最后一个字节不可访问
83 : struct AddressRange {
84 0 : AddressRange(void* startPtr, u64 length) : start(reinterpret_cast<u64>(startPtr)), end(start + length) {}
85 0 : AddressRange(u64 startPtr, u64 length) : start(startPtr), end(start + length) {}
86 : ~AddressRange() = default;
87 :
88 : // 当前场景下不允许内存范围有重叠,一旦重叠就认为是相同
89 : // 比如 a=[0, 100) 与 b=[10, 200)就是相等的,
90 : // 使用下面的判断a<b与b<a均会返回false,那么它们就是相等的
91 0 : bool operator<(const AddressRange& other) const
92 : {
93 : // 因为最后一字节不可访问,所以这里可以等于
94 0 : return this->end <= other.start;
95 : }
96 :
97 : u64 start = 0;
98 : u64 end = 0;
99 : };
100 :
101 : u32 commRefCnt_{0};
102 : std::mutex lock_;
103 : std::mutex processRingBufferLock_;
104 : // 每个device保存自己的预留内存,每个内存的key都是对端基地址,value是映射关系
105 : ZeroCopyReserveAddrMap reserveAddrMappings_;
106 : std::unordered_map<u32, std::set<AddressRange>> reserveRanges_;
107 : DeviceMem ringBuffer_;
108 : DeviceMem ringBufferCtl_;
109 : ZeroCopyRingBufferItem* devRingBufBase_ = nullptr;
110 : u32* devRingHead_ = nullptr;
111 : u32* devRingTail_ = nullptr;
112 :
113 : bool needPushOne{true};
114 : std::set<AddressRange> validAddressRanges_{};
115 : std::unordered_map<void*, void*> importAddrs_{};
116 : };
117 : } // namespace hccl
118 :
119 : #endif
|