/home/runner/work/HiCR/HiCR/include/hicr/core/memorySpace.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/memorySpace.hpp Source File
HiCR
memorySpace.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
23#pragma once
24
25#include <string>
26#include <nlohmann_json/json.hpp>
28
29namespace HiCR
30{
31
44{
45 public:
46
52 [[nodiscard]] virtual std::string getType() const = 0;
53
59 [[nodiscard]] __INLINE__ virtual const size_t getSize() const { return _size; }
60
69 [[nodiscard]] __INLINE__ virtual size_t getUsage() const { return _usage; };
70
76 __INLINE__ void increaseUsage(const size_t delta)
77 {
78 if (_usage + delta > _size)
79 HICR_THROW_LOGIC("Increasing memory space usage beyond its capacity (current_usage + increase > capacity | %lu + %lu > %lu)\n", _usage, delta, _size);
80
81 _usage += delta;
82 }
83
89 __INLINE__ void decreaseUsage(const size_t delta)
90 {
91 if (delta > _usage) HICR_THROW_LOGIC("Decreasing memory space usage below zero (probably a bug in HiCR) (current_usage - decrease < 0 | %lu - %lu < 0)\n", _usage, delta);
92
93 _usage -= delta;
94 }
95
99 virtual ~MemorySpace() = default;
100
106 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
107 {
108 // Storage for newly created serialized output
109 nlohmann::json output;
110
111 // Adding backend-specific information
112 serializeImpl(output);
113
114 // Getting memory space type
115 output["Type"] = getType();
116
117 // Getting size
118 output["Size"] = getSize();
119
120 // Getting current usage
121 output["Usage"] = getUsage();
122
123 // Returning serialized information
124 return output;
125 }
126
132 __INLINE__ void deserialize(const nlohmann::json &input)
133 {
134 // Obtaining backend-specific information
135 deserializeImpl(input);
136
137 // Deserializing size
138 std::string key = "Size";
139 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
140 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
141 _size = input[key].get<size_t>();
142
143 // Deserializing usage
144 key = "Usage";
145 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
146 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
147 _usage = input[key].get<size_t>();
148 }
149
150 protected:
151
157 MemorySpace(const size_t size)
158 : _size(size){};
159
163 MemorySpace() = default;
164
171 virtual void serializeImpl(nlohmann::json &output) const = 0;
172
178 virtual void deserializeImpl(const nlohmann::json &input) = 0;
179
180 private:
181
185 size_t _size{};
186
190 size_t _usage = 0;
191};
192
193} // namespace HiCR
Definition memorySpace.hpp:44
MemorySpace(const size_t size)
Definition memorySpace.hpp:157
__INLINE__ nlohmann::json serialize() const
Definition memorySpace.hpp:106
__INLINE__ void deserialize(const nlohmann::json &input)
Definition memorySpace.hpp:132
virtual ~MemorySpace()=default
MemorySpace()=default
__INLINE__ void increaseUsage(const size_t delta)
Definition memorySpace.hpp:76
virtual void serializeImpl(nlohmann::json &output) const =0
virtual void deserializeImpl(const nlohmann::json &input)=0
virtual __INLINE__ size_t getUsage() const
Definition memorySpace.hpp:69
__INLINE__ void decreaseUsage(const size_t delta)
Definition memorySpace.hpp:89
virtual std::string getType() const =0
virtual __INLINE__ const size_t getSize() const
Definition memorySpace.hpp:59
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67