The Sparta Modeling Framework
Loading...
Searching...
No Matches
RootArchiveNode.hpp
1// <RootArchiveNode> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/statistics/dispatch/archives/ArchiveNode.hpp"
7
8#include <boost/serialization/map.hpp>
9#include <boost/serialization/vector.hpp>
10
11namespace sparta {
12namespace statistics {
13
14class ArchiveNode;
15class ArchiveController;
16
35{
36public:
37 RootArchiveNode() = default;
38
39 explicit RootArchiveNode(const std::string & name) :
40 ArchiveNode(name)
41 {}
42
43 void initialize() {
44 size_t num_leaves = 0;
45 recursGetNumLeafChildren_(*this, num_leaves);
46 total_num_leaves_ = num_leaves;
47 }
48
50 void setMetadata(const app::NamedExtensions & metadata) {
51 metadata_ = metadata;
52 }
53
54 template <typename MetadataT>
55 void setMetadataValue(const std::string & name, const MetadataT & value) {
56 metadata_[name] = boost::any(value);
57 }
58
59 template <typename MetadataT>
60 const MetadataT & getMetadataValue(const std::string & name) const {
61 auto iter = metadata_.find(name);
62 if (iter == metadata_.end()) {
63 throw SpartaException("Metadata does not exist: ") << name;
64 }
65 try {
66 return boost::any_cast<const MetadataT &>(iter->second);
67 } catch (const boost::bad_any_cast &) {
68 throw SpartaException("Metadata named '")
69 << name << "' does exist, but is not of type '"
70 << typeid(MetadataT).name() << "'";
71 }
72 }
73
74 template <typename MetadataT>
75 const MetadataT * tryGetMetadataValue(const std::string & name) const {
76 auto iter = metadata_.find(name);
77 if (iter == metadata_.end()) {
78 return nullptr;
79 }
80 try {
81 return &boost::any_cast<const MetadataT &>(iter->second);
82 } catch (const boost::bad_any_cast &) {
83 return nullptr;
84 }
85 }
86
87 size_t getTotalNumLeaves() const {
88 return total_num_leaves_;
89 }
90
96 void setArchiveController(const std::shared_ptr<ArchiveController> & controller) {
97 archive_controller_ = controller;
98 }
99 void saveTo(const std::string & dir);
100 bool synchronize();
101
102private:
103 void recursGetNumLeafChildren_(
104 const RootArchiveNode & node, size_t & num_leaves) const
105 {
106 if (node.getChildren().empty()) {
107 ++num_leaves;
108 } else {
109 for (const auto & child : node.getChildren()) {
110 num_leaves += child->getTotalNumLeaves();
111 }
112 }
113 }
114
118 template <class Archive>
119 void serialize(Archive & ar, const unsigned int) {
120 //Keep in mind that this one method is called for both
121 //serialization to disk, and deserialization from disk.
122
123 //Read/write node name
124 ar & name_;
125
126 //Read/write children vector
127 ar & children_;
128
129 //Read/write the total_num_leaves_ value. This serialize()
130 //method is defined at the bottom of ArchiveNode.h
131 ar & total_num_leaves_;
132
133 //Serialize trigger metadata
134 if (!metadata_.empty()) {
135 //Boost has called this serialize() method, and since
136 //our metadata map is not empty, that means we are now
137 //*writing* to disk. This map is a bunch of boost::any's
138 //which cannot be serialized as easily as a std::string
139 //or size_t, etc. We will have to pick it apart into a
140 //std::map<std::string, std::string> and just serialize
141 //that data structure to get around it.
142 //
143 //Note that we are using a map and not an unordered_map
144 //because older Boost versions do not support serialization
145 //of unordered_map. This map is never larger than 4 or 5
146 //items, so performance is not really different either way.
147 auto iter = metadata_.find("trigger");
148 sparta_assert(iter != metadata_.end());
149 const app::TriggerKeyValues & source_kvs =
150 boost::any_cast<app::TriggerKeyValues&>(iter->second);
151
152 std::map<std::string, std::string> dest_kvs;
153 for (const auto & kv : source_kvs) {
154 dest_kvs[kv.first] = kv.second;
155 }
156
157 //Write the map<string,string> to disk
158 ar & dest_kvs;
159 } else {
160 //Read the trigger information from disk, and store
161 //it in our metadata. This map<string,string> is the
162 //same data structure we used when we serialized this
163 //node to disk.
164 std::map<std::string, std::string> source_kvs;
165 ar & source_kvs;
166
167 //The metadata variable's data structure is an unordered_map,
168 //however, so we have to convert the key-value pairs accordingly.
169 //Note that if SPARTA and all downstream users/repos start using a
170 //new enough Boost version, this could be switched to serialize
171 //unordered_map<string,string> directly.
172 //
173 // --> Boost v1.56 and above required <--
174 //
175 //--------------------------------------------------------------
176 app::TriggerKeyValues dest_kvs;
177 for (const auto & kv : source_kvs) {
178 dest_kvs[kv.first] = kv.second;
179 }
180 setMetadataValue("trigger", dest_kvs);
181 }
182 }
183
184 std::shared_ptr<ArchiveController> archive_controller_;
185 utils::ValidValue<size_t> total_num_leaves_;
186 app::NamedExtensions metadata_;
187};
188
189} // namespace statistics
190} // namespace sparta
191
Describes reports to instantiate and tracks their instantiations.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
When a simulation is configured to archive its statistics values, it will build a subset of its devic...
std::vector< std::shared_ptr< ArchiveNode > > children_
There is one of these root node objects at the top of each report's archive. The hierarchy looks like...
void setMetadata(const app::NamedExtensions &metadata)
Metadata.
friend class boost::serialization::access
void setArchiveController(const std::shared_ptr< ArchiveController > &controller)
Macros for handling exponential backoff.