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 HCCLV2_DATA_SLICE_H
12 : #define HCCLV2_DATA_SLICE_H
13 :
14 : #include <sstream>
15 : #include <memory>
16 : #include "buffer_type.h"
17 : #include "types/types.h"
18 : #include "string_util.h"
19 :
20 : namespace Hccl {
21 :
22 : class DataSlice {
23 : public:
24 2 : explicit DataSlice() : type(BufferType::INPUT), offset(0), size(0)
25 : {
26 2 : }
27 :
28 : explicit DataSlice(u64 size) : type(BufferType::INPUT), offset(0), size(size)
29 : {
30 : }
31 :
32 424 : DataSlice(BufferType bufferType, u64 offset, u64 size) : type(bufferType), offset(offset), size(size)
33 : {
34 424 : }
35 :
36 18 : bool operator==(const DataSlice &other) const
37 : {
38 18 : return (type == other.type && offset == other.offset && size == other.size);
39 : }
40 :
41 0 : bool operator!=(const DataSlice &other) const
42 : {
43 0 : return (type != other.type || offset != other.offset || size != other.size);
44 : }
45 :
46 262 : std::string Describe() const
47 : {
48 786 : return StringFormat("DataSlice[%s, offset=%s, size=%s]", type.Describe().c_str(), Dec2hex(offset).c_str(),
49 1048 : Dec2hex(size).c_str());
50 : }
51 :
52 80 : inline BufferType GetType() const
53 : {
54 80 : return type;
55 : }
56 :
57 68 : inline u64 GetOffset() const
58 : {
59 68 : return offset;
60 : }
61 :
62 438 : inline u64 GetSize() const
63 : {
64 438 : return size;
65 : }
66 :
67 0 : void SetBufferType(const BufferType bufferType)
68 : {
69 0 : type = bufferType;
70 0 : }
71 :
72 0 : void SetOffset(u64 off)
73 : {
74 0 : offset = off;
75 0 : }
76 :
77 0 : void SetSize(u64 size)
78 : {
79 0 : this->size = size;
80 0 : }
81 : private:
82 : BufferType type;
83 : u64 offset;
84 : u64 size;
85 : };
86 :
87 : inline bool DataSliceSizeIsEqual(std::unique_ptr<DataSlice> &a, std::unique_ptr<DataSlice> &b)
88 : {
89 : return a->GetSize() == b->GetSize();
90 : }
91 :
92 : inline bool DataSliceSizeIsEqual(std::unique_ptr<DataSlice> &a, std::unique_ptr<DataSlice> &b,
93 : std::unique_ptr<DataSlice> &c)
94 : {
95 : return (a->GetSize() == b->GetSize()) && (b->GetSize() == c->GetSize());
96 : }
97 :
98 : } // namespace Hccl
99 : #endif
|