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 HCCL_BUFFER_H
11 : #define HCCL_BUFFER_H
12 :
13 : #include <cstdint>
14 : #include <cstddef>
15 : #include <string>
16 : #include "hccl_mem_defs.h"
17 : #include "enum_factory.h"
18 : namespace Hccl {
19 :
20 : class Buffer {
21 : public:
22 : Buffer(uintptr_t addr, std::size_t size);
23 :
24 : explicit Buffer(std::size_t size);
25 :
26 : explicit Buffer(uintptr_t addr, std::size_t size, HcclMemType memType);
27 :
28 : explicit Buffer(uintptr_t addr, std::size_t size, HcclMemType memType, const char *memInfo);
29 :
30 : explicit Buffer(uintptr_t addr, std::size_t size, const char *memInfo);
31 :
32 3995 : virtual ~Buffer() = default;
33 :
34 : uintptr_t GetAddr() const;
35 :
36 : size_t GetSize() const;
37 :
38 : HcclMemType GetMemType() const;
39 :
40 : const std::string GetMemInfo() const;
41 :
42 : virtual std::string Describe() const;
43 :
44 : bool Contains(Buffer *buf) const;
45 :
46 : bool Contains(uintptr_t bufAddr, size_t bufSize) const;
47 :
48 : Buffer Range(std::size_t offset, std::size_t givenSize) const;
49 :
50 : // "bool"运算符(可执行if(object){...}的操作判断该buffer是否为空)
51 0 : operator bool() const
52 : {
53 0 : return addr_ != 0;
54 : }
55 :
56 : // "=="运算符
57 : bool operator==(const Buffer &that) const
58 : {
59 : return (addr_ == that.GetAddr()) && (size_ == that.GetSize());
60 : }
61 :
62 : // "!="运算符
63 : bool operator!=(const Buffer &that) const
64 : {
65 : return (addr_ != that.GetAddr()) || (size_ != that.GetSize());
66 : }
67 :
68 : protected:
69 : uintptr_t addr_{0};
70 : std::size_t size_{0};
71 : HcclMemType memType_{HcclMemType::HCCL_MEM_TYPE_DEVICE};
72 : char memInfo_[256]{};
73 : };
74 :
75 : } // namespace Hccl
76 : #endif // HCCL_BUFFER_H
|