SST 15.0
Structural Simulation Toolkit
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
28DISABLE_WARN_DEPRECATED_REGISTER
29#include <Python.h>
30REENABLE_WARNING
31
32#include <map>
33#include <string>
34#include <vector>
35
36using namespace SST;
37
38namespace SST::Core {
39
40class SSTPythonModelDefinition : public SSTModelDescription
41{
42public:
43 SST_ELI_REGISTER_MODEL_DESCRIPTION(
45 "sst",
46 "model.python",
47 SST_ELI_ELEMENT_VERSION(1,0,0),
48 "Python model for building SST simulation graphs",
49 true)
50
51 SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".py")
52
53
54 // SSTPythonModelDefinition(
55 // const std::string& script_file, int verbosity, Config* config, double start_time, int argc, char** argv);
56 SSTPythonModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time);
58
60
61protected:
62 void initModel(const std::string& script_file, int verbosity, Config* config, int argc, char** argv);
63 std::string scriptName;
64 Output* output;
65 Config* config;
66 ConfigGraph* graph;
67 char* namePrefix;
68 size_t namePrefixLen;
69 std::vector<size_t> nameStack;
70 std::map<std::string, ComponentId_t> compNameMap;
71 ComponentId_t nextComponentId;
72 double start_time;
73 bool callPythonFinalize;
74#if PY_MINOR_VERSION >= 9
75 bool enablePythonCoverage = false;
76#endif
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 void addLink(ComponentId_t id, const char* link_name, const char* port, const char* latency, bool no_cut) const
106 {
107 graph->addLink(id, link_name, port, latency, no_cut);
108 }
109 void setLinkNoCut(const char* link_name) const { graph->setLinkNoCut(link_name); }
110
111 void pushNamePrefix(const char* name);
112 void popNamePrefix();
113 char* addNamePrefix(const char* name) const;
114
115 void setStatisticOutput(const char* Name) { graph->setStatisticOutput(Name); }
116 void addStatisticOutputParameter(const std::string& param, const std::string& value)
117 {
118 graph->addStatisticOutputParameter(param, value);
119 }
120 void setStatisticLoadLevel(uint8_t loadLevel) { graph->setStatisticLoadLevel(loadLevel); }
121
122 void addGlobalParameter(const char* set, const char* key, const char* value, bool overwrite)
123 {
124 insertGlobalParameter(set, key, value, overwrite);
125 }
126
127 UnitAlgebra getElapsedExecutionTime() const;
128 UnitAlgebra getLocalMemoryUsage() const;
129
130 void setCallPythonFinalize(bool state) { callPythonFinalize = state; }
131};
132
133// For xml inputs (.xml or .sdl), we just use a python script to parse
134// the xml. So, this model definition just uses the python model with
135// a few tweaked inputs to the constructor.
136class SSTXmlModelDefinition : public SSTModelDescription
137{
138public:
139 SST_ELI_REGISTER_MODEL_DESCRIPTION(
141 "sst",
142 "model.xml",
143 SST_ELI_ELEMENT_VERSION(1,0,0),
144 "XML model for building SST simulation graphs",
145 false)
146
147 SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".xml",".sdl")
148
149
150 SSTXmlModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time) :
151 SSTModelDescription(config)
152 {
153 // Overwrite model_options. XML input doesn't allow model
154 // options and we need to set it to the script_file to use as
155 // input to the xmlToPython.py file.
156 setModelOptions(script_file);
157
158 actual_model_ =
159 new SSTPythonModelDefinition(SST_INSTALL_PREFIX "/libexec/xmlToPython.py", verbosity, config, start_time);
160 }
161
162 ConfigGraph* createConfigGraph() override { return actual_model_->createConfigGraph(); }
163
164 virtual ~SSTXmlModelDefinition() {}
165
166private:
167 SSTPythonModelDefinition* actual_model_;
168};
169
170std::map<std::string, std::string> generateStatisticParameters(PyObject* statParamDict);
171SST::Params pythonToCppParams(PyObject* statParamDict);
172PyObject* buildStatisticObject(StatisticId_t id);
173PyObject* buildEnabledStatistic(
174 ConfigComponent* cc, const char* statName, PyObject* statParamDict, bool apply_to_children);
175PyObject* buildEnabledStatistics(ConfigComponent* cc, PyObject* statList, PyObject* paramDict, bool apply_to_children);
176
177} // namespace SST::Core
178
179#endif // SST_CORE_MODEL_PYTHON_PYMODEL_H
Represents the configuration of a generic component.
Definition configGraph.h:263
A Configuration Graph A graph representing Components and Links.
Definition configGraph.h:450
Class to contain SST Simulation Configuration variables.
Definition config.h:41
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition pymodel.cc:1271
Definition pymodel.h:137
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition pymodel.h:162
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Parameter store.
Definition params.h:58
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