/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>
27#include <nlohmann_json/parser.hpp>
29
30namespace HiCR
31{
32
45{
46 public:
47
56 MemorySpace(const nlohmann::json &input) { deserialize(input); }
57
61 virtual ~MemorySpace() = default;
62
68 [[nodiscard]] __INLINE__ std::string getType() const { return _type; };
69
75 [[nodiscard]] __INLINE__ virtual const size_t getSize() const { return _size; }
76
85 [[nodiscard]] __INLINE__ virtual size_t getUsage() const { return _usage; };
86
92 __INLINE__ void increaseUsage(const size_t delta)
93 {
94 if (_usage + delta > _size)
95 HICR_THROW_LOGIC("Increasing memory space usage beyond its capacity (current_usage + increase > capacity | %lu + %lu > %lu)\n", _usage, delta, _size);
96
97 _usage += delta;
98 }
99
105 __INLINE__ void decreaseUsage(const size_t delta)
106 {
107 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);
108
109 _usage -= delta;
110 }
111
117 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
118 {
119 // Storage for newly created serialized output
120 nlohmann::json output;
121
122 // Adding backend-specific information
123 serializeImpl(output);
124
125 // Getting memory space type
126 output["Type"] = _type;
127
128 // Getting size
129 output["Size"] = getSize();
130
131 // Getting current usage
132 output["Usage"] = getUsage();
133
134 // Returning serialized information
135 return output;
136 }
137
143 __INLINE__ void deserialize(const nlohmann::json &input)
144 {
145 // Setting memory space type
146 _type = hicr::json::getString(input, "Type");
147
148 // Obtaining backend-specific information
149 deserializeImpl(input);
150
151 // Deserializing size
152 std::string key = "Size";
153 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
154 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
155 _size = input[key].get<size_t>();
156
157 // Deserializing usage
158 key = "Usage";
159 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
160 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
161 _usage = input[key].get<size_t>();
162 }
163
164 protected:
165
171 MemorySpace(const size_t size)
172 : _size(size){};
173
177 MemorySpace() = default;
178
185 virtual void serializeImpl(nlohmann::json &output) const {}
186
192 virtual void deserializeImpl(const nlohmann::json &input) {}
193
197 std::string _type;
198
199 private:
200
204 size_t _size{};
205
209 size_t _usage = 0;
210};
211
212} // namespace HiCR
Definition memorySpace.hpp:45
MemorySpace(const size_t size)
Definition memorySpace.hpp:171
virtual void serializeImpl(nlohmann::json &output) const
Definition memorySpace.hpp:185
__INLINE__ nlohmann::json serialize() const
Definition memorySpace.hpp:117
__INLINE__ std::string getType() const
Definition memorySpace.hpp:68
__INLINE__ void deserialize(const nlohmann::json &input)
Definition memorySpace.hpp:143
virtual ~MemorySpace()=default
MemorySpace()=default
MemorySpace(const nlohmann::json &input)
Definition memorySpace.hpp:56
__INLINE__ void increaseUsage(const size_t delta)
Definition memorySpace.hpp:92
virtual __INLINE__ size_t getUsage() const
Definition memorySpace.hpp:85
__INLINE__ void decreaseUsage(const size_t delta)
Definition memorySpace.hpp:105
std::string _type
Definition memorySpace.hpp:197
virtual void deserializeImpl(const nlohmann::json &input)
Definition memorySpace.hpp:192
virtual __INLINE__ const size_t getSize() const
Definition memorySpace.hpp:75
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67