The Sparta Modeling Framework
Loading...
Searching...
No Matches
BinaryIArchive.hpp
1// <BinaryIArchive> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/statistics/dispatch/archives/ArchiveSource.hpp"
7
8#include <fstream>
9
10namespace sparta {
11namespace statistics {
12
18{
19public:
20 //One-time initialization. Open input files.
21 void initialize() override {
22 const std::string & path = getPath();
23 const std::string & subpath = getSubpath();
24 openBinaryArchiveFile_(path, subpath);
25 }
26
27 const std::vector<double> & readFromSource() override {
28 //Pick an arbitrary buffer size to read. This can be
29 //tuned for better overall performance, but 10000 values
30 //at a time is reasonable.
31 static const size_t BUF_LEN = 10000;
32 static const size_t BUF_NUM_BYTES = BUF_LEN * sizeof(double);
33
34 values_.resize(BUF_LEN);
35 char * buffer = reinterpret_cast<char*>(&values_[0]);
36 binary_fin_.read(buffer, BUF_NUM_BYTES);
37 auto actual_bytes_read = binary_fin_.gcount();
38
39 //We store statistics values as doubles on disk, so no
40 //matter what BUF_LEN value is chosen, the number of bytes
41 //read out must be a multiple of 8, or something went wrong.
42 sparta_assert(actual_bytes_read % sizeof(double) == 0);
43
44 values_.resize(actual_bytes_read / sizeof(double));
45 return values_;
46 }
47
48private:
49 void openBinaryArchiveFile_(const std::string & path,
50 const std::string & subpath)
51 {
52 const std::string filename = path + "/" + subpath + "/values.bin";
53 binary_fin_.open(filename, std::ios::binary);
54 if (!binary_fin_) {
55 throw SpartaException(
56 "Unable to open archive file for read: ") << filename;
57 }
58 }
59
60 std::ifstream binary_fin_;
61 std::vector<double> values_;
62};
63
64} // namespace statistics
65} // namespace sparta
66
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Generic statistic source base class for report archives.
Use a binary archive file as a source of statistics values.
Macros for handling exponential backoff.