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