The Sparta Modeling Framework
Loading...
Searching...
No Matches
ExpressionNodeVariables.hpp
Go to the documentation of this file.
1// <ExpressionNodeVariables> -*- C++ -*-
2
9#pragma once
10
11#include <memory>
12
13#include "sparta/statistics/ExpressionNode.hpp"
15
16namespace sparta {
17
18class StatInstCalculator;
19
20 namespace statistics {
21 namespace expression {
22
24{
29
33 StatVariable() = delete;
34
42 StatVariable(const TreeNode* n, std::vector<const TreeNode*>& used) :
43 stat_(n, used)
44 {
45 //std::cout << "StatVariable at " << this << " with stat " << &stat_ << std::endl;
46 }
47
57 StatVariable(std::shared_ptr<StatInstCalculator> & calculator,
58 std::vector<const TreeNode*>& used) :
59 stat_(calculator, used)
60 { }
61
62 StatVariable(const StatVariable& rhp) :
64 stat_(rhp.stat_)
65 { }
66
67 virtual ~StatVariable() {
68 //std::cout << "~StatVariable at " << this << " with stat " << &stat_ << std::endl;
69 }
70
71 virtual StatVariable* clone_() const override {
72 return new StatVariable(*this);
73 }
74
75 virtual double evaluate_() const override {
76 return stat_.getValue();
77 }
78
79 virtual bool supportsCompression() const override {
81 }
82
93
94 virtual void start() override {
95 stat_.start();
96 }
97 virtual void end() override {
98 stat_.end();
99 }
100
101 virtual void dump(std::ostream& o,
102 bool show_range=true,
103 bool resolve_subexprs=true) const override {
104 o << stat_.getExpressionString(show_range, resolve_subexprs);
105 }
106
107 virtual void getClocks(std::vector<const Clock*>& clocks) const override {
108 stat_.getClocks(clocks);
109 }
110
111private:
112
113 virtual uint32_t getStats_(std::vector<const StatisticInstance*>& results) const override {
114 results.push_back(&stat_);
115 return 1;
116 }
117}; // class StatVariable
118
126{
130 typedef double (*getter_t)();
131
135 std::string which_;
136
141
142 SimVariable() = delete;
143
144 SimVariable(const SimVariable& rhp) :
146 which_(rhp.which_),
147 getter_(rhp.getter_)
148 { }
149
155 SimVariable(const std::string& which, const getter_t getter) :
156 which_(which),
157 getter_(getter)
158 { }
159
160 virtual SimVariable* clone_() const override {
161 return new SimVariable(*this);
162 }
163
164 virtual double evaluate_() const override {
165 // Look up the value
166 return getter_();
167 }
168
173 virtual bool supportsCompression() const override {
174 return false;
175 }
176
177 virtual void start() override {
178 // No action on start
179 }
180
181 virtual void end() override {
182 // No action on end
183 }
184
185 virtual void dump(std::ostream& o,
186 bool show_range=true,
187 bool resolve_subexprs=true) const override {
188 (void) show_range;
189 (void) resolve_subexprs;
190 o << "{simvar " << which_ << "}";
191 }
192
193 virtual void getClocks(std::vector<const Clock*>& clocks) const override {
194 (void) clocks;
195 // No clocks in a SimVariable
196 }
197
198private:
199
200 virtual uint32_t getStats_(std::vector<const StatisticInstance*>& results) const override {
201 (void) results;
202 // No stats used
203 return 0;
204 }
205}; // class SimVariable
206
216{
220 std::string which_;
221
225 const double& ref_;
226
227 ReferenceVariable() = delete;
228
231 which_(rhp.which_),
232 ref_(rhp.ref_)
233 { }
234
240 template <typename T>
241 ReferenceVariable(const std::string& which,
242 const T& ref) :
243 which_(which),
244 ref_(ref)
245 {
246 // C++ was allowing implicit casting from uint64_t& to double& which
247 // compiles but produces incorrect results. This assertion exists to
248 // catch misuses.
249 static_assert(std::is_same<T,double>::value,
250 "ReferenceVariable must be constructed with a double&. Someone called this "
251 "method with a non-double argument");
252 }
253
258
259 virtual ReferenceVariable* clone_() const override {
260 return new ReferenceVariable(*this);
261 }
262
263 virtual double evaluate_() const override {
264 // Read the value by refrence
265 return ref_;
266 }
267
271 virtual bool supportsCompression() const override {
272 return false;
273 }
274
275 virtual void start() override {
276 // No action on start
277 }
278
279 virtual void end() override {
280 // No action on end
281 }
282
283 virtual void dump(std::ostream& o,
284 bool show_range=true,
285 bool resolve_subexprs=true) const override {
286 (void) show_range;
287 (void) resolve_subexprs;
288 o << "{" << which_ << ": " << ref_ << "}";
289 }
290
291 virtual void getClocks(std::vector<const Clock*>& clocks) const override {
292 (void) clocks;
293 // No clocks in a ReferenceVariable
294 }
295
296private:
297
298 virtual uint32_t getStats_(std::vector<const StatisticInstance*>& results) const override {
299 (void) results;
300 // No stats used
301 return 0;
302 }
303}; // class ReferenceVariable
304
305
306 } // namespace expression
307 } // namespace statistics
308} // namespace sparta
Contains a StatisticInstance which refers to a StatisticDef or Counter and some local state to comput...
Instance of either a StatisticDef or CounterBase or an Expression. Has a sample window (simulator tic...
std::string getExpressionString(bool show_range=true, bool resolve_subexprs=true) const
Returns a string containing the expression that this statistic will evaluate.
void start()
Start the computation window for this instance.
void getClocks(std::vector< const Clock * > &clocks) const
Gets all clocks associated with this Statistic instance (if any) whether it points to a StatisticDef,...
void end()
Ends the window for this instance. Computes and caches the result of the statistic.
double getValue() const
Returns the value computed for this statistic instance at the current time.
bool supportsCompression() const
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
Abstract interface class for an item in an expression. Subclasses can contain other ExpressionNodes.
Macros for handling exponential backoff.
Expression node for a reference to a double. Anything that cannot actually vary can be handled as a c...
virtual void dump(std::ostream &o, bool show_range=true, bool resolve_subexprs=true) const override
Dump the content of this expression item.
virtual void getClocks(std::vector< const Clock * > &clocks) const override
Populates a vector with the clocks found in this subexpression node.
virtual double evaluate_() const override
Compute value of this item in simulation.
ReferenceVariable(const std::string &which, const T &ref)
Construct with a value reference.
const double & ref_
Reference to actual variable.
virtual ReferenceVariable * clone_() const override
Deep copy of the content of this item.
ReferenceVariable operator=(const ReferenceVariable &)=delete
Copy-assignment Disallowed.
Expression node for a simulation variable. Anything that cannot actually vary within a simulation can...
getter_t getter_
Function for retrieving the value of this variable.
virtual double evaluate_() const override
Compute value of this item in simulation.
virtual void getClocks(std::vector< const Clock * > &clocks) const override
Populates a vector with the clocks found in this subexpression node.
double(* getter_t)()
Typedef of getter function used to retrieve a variable.
SimVariable(const std::string &which, const getter_t getter)
Construct with a getter function.
virtual SimVariable * clone_() const override
Deep copy of the content of this item.
virtual void dump(std::ostream &o, bool show_range=true, bool resolve_subexprs=true) const override
Dump the content of this expression item.
sparta::StatisticInstance stat_
Contained statistic.
StatVariable(std::shared_ptr< StatInstCalculator > &calculator, std::vector< const TreeNode * > &used)
Construct from a given StatInstCalculator (wrapper class around a SpartaHandler).
virtual double evaluate_() const override
Compute value of this item in simulation.
StatVariable(const TreeNode *n, std::vector< const TreeNode * > &used)
Construct from a given Counter or Statistic.
StatVariable()=delete
Not default constructable.
virtual void getClocks(std::vector< const Clock * > &clocks) const override
Populates a vector with the clocks found in this subexpression node.
virtual StatVariable * clone_() const override
Deep copy of the content of this item.
virtual void dump(std::ostream &o, bool show_range=true, bool resolve_subexprs=true) const override
Dump the content of this expression item.
StatisticInstance * getStatisticInstance()
Returns the sparta StatisticInstance contained in this ExpressionNode Node.