SST 16.0.0
Structural Simulation Toolkit
pymodel.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_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/model/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
28DISABLE_WARN_DEPRECATED_REGISTER
29#include <Python.h>
30#include <cstddef>
31#include <cstdint>
32REENABLE_WARNING
33
34#include <map>
35#include <string>
36#include <vector>
37
38using namespace SST;
39
40namespace SST::Core {
41
42class SSTPythonModelDefinition : public SSTModelDescription
43{
44public:
45 SST_ELI_REGISTER_MODEL_DESCRIPTION(
47 "sst",
48 "model.python",
49 SST_ELI_ELEMENT_VERSION(1,0,0),
50 "Python model for building SST simulation graphs",
51 true)
52
53 SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".py")
54
55
56 // SSTPythonModelDefinition(
57 // const std::string& script_file, int verbosity, Config* config, double start_time, int argc, char** argv);
58 SSTPythonModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time);
60
62
63protected:
64 void initModel(const std::string& script_file, int verbosity, Config* config, int argc, char** argv);
65 std::string scriptName;
66 Output* output;
67 Config* config;
68 ConfigGraph* graph;
69 char* namePrefix;
70 size_t namePrefixLen;
71 std::vector<size_t> nameStack;
72 std::map<std::string, ComponentId_t> compNameMap;
73 ComponentId_t nextComponentId;
74 double start_time;
75 bool callPythonFinalize;
76 bool enablePythonCoverage = false;
77
78public: /* Public, but private. Called only from Python functions */
79 Config* getConfig() const { return config; }
80
81 bool setConfigEntryFromModel(const std::string& entryName, const std::string& value)
82 {
83 return setOptionFromModel(entryName, value);
84 }
85
86 ConfigGraph* getGraph() const { return graph; }
87
88 Output* getOutput() const { return output; }
89
90 ComponentId_t getNextComponentId() { return nextComponentId++; }
91
92 ComponentId_t addComponent(const char* name, const char* type)
93 {
94 auto id = graph->addComponent(name, type);
95 return id;
96 }
97
98 ConfigComponent* findComponentByName(const char* name) const
99 {
100 return graph->findComponentByName(std::string(name));
101 }
102
103 ConfigComponentMap_t& components() { return graph->getComponentMap(); }
104
105 LinkId_t createLink(const char* link_name, const char* latency) { return graph->createLink(link_name, latency); }
106
107 void addLink(ComponentId_t id, LinkId_t link_id, const char* port, const char* latency) const
108 {
109 graph->addLink(id, link_id, port, latency);
110 }
111
112 void addNonLocalLink(LinkId_t link_id, int rank, int thread) const
113 {
114 graph->addNonLocalLink(link_id, rank, thread);
115 }
116
117 void setLinkNoCut(LinkId_t link_id) const { graph->setLinkNoCut(link_id); }
118
119 void pushNamePrefix(const char* name);
120 void popNamePrefix();
121 char* addNamePrefix(const char* name) const;
122
123 void setStatisticOutput(const char* Name) { graph->setStatisticOutput(Name); }
124 void addStatisticOutputParameter(const std::string& param, const std::string& value)
125 {
126 graph->addStatisticOutputParameter(param, value);
127 }
128 void setStatisticLoadLevel(uint8_t loadLevel) { graph->setStatisticLoadLevel(loadLevel); }
129
130 void addGlobalParameter(const char* set, const char* key, const char* value, bool overwrite)
131 {
132 insertGlobalParameter(set, key, value, overwrite);
133 }
134
135 UnitAlgebra getElapsedExecutionTime() const;
136 UnitAlgebra getLocalMemoryUsage() const;
137
138 void setCallPythonFinalize(bool state) { callPythonFinalize = state; }
139};
140
141// For xml inputs (.xml or .sdl), we just use a python script to parse
142// the xml. So, this model definition just uses the python model with
143// a few tweaked inputs to the constructor.
144class SSTXmlModelDefinition : public SSTModelDescription
145{
146public:
147 SST_ELI_REGISTER_MODEL_DESCRIPTION(
149 "sst",
150 "model.xml",
151 SST_ELI_ELEMENT_VERSION(1,0,0),
152 "XML model for building SST simulation graphs",
153 false)
154
155 SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".xml",".sdl")
156
157
158 SSTXmlModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time) :
159 SSTModelDescription(config)
160 {
161 // Overwrite model_options. XML input doesn't allow model
162 // options and we need to set it to the script_file to use as
163 // input to the xmlToPython.py file.
164 setModelOptions(script_file);
165
166 actual_model_ =
167 new SSTPythonModelDefinition(SST_INSTALL_PREFIX "/libexec/xmlToPython.py", verbosity, config, start_time);
168 }
169
170 ConfigGraph* createConfigGraph() override { return actual_model_->createConfigGraph(); }
171
172 virtual ~SSTXmlModelDefinition() {}
173
174private:
175 SSTPythonModelDefinition* actual_model_;
176};
177
178std::map<std::string, std::string> generateStatisticParameters(PyObject* statParamDict);
179SST::Params pythonToCppParams(PyObject* statParamDict);
180PyObject* buildStatisticObject(ComponentId_t comp_id, StatisticId_t stat_id);
181PyObject* buildEnabledStatistic(
182 ConfigComponent* cc, const char* statName, PyObject* statParamDict, bool apply_to_children);
183PyObject* buildEnabledStatistics(ConfigComponent* cc, PyObject* statList, PyObject* paramDict, bool apply_to_children);
184
185} // namespace SST::Core
186
187#endif // SST_CORE_MODEL_PYTHON_PYMODEL_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 to contain SST Simulation Configuration variables.
Definition config.h:52
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition pymodel.cc:1332
Definition pymodel.h:145
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition pymodel.h:170
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 setModelOptions(const std::string &options)
Sets the model options field of the Config object.
Definition sstmodel.cc:35
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
bool setOptionFromModel(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition sstmodel.cc:29
Performs Unit math in full precision.
Definition unitAlgebra.h:107