SST 16.0.0
Structural Simulation Toolkit
jsonmodel.h
1// -*- c++ -*-
2
3// Copyright 2009-2026 NTESS. Under the terms
4// of Contract DE-NA0003525 with NTESS, the U.S.
5// Government retains certain rights in this software.
6//
7// Copyright (c) 2009-2026, NTESS
8// All rights reserved.
9//
10// This file is part of the SST software package. For license
11// information, see the LICENSE file in the top level directory of the
12// distribution.
13
14#ifndef SST_CORE_MODEL_JSON_JSONMODEL_H
15#define SST_CORE_MODEL_JSON_JSONMODEL_H
16
17#include "sst/core/config.h"
18#include "sst/core/cputimer.h"
19#include "sst/core/factory.h"
20#include "sst/core/memuse.h"
21#include "sst/core/model/configGraph.h"
22#include "sst/core/model/json/jsonstates.h"
23#include "sst/core/model/sstmodel.h"
24#include "sst/core/output.h"
25#include "sst/core/rankInfo.h"
26#include "sst/core/sst_types.h"
27#include "sst/core/warnmacros.h"
28
29#include "nlohmann/json.hpp"
30
31#include <cstdint>
32#include <fstream>
33#include <map>
34#include <stack>
35#include <string>
36#include <tuple>
37#include <vector>
38
39using namespace SST;
40using namespace SST::Core;
41using namespace SST::Core::JSONParser;
42using json = nlohmann::json;
43
44namespace SST::Core {
45
47{
48public:
49 // SAX event handlers
50 bool null();
51 bool boolean(bool val);
52 bool number_integer(json::number_integer_t val);
53 bool number_unsigned(json::number_unsigned_t val);
54 bool number_float(json::number_float_t val, const std::string& str);
55 bool string(std::string& val);
56 bool binary(json::binary_t& val);
57 bool start_object(std::size_t elements);
58 bool end_object();
59 bool start_array(std::size_t elements);
60 bool end_array();
61 bool key(std::string& val);
62
63 bool parse_error(std::size_t position, const std::string& lastToken, const json::exception& ex);
64 void setConfigGraph(ConfigGraph* graph);
65 const std::map<std::string, std::string> getProgramOptions();
66
67 // error state
68 std::size_t error_pos_;
69 std::string error_str_;
70
71private:
72 // Helpers for constructing objects
73 void constructComponent();
74 void constructSubComponent();
75 void constructPortModule();
76
77 // Holds information needed to construct a component or subcomponent
78 struct JSONShadowComponent
79 {
80 std::string name = ""; // Component or slot name
81 std::string type = ""; // Type
82 unsigned slot_number = 0; // Slot number for subcomponents
83 SST::ConfigComponent* comp = nullptr;
84 };
85
86 struct JSONShadowStatistic
87 {
88 std::string name = "";
89 Params params;
90 bool shared = false;
91 std::string shared_name = "";
92
93 void reset()
94 {
95 name = "";
96 params.clear();
97 shared = false;
98 shared_name = "";
99 }
100 };
101
102 struct JSONShadowLink
103 {
104 std::string name = "";
105 bool nocut = false;
106 bool nonlocal = false;
107 // Left side
108 SST::ComponentId_t leftcomp = -1;
109 std::string leftport = "";
110 std::string leftlat = "";
111 // Right side if not nonlocal
112 SST::ComponentId_t rightcomp = -1;
113 std::string rightport = "";
114 std::string rightlat = "";
115 // Right side if nonlocal
116 int rank = -1;
117 int thread = -1;
118
119 void reset()
120 {
121 name = "";
122 nocut = false;
123 nonlocal = false;
124 leftcomp = -1;
125 leftport = "";
126 leftlat = "";
127 rightcomp = -1;
128 rightport = "";
129 rightlat = "";
130 rank = -1;
131 thread = -1;
132 }
133 };
134
135 struct JSONShadowPortModule
136 {
137 std::string port = "";
138 std::string type = "";
139 Params params;
140 uint8_t stat_load_level = STATISTICLOADLEVELUNINITIALIZED;
141 std::vector<std::string> shared_param_sets;
142 ConfigPortModule* pm = nullptr;
143
144 void reset()
145 {
146 port = "";
147 type = "";
148 params.clear();
149 stat_load_level = STATISTICLOADLEVELUNINITIALIZED;
150 shared_param_sets.clear();
151 pm = nullptr;
152 }
153 };
154
155 ConfigGraph* graph_;
156
157 State current_state_ = State::Entry;
158 std::vector<JSONShadowComponent> shadow_component_stack_;
159 JSONShadowStatistic shadow_statistic_;
160 JSONShadowLink shadow_link_;
161 JSONShadowPortModule shadow_port_module_;
162 ConfigStatGroup* current_stat_group_;
163 std::map<std::string, ConfigStatistic*> current_shared_stats_;
164
165 std::map<std::string, std::string> program_options_;
166
167 std::string current_key_;
168 std::string current_shared_name_;
169 RankInfo rank_;
170
171 bool found_components_ = false; // components must be declared before links
172
173 ComponentId_t findComponentIdByName(const std::string& name, bool& success);
174};
175
176class SSTJSONModelDefinition : public SSTModelDescription
177{
178public:
179 SST_ELI_REGISTER_MODEL_DESCRIPTION(
181 "sst",
182 "model.json",
183 SST_ELI_ELEMENT_VERSION(1,0,0),
184 "JSON model for building SST simulation graphs",
185 true)
186
187 SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".json")
188
189 SSTJSONModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time);
190 virtual ~SSTJSONModelDefinition();
191
192 ConfigGraph* createConfigGraph() override;
193
194protected:
195 std::string script_name_;
196 Output* output_;
197 Config* config_;
198 ConfigGraph* graph_;
199 double start_time_;
200};
201
202} // namespace SST::Core
203
204#endif // SST_CORE_MODEL_JSON_JSONMODEL_H
Represents the configuration of a generic component.
Definition configComponent.h:83
A Configuration Graph A graph representing Components and Links.
Definition configGraph.h:76
Class that represents a PortModule in ConfigGraph.
Definition configComponent.h:50
Definition configStatistic.h:82
Class to contain SST Simulation Configuration variables.
Definition config.h:52
Definition jsonmodel.h:47
Definition jsonmodel.h:177
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition jsonmodel.cc:1168
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
Parameter store.
Definition params.h:65
void clear()
Erases all elements, including deleting reference to shared param sets.
Definition params.cc:86
Definition rankInfo.h:24