docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
arg_capture_io.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <filesystem>
5#include <fstream>
6#include <iostream>
7#include <memory>
8#include <nlohmann/json.hpp>
9#include <sys/types.h>
10#include <unordered_map>
11#include <vector>
12
13#include "base64.h"
14
15namespace arg_capture {
16
17struct MyHash {
18 std::size_t operator()(const std::pair<int32_t, bool>& p) const {
19 return std::hash<int32_t>()(p.first) ^ p.second ? 0x40000000 : 0;
20 }
21};
22
23struct ArgCapture {
24 int32_t arg_idx;
25 bool after;
29 const std::vector<size_t> dims{0};
31 std::shared_ptr<const std::filesystem::path> ext_file;
32 std::shared_ptr<const std::vector<uint8_t>> data;
33
34
35 ArgCapture() = default;
36
37 ArgCapture(int32_t idx, bool after, int primitive_type, const std::vector<size_t> dims)
39
40 ArgCapture(const ArgCapture& other)
41 : arg_idx(other.arg_idx), after(other.after), dims(other.dims), primitive_type(other.primitive_type),
42 ext_file(other.ext_file), data(other.data) {}
43
44 ArgCapture(const ArgCapture&& other) noexcept
45 : arg_idx(other.arg_idx), after(other.after), dims(std::move(other.dims)), primitive_type(other.primitive_type),
46 ext_file(std::move(other.ext_file)), data(std::move(other.data)) {}
47
48 void serialize_into(nlohmann::json& j) const;
49
50 static void parse_from(
51 const nlohmann::json& entry,
52 std::unordered_map<std::pair<int32_t, bool>, ArgCapture, MyHash>& map,
53 std::filesystem::path base_path = std::filesystem::path()
54 );
55};
56
57
59protected:
60 std::string name_;
61 std::unordered_map<std::string, uint32_t> invokes_ = std::unordered_map<std::string, uint32_t>{};
62 std::unordered_map<std::string, std::unordered_map<std::pair<int32_t, bool>, ArgCapture, MyHash>> current_captures_;
63
64public:
65 explicit ArgCaptureIO(const char* name) : name_(name) {}
66 const std::string& get_name() const;
67 uint32_t get_current_invocation(std::string element_id) const;
68
69 void invocation(std::string element_id);
70
71 void clear();
72
74 int arg_idx,
75 bool after,
76 int primitive_type,
77 const std::vector<size_t>& dims,
78 const void* data,
79 std::string node_id = std::string{}
80 );
82 int arg_idx,
83 bool after,
84 int primitive_type,
85 const std::vector<size_t>& dims,
86 std::filesystem::path& file,
87 const void* data,
88 std::string node_id = std::string{}
89 );
90
91 bool capture_inline(ArgCapture& capture, const void* data, std::string node_id = std::string{});
92 bool write_capture_to_file(ArgCapture& capture, std::filesystem::path file, const void* data);
93
94 void write_index(std::filesystem::path base_file);
95
96 const std::unordered_map<std::string, std::unordered_map<std::pair<int32_t, bool>, ArgCapture, MyHash>>& get_captures()
97 const;
98
99 template<typename T = ArgCaptureIO>
100 static std::shared_ptr<T> from_index(const std::filesystem::path& file);
101
102 static bool write_data_to_raw_file(const std::filesystem::path& file, const void* src_data, size_t size);
103
104 static void read_data_from_raw_file(const std::filesystem::path& file, void* write_ptr, size_t size);
105};
106
107
108static const uint32_t INDEX_FORMAT_VERSION = 0x00000001;
109
110template<typename T>
111std::shared_ptr<T> ArgCaptureIO::from_index(const std::filesystem::path& file) {
112 if (!std::filesystem::exists(file)) {
113 throw std::runtime_error("Index file does not exist: " + file.string());
114 }
115
116 std::ifstream ifs(file);
117 if (!ifs.is_open()) {
118 throw std::runtime_error("Failed to open index file for reading: " + file.string());
119 }
120
121 nlohmann::json j;
122 ifs >> j;
123
124 if (j["format"].get<uint32_t>() != INDEX_FORMAT_VERSION) {
125 throw std::runtime_error("Unsupported index format version");
126 }
127
128 auto name = j["target"].get<std::string>();
129 auto invokes = j["invocation"].get<uint32_t>();
130 auto element_id = j["element_id"].get<std::string>();
131
132 auto captureIO = std::make_shared<T>(name.c_str());
133 captureIO->invokes_[element_id] = invokes;
134
135 auto& capture_map = captureIO->current_captures_[element_id];
136 for (const auto& entry : j["captures"]) {
137 ArgCapture::parse_from(entry, capture_map, file.parent_path());
138 }
139
140 return captureIO;
141}
142
143} // namespace arg_capture
ArgCaptureIO(const char *name)
Definition arg_capture_io.h:65
bool create_and_capture_to_file(int arg_idx, bool after, int primitive_type, const std::vector< size_t > &dims, std::filesystem::path &file, const void *data, std::string node_id=std::string{})
std::unordered_map< std::string, uint32_t > invokes_
Definition arg_capture_io.h:61
static std::shared_ptr< T > from_index(const std::filesystem::path &file)
Definition arg_capture_io.h:111
uint32_t get_current_invocation(std::string element_id) const
bool write_capture_to_file(ArgCapture &capture, std::filesystem::path file, const void *data)
void invocation(std::string element_id)
std::string name_
Definition arg_capture_io.h:60
static bool write_data_to_raw_file(const std::filesystem::path &file, const void *src_data, size_t size)
const std::string & get_name() const
bool capture_inline(ArgCapture &capture, const void *data, std::string node_id=std::string{})
bool create_and_capture_inline(int arg_idx, bool after, int primitive_type, const std::vector< size_t > &dims, const void *data, std::string node_id=std::string{})
std::unordered_map< std::string, std::unordered_map< std::pair< int32_t, bool >, ArgCapture, MyHash > > current_captures_
Definition arg_capture_io.h:62
static void read_data_from_raw_file(const std::filesystem::path &file, void *write_ptr, size_t size)
const std::unordered_map< std::string, std::unordered_map< std::pair< int32_t, bool >, ArgCapture, MyHash > > & get_captures() const
void write_index(std::filesystem::path base_file)
Definition arg_capture_io.h:15
static const uint32_t INDEX_FORMAT_VERSION
Definition arg_capture_io.h:108
Definition arg_capture_io.h:23
std::shared_ptr< const std::vector< uint8_t > > data
Definition arg_capture_io.h:32
int primitive_type
Definition arg_capture_io.h:30
void serialize_into(nlohmann::json &j) const
const std::vector< size_t > dims
innermost dimension is always the element size
Definition arg_capture_io.h:29
std::shared_ptr< const std::filesystem::path > ext_file
Definition arg_capture_io.h:31
ArgCapture(const ArgCapture &&other) noexcept
Definition arg_capture_io.h:44
ArgCapture(int32_t idx, bool after, int primitive_type, const std::vector< size_t > dims)
Definition arg_capture_io.h:37
bool after
Definition arg_capture_io.h:25
int32_t arg_idx
Definition arg_capture_io.h:24
static void parse_from(const nlohmann::json &entry, std::unordered_map< std::pair< int32_t, bool >, ArgCapture, MyHash > &map, std::filesystem::path base_path=std::filesystem::path())
ArgCapture(const ArgCapture &other)
Definition arg_capture_io.h:40
Definition arg_capture_io.h:17
std::size_t operator()(const std::pair< int32_t, bool > &p) const
Definition arg_capture_io.h:18