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_Data_DataBuffer_H
11 : #define HCCL_Data_DataBuffer_H
12 :
13 : #include <cstdint>
14 : #include <cstddef>
15 : #include <string>
16 : #include "string_util.h"
17 : #include "hash_utils.h"
18 : namespace Hccl {
19 :
20 : class DataBuffer {
21 : public:
22 75 : DataBuffer(uintptr_t addr, std::size_t size): addr(addr), size(size) {};
23 :
24 : explicit DataBuffer(std::size_t size) : size(size) {};
25 :
26 466 : virtual ~DataBuffer() = default;
27 :
28 7 : uintptr_t GetAddr() const
29 : {
30 7 : return addr;
31 : }
32 :
33 13 : size_t GetSize() const
34 : {
35 13 : return size;
36 : }
37 :
38 8 : virtual std::string Describe() const
39 : {
40 8 : return StringFormat("Buffer[addr=0x%llx, size=0x%llx]", addr, size);
41 : }
42 :
43 : // "=="运算符
44 : bool operator==(const DataBuffer &that) const
45 : {
46 : return (addr == that.GetAddr()) && (size == that.GetSize());
47 : }
48 :
49 : // "!="运算符
50 : bool operator!=(const DataBuffer &that) const
51 : {
52 : return (addr != that.GetAddr()) || (size != that.GetSize());
53 : }
54 :
55 : protected:
56 : uintptr_t addr{0};
57 : std::size_t size{0};
58 : };
59 : } // namespace Hccl
60 :
61 : namespace std {
62 :
63 : template <> class hash<Hccl::DataBuffer> {
64 : public:
65 : size_t operator()(const Hccl::DataBuffer &dataBuffer) const
66 : {
67 : auto addrHash = hash<uint8_t>{}(dataBuffer.GetAddr());
68 : auto sizeHash = hash<uint8_t>{}(dataBuffer.GetSize());
69 :
70 : return Hccl::HashCombine(
71 : {addrHash, sizeHash});
72 : }
73 : };
74 : } // namespace std
75 : #endif // HCCL_DataBuffer_H
|