SST  15.1.0
StructuralSimulationToolkit
jsonmodel.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2025 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-2025, 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/component.h"
18 #include "sst/core/config.h"
19 #include "sst/core/configGraph.h"
20 #include "sst/core/cputimer.h"
21 #include "sst/core/factory.h"
22 #include "sst/core/memuse.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 
39 using namespace SST;
40 using json = nlohmann::json;
41 
42 namespace SST::Core {
43 
45 {
46 public:
47  // SAX event handlers
48  bool null();
49  bool boolean(bool val);
50  bool number_integer(json::number_integer_t val);
51  bool number_unsigned(json::number_unsigned_t val);
52  bool number_float(json::number_float_t val, const std::string& str);
53  bool string(std::string& val);
54  bool binary(json::binary_t& val);
55  bool start_object(std::size_t elements);
56  bool end_object();
57  bool start_array(std::size_t elements);
58  bool end_array();
59  bool key(std::string& val);
60 
61  bool parse_error(std::size_t position, const std::string& lastToken, const json::exception& ex);
62  void setConfigGraph(ConfigGraph* graph);
63  const std::map<std::string, std::string> getProgramOptions();
64 
65  // error state
66  std::size_t errorPos;
67  std::string errorStr;
68 
69 private:
70  // Current parsing state for major sections
71  enum ParseState {
72  ROOT,
73  PROGRAM_OPTIONS,
74  SHARED_PARAMS,
75  STATISTICS_OPTIONS,
76  STATISTICS_GROUP,
77  COMPONENTS,
78  LINKS
79  } current_state = ROOT;
80 
81  ConfigGraph* graph;
82 
83  std::vector<std::string> path_stack;
84  std::stack<json::value_t> context_stack;
85  std::stack<ConfigComponent*> Parents;
86  std::map<std::string, std::string> ProgramOptions;
87 
88  std::string current_key;
89  std::string current_shared_name;
90  std::string current_comp_name;
91  std::string current_comp_stat_name;
92  std::string current_subcomp_name;
93  std::string current_subcomp_type;
94  std::string current_grp_stat_name;
95  std::string link_name;
96  std::string left_comp;
97  std::string left_port;
98  std::string left_latency;
99  std::string right_comp;
100  std::string right_port;
101  std::string right_latency;
102  bool no_cut = false;
103  bool nonlocal = false;
104  uint32_t current_comp_rank;
105  int subcomp_slot = -1;
106  int right_rank = -1;
107  int right_thread = -1;
108 
109  // FSM values
110  bool foundComponents = false;
111  bool in_shared_params_object = false;
112  bool in_comp_params = false;
113  bool in_comp_stats = false;
114  bool in_comp_stats_params = false;
115  bool in_comp_subcomp = false;
116  bool in_comp_subsubcomp = false;
117  bool in_comp_subcomp_params = false;
118  bool in_grp_stats_output = false;
119  bool in_grp_stats_output_params = false;
120  bool in_grp_stats_def = false;
121  bool in_grp_stats_def_params = false;
122  bool in_grp_stats_comps = false;
123  bool in_left_link = false;
124  bool in_right_link = false;
125 
126  int array_depth = 0;
127 
128  ComponentId_t current_comp_id;
129  ConfigStatGroup* current_stat_group;
130  ConfigComponent* current_component;
131  ConfigComponent* current_sub_component;
132  Params current_stat_params;
133 
134  // internal parsing methods
135  std::string getCurrentPath() const;
136  void processValue(const json& value);
137  ComponentId_t findComponentIdByName(const std::string& Name, bool& success);
138 };
139 
141 {
142 public:
143  SST_ELI_REGISTER_MODEL_DESCRIPTION(
145  "sst",
146  "model.json",
147  SST_ELI_ELEMENT_VERSION(1,0,0),
148  "JSON model for building SST simulation graphs",
149  true)
150 
151  SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".json")
152 
153  SSTJSONModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time);
154  virtual ~SSTJSONModelDefinition();
155 
156  ConfigGraph* createConfigGraph() override;
157 
158 protected:
159  std::string scriptName;
160  Output* output;
161  Config* config;
162  ConfigGraph* graph;
163  ComponentId_t nextComponentId;
164  double start_time;
165 };
166 
167 } // namespace SST::Core
168 
169 #endif // SST_CORE_MODEL_JSON_JSONMODEL_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:57
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: jsonmodel.cc:588
Class to contain SST Simulation Configuration variables.
Definition: config.h:51
Represents the configuration of a generic component.
Definition: configGraph.h:381
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:585
Definition: action.cc:18
Definition: jsonmodel.h:140
Definition: jsonmodel.h:44
Parameter store.
Definition: params.h:63
Base class for Model Generation.
Definition: sstmodel.h:29
Definition: configGraph.h:282