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 BUFFER_KEY_H
12 : #define BUFFER_KEY_H
13 : #include <string>
14 : #include <stdint.h>
15 :
16 : namespace hcomm {
17 : template<typename A, typename S>
18 : class BufferKey {
19 : public:
20 : using AddrType = A;
21 : using SizeType = S;
22 :
23 : private:
24 : AddrType addr_;
25 : SizeType size_;
26 :
27 : public:
28 1829 : BufferKey(AddrType addr, SizeType size) : addr_(addr), size_(size) {}
29 :
30 241 : AddrType Addr() const
31 : {
32 241 : return addr_;
33 : }
34 :
35 241 : SizeType Size() const
36 : {
37 241 : return size_;
38 : }
39 :
40 93 : bool operator==(const BufferKey& other) const
41 : {
42 93 : return addr_ == other.addr_ && size_ == other.size_;
43 : }
44 :
45 10 : bool operator!=(const BufferKey& other) const
46 : {
47 10 : return addr_ != other.addr_ || size_ != other.size_;
48 : }
49 :
50 253 : bool operator<(const BufferKey& other) const
51 : {
52 253 : return addr_ < other.addr_ || (addr_ == other.addr_ && size_ < other.size_);
53 : }
54 :
55 : // 不含等于情况
56 0 : inline bool IsSubset(const BufferKey& other) const
57 : {
58 : // 子集判断:当前 key 的起始地址 >= 其他 key 的起始地址,且结束地址 <= 其他 key 的结束地址
59 0 : return *this != other && addr_ >= other.addr_ && (addr_ + size_) <= (other.addr_ + other.size_);
60 : }
61 :
62 : // 不含等于情况
63 10 : inline bool IsSuperset(const BufferKey& other) const
64 : {
65 : // 超集判断:当前 key 的起始地址 < 其他 key 的起始地址,且结束地址 > 其他 key 的结束地址
66 10 : return *this != other && addr_ <= other.addr_ && (addr_ + size_) >= (other.addr_ + other.size_);
67 : }
68 :
69 9824 : inline bool IsIntersect(const BufferKey& other) const
70 : {
71 : // 检查是否有交集:两个区域重叠。
72 18826 : return (addr_ < other.addr_ + other.size_ && addr_ + size_ > other.addr_) ||
73 18826 : (other.addr_ < addr_ + size_ && other.addr_ + other.size_ > addr_);
74 : }
75 :
76 : inline bool IsDisjoint(const BufferKey& other) const
77 : {
78 : // 检查是否没有交集:一方完全在另一方之前或之后。
79 : return (addr_ + size_ <= other.addr_) || (addr_ >= other.addr_ + other.size_);
80 : }
81 :
82 82 : inline std::string ToString() const
83 : {
84 410 : return std::string("addr:") + std::to_string(addr_) + std::string(", size:") + std::to_string(size_);
85 : }
86 : };
87 : }
88 :
89 : //兼容旧 Hccl namespace 引用
90 : namespace Hccl {
91 : template<typename A, typename S>
92 : using BufferKey = hcomm::BufferKey<A, S>;
93 : }
94 :
95 : //兼容旧 hccl namespace 引用
96 : namespace hccl {
97 : template<typename A, typename S>
98 : using BufferKey = hcomm::BufferKey<A, S>;
99 : }
100 : #endif
|