SST  14.1.0
StructuralSimulationToolkit
pymodel.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2024 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-2024, 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 REENABLE_WARNING
31 
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 using namespace SST;
37 
38 namespace SST {
39 namespace 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 
76 public: /* Public, but private. Called only from Python functions */
77  Config* getConfig(void) const { return config; }
78 
79  bool setConfigEntryFromModel(const std::string& entryName, const std::string& value)
80  {
81  return setOptionFromModel(entryName, value);
82  }
83 
84  ConfigGraph* getGraph(void) const { return graph; }
85 
86  Output* getOutput() const { return output; }
87 
88  ComponentId_t getNextComponentId() { return nextComponentId++; }
89 
90  ComponentId_t addComponent(const char* name, const char* type)
91  {
92  auto id = graph->addComponent(name, type);
93  return id;
94  }
95 
96  ConfigComponent* findComponentByName(const char* name) const
97  {
98  return graph->findComponentByName(std::string(name));
99  }
100 
101  ConfigComponentMap_t& components() { return graph->getComponentMap(); }
102 
103  void addLink(ComponentId_t id, const char* link_name, const char* port, const char* latency, bool no_cut) const
104  {
105  graph->addLink(id, link_name, port, latency, no_cut);
106  }
107  void setLinkNoCut(const char* link_name) const { graph->setLinkNoCut(link_name); }
108 
109  void pushNamePrefix(const char* name);
110  void popNamePrefix(void);
111  char* addNamePrefix(const char* name) const;
112 
113  void setStatisticOutput(const char* Name) { graph->setStatisticOutput(Name); }
114  void addStatisticOutputParameter(const std::string& param, const std::string& value)
115  {
116  graph->addStatisticOutputParameter(param, value);
117  }
118  void setStatisticLoadLevel(uint8_t loadLevel) { graph->setStatisticLoadLevel(loadLevel); }
119 
120  void addGlobalParameter(const char* set, const char* key, const char* value, bool overwrite)
121  {
122  insertGlobalParameter(set, key, value, overwrite);
123  }
124 
125  UnitAlgebra getElapsedExecutionTime() const;
126  UnitAlgebra getLocalMemoryUsage() const;
127 
128  void setCallPythonFinalize(bool state) { callPythonFinalize = state; }
129 };
130 
131 // For xml inputs (.xml or .sdl), we just use a python script to parse
132 // the xml. So, this model definition just uses the python model with
133 // a few tweaked inputs to the constructor.
135 {
136 public:
137  SST_ELI_REGISTER_MODEL_DESCRIPTION(
139  "sst",
140  "model.xml",
141  SST_ELI_ELEMENT_VERSION(1,0,0),
142  "XML model for building SST simulation graphs",
143  false)
144 
145  SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".xml",".sdl")
146 
147 
148  SSTXmlModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time) :
149  SSTModelDescription(config)
150  {
151  // Overwrite model_options. XML input doesn't allow model
152  // options and we need to set it to the script_file to use as
153  // input to the xmlToPython.py file.
154  setModelOptions(script_file);
155 
156  actual_model_ =
157  new SSTPythonModelDefinition(SST_INSTALL_PREFIX "/libexec/xmlToPython.py", verbosity, config, start_time);
158  }
159 
160  ConfigGraph* createConfigGraph() override { return actual_model_->createConfigGraph(); }
161 
162  virtual ~SSTXmlModelDefinition() {}
163 
164 private:
165  SSTPythonModelDefinition* actual_model_;
166 };
167 
168 std::map<std::string, std::string> generateStatisticParameters(PyObject* statParamDict);
169 SST::Params pythonToCppParams(PyObject* statParamDict);
170 PyObject* buildStatisticObject(StatisticId_t id);
171 PyObject*
172 buildEnabledStatistic(ConfigComponent* cc, const char* statName, PyObject* statParamDict, bool apply_to_children);
173 PyObject* buildEnabledStatistics(ConfigComponent* cc, PyObject* statList, PyObject* paramDict, bool apply_to_children);
174 
175 } // namespace Core
176 } // namespace SST
177 
178 #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:53
ConfigComponentMap_t & getComponentMap()
Return the map of components.
Definition: configGraph.h:465
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: pymodel.cc:1234
void setStatisticLoadLevel(uint8_t loadLevel)
Set the statistic system load level.
Definition: configGraph.cc:815
Class to contain SST Simulation Configuration variables.
Definition: config.h:38
Represents the configuration of a generic component.
Definition: configGraph.h:217
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:39
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:389
Definition: pymodel.h:134
void setModelOptions(const std::string &options)
Sets the model options field of the Config object.
Definition: sstmodel.cc:33
void addLink(ComponentId_t comp_id, const std::string &link_name, const std::string &port, const std::string &latency_str, bool no_cut=false)
Add a Link to a Component on a given Port.
Definition: configGraph.cc:821
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:27
void addStatisticOutputParameter(const std::string &param, const std::string &value)
Add parameter to the statistic output module.
Definition: configGraph.cc:809
void setLinkNoCut(const std::string &link_name)
Set a Link to be no-cut.
Definition: configGraph.cc:866
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: pymodel.h:160
Parameter store.
Definition: params.h:55
Base class for Model Generation.
Definition: sstmodel.h:25
ComponentId_t addComponent(const std::string &name, const std::string &type)
Create a new component.
Definition: configGraph.cc:776
void setStatisticOutput(const std::string &name)
Set the statistic output module.
Definition: configGraph.cc:797
Class that stores data in a vector, but can access the data similar to a map.
Definition: sparseVectorMap.h:43
Performs Unit math in full precision.
Definition: unitAlgebra.h:106
Definition: pymodel.h:41