SST  10.1.0
StructuralSimulationToolkit
configGraph.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2020 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-2020, 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_CONFIGGRAPH_H
15 #define SST_CORE_CONFIGGRAPH_H
16 
17 #include "sst/core/sst_types.h"
18 
19 #include <vector>
20 #include <map>
21 #include <set>
22 #include <climits>
23 
24 #include "sst/core/sparseVectorMap.h"
25 #include "sst/core/params.h"
26 #include "sst/core/statapi/statbase.h"
27 #include "sst/core/statapi/statoutput.h"
28 #include "sst/core/rankInfo.h"
29 #include "sst/core/unitAlgebra.h"
30 
31 #include "sst/core/serialization/serializable.h"
32 
33 using namespace SST::Statistics;
34 
35 namespace SST {
36 
37 
38 class Simulation;
39 
40 class Config;
41 class TimeLord;
42 class ConfigGraph;
43 
44 typedef SparseVectorMap<ComponentId_t> ComponentIdMap_t;
45 typedef std::vector<LinkId_t> LinkIdMap_t;
46 
47 
48 /** Represents the configuration of a generic Link */
50 public:
51  LinkId_t id; /*!< ID of this link */
52  std::string name; /*!< Name of this link */
53  ComponentId_t component[2]; /*!< IDs of the connected components */
54  std::string port[2]; /*!< Names of the connected ports */
55  SimTime_t latency[2]; /*!< Latency from each side */
56  std::string latency_str[2];/*!< Temp string holding latency */
57  int current_ref; /*!< Number of components currently referring to this Link */
58  bool no_cut; /*!< If set to true, partitioner will not make a cut through this Link */
59 
60  // inline const std::string& key() const { return name; }
61  inline LinkId_t key() const { return id; }
62 
63  /** Return the minimum latency of this link (from both sides) */
64  SimTime_t getMinLatency() const {
65  if ( latency[0] < latency[1] ) return latency[0];
66  return latency[1];
67  }
68 
69  /** Print the Link information */
70  void print(std::ostream &os) const {
71  os << "Link " << name << " (id = " << id << ")" << std::endl;
72  os << " component[0] = " << component[0] << std::endl;
73  os << " port[0] = " << port[0] << std::endl;
74  os << " latency[0] = " << latency[0] << std::endl;
75  os << " component[1] = " << component[1] << std::endl;
76  os << " port[1] = " << port[1] << std::endl;
77  os << " latency[1] = " << latency[1] << std::endl;
78  }
79 
80  /* Do not use. For serialization only */
81  ConfigLink() {}
82 
83  void serialize_order(SST::Core::Serialization::serializer &ser) override {
84  ser & id;
85  ser & name;
86  ser & component[0];
87  ser & component[1];
88  ser & port[0];
89  ser & port[1];
90  ser & latency[0];
91  ser & latency[1];
92  ser & current_ref;
93  }
94 
95  ImplementSerializable(SST::ConfigLink)
96 
97 private:
98  friend class ConfigGraph;
99  ConfigLink(LinkId_t id) :
100  id(id),
101  no_cut(false)
102  {
103  current_ref = 0;
104 
105  // Initialize the component data items
106  component[0] = ULONG_MAX;
107  component[1] = ULONG_MAX;
108  }
109 
110  ConfigLink(LinkId_t id, const std::string& n) :
111  id(id),
112  no_cut(false)
113  {
114  current_ref = 0;
115  name = n;
116 
117  // Initialize the component data items
118  component[0] = ULONG_MAX;
119  component[1] = ULONG_MAX;
120  }
121 
122  void updateLatencies(TimeLord*);
123 
124 
125 };
126 
127 
128 
130 public:
131  std::string name;
132  std::map<std::string, Params> statMap;
133  std::vector<ComponentId_t> components;
134  size_t outputID;
135  UnitAlgebra outputFrequency;
136 
137  ConfigStatGroup(const std::string& name) : name(name), outputID(0) { }
138  ConfigStatGroup() {} /* Do not use */
139 
140 
141  bool addComponent(ComponentId_t id);
142  bool addStatistic(const std::string& name, Params &p);
143  bool setOutput(size_t id);
144  bool setFrequency(const std::string& freq);
145 
146  /**
147  * Checks to make sure that all components in the group support all
148  * of the statistics as configured in the group.
149  * @return pair of: bool for OK, string for error message (if any)
150  */
151  std::pair<bool, std::string> verifyStatsAndComponents(const ConfigGraph* graph);
152 
153 
154 
155  void serialize_order(SST::Core::Serialization::serializer &ser) override {
156  ser & name;
157  ser & statMap;
158  ser & components;
159  ser & outputID;
160  ser & outputFrequency;
161  }
162 
163  ImplementSerializable(SST::ConfigStatGroup)
164 
165 };
166 
167 
169 public:
170 
171  std::string type;
172  Params params;
173 
174  ConfigStatOutput(const std::string& type) : type(type) { }
175  ConfigStatOutput() { }
176 
177  void addParameter(const std::string& key, const std::string& val) {
178  params.insert(key, val);
179  }
180 
181  void serialize_order(SST::Core::Serialization::serializer &ser) override {
182  ser & type;
183  ser & params;
184  }
185 
186  ImplementSerializable(SST::ConfigStatOutput)
187 };
188 
189 
191 
192 /** Represents the configuration of a generic component */
194 public:
195  ComponentId_t id; /*!< Unique ID of this component */
196  ConfigGraph* graph; /*!< Graph that this component belongs to */
197  std::string name; /*!< Name of this component, or slot name for subcomp */
198  int slot_num; /*!< Slot number. Only valid for subcomponents */
199  std::string type; /*!< Type of this component */
200  float weight; /*!< Partitioning weight for this component */
201  RankInfo rank; /*!< Parallel Rank for this component */
202  std::vector<LinkId_t> links; /*!< List of links connected */
203  Params params; /*!< Set of Parameters */
204  uint8_t statLoadLevel; /*!< Statistic load level for this component */
205  std::vector<Statistics::StatisticInfo> enabledStatistics; /*!< List of statistics to be enabled */
206  std::vector<ConfigComponent> subComponents; /*!< List of subcomponents */
207  std::vector<double> coords;
208  uint16_t nextSubID; /*!< Next subID to use for children, if component, if subcomponent, subid of parent */
209  bool visited; /*! Used when traversing graph to indicate component was visited already */
210 
211  static constexpr ComponentId_t null_id = std::numeric_limits<ComponentId_t>::max();
212 
213  inline const ComponentId_t& key()const { return id; }
214 
215  /** Print Component information */
216  void print(std::ostream &os) const;
217 
218  ConfigComponent cloneWithoutLinks() const;
219  ConfigComponent cloneWithoutLinksOrParams() const;
220 
221  ~ConfigComponent() {}
222  ConfigComponent() : id(null_id), statLoadLevel(STATISTICLOADLEVELUNINITIALIZED), nextSubID(1), visited(false) { }
223 
224  ComponentId_t getNextSubComponentID();
225 
226  ConfigComponent* getParent() const;
227  std::string getFullName() const;
228 
229 
230  void setRank(RankInfo r);
231  void setWeight(double w);
232  void setCoordinates(const std::vector<double> &c);
233  void addParameter(const std::string& key, const std::string& value, bool overwrite);
234  ConfigComponent* addSubComponent(ComponentId_t, const std::string& name, const std::string& type, int slot);
235  ConfigComponent* findSubComponent(ComponentId_t);
236  const ConfigComponent* findSubComponent(ComponentId_t) const;
237  ConfigComponent* findSubComponentByName(const std::string& name);
238  void enableStatistic(const std::string& statisticName, bool recursively = false);
239  void addStatisticParameter(const std::string& statisticName, const std::string& param, const std::string& value, bool recursively = false);
240  void setStatisticParameters(const std::string& statisticName, const Params &params, bool recursively = false);
241  void setStatisticLoadLevel(uint8_t level, bool recursively = false);
242 
243  std::vector<LinkId_t> allLinks() const;
244 
245  void serialize_order(SST::Core::Serialization::serializer &ser) override {
246  ser & id;
247  ser & name;
248  ser & slot_num;
249  ser & type;
250  ser & weight;
251  ser & rank.rank;
252  ser & rank.thread;
253  ser & links;
254  ser & params;
255  ser & statLoadLevel;
256  ser & enabledStatistics;
257  ser & subComponents;
258  ser & coords;
259  ser & nextSubID;
260  }
261 
262  ImplementSerializable(SST::ConfigComponent)
263 
264 private:
265 
266  friend class ConfigGraph;
267  /** Checks to make sure port names are valid and that a port isn't used twice
268  */
269  void checkPorts() const;
270 
271  /** Create a new Component */
272  ConfigComponent(ComponentId_t id, ConfigGraph* graph, const std::string& name, const std::string& type, float weight, RankInfo rank) :
273  id(id),
274  graph(graph),
275  name(name),
276  type(type),
277  weight(weight),
278  rank(rank),
279  statLoadLevel(STATISTICLOADLEVELUNINITIALIZED),
280  nextSubID(1)
281  {
282  coords.resize(3, 0.0);
283  }
284 
285  ConfigComponent(ComponentId_t id, ConfigGraph* graph, uint16_t parent_subid, const std::string& name, int slot_num, const std::string& type, float weight, RankInfo rank) :
286  id(id),
287  graph(graph),
288  name(name),
289  slot_num(slot_num),
290  type(type),
291  weight(weight),
292  rank(rank),
293  statLoadLevel(STATISTICLOADLEVELUNINITIALIZED),
294  nextSubID(parent_subid)
295  {
296  coords.resize(3, 0.0);
297  }
298 
299 
300 };
301 
302 
303 
304 /** Map names to Links */
305 // typedef std::map<std::string,ConfigLink> ConfigLinkMap_t;
306 // typedef SparseVectorMap<std::string,ConfigLink> ConfigLinkMap_t;
307 /** Map IDs to Components */
308 typedef SparseVectorMap<ComponentId_t,ConfigComponent> ConfigComponentMap_t;
309 /** Map names to Components */
310 typedef std::map<std::string,ComponentId_t> ConfigComponentNameMap_t;
311 /** Map names to Parameter Sets: XML only */
312 typedef std::map<std::string,Params*> ParamsMap_t;
313 /** Map names to variable values: XML only */
314 typedef std::map<std::string,std::string> VariableMap_t;
315 
316 class PartitionGraph;
317 
318 /** A Configuration Graph
319  * A graph representing Components and Links
320  */
321 class ConfigGraph : public SST::Core::Serialization::serializable {
322 public:
323  /** Print the configuration graph */
324  void print(std::ostream &os) const {
325  os << "Printing graph" << std::endl;
326  for (ConfigComponentMap_t::const_iterator i = comps.begin() ; i != comps.end() ; ++i) {
327  i->print(os);
328  }
329  }
330 
331  ConfigGraph() :
332  output(Output::getDefaultObject()),
333  nextComponentId(0)
334  {
335  links.clear();
336  comps.clear();
337  // Init the statistic output settings
338  statLoadLevel = STATISTICSDEFAULTLOADLEVEL;
339  statOutputs.emplace_back(STATISTICSDEFAULTOUTPUTNAME);
340  }
341 
342  size_t getNumComponents() { return comps.data.size(); }
343 
344  /** Helper function to set all the ranks to the same value */
345  void setComponentRanks(RankInfo rank);
346  /** Checks to see if rank contains at least one component */
347  bool containsComponentInRank(RankInfo rank);
348  /** Verify that all components have valid Ranks assigned */
349  bool checkRanks(RankInfo ranks);
350 
351 
352  // API for programmatic initialization
353  /** Create a new component with weight and rank */
354  ComponentId_t addComponent(const std::string& name, const std::string& type, float weight, RankInfo rank);
355  /** Create a new component */
356  ComponentId_t addComponent(const std::string& name, const std::string& type);
357 
358 
359  /** Set the statistic output module */
360  void setStatisticOutput(const std::string& name);
361 
362  /** Add parameter to the statistic output module */
363  void addStatisticOutputParameter(const std::string& param, const std::string& value);
364 
365  /** Set a set of parameter to the statistic output module */
366  void setStatisticOutputParams(const Params& p);
367 
368  /** Set the statistic system load level */
369  void setStatisticLoadLevel(uint8_t loadLevel);
370 
371  /** Enable a Statistics assigned to a component */
372  void enableStatisticForComponentName(const std::string& ComponentName, const std::string& statisticName, bool recursive = false);
373  void enableStatisticForComponentType(const std::string& ComponentType, const std::string& statisticName, bool recursive = false);
374 
375  /** Add Parameters for a Statistic */
376  void addStatisticParameterForComponentName(const std::string& ComponentName, const std::string& statisticName, const std::string& param, const std::string& value, bool recursively);
377  void addStatisticParameterForComponentType(const std::string& ComponentType, const std::string& statisticName, const std::string& param, const std::string& value, bool recursively);
378  void setStatisticLoadLevelForComponentType(const std::string& compType, uint8_t level, bool recursively = false);
379 
380  std::vector<ConfigStatOutput>& getStatOutputs() {return statOutputs;}
381  const ConfigStatOutput& getStatOutput(size_t index = 0) const {return statOutputs[index];}
382  long getStatLoadLevel() const {return statLoadLevel;}
383 
384  /** Add a Link to a Component on a given Port */
385  void addLink(ComponentId_t comp_id, const std::string& link_name, const std::string& port, const std::string& latency_str, bool no_cut = false);
386 
387  /** Set a Link to be no-cut */
388  void setLinkNoCut(const std::string& link_name);
389 
390  /** Perform any post-creation cleanup processes */
391  void postCreationCleanup();
392 
393  /** Check the graph for Structural errors */
394  bool checkForStructuralErrors();
395 
396  // Temporary until we have a better API
397  /** Return the map of components */
399  return comps;
400  }
401 
402  const std::map<std::string, ConfigStatGroup>& getStatGroups() const { return statGroups; }
403  ConfigStatGroup* getStatGroup(const std::string& name) {
404  auto found = statGroups.find(name);
405  if ( found == statGroups.end() ) {
406  bool ok;
407  std::tie(found, ok) = statGroups.emplace(name, name);
408  }
409  return &(found->second);
410  }
411 
412  bool containsComponent(ComponentId_t id) const;
413  ConfigComponent* findComponent(ComponentId_t);
414  ConfigComponent* findComponentByName(const std::string& name);
415  const ConfigComponent* findComponent(ComponentId_t) const;
416 
417  /** Return the map of links */
419  return links;
420  }
421 
422  ConfigGraph* getSubGraph(uint32_t start_rank, uint32_t end_rank);
423  ConfigGraph* getSubGraph(const std::set<uint32_t>& rank_set);
424 
425  PartitionGraph* getPartitionGraph();
426  PartitionGraph* getCollapsedPartitionGraph();
427  void annotateRanks(PartitionGraph* graph);
428  void getConnectedNoCutComps(ComponentId_t start, ComponentIdMap_t& group);
429 
430  void serialize_order(SST::Core::Serialization::serializer &ser) override
431  {
432  ser & links;
433  ser & comps;
434  ser & statOutputs;
435  ser & statLoadLevel;
436  ser & statGroups;
437  }
438 
439 private:
440  friend class Simulation;
441  friend class SSTSDLModelDefinition;
442 
443  Output& output;
444 
445  ComponentId_t nextComponentId;
446 
447  ConfigLinkMap_t links;
448  ConfigComponentMap_t comps;
449  ConfigComponentNameMap_t compsByName;
450  std::map<std::string, ConfigStatGroup> statGroups;
451 
452  std::map<std::string,LinkId_t> link_names;
453 
454  std::vector<ConfigStatOutput> statOutputs; // [0] is default
455  uint8_t statLoadLevel;
456 
457  ImplementSerializable(SST::ConfigGraph)
458 
459 };
460 
461 
463 public:
464  ComponentId_t id;
465  float weight;
466  RankInfo rank;
467  LinkIdMap_t links;
468 
469  ComponentIdMap_t group;
470 
472  id = cc.id;
473  weight = cc.weight;
474  rank = cc.rank;
475  }
476 
477  PartitionComponent(LinkId_t id) :
478  id(id),
479  weight(0),
480  rank(RankInfo(RankInfo::UNASSIGNED, 0))
481  {}
482 
483  // PartitionComponent(ComponentId_t id, ConfigGraph* graph, const ComponentIdMap_t& group);
484  void print(std::ostream &os, const PartitionGraph* graph) const;
485 
486  inline ComponentId_t key() const { return id; }
487 
488 };
489 
491 public:
492  LinkId_t id;
493  ComponentId_t component[2];
494  SimTime_t latency[2];
495  bool no_cut;
496 
497  PartitionLink(const ConfigLink& cl) {
498  id = cl.id;
499  component[0] = cl.component[0];
500  component[1] = cl.component[1];
501  latency[0] = cl.latency[0];
502  latency[1] = cl.latency[1];
503  no_cut = cl.no_cut;
504  }
505 
506  inline LinkId_t key() const { return id; }
507 
508  /** Return the minimum latency of this link (from both sides) */
509  SimTime_t getMinLatency() const {
510  if ( latency[0] < latency[1] ) return latency[0];
511  return latency[1]; }
512 
513  /** Print the Link information */
514  void print(std::ostream &os) const {
515  os << " Link " << id << std::endl;
516  os << " component[0] = " << component[0] << std::endl;
517  os << " latency[0] = " << latency[0] << std::endl;
518  os << " component[1] = " << component[1] << std::endl;
519  os << " latency[1] = " << latency[1] << std::endl;
520  }
521 };
522 
523 typedef SparseVectorMap<ComponentId_t,PartitionComponent> PartitionComponentMap_t;
524 typedef SparseVectorMap<LinkId_t,PartitionLink> PartitionLinkMap_t;
525 
527 private:
529  PartitionLinkMap_t links;
530 
531 public:
532  /** Print the configuration graph */
533  void print(std::ostream &os) const {
534  os << "Printing graph" << std::endl;
535  for (PartitionComponentMap_t::const_iterator i = comps.begin() ; i != comps.end() ; ++i) {
536  i->print(os,this);
537  }
538  }
539 
540  PartitionComponentMap_t& getComponentMap() {
541  return comps;
542  }
543  PartitionLinkMap_t& getLinkMap() {
544  return links;
545  }
546 
547  const PartitionLink& getLink(LinkId_t id) const {
548  return links[id];
549  }
550 
551  size_t getNumComponents() { return comps.size(); }
552 
553 };
554 
555 } // namespace SST
556 
557 #endif // SST_CORE_CONFIGGRAPH_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:54
int slot_num
Definition: configGraph.h:198
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
float weight
Definition: configGraph.h:200
ConfigComponentMap_t & getComponentMap()
Return the map of components.
Definition: configGraph.h:398
Represents the configuration of a generic component.
Definition: configGraph.h:193
Definition: configGraph.h:526
std::string type
Definition: configGraph.h:199
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:321
void print(std::ostream &os) const
Print the configuration graph.
Definition: configGraph.h:533
std::vector< Statistics::StatisticInfo > enabledStatistics
Definition: configGraph.h:205
Definition: configGraph.h:462
RankInfo rank
Definition: configGraph.h:201
uint16_t nextSubID
Definition: configGraph.h:208
uint8_t statLoadLevel
Definition: configGraph.h:204
Definition: serializable.h:109
ConfigLinkMap_t & getLinkMap()
Return the map of links.
Definition: configGraph.h:418
ComponentId_t id
Definition: configGraph.h:195
Definition: rankInfo.h:21
Params params
Definition: configGraph.h:203
void print(std::ostream &os) const
Print the configuration graph.
Definition: configGraph.h:324
std::vector< LinkId_t > links
Definition: configGraph.h:202
Parameter store.
Definition: params.h:44
Definition: configGraph.h:168
std::string name
Definition: configGraph.h:197
Definition: configGraph.h:129
ConfigGraph * graph
Definition: configGraph.h:196
std::vector< ConfigComponent > subComponents
Definition: configGraph.h:206
Performs Unit math in full precision.
Definition: unitAlgebra.h:107