/home/runner/work/HiCR/HiCR/include/hicr/backends/hwloc/cache.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/hwloc/cache.hpp Source File
HiCR
cache.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 <utility>
26#include <vector>
27#include <string>
28#include <hicr/core/definitions.hpp>
31
32namespace HiCR::backend::hwloc
33{
34
38class Cache
39{
40 public:
41
46 {
48 L1 = 1,
49
51 L2 = 2,
52
54 L3 = 3,
55
57 L4 = 4,
58
60 L5 = 5
61 };
62
72 Cache(const cacheLevel_t level, std::string type, const size_t size, const size_t lineSize, const bool shared)
73 : _level(level),
74 _type(std::move(type)),
75 _cacheSize(size),
76 _lineSize(lineSize),
77 _shared(shared)
78 {}
79
88 Cache(const nlohmann::json &input) { deserialize(input); }
89
95 [[nodiscard]] __INLINE__ size_t getSize() const { return _cacheSize; }
96
102 [[nodiscard]] __INLINE__ size_t getLineSize() const { return _lineSize; }
103
109 [[nodiscard]] __INLINE__ cacheLevel_t getLevel() const { return _level; }
110
116 [[nodiscard]] __INLINE__ bool getShared() const { return _shared; }
117
123 [[nodiscard]] __INLINE__ const std::string &getType() const { return _type; }
124
130 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
131 {
132 // Storage for newly created serialized output
133 nlohmann::json output;
134
135 // Getting Cache information
136 output["Size (Bytes)"] = getSize();
137 output["Line Size (Bytes)"] = getLineSize();
138 output["Level"] = getLevel();
139 output["Type"] = getType();
140 output["Shared"] = getShared();
141
142 // Returning serialized information
143 return output;
144 }
145
151 __INLINE__ void deserialize(const nlohmann::json &input)
152 {
153 std::string key = "Size (Bytes)";
154 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
155 if (input[key].is_number_unsigned() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
156 _cacheSize = input[key].get<size_t>();
157
158 key = "Line Size (Bytes)";
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 _lineSize = input[key].get<size_t>();
162
163 key = "Level";
164 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
165 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number. Value: '%s'", key.c_str(), input[key].dump().c_str());
166 _level = input[key].get<cacheLevel_t>();
167
168 key = "Type";
169 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
170 if (input[key].is_string() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
171 _type = input[key].get<std::string>();
172
173 key = "Shared";
174 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
175 if (input[key].is_boolean() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
176 _shared = input[key].get<bool>();
177 }
178
179 private:
180
184 cacheLevel_t _level{};
185
189 std::string _type;
190
194 size_t _cacheSize{};
195
199 size_t _lineSize{};
200
204 bool _shared{};
205
206}; // class Cache
207
208} // namespace HiCR::backend::hwloc
Definition cache.hpp:39
__INLINE__ size_t getLineSize() const
Definition cache.hpp:102
Cache(const cacheLevel_t level, std::string type, const size_t size, const size_t lineSize, const bool shared)
Definition cache.hpp:72
__INLINE__ bool getShared() const
Definition cache.hpp:116
Cache(const nlohmann::json &input)
Definition cache.hpp:88
__INLINE__ const std::string & getType() const
Definition cache.hpp:123
cacheLevel_t
Definition cache.hpp:46
@ L1
Cache Level L1.
Definition cache.hpp:48
@ L3
Cache Level L3.
Definition cache.hpp:54
@ L2
Cache Level L2.
Definition cache.hpp:51
@ L4
Cache Level L4.
Definition cache.hpp:57
@ L5
Cache Level L5.
Definition cache.hpp:60
__INLINE__ cacheLevel_t getLevel() const
Definition cache.hpp:109
__INLINE__ size_t getSize() const
Definition cache.hpp:95
__INLINE__ void deserialize(const nlohmann::json &input)
Definition cache.hpp:151
__INLINE__ nlohmann::json serialize() const
Definition cache.hpp:130
Provides a base definition for a HiCR ComputeResource class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67