Centipede 0.0.1
Centipede program
Loading...
Searching...
No Matches
binary.hpp
1#pragma once
2
3#include "centipede/data/entry.hpp"
4#include "centipede/util/error_types.hpp"
5#include "centipede/util/return_types.hpp"
6
7#include <cassert>
8#include <cstddef>
9#include <cstdint>
10#include <expected>
11#include <fstream>
12#include <ranges>
13#include <string>
14#include <utility>
15#include <vector>
16
17namespace centipede::writer
18{
62 class Binary
63 {
64 public:
77 struct Config
78 {
79 static constexpr auto DEFAULT_BUFFER_SIZE = std::size_t{ 10000 };
80
81 std::string out_filename = "output.bin";
82 uint32_t max_bufferpoint_size = DEFAULT_BUFFER_SIZE;
83 };
84
85 using BufferType = std::pair<std::vector<uint32_t>, std::vector<float>>;
86 using BufferPoint = std::pair<uint32_t, float>;
87
91 Binary() = default;
92
100 constexpr explicit Binary(Config config)
101 : config_{ std::move(config) }
102 {
103 }
104
114 [[nodiscard]] auto init() -> EnumError<>;
115
133 template <std::size_t NLocals, std::size_t NGlobals>
134 [[nodiscard]] auto add_entrypoint(const EntryPoint<NLocals, NGlobals>& entry_point) -> EnumError<>;
135
145
151 void close() { output_file_.close(); };
152
159 constexpr auto get_config() const -> const Config& { return config_; }
160
167 constexpr auto get_buffer() const -> const BufferType& { return data_buffer_; }
168
169 private:
170 bool has_entry_ = false;
173 std::ofstream output_file_;
174
175 auto check_buffer_size(std::size_t size_to_add) const -> bool;
176 auto write_to_binary() -> std::size_t;
177 void reset();
178 void resize_data_buffer(std::size_t size);
179 auto fill_entrypoint_to_buffer(BufferPoint buffer_point, bool has_check_value = false) -> bool;
180 };
181
182 template <std::size_t NLocals, std::size_t NGlobals>
184 {
185 assert(data_buffer_.first.size() == data_buffer_.second.size());
186
187 if (entry_point.sigma <= 0.)
188 {
189 return std::unexpected{ ErrorCode::writer_neg_or_zero_sigma };
190 }
191 if (data_buffer_.first.empty())
192 {
193 return std::unexpected{ ErrorCode::writer_uninitialized };
194 }
195 if (not check_buffer_size(NLocals + NGlobals + 2))
196 {
197 return std::unexpected{ ErrorCode::writer_buffer_overflow };
198 }
199
200 auto old_size = data_buffer_.first.size();
201 auto has_entry = false;
202
203 fill_entrypoint_to_buffer(BufferPoint{ 0, entry_point.measurement });
204
205 // NOTE: Can be changed to concat in C++26
206
207 for (const auto& [idx, local_deriv] : std::views::zip(std::views::iota(0), entry_point.local_derivs))
208 {
209 has_entry |= fill_entrypoint_to_buffer(BufferPoint{ idx + 1, local_deriv }, true);
210 }
211
212 fill_entrypoint_to_buffer(BufferPoint{ 0, entry_point.sigma });
213
214 for (const auto& [idx, global_deriv] : entry_point.global_derivs)
215 {
216 has_entry |= fill_entrypoint_to_buffer(BufferPoint{ idx + 1, global_deriv }, true);
217 }
218
219 has_entry_ |= has_entry;
220 if (not has_entry)
221 {
222 resize_data_buffer(old_size);
223 return std::unexpected{ ErrorCode::writer_entrypoint_rejected };
224 }
225 return {};
226 }
227} // namespace centipede::writer
228
229namespace centipede
230{
231 using BinaryWriter = writer::Binary;
232}
Class for configuring the binary writer class (Binary).
Definition binary.hpp:78
Binary()=default
Default constructor.
Class for writing binary files.
Definition binary.hpp:63
constexpr Binary(Config config)
Constructor takes an argument for the configuration.
Definition binary.hpp:100
auto add_entrypoint(const EntryPoint< NLocals, NGlobals > &entry_point) -> EnumError<>
Add an entrypoint to the internal data buffer.
Definition binary.hpp:183
auto init() -> EnumError<>
Initialization.
Definition binary.cpp:50
auto write_current_entry() -> EnumError< std::size_t >
Streaming an entry data to the output file.
Definition binary.cpp:63
std::pair< std::vector< uint32_t >, std::vector< float > > BufferType
Type of the data_buffer_.
Definition binary.hpp:85
constexpr auto get_buffer() const -> const BufferType &
Getter of the buffer.
Definition binary.hpp:167
void close()
Manually close the output file handler.
Definition binary.hpp:151
BufferType data_buffer_
Data buffer to store entry_point.
Definition binary.hpp:172
Binary()=default
Default constructor.
Config config_
Member variable for the configuration.
Definition binary.hpp:171
std::pair< uint32_t, float > BufferPoint
Type of the buffer point stored in the data_buffer_.
Definition binary.hpp:86
std::ofstream output_file_
Output file handler.
Definition binary.hpp:173
constexpr auto get_config() const -> const Config &
Getter of the configuration.
Definition binary.hpp:159
Types for errors.
Definition handler.cpp:5
@ writer_buffer_overflow
Buffer size is too small for a new entry occurs. See writer::Binary.
@ writer_uninitialized
Write is not initialized.
@ writer_neg_or_zero_sigma
Zero or negative sigma occurs. See writer::Binary.
std::expected< T, ErrorCode > EnumError
Template alias for expected return values.
Structure of a entrypoint.
Definition entry.hpp:20
Class for configuring the binary writer class (Binary).
Definition binary.hpp:78
std::string out_filename
Output binary filename.
Definition binary.hpp:81
uint32_t max_bufferpoint_size
maximum bufferpoint for an entry.
Definition binary.hpp:82