SST  15.1.0
StructuralSimulationToolkit
pymodel.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_PYTHON_PYMODEL_H
15 #define SST_CORE_MODEL_PYTHON_PYMODEL_H
16 
17 // This only works if we have Python defined from configure, otherwise this is
18 // a compile time error.
19 // #ifdef SST_CONFIG_HAVE_PYTHON
20 
21 #include "sst/core/config.h"
22 #include "sst/core/configGraph.h"
23 #include "sst/core/model/sstmodel.h"
24 #include "sst/core/output.h"
25 #include "sst/core/rankInfo.h"
26 #include "sst/core/warnmacros.h"
27 
28 DISABLE_WARN_DEPRECATED_REGISTER
29 #include <Python.h>
30 #include <cstddef>
31 REENABLE_WARNING
32 
33 #include <map>
34 #include <string>
35 #include <vector>
36 
37 using namespace SST;
38 
39 namespace SST::Core {
40 
42 {
43 public:
44  SST_ELI_REGISTER_MODEL_DESCRIPTION(
46  "sst",
47  "model.python",
48  SST_ELI_ELEMENT_VERSION(1,0,0),
49  "Python model for building SST simulation graphs",
50  true)
51 
52  SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".py")
53 
54 
55  // SSTPythonModelDefinition(
56  // const std::string& script_file, int verbosity, Config* config, double start_time, int argc, char** argv);
57  SSTPythonModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time);
58  virtual ~SSTPythonModelDefinition();
59 
60  ConfigGraph* createConfigGraph() override;
61 
62 protected:
63  void initModel(const std::string& script_file, int verbosity, Config* config, int argc, char** argv);
64  std::string scriptName;
65  Output* output;
66  Config* config;
67  ConfigGraph* graph;
68  char* namePrefix;
69  size_t namePrefixLen;
70  std::vector<size_t> nameStack;
71  std::map<std::string, ComponentId_t> compNameMap;
72  ComponentId_t nextComponentId;
73  double start_time;
74  bool callPythonFinalize;
75 #if PY_MINOR_VERSION >= 9
76  bool enablePythonCoverage = false;
77 #endif
78 
79 public: /* Public, but private. Called only from Python functions */
80  Config* getConfig() const { return config; }
81 
82  bool setConfigEntryFromModel(const std::string& entryName, const std::string& value)
83  {
84  return setOptionFromModel(entryName, value);
85  }
86 
87  ConfigGraph* getGraph() const { return graph; }
88 
89  Output* getOutput() const { return output; }
90 
91  ComponentId_t getNextComponentId() { return nextComponentId++; }
92 
93  ComponentId_t addComponent(const char* name, const char* type)
94  {
95  auto id = graph->addComponent(name, type);
96  return id;
97  }
98 
99  ConfigComponent* findComponentByName(const char* name) const
100  {
101  return graph->findComponentByName(std::string(name));
102  }
103 
104  ConfigComponentMap_t& components() { return graph->getComponentMap(); }
105 
106  LinkId_t createLink(const char* link_name, const char* latency) { return graph->createLink(link_name, latency); }
107 
108  void addLink(ComponentId_t id, LinkId_t link_id, const char* port, const char* latency) const
109  {
110  graph->addLink(id, link_id, port, latency);
111  }
112 
113  void addNonLocalLink(LinkId_t link_id, int rank, int thread) const
114  {
115  graph->addNonLocalLink(link_id, rank, thread);
116  }
117 
118  void setLinkNoCut(LinkId_t link_id) const { graph->setLinkNoCut(link_id); }
119 
120  void pushNamePrefix(const char* name);
121  void popNamePrefix();
122  char* addNamePrefix(const char* name) const;
123 
124  void setStatisticOutput(const char* Name) { graph->setStatisticOutput(Name); }
125  void addStatisticOutputParameter(const std::string& param, const std::string& value)
126  {
127  graph->addStatisticOutputParameter(param, value);
128  }
129  void setStatisticLoadLevel(uint8_t loadLevel) { graph->setStatisticLoadLevel(loadLevel); }
130 
131  void addGlobalParameter(const char* set, const char* key, const char* value, bool overwrite)
132  {
133  insertGlobalParameter(set, key, value, overwrite);
134  }
135 
136  UnitAlgebra getElapsedExecutionTime() const;
137  UnitAlgebra getLocalMemoryUsage() const;
138 
139  void setCallPythonFinalize(bool state) { callPythonFinalize = state; }
140 };
141 
142 // For xml inputs (.xml or .sdl), we just use a python script to parse
143 // the xml. So, this model definition just uses the python model with
144 // a few tweaked inputs to the constructor.
146 {
147 public:
148  SST_ELI_REGISTER_MODEL_DESCRIPTION(
150  "sst",
151  "model.xml",
152  SST_ELI_ELEMENT_VERSION(1,0,0),
153  "XML model for building SST simulation graphs",
154  false)
155 
156  SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".xml",".sdl")
157 
158 
159  SSTXmlModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time) :
160  SSTModelDescription(config)
161  {
162  // Overwrite model_options. XML input doesn't allow model
163  // options and we need to set it to the script_file to use as
164  // input to the xmlToPython.py file.
165  setModelOptions(script_file);
166 
167  actual_model_ =
168  new SSTPythonModelDefinition(SST_INSTALL_PREFIX "/libexec/xmlToPython.py", verbosity, config, start_time);
169  }
170 
171  ConfigGraph* createConfigGraph() override { return actual_model_->createConfigGraph(); }
172 
173  virtual ~SSTXmlModelDefinition() {}
174 
175 private:
176  SSTPythonModelDefinition* actual_model_;
177 };
178 
179 std::map<std::string, std::string> generateStatisticParameters(PyObject* statParamDict);
180 SST::Params pythonToCppParams(PyObject* statParamDict);
181 PyObject* buildStatisticObject(StatisticId_t id);
182 PyObject* buildEnabledStatistic(
183  ConfigComponent* cc, const char* statName, PyObject* statParamDict, bool apply_to_children);
184 PyObject* buildEnabledStatistics(ConfigComponent* cc, PyObject* statList, PyObject* paramDict, bool apply_to_children);
185 
186 } // namespace SST::Core
187 
188 #endif // SST_CORE_MODEL_PYTHON_PYMODEL_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:57
ConfigComponentMap_t & getComponentMap()
Return the map of components.
Definition: configGraph.h:689
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: pymodel.cc:1329
void setStatisticLoadLevel(uint8_t loadLevel)
Set the statistic system load level.
Definition: configGraph.cc:954
Class to contain SST Simulation Configuration variables.
Definition: config.h:51
Represents the configuration of a generic component.
Definition: configGraph.h:381
void insertGlobalParameter(const std::string &set, const Params::key_type &key, const Params::key_type &value, bool overwrite=true)
Allows ModelDefinition to set global parameters.
Definition: sstmodel.cc:41
void addNonLocalLink(LinkId_t link_id, int rank, int thread)
Adds the remote rank info for nonlocal links.
Definition: configGraph.cc:1010
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:585
Definition: pymodel.h:145
void setModelOptions(const std::string &options)
Sets the model options field of the Config object.
Definition: sstmodel.cc:35
Definition: action.cc:18
bool setOptionFromModel(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition: sstmodel.cc:29
void addStatisticOutputParameter(const std::string &param, const std::string &value)
Add parameter to the statistic output module.
Definition: configGraph.cc:948
void addLink(ComponentId_t comp_id, LinkId_t link_id, const char *port, const char *latency_str)
Add a Link to a Component on a given Port.
Definition: configGraph.cc:960
LinkId_t createLink(const char *name, const char *latency=nullptr)
Create link and return it&#39;s ID.
Definition: configGraph.cc:1030
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: pymodel.h:171
Parameter store.
Definition: params.h:63
Base class for Model Generation.
Definition: sstmodel.h:29
void setLinkNoCut(LinkId_t link_name)
Set a Link to be no-cut.
Definition: configGraph.cc:1045
ComponentId_t addComponent(const std::string &name, const std::string &type)
Create a new component.
Definition: configGraph.cc:915
void setStatisticOutput(const std::string &name)
Set the statistic output module.
Definition: configGraph.cc:936
Class that stores data in a vector, but can access the data similar to a map.
Definition: sparseVectorMap.h:46
Performs Unit math in full precision.
Definition: unitAlgebra.h:105
Definition: pymodel.h:41