docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
json_serializer.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <nlohmann/json_fwd.hpp>
5#include <string>
6
15#include "sdfg/types/type.h"
16#include "symengine/logic.h"
17#include "symengine/printers/codegen.h"
18
19namespace sdfg {
20namespace serializer {
21
23private:
24 bool recurse_;
25
26 // Sub-type of structured loops
27 void json_to_map_node(
28 const nlohmann::json& j,
31 );
32
33 void json_to_reduce_node(
34 const nlohmann::json& j,
37 );
38
39public:
40 JSONSerializer(bool recurse = true) : recurse_(recurse) {
41 }
42
44
45 virtual nlohmann::json serialize(
47 analysis::AnalysisManager* analysis_manager = nullptr,
49 );
50
51 void serialize_node(nlohmann::json& j, const structured_control_flow::ControlFlowNode& node);
52
53 static void element_metadata_to_json(nlohmann::json& j, const sdfg::Element& element);
54
55 std::unique_ptr<sdfg::StructuredSDFG> deserialize(nlohmann::json& j);
56 void structure_definition_to_json(nlohmann::json& j, const sdfg::types::StructureDefinition& definition);
57 void type_to_json(nlohmann::json& j, const sdfg::types::IType& type);
58 void dataflow_to_json(nlohmann::json& j, const sdfg::data_flow::DataFlowGraph& dataflow);
59
60 void sequence_to_json(nlohmann::json& j, const sdfg::structured_control_flow::Sequence& sequence);
61 void block_to_json(nlohmann::json& j, const sdfg::structured_control_flow::Block& block);
63
64 void if_else_to_json(nlohmann::json& j, const sdfg::structured_control_flow::IfElse& if_else_node);
65 void while_node_to_json(nlohmann::json& j, const sdfg::structured_control_flow::While& while_node);
66 void break_node_to_json(nlohmann::json& j, const sdfg::structured_control_flow::Break& break_node);
67 void continue_node_to_json(nlohmann::json& j, const sdfg::structured_control_flow::Continue& continue_node);
68 void return_node_to_json(nlohmann::json& j, const sdfg::structured_control_flow::Return& return_node);
69
70 // Includes all sub-types of StructuredLoop (for, map, reduce)
72
73 void debug_info_to_json(nlohmann::json& j, const sdfg::DebugInfo& debug_info);
74
75 void schedule_type_to_json(nlohmann::json& j, const sdfg::structured_control_flow::ScheduleType& schedule_type);
76
77 void storage_type_to_json(nlohmann::json& j, const sdfg::types::StorageType& storage_type);
78
81 const nlohmann::json& j,
84 );
85
86 control_flow::Assignments parse_assignments(const nlohmann::json& assignments_arr);
87
89 const nlohmann::json& j,
92 );
93
95 const nlohmann::json& j,
98 );
100 const nlohmann::json& j,
103 );
105 const nlohmann::json& j,
108 );
110 const nlohmann::json& j,
113 );
115 const nlohmann::json& j,
118 );
120 const nlohmann::json& j,
123 );
125 const nlohmann::json& j,
128 );
130 const nlohmann::json& j,
133 );
134 // Includes all sub-types of StructuredLoop (for, map, reduce)
136 const nlohmann::json& j,
139 );
140 std::unique_ptr<sdfg::types::IType> json_to_type(const nlohmann::json& j);
141 std::vector<std::pair<std::string, types::Scalar>> json_to_arguments(const nlohmann::json& j);
142 DebugInfo json_to_debug_info(const nlohmann::json& j);
143
144 ScheduleType json_to_schedule_type(const nlohmann::json& j);
145
146 types::StorageType json_to_storage_type(const nlohmann::json& j);
147
148 static std::string expression(const symbolic::Expression expr);
149
150 symbolic::Expression json_to_expr(const nlohmann::json& j);
151
152 static void writeToFile(const StructuredSDFG& sdfg, const std::filesystem::path& file);
153};
154
155class JSONSymbolicPrinter : public SymEngine::BaseVisitor<JSONSymbolicPrinter, SymEngine::CodePrinter> {
156public:
157 using SymEngine::CodePrinter::apply;
158 using SymEngine::CodePrinter::bvisit;
159 using SymEngine::CodePrinter::str_;
160
161 // Logical expressions
162 void bvisit(const SymEngine::BooleanAtom& x);
163 void bvisit(const SymEngine::And& expr);
164 void bvisit(const SymEngine::Or& expr);
165 void bvisit(const SymEngine::Not& expr);
166 void bvisit(const SymEngine::Xor& expr);
167 void bvisit(const SymEngine::Equality& x);
168 void bvisit(const SymEngine::Unequality& x);
169
170 void bvisit(const SymEngine::LessThan& x);
171 void bvisit(const SymEngine::StrictLessThan& x);
172
173 // Min and Max
174 void bvisit(const SymEngine::Min& x);
175 void bvisit(const SymEngine::Max& x);
176};
177
196public:
197 virtual ~LibraryNodeSerializer() = default;
198
204 virtual nlohmann::json serialize(const sdfg::data_flow::LibraryNode& library_node) = 0;
205
214 const nlohmann::json& j,
217 ) = 0;
218};
219
220using LibraryNodeSerializerFn = std::function<std::unique_ptr<LibraryNodeSerializer>()>;
221
253private:
254 mutable std::mutex mutex_;
255 std::unordered_map<std::string, LibraryNodeSerializerFn> factory_map_;
256
257public:
259 static LibraryNodeSerializerRegistry registry;
260 return registry;
261 }
262
263 void register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn);
264
266
267 size_t size() const;
268};
269
271
272} // namespace serializer
273} // namespace sdfg
Definition element.h:30
Definition element.h:159
Definition structured_sdfg.h:30
Definition analysis.h:53
Note: Even though the class references unique_ptr, it will never delete an SDFG it has a reference to...
Definition structured_sdfg_builder.h:44
Definition data_flow_graph.h:29
Base class for all library nodes in the dataflow graph.
Definition library_node.h:106
JSONSerializer(bool recurse=true)
Definition json_serializer.h:40
void json_to_structured_loop_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
void parse_legacy_transition_to_assignment_block(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
ScheduleType json_to_schedule_type(const nlohmann::json &j)
types::StorageType json_to_storage_type(const nlohmann::json &j)
void return_node_to_json(nlohmann::json &j, const sdfg::structured_control_flow::Return &return_node)
void json_to_continue_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
void json_to_dataflow(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Block &parent)
static std::string expression(const symbolic::Expression expr)
void dataflow_to_json(nlohmann::json &j, const sdfg::data_flow::DataFlowGraph &dataflow)
symbolic::Expression json_to_expr(const nlohmann::json &j)
std::unique_ptr< sdfg::types::IType > json_to_type(const nlohmann::json &j)
virtual nlohmann::json serialize(const sdfg::StructuredSDFG &sdfg, analysis::AnalysisManager *analysis_manager=nullptr, structured_control_flow::Sequence *root=nullptr)
void structured_loop_to_json(nlohmann::json &j, const sdfg::structured_control_flow::StructuredLoop &loop_node)
void structure_definition_to_json(nlohmann::json &j, const sdfg::types::StructureDefinition &definition)
std::vector< std::pair< std::string, types::Scalar > > json_to_arguments(const nlohmann::json &j)
void block_to_json(nlohmann::json &j, const sdfg::structured_control_flow::Block &block)
void json_to_break_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
static void element_metadata_to_json(nlohmann::json &j, const sdfg::Element &element)
void debug_info_to_json(nlohmann::json &j, const sdfg::DebugInfo &debug_info)
void json_to_return_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
bool skip_empty_transitions
Definition json_serializer.h:43
void json_to_if_else_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
void json_to_block_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
void json_to_structure_definition(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder)
void type_to_json(nlohmann::json &j, const sdfg::types::IType &type)
void json_to_while_node(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
DebugInfo json_to_debug_info(const nlohmann::json &j)
void storage_type_to_json(nlohmann::json &j, const sdfg::types::StorageType &storage_type)
void assignment_block_to_json(nlohmann::json &j, const structured_control_flow::AssignmentBlock &block)
void break_node_to_json(nlohmann::json &j, const sdfg::structured_control_flow::Break &break_node)
void json_to_sequence(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &sequence)
void sequence_to_json(nlohmann::json &j, const sdfg::structured_control_flow::Sequence &sequence)
void while_node_to_json(nlohmann::json &j, const sdfg::structured_control_flow::While &while_node)
static void writeToFile(const StructuredSDFG &sdfg, const std::filesystem::path &file)
void serialize_node(nlohmann::json &j, const structured_control_flow::ControlFlowNode &node)
control_flow::Assignments parse_assignments(const nlohmann::json &assignments_arr)
void if_else_to_json(nlohmann::json &j, const sdfg::structured_control_flow::IfElse &if_else_node)
std::unique_ptr< sdfg::StructuredSDFG > deserialize(nlohmann::json &j)
void schedule_type_to_json(nlohmann::json &j, const sdfg::structured_control_flow::ScheduleType &schedule_type)
void continue_node_to_json(nlohmann::json &j, const sdfg::structured_control_flow::Continue &continue_node)
void json_to_assignment_block(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Sequence &parent)
Definition json_serializer.h:155
void bvisit(const SymEngine::Min &x)
void bvisit(const SymEngine::Xor &expr)
void bvisit(const SymEngine::Unequality &x)
void bvisit(const SymEngine::StrictLessThan &x)
void bvisit(const SymEngine::BooleanAtom &x)
void bvisit(const SymEngine::Not &expr)
void bvisit(const SymEngine::And &expr)
void bvisit(const SymEngine::Max &x)
void bvisit(const SymEngine::Or &expr)
void bvisit(const SymEngine::Equality &x)
void bvisit(const SymEngine::LessThan &x)
Registry for library node serializers.
Definition json_serializer.h:252
void register_library_node_serializer(std::string library_node_code, LibraryNodeSerializerFn fn)
LibraryNodeSerializerFn get_library_node_serializer(std::string library_node_code)
static LibraryNodeSerializerRegistry & instance()
Definition json_serializer.h:258
Base class for library node serialization.
Definition json_serializer.h:195
virtual data_flow::LibraryNode & deserialize(const nlohmann::json &j, sdfg::builder::StructuredSDFGBuilder &builder, sdfg::structured_control_flow::Block &parent)=0
Deserialize a library node from JSON.
virtual nlohmann::json serialize(const sdfg::data_flow::LibraryNode &library_node)=0
Serialize a library node to JSON.
A control flow node representing a basic block with dataflow computations.
Definition block.h:42
Represents a break statement that exits the innermost loop.
Definition while.h:93
Represents a continue statement that skips to the next loop iteration.
Definition while.h:128
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
Represents a conditional if-else control flow node with multiple cases.
Definition if_else.h:47
Represents a return statement that exits the function with a value.
Definition return.h:32
Represents a schedule type for Map nodes.
Definition structured_loop.h:31
A sequential container of control flow nodes.
Definition sequence.h:52
Base class for structured loop constructs.
Definition structured_loop.h:244
Represents a while-loop with condition-controlled iteration.
Definition while.h:37
Abstract base interface for all types in the sdfglib type system.
Definition type.h:625
Represents the storage location and management characteristics of data.
Definition type.h:64
Defines the layout and members of a structure type.
Definition structure.h:120
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
Definition function_builder.h:10
symbolic::SymbolMap Assignments
Type alias for symbol assignments on interstate edges.
Definition interstate_edge.h:30
Definition cutout_serializer.h:13
std::function< std::unique_ptr< LibraryNodeSerializer >()> LibraryNodeSerializerFn
Definition json_serializer.h:220
void register_default_serializers()
Definition analysis.h:7
Core type system definitions for sdfglib.