SST  11.0.0
StructuralSimulationToolkit
configGraph.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_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_impl;
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 
128 public:
129  StatisticId_t id; /*!< Unique ID of this statistic */
130  Params params;
131  bool shared;
132  std::string name;
133 
134  ConfigStatistic(StatisticId_t _id, bool _shared = false, std::string _name = "")
135  : id(_id), shared(_shared), name(_name) {}
136 
137  ConfigStatistic() : id(stat_null_id) {}
138 
139  inline const StatisticId_t& getId() const { return id; }
140 
141  void addParameter(const std::string& key, const std::string& value, bool overwrite);
142 
143  void serialize_order(SST::Core::Serialization::serializer& ser) override {
144  ser & id;
145  ser & shared;
146  ser & name;
147  ser & params;
148  }
149 
150  ImplementSerializable(ConfigStatistic)
151 
152  static constexpr StatisticId_t stat_null_id = std::numeric_limits<StatisticId_t>::max();
153 };
154 
155 class ConfigStatGroup : public SST::Core::Serialization::serializable {
156 public:
157  std::string name;
158  std::map<std::string, Params> statMap;
159  std::vector<ComponentId_t> components;
160  size_t outputID;
161  UnitAlgebra outputFrequency;
162 
163  ConfigStatGroup(const std::string& name) : name(name), outputID(0) { }
164  ConfigStatGroup() {} /* Do not use */
165 
166 
167  bool addComponent(ComponentId_t id);
168  bool addStatistic(const std::string& name, Params &p);
169  bool setOutput(size_t id);
170  bool setFrequency(const std::string& freq);
171 
172  /**
173  * Checks to make sure that all components in the group support all
174  * of the statistics as configured in the group.
175  * @return pair of: bool for OK, string for error message (if any)
176  */
177  std::pair<bool, std::string> verifyStatsAndComponents(const ConfigGraph* graph);
178 
179 
180 
181  void serialize_order(SST::Core::Serialization::serializer &ser) override {
182  ser & name;
183  ser & statMap;
184  ser & components;
185  ser & outputID;
186  ser & outputFrequency;
187  }
188 
189  ImplementSerializable(SST::ConfigStatGroup)
190 
191 };
192 
193 
195 public:
196 
197  std::string type;
198  Params params;
199 
200  ConfigStatOutput(const std::string& type) : type(type) { }
201  ConfigStatOutput() { }
202 
203  void addParameter(const std::string& key, const std::string& val) {
204  params.insert(key, val);
205  }
206 
207  void serialize_order(SST::Core::Serialization::serializer &ser) override {
208  ser & type;
209  ser & params;
210  }
211 
212  ImplementSerializable(SST::ConfigStatOutput)
213 };
214 
215 
217 
218 /** Represents the configuration of a generic component */
220  friend class ComponentInfo;
221 
222 public:
223  ComponentId_t id; /*!< Unique ID of this component */
224  ConfigGraph* graph; /*!< Graph that this component belongs to */
225  std::string name; /*!< Name of this component, or slot name for subcomp */
226  int slot_num; /*!< Slot number. Only valid for subcomponents */
227  std::string type; /*!< Type of this component */
228  float weight; /*!< Partitioning weight for this component */
229  RankInfo rank; /*!< Parallel Rank for this component */
230  std::vector<LinkId_t> links; /*!< List of links connected */
231  Params params; /*!< Set of Parameters */
232  uint8_t statLoadLevel; /*!< Statistic load level for this component */
233  // std::vector<ConfigStatistic> enabledStatistics; /*!< List of subcomponents */
234 
235  std::map<std::string, StatisticId_t> enabledStatNames;
236  bool enabledAllStats;
237  ConfigStatistic allStatConfig;
238 
239  std::vector<ConfigComponent*> subComponents; /*!< List of subcomponents */
240  std::vector<double> coords;
241  uint16_t nextSubID; /*!< Next subID to use for children, if component, if subcomponent, subid of parent */
242  bool visited; /*! Used when traversing graph to indicate component was visited already */
243  uint16_t nextStatID; /*!< Next statID to use for children */
244 
245  static constexpr ComponentId_t null_id = std::numeric_limits<ComponentId_t>::max();
246 
247  inline const ComponentId_t& key() const { return id; }
248 
249  /** Print Component information */
250  void print(std::ostream &os) const;
251 
252  ConfigComponent* cloneWithoutLinks() const;
253  ConfigComponent* cloneWithoutLinksOrParams() const;
254 
255  ~ConfigComponent() {}
256  ConfigComponent()
257  : id(null_id), statLoadLevel(STATISTICLOADLEVELUNINITIALIZED), enabledAllStats(false), nextSubID(1),
258  visited(false) {
259  }
260 
261  ComponentId_t getNextSubComponentID();
262  StatisticId_t getNextStatisticID();
263 
264  ConfigComponent* getParent() const;
265  std::string getFullName() const;
266 
267  void setRank(RankInfo r);
268  void setWeight(double w);
269  void setCoordinates(const std::vector<double> &c);
270  void addParameter(const std::string& key, const std::string& value, bool overwrite);
271  ConfigComponent* addSubComponent(ComponentId_t, const std::string& name, const std::string& type, int slot);
272  ConfigComponent* findSubComponent(ComponentId_t);
273  const ConfigComponent* findSubComponent(ComponentId_t) const;
274  ConfigComponent* findSubComponentByName(const std::string& name);
275  ConfigStatistic* findStatistic(const std::string& name) const;
276  ConfigStatistic* insertStatistic(StatisticId_t id);
277  ConfigStatistic* findStatistic(StatisticId_t) const;
278  ConfigStatistic* enableStatistic(const std::string& statisticName, const SST::Params& params,
279  bool recursively = false);
280  ConfigStatistic* createStatistic();
281  bool reuseStatistic(const std::string& statisticName, StatisticId_t sid);
282  void addStatisticParameter(const std::string& statisticName, const std::string& param, const std::string& value, bool recursively = false);
283  void setStatisticParameters(const std::string& statisticName, const Params &params, bool recursively = false);
284  void setStatisticLoadLevel(uint8_t level, bool recursively = false);
285 
286  void addGlobalParamSet(const std::string& set) {
287  params.addGlobalParamSet(set); }
288 
289  std::vector<LinkId_t> allLinks() const;
290 
291  void serialize_order(SST::Core::Serialization::serializer &ser) override {
292  ser & id;
293  ser & name;
294  ser & slot_num;
295  ser & type;
296  ser & weight;
297  ser & rank.rank;
298  ser & rank.thread;
299  ser & links;
300  ser & params;
301  ser & enabledStatNames;
302  ser & enabledAllStats;
303  ser & statistics;
304  ser & enabledAllStats;
305  ser & statLoadLevel;
306  ser & subComponents;
307  ser & coords;
308  ser & nextSubID;
309  ser & nextStatID;
310  }
311 
312  ImplementSerializable(SST::ConfigComponent)
313 
314 private:
315  std::map<StatisticId_t, ConfigStatistic> statistics;
316 
317  friend class ConfigGraph;
318  /** Checks to make sure port names are valid and that a port isn't used twice
319  */
320  void checkPorts() const;
321 
322  /** Create a new Component */
323  ConfigComponent(ComponentId_t id, ConfigGraph* graph, const std::string& name, const std::string& type,
324  float weight, RankInfo rank)
325  : id(id), graph(graph), name(name), type(type), weight(weight), rank(rank),
326  statLoadLevel(STATISTICLOADLEVELUNINITIALIZED), enabledAllStats(false), nextSubID(1), nextStatID(1) {
327  coords.resize(3, 0.0);
328  }
329 
330  ConfigComponent(ComponentId_t id, ConfigGraph* graph, uint16_t parent_subid, const std::string& name, int slot_num,
331  const std::string& type, float weight, RankInfo rank)
332  : id(id), graph(graph), name(name), slot_num(slot_num), type(type), weight(weight), rank(rank),
333  statLoadLevel(STATISTICLOADLEVELUNINITIALIZED), enabledAllStats(false), nextSubID(parent_subid),
334  nextStatID(parent_subid) {
335  coords.resize(3, 0.0);
336  }
337 };
338 
339 
340 
341 /** Map names to Links */
342 // typedef std::map<std::string,ConfigLink> ConfigLinkMap_t;
343 // typedef SparseVectorMap<std::string,ConfigLink> ConfigLinkMap_t;
344 /** Map IDs to Components */
345 typedef SparseVectorMap<ComponentId_t,ConfigComponent*> ConfigComponentMap_t;
346 /** Map names to Components */
347 typedef std::map<std::string,ComponentId_t> ConfigComponentNameMap_t;
348 /** Map names to Parameter Sets: XML only */
349 typedef std::map<std::string,Params*> ParamsMap_t;
350 /** Map names to variable values: XML only */
351 typedef std::map<std::string,std::string> VariableMap_t;
352 
353 class PartitionGraph;
354 
355 /** A Configuration Graph
356  * A graph representing Components and Links
357  */
358 class ConfigGraph : public SST::Core::Serialization::serializable {
359 public:
360  /** Print the configuration graph */
361  void print(std::ostream &os) const {
362  os << "Printing graph" << std::endl;
363  for (ConfigComponentMap_t::const_iterator i = comps.begin() ; i != comps.end() ; ++i) {
364  (*i)->print(os);
365  }
366  }
367 
368  ConfigGraph() :
369  output(Output::getDefaultObject()),
370  nextComponentId(0)
371  {
372  links.clear();
373  comps.clear();
374  // Init the statistic output settings
375  statLoadLevel = STATISTICSDEFAULTLOADLEVEL;
376  statOutputs.emplace_back(STATISTICSDEFAULTOUTPUTNAME);
377  }
378 
379  size_t getNumComponents() { return comps.data.size(); }
380 
381  /** Helper function to set all the ranks to the same value */
382  void setComponentRanks(RankInfo rank);
383  /** Checks to see if rank contains at least one component */
384  bool containsComponentInRank(RankInfo rank);
385  /** Verify that all components have valid Ranks assigned */
386  bool checkRanks(RankInfo ranks);
387 
388 
389  // API for programmatic initialization
390  /** Create a new component with weight and rank */
391  ComponentId_t addComponent(const std::string& name, const std::string& type, float weight, RankInfo rank);
392  /** Create a new component */
393  ComponentId_t addComponent(const std::string& name, const std::string& type);
394 
395  /** Add a parameter to a global param set */
396  void addGlobalParam(const std::string& global_set, const std::string& key, const std::string& value);
397 
398  /** Set the statistic output module */
399  void setStatisticOutput(const std::string& name);
400 
401  /** Add parameter to the statistic output module */
402  void addStatisticOutputParameter(const std::string& param, const std::string& value);
403 
404  /** Set a set of parameter to the statistic output module */
405  void setStatisticOutputParams(const Params& p);
406 
407  /** Set the statistic system load level */
408  void setStatisticLoadLevel(uint8_t loadLevel);
409 
410  std::vector<ConfigStatOutput>& getStatOutputs() {return statOutputs;}
411 
412  const ConfigStatOutput& getStatOutput(size_t index = 0) const {return statOutputs[index];}
413 
414  long getStatLoadLevel() const {return statLoadLevel;}
415 
416  /** Add a Link to a Component on a given Port */
417  void addLink(ComponentId_t comp_id, const std::string& link_name, const std::string& port, const std::string& latency_str, bool no_cut = false);
418 
419  /** Set a Link to be no-cut */
420  void setLinkNoCut(const std::string& link_name);
421 
422  /** Perform any post-creation cleanup processes */
423  void postCreationCleanup();
424 
425  /** Check the graph for Structural errors */
426  bool checkForStructuralErrors();
427 
428  // Temporary until we have a better API
429  /** Return the map of components */
431  return comps;
432  }
433 
434  const std::map<std::string, ConfigStatGroup>& getStatGroups() const { return statGroups; }
435  ConfigStatGroup* getStatGroup(const std::string& name) {
436  auto found = statGroups.find(name);
437  if ( found == statGroups.end() ) {
438  bool ok;
439  std::tie(found, ok) = statGroups.emplace(name, name);
440  }
441  return &(found->second);
442  }
443 
444  bool containsComponent(ComponentId_t id) const;
445  ConfigComponent* findComponent(ComponentId_t);
446  ConfigComponent* findComponentByName(const std::string& name);
447  const ConfigComponent* findComponent(ComponentId_t) const;
448 
449  bool containsStatistic(StatisticId_t id) const;
450  ConfigStatistic* findStatistic(StatisticId_t) const;
451 
452  /** Return the map of links */
454  return links;
455  }
456 
457  ConfigGraph* getSubGraph(uint32_t start_rank, uint32_t end_rank);
458  ConfigGraph* getSubGraph(const std::set<uint32_t>& rank_set);
459 
460  PartitionGraph* getPartitionGraph();
461  PartitionGraph* getCollapsedPartitionGraph();
462  void annotateRanks(PartitionGraph* graph);
463  void getConnectedNoCutComps(ComponentId_t start, std::set<ComponentId_t>& group);
464 
465  void serialize_order(SST::Core::Serialization::serializer &ser) override
466  {
467  ser & links;
468  ser & comps;
469  ser & statOutputs;
470  ser & statLoadLevel;
471  ser & statGroups;
472  }
473 
474 private:
475  friend class Simulation_impl;
476  friend class SSTSDLModelDefinition;
477 
478  Output& output;
479 
480  ComponentId_t nextComponentId;
481 
482  ConfigLinkMap_t links; // SparseVectorMap
483  ConfigComponentMap_t comps; // SparseVectorMap
484  ConfigComponentNameMap_t compsByName; // std::map
485  std::map<std::string, ConfigStatGroup> statGroups;
486 
487  std::map<std::string,LinkId_t> link_names;
488 
489  std::vector<ConfigStatOutput> statOutputs; // [0] is default
490  uint8_t statLoadLevel;
491 
492  ImplementSerializable(SST::ConfigGraph)
493 
494 };
495 
496 
498 public:
499  ComponentId_t id;
500  float weight;
501  RankInfo rank;
502  LinkIdMap_t links;
503 
504  ComponentIdMap_t group;
505 
507  id = cc->id;
508  weight = cc->weight;
509  rank = cc->rank;
510  }
511 
512  PartitionComponent(LinkId_t id) :
513  id(id),
514  weight(0),
515  rank(RankInfo(RankInfo::UNASSIGNED, 0))
516  {}
517 
518  // PartitionComponent(ComponentId_t id, ConfigGraph* graph, const ComponentIdMap_t& group);
519  void print(std::ostream &os, const PartitionGraph* graph) const;
520 
521  inline ComponentId_t key() const { return id; }
522 
523 };
524 
526 public:
527  LinkId_t id;
528  ComponentId_t component[2];
529  SimTime_t latency[2];
530  bool no_cut;
531 
532  PartitionLink(const ConfigLink& cl) {
533  id = cl.id;
534  component[0] = cl.component[0];
535  component[1] = cl.component[1];
536  latency[0] = cl.latency[0];
537  latency[1] = cl.latency[1];
538  no_cut = cl.no_cut;
539  }
540 
541  inline LinkId_t key() const { return id; }
542 
543  /** Return the minimum latency of this link (from both sides) */
544  SimTime_t getMinLatency() const {
545  if ( latency[0] < latency[1] ) return latency[0];
546  return latency[1]; }
547 
548  /** Print the Link information */
549  void print(std::ostream &os) const {
550  os << " Link " << id << std::endl;
551  os << " component[0] = " << component[0] << std::endl;
552  os << " latency[0] = " << latency[0] << std::endl;
553  os << " component[1] = " << component[1] << std::endl;
554  os << " latency[1] = " << latency[1] << std::endl;
555  }
556 };
557 
558 typedef SparseVectorMap<ComponentId_t,PartitionComponent*> PartitionComponentMap_t;
559 typedef SparseVectorMap<LinkId_t,PartitionLink> PartitionLinkMap_t;
560 
562 private:
564  PartitionLinkMap_t links;
565 
566 public:
567  /** Print the configuration graph */
568  void print(std::ostream &os) const {
569  os << "Printing graph" << std::endl;
570  for (PartitionComponentMap_t::const_iterator i = comps.begin() ; i != comps.end() ; ++i) {
571  (*i)->print(os,this);
572  }
573  }
574 
575  PartitionComponentMap_t& getComponentMap() {
576  return comps;
577  }
578  PartitionLinkMap_t& getLinkMap() {
579  return links;
580  }
581 
582  const PartitionLink& getLink(LinkId_t id) const {
583  return links[id];
584  }
585 
586  size_t getNumComponents() { return comps.size(); }
587 
588 };
589 
590 } // namespace SST
591 
592 #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:226
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:228
StatisticId_t id
Definition: configGraph.h:129
ConfigComponentMap_t & getComponentMap()
Return the map of components.
Definition: configGraph.h:430
Represents the configuration of a generic component.
Definition: configGraph.h:219
Definition: configGraph.h:561
std::string type
Definition: configGraph.h:227
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:358
void print(std::ostream &os) const
Print the configuration graph.
Definition: configGraph.h:568
Definition: configGraph.h:127
Definition: configGraph.h:497
RankInfo rank
Definition: configGraph.h:229
uint16_t nextSubID
Definition: configGraph.h:241
uint8_t statLoadLevel
Definition: configGraph.h:232
Definition: serializable.h:109
size_t size()
Returns the number of items in the SparseVectorMap.
Definition: sparseVectorMap.h:265
ConfigLinkMap_t & getLinkMap()
Return the map of links.
Definition: configGraph.h:453
ComponentId_t id
Definition: configGraph.h:223
Definition: rankInfo.h:21
Params params
Definition: configGraph.h:231
void print(std::ostream &os) const
Print the configuration graph.
Definition: configGraph.h:361
uint16_t nextStatID
Definition: configGraph.h:243
std::vector< LinkId_t > links
Definition: configGraph.h:230
Parameter store.
Definition: params.h:44
Definition: configGraph.h:194
std::string name
Definition: configGraph.h:225
std::vector< ConfigComponent * > subComponents
Definition: configGraph.h:239
Definition: configGraph.h:155
Definition: componentInfo.h:40
ConfigGraph * graph
Definition: configGraph.h:224
Performs Unit math in full precision.
Definition: unitAlgebra.h:107