The Sparta Modeling Framework
Loading...
Searching...
No Matches
SkeletonSimulator.cpp
1// <SkeletonSimulatorn.cpp> -*- C++ -*-
2
3
4#include <iostream>
5
6#include "SkeletonSimulator.hpp"
7
9#include "sparta/utils/TimeManager.hpp"
12
13#include "Producer.hpp"
14#include "Consumer.hpp"
15
17 sparta::app::Simulation("sparta_skeleton", &scheduler),
18 be_noisy_(be_noisy)
19{
20 // Using the macro SPARTA_EXPECT_FALSE will tell the compiler that
21 // most of the time, this if statement is falses -- it's an
22 // optimization. There is also a SPARTA_EXPECT_TRUE
23 if(SPARTA_EXPECT_FALSE(be_noisy_)) {
24 std::cout << "NOISE: " << __PRETTY_FUNCTION__ << ": Constructing" << std::endl;
25 }
26
27 // Set up all resources to be available through ResourceTreeNode.
28 // These factories will be instantiated during the buildTree_
29 // phase (after their registration -- see below).
32}
33
34
35SkeletonSimulator::~SkeletonSimulator()
36{
37 if(SPARTA_EXPECT_FALSE(be_noisy_)) {
38 std::cout << __PRETTY_FUNCTION__ << ": Tearing down" << std::endl;
39 }
40 getRoot()->enterTeardown(); // Allow deletion of nodes without error now
41}
42
43void SkeletonSimulator::buildTree_()
44{
45 if(SPARTA_EXPECT_FALSE(be_noisy_)) {
46 std::cout << "NOISE: " << __PRETTY_FUNCTION__ << ": Building the ResourceTreeNodes -- not instantiated yet" << std::endl;
47 }
48
49 // TREE_BUILDING Phase. See sparta::PhasedObject::TreePhase
50
51 // Create a single consumer
53 new sparta::ResourceTreeNode(getRoot(), "consumer", // Could use Consumer::name here...
54 sparta::TreeNode::GROUP_NAME_NONE, // Do not allow consumer[n] -- there's only one!
56 "Consumer Object",
57 getResourceSet()->getResourceFactory(Consumer::name));
58 to_delete_.emplace_back(rtn);
59
60 // Get the producer count from the created parameter in the
61 // created ParameterSet. Note that you get the ParameterSet, but
62 // not the Consumer resource/unit -- that has not been created yet.
63 sparta::ParameterSet * param_set = rtn->getParameterSet();
64 const uint32_t num_producers = param_set->getParameterAs<uint32_t>("num_producers");
65
66 // Create the producers
67 for (uint32_t i = 0; i < num_producers; ++i)
68 {
69 std::stringstream nodeName, humanName;
70 nodeName << "producer" << i;
71 humanName << "Producer " << i;
72
73 // We create resource tree nodes because each component of the
74 // core requires parameters and a clock. TreeNode does not
75 // provide this.
78 nodeName.str(),
79 "producer", i, // Grouping, i.e. producer[n]
80 humanName.str(),
81 getResourceSet()->getResourceFactory(Producer::name));
82 to_delete_.emplace_back(prod_tn);
83 }
84
85}
86
87void SkeletonSimulator::configureTree_()
88{
89 if(SPARTA_EXPECT_FALSE(be_noisy_)) {
90 std::cout << "NOISE: " << __PRETTY_FUNCTION__
91 << ": Configuring the parameters in the ResourceTreeNodes, "
92 << "but not the simulated objects are still not instantiated yet!" << std::endl;
93 }
94
95 // In TREE_CONFIGURING phase
96 // Configuration from command line is already applied
97}
98
99void SkeletonSimulator::bindTree_()
100{
101 if(SPARTA_EXPECT_FALSE(be_noisy_)) {
102 std::cout << "NOISE: " << __PRETTY_FUNCTION__
103 << ": The simulated objects are instantiated. Can be bound now." << std::endl;
104 }
105 // In TREE_FINALIZED phase
106 // Tree is finalized. Taps placed. No new nodes at this point
107 // Bind appropriate ports
108
109 sparta::TreeNode* root_tree_node = getRoot();
110 sparta_assert(root_tree_node != nullptr);
111
112 sparta::ParameterSet * param_set = root_tree_node->getChildAs<sparta::ParameterSet>("consumer.params");
113 const uint32_t num_producers = param_set->getParameterAs<uint32_t>("num_producers");
114
115 for (uint32_t i = 0; i < num_producers; ++i)
116 {
117 std::stringstream nodeName, humanName;
118 nodeName << "producer" << i;
119
120 // Bind all the producers to the Consumer
121 sparta::bind(root_tree_node->getChildAs<sparta::Port>(nodeName.str() + ".ports.producer_out_port"),
122 root_tree_node->getChildAs<sparta::Port>("consumer.ports.consumer_in_port"));
123
124 sparta::bind(root_tree_node->getChildAs<sparta::Port>(nodeName.str() + ".ports.producer_go_port"),
125 root_tree_node->getChildAs<sparta::Port>("consumer.ports." + nodeName.str() + "_go_port"));
126
127 }
128}
File that defines the Clock class.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define SPARTA_EXPECT_FALSE(x)
A macro for hinting to the compiler a particular condition should be considered most likely false.
Cool string utilities.
Basic Node framework in sparta device tree composite pattern.
static const char * name
Name of this resource. Required by sparta::ResourceFactory.
Definition Consumer.hpp:19
SkeletonSimulator(sparta::Scheduler &scheduler, bool be_noisy)
Construct SkeletonSimulator.
Generic container of Parameters.
const Parameter< ContentT > & getParameterAs(const std::string &name) const
Retrieves a sparta::Parameter<ContentT> reference from this parameter set.
The port interface used to bind port types together and defines a port behavior.
Definition Port.hpp:59
Templated ResourceFactoryBase implementation which can be used to trivially define Resource Factories...
void addResourceFactory()
Add a resource factory by its template type.
TreeNode subclass representing a node in the device tree which contains a single ResourceFactory and ...
ParameterSet * getParameterSet()
Gets the ParameterSet associated with this ResourceTreeNode.
void enterTeardown()
Places this tree into TREE_TEARDOWN phase so that nodes may be deleted without errors.
A class that lets you schedule events now and in the future.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
static const group_idx_type GROUP_IDX_NONE
GroupIndex indicating that a node has no group index because it belongs to no group.
Definition TreeNode.hpp:303
static constexpr char GROUP_NAME_NONE[]
Group name indicating that a node belongs to no group.
Definition TreeNode.hpp:314
const ConstT getChildAs(const std::string &name, bool must_exist=true) const
Retrieves a child that is castable to T with the given dotted path.
std::vector< std::unique_ptr< sparta::TreeNode > > to_delete_
Vector of TreeNodes to delete automatically at destruction. Add any nodes allocated to this list to a...
sparta::RootTreeNode * getRoot() noexcept
Returns the tree root.
sparta::ResourceSet * getResourceSet() noexcept
Returns the resource set for this Simulation.
Macros for handling exponential backoff.
void bind(Bus *p1, Bus *p2)
Bind two buses together.
Definition Bus.hpp:333