SST  11.1.0
StructuralSimulationToolkit
pymodel.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2021 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-2021, 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  ConfigGraph* getGraph(void) const { return graph; }
79 
80  Output* getOutput() const { return output; }
81 
82  ComponentId_t getNextComponentId() { return nextComponentId++; }
83 
84  ComponentId_t addComponent(const char* name, const char* type)
85  {
86  auto id = graph->addComponent(name, type);
87  return id;
88  }
89 
90  ConfigComponent* findComponentByName(const char* name) const
91  {
92  return graph->findComponentByName(std::string(name));
93  }
94 
95  ConfigComponentMap_t& components() { return graph->getComponentMap(); }
96 
97  void addLink(ComponentId_t id, const char* link_name, const char* port, const char* latency, bool no_cut) const
98  {
99  graph->addLink(id, link_name, port, latency, no_cut);
100  }
101  void setLinkNoCut(const char* link_name) const { graph->setLinkNoCut(link_name); }
102 
103  void pushNamePrefix(const char* name);
104  void popNamePrefix(void);
105  char* addNamePrefix(const char* name) const;
106 
107  void setStatisticOutput(const char* Name) { graph->setStatisticOutput(Name); }
108  void addStatisticOutputParameter(const std::string& param, const std::string& value)
109  {
110  graph->addStatisticOutputParameter(param, value);
111  }
112  void setStatisticLoadLevel(uint8_t loadLevel) { graph->setStatisticLoadLevel(loadLevel); }
113 
114  void addGlobalParameter(const char* set, const char* key, const char* value, bool overwrite)
115  {
116  Params::insert_global(set, key, value, overwrite);
117  }
118 
119  UnitAlgebra getElapsedExecutionTime() const;
120  UnitAlgebra getLocalMemoryUsage() const;
121 };
122 
123 // For xml inputs (.xml or .sdl), we just use a python script to parse
124 // the xml. So, this model definition just uses the python model with
125 // a few tweaked inputs to the constructor.
127 {
128 public:
129  SST_ELI_REGISTER_MODEL_DESCRIPTION(
131  "sst",
132  "model.xml",
133  SST_ELI_ELEMENT_VERSION(1,0,0),
134  "XML model for building SST simulation graphs",
135  false)
136 
137  SST_ELI_DOCUMENT_MODEL_SUPPORTED_EXTENSIONS(".xml",".sdl")
138 
139 
140  SSTXmlModelDefinition(const std::string& script_file, int verbosity, Config* config, double start_time)
141  {
142  config->model_options = script_file;
143 
144  actual_model_ =
145  new SSTPythonModelDefinition(SST_INSTALL_PREFIX "/libexec/xmlToPython.py", verbosity, config, start_time);
146  }
147 
148  ConfigGraph* createConfigGraph() override { return actual_model_->createConfigGraph(); }
149 
150  virtual ~SSTXmlModelDefinition() {}
151 
152 private:
153  SSTPythonModelDefinition* actual_model_;
154 };
155 
156 std::map<std::string, std::string> generateStatisticParameters(PyObject* statParamDict);
157 SST::Params pythonToCppParams(PyObject* statParamDict);
158 PyObject* buildStatisticObject(StatisticId_t id);
159 PyObject*
160 buildEnabledStatistic(ConfigComponent* cc, const char* statName, PyObject* statParamDict, bool apply_to_children);
161 PyObject* buildEnabledStatistics(ConfigComponent* cc, PyObject* statList, PyObject* paramDict, bool apply_to_children);
162 
163 } // namespace Core
164 } // namespace SST
165 
166 #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:213
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:375
Definition: pymodel.h:126
std::string model_options
Definition: config.h:75
ConfigGraph * createConfigGraph() override
Create the ConfigGraph.
Definition: pymodel.h:148
Parameter store.
Definition: params.h:43
static void insert_global(const std::string &set, const key_type &key, const key_type &value, bool overwrite=true)
Adds a key/value pair to the specified global set.
Definition: params.cc:331
Base class for Model Generation.
Definition: sstmodel.h:25
Performs Unit math in full precision.
Definition: unitAlgebra.h:106
Definition: pymodel.h:41