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 2012 : BufferKey(AddrType addr, SizeType size) : addr_(addr), size_(size) {}
29 :
30 169 : AddrType Addr() const { return addr_; }
31 :
32 169 : SizeType Size() const { return size_; }
33 :
34 136 : bool operator==(const BufferKey& other) const { return addr_ == other.addr_ && size_ == other.size_; }
35 :
36 28 : bool operator!=(const BufferKey& other) const { return addr_ != other.addr_ || size_ != other.size_; }
37 :
38 422 : bool operator<(const BufferKey& other) const
39 : {
40 422 : return addr_ < other.addr_ || (addr_ == other.addr_ && size_ < other.size_);
41 : }
42 :
43 : // 不含等于情况
44 1 : inline bool IsSubset(const BufferKey& other) const
45 : {
46 : // 子集判断:当前 key 的起始地址 >= 其他 key 的起始地址,且结束地址 <= 其他 key 的结束地址
47 1 : return *this != other && addr_ >= other.addr_ && (addr_ + size_) <= (other.addr_ + other.size_);
48 : }
49 :
50 : // 不含等于情况
51 27 : inline bool IsSuperset(const BufferKey& other) const
52 : {
53 : // 超集判断:当前 key 的起始地址 < 其他 key 的起始地址,且结束地址 > 其他 key 的结束地址
54 27 : return *this != other && addr_ <= other.addr_ && (addr_ + size_) >= (other.addr_ + other.size_);
55 : }
56 :
57 9055 : inline bool IsIntersect(const BufferKey& other) const
58 : {
59 : // 检查是否有交集:两个区域重叠。
60 9055 : return (addr_ < other.addr_ + other.size_ && addr_ + size_ > other.addr_)
61 18110 : || (other.addr_ < addr_ + size_ && other.addr_ + other.size_ > addr_);
62 : }
63 :
64 : inline bool IsDisjoint(const BufferKey& other) const
65 : {
66 : // 检查是否没有交集:一方完全在另一方之前或之后。
67 : return (addr_ + size_ <= other.addr_) || (addr_ >= other.addr_ + other.size_);
68 : }
69 :
70 136 : inline std::string ToString() const
71 : {
72 680 : return std::string("addr:") + std::to_string(addr_) + std::string(", size:") + std::to_string(size_);
73 : }
74 : };
75 : } // namespace hcomm
76 :
77 : // 兼容旧 Hccl namespace 引用
78 : namespace Hccl {
79 : template <typename A, typename S>
80 : using BufferKey = hcomm::BufferKey<A, S>;
81 : }
82 :
83 : // 兼容旧 hccl namespace 引用
84 : namespace hccl {
85 : template <typename A, typename S>
86 : using BufferKey = hcomm::BufferKey<A, S>;
87 : }
88 : #endif
|