/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
82 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
83 {
84 // Storage for newly created serialized output
85 nlohmann::json output;
86
87 // Adding backend-specific information
88 serializeImpl(output);
89
90 // Getting memory space type
91 output["Type"] = _type;
92
93 // Getting size
94 output["Size"] = getSize();
95
96 // Returning serialized information
97 return output;
98 }
99
105 __INLINE__ void deserialize(const nlohmann::json &input)
106 {
107 // Setting memory space type
108 _type = hicr::json::getString(input, "Type");
109
110 // Obtaining backend-specific information
111 deserializeImpl(input);
112
113 // Deserializing size
114 std::string key = "Size";
115 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
116 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
117 _size = input[key].get<size_t>();
118 }
119
120 protected:
121
127 MemorySpace(const size_t size)
128 : _size(size){};
129
133 MemorySpace() = default;
134
141 virtual void serializeImpl(nlohmann::json &output) const {}
142
148 virtual void deserializeImpl(const nlohmann::json &input) {}
149
153 std::string _type;
154
155 private:
156
160 size_t _size{};
161};
162
163} // namespace HiCR
Definition memorySpace.hpp:45
MemorySpace(const size_t size)
Definition memorySpace.hpp:127
virtual void serializeImpl(nlohmann::json &output) const
Definition memorySpace.hpp:141
__INLINE__ nlohmann::json serialize() const
Definition memorySpace.hpp:82
__INLINE__ std::string getType() const
Definition memorySpace.hpp:68
__INLINE__ void deserialize(const nlohmann::json &input)
Definition memorySpace.hpp:105
virtual ~MemorySpace()=default
MemorySpace()=default
MemorySpace(const nlohmann::json &input)
Definition memorySpace.hpp:56
std::string _type
Definition memorySpace.hpp:153
virtual void deserializeImpl(const nlohmann::json &input)
Definition memorySpace.hpp:148
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