KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Kernel.cpp
Go to the documentation of this file.
1#include "kids/Kernel.h"
2
3#include <chrono>
4#include <ctime>
5
6#include "kids/Exception.h"
7#include "kids/hash_fnv1a.h"
8#include "kids/macro_utils.h"
9
10namespace PROJECT_NS {
11
12Kernel::Kernel(const std::string& customized_name) : kernel_name{customized_name} {
15 max_align_size = getName().size();
16 Kernel::getKernels().push_back(this);
17};
18
20
21void Kernel::setInputParam(std::shared_ptr<Param>& PM) {
22 _param = PM;
23 is_timing = _param->get_bool("timing", LOC(), false);
25 for (auto& pkernel : _child_kernels) pkernel->setInputParam(PM);
26}
27
28void Kernel::setInputDataSet(std::shared_ptr<DataSet>& DS) {
29 if (!_param) throw kids_error("Param must be passed before");
30 _dataset = DS;
32 for (auto& pkernel : _child_kernels) pkernel->setInputDataSet(DS);
33}
34
35std::shared_ptr<Param> Kernel::getParam() const { return _param; }
36
37std::shared_ptr<DataSet> Kernel::getDataSet() const { return _dataset; }
38
40 if (!_dataset) throw kids_error("DataSet must be passed before");
41 // @todo: Consider if the load option is available and ensure it is not overwritten by this function.
43 for (auto& pkernel : _child_kernels) pkernel->initializeKernel(stat);
44 count_calc++; // random only called once!!
45 return stat;
46}
47
49 if (!_dataset) throw kids_error("DataSet must be passed before");
50 if (!_ruleset & !has_parent) std::cerr << "run without rules\n";
51
52 std::chrono::time_point<std::chrono::steady_clock> begin, end;
53 if (is_timing) begin = std::chrono::steady_clock::now();
54 {
56 for (auto& pkernel : _child_kernels) pkernel->executeKernel(stat);
57 }
58 if (is_timing) {
59 end = std::chrono::steady_clock::now();
60 exec_time += static_cast<std::chrono::duration<double>>(end - begin).count();
61 }
62 count_exec++;
63 return stat;
64}
65
68 for (auto& pkernel : _child_kernels) pkernel->finalizeKernel(stat);
69 return stat;
70}
71
73
74int Kernel::getID() const { return kernel_id; }
75
76bool Kernel::operator==(const Kernel& ker) { return getID() == ker.getID(); }
77
78Kernel& Kernel::appendChild(std::shared_ptr<Kernel> ker) {
79 ker->_order_in_parent = _child_kernels.size();
80 _child_kernels.push_back(ker);
81 ker->_parent_kernel = this;
82 ker->has_parent = true;
83
84 if (ker->_ruleset) {
85 _ruleset = ker->_ruleset;
86 Kernel* current;
87 size_t _dummy;
88 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
89 while (current != nullptr) {
90 current->_ruleset = ker->_ruleset;
91 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
92 }
93 }
94
95 // shouldn't be here!!
96 depth = std::max(depth, ker->depth + 1);
97 if (ker->getName().size() > max_align_size) { max_align_size = ker->getName().size(); }
98
99 return *this;
100}
101
102Kernel& Kernel::insertAt(std::vector<std::size_t> indexes, std::shared_ptr<Kernel> ker) {
103 if (indexes.size() <= 0) throw kids_error("Bad indexes for accessing Kernel");
104 std::size_t idx0 = indexes[0];
105 if (idx0 > _child_kernels.size()) throw kids_error("Bad indexes for accessing Kernel");
106 if (indexes.size() == 1) {
107 ker->_order_in_parent = idx0;
108 _child_kernels.insert(_child_kernels.begin() + idx0, ker);
109 ker->_parent_kernel = this;
110 ker->has_parent = true;
111 if (ker->_ruleset) {
112 _ruleset = ker->_ruleset;
113 Kernel* current;
114 size_t _dummy;
115 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
116 while (current != nullptr) {
117 current->_ruleset = ker->_ruleset;
118 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
119 }
120 }
121 } else {
122 indexes.erase(indexes.begin());
123 _child_kernels[idx0]->insertAt(indexes, ker);
124 }
125 return *this;
126}
127
128Kernel& Kernel::removeAt(std::vector<std::size_t> indexes) {
129 if (indexes.size() <= 0) throw kids_error("Bad indexes for accessing Kernel");
130 std::size_t idx0 = indexes[0];
131 if (idx0 >= _child_kernels.size()) throw kids_error("Bad indexes for accessing Kernel");
132 if (indexes.size() == 1) {
133 auto& ker = _child_kernels[idx0];
134 ker->_parent_kernel = nullptr;
135 ker->has_parent = false;
136 _child_kernels.erase(_child_kernels.begin() + idx0);
137 } else {
138 indexes.erase(indexes.begin());
139 _child_kernels[idx0]->removeAt(indexes);
140 }
141 return *this;
142}
143
147Kernel& Kernel::updateAt(std::vector<std::size_t> indexes, std::shared_ptr<Kernel> ker) {
148 if (indexes.size() <= 0) throw kids_error("Bad indexes for accessing Kernel");
149 std::size_t idx0 = indexes[0];
150 if (idx0 >= _child_kernels.size()) throw kids_error("Bad indexes for accessing Kernel");
151 if (indexes.size() == 1) {
152 auto& kerold = _child_kernels[idx0];
153 kerold->_parent_kernel = nullptr;
154 kerold->has_parent = false;
155
156 ker->_order_in_parent = idx0;
157 _child_kernels[idx0] = ker;
158 ker->_parent_kernel = this;
159 ker->has_parent = true;
160
161 if (ker->_ruleset) {
162 _ruleset = ker->_ruleset;
163 Kernel* current;
164 size_t _dummy;
165 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
166 while (current != nullptr) {
167 current->_ruleset = ker->_ruleset;
168 std::tie(current, _dummy) = getLastParentKernelAndChildOrder();
169 }
170 }
171 } else {
172 indexes.erase(indexes.begin());
173 _child_kernels[idx0]->updateAt(indexes, ker);
174 }
175 return *this;
176}
177
178std::tuple<Kernel*, std::size_t> Kernel::getLastParentKernelAndChildOrder() {
179 if (has_parent) return std::make_tuple(_parent_kernel, _order_in_parent);
180 return std::make_tuple(nullptr, 0);
181}
182
183std::shared_ptr<RuleSet> Kernel::getRuleSet() { return _ruleset; }
184
185const std::string Kernel::serializeKernel(const Kernel& ker) { return ""; }
186
187std::shared_ptr<Kernel> Kernel::deserializeKernel(const std::string& str) { return nullptr; }
188
189const std::string Kernel::generateInformationString(double total_time, int current_layer, int total_depth,
190 int total_align_size) {
191 std::stringstream ss;
192
193 if (total_depth == 0) total_depth = depth;
194 if (total_align_size == 0) total_align_size = max_align_size;
195 if (total_time < 0) total_time = exec_time;
196
197 if (current_layer == 0) {
198 ss << std::left << std::setw(2 * total_depth + 5) << "[Index]" //
199 << std::left << std::setw(total_align_size + 10) << "[Kernel]" //
200 << "[Time]" //
201 << std::right << std::setw(11) << "[Percent]" << std::endl;
202 }
203
204 ss << std::setw(2 * current_layer + 1) << "#" //
205 << std::setfill('0') << std::setw(2) << kernel_id //
206 << std::setfill('.') << std::setw(2 * (total_depth - current_layer) + 2) << ": " //
207 << std::setfill(' ') << std::left << std::setw(total_align_size + 10) << getName() //
208 << std::fixed << std::setprecision(3) << exec_time << "s" //
209 << std::right << std::setw(10) << std::fixed << std::setprecision(2) //
210 << 100 * exec_time / total_time << "%" //
211 << std::endl;
212 for (auto pkernel : _child_kernels) {
213 ss << pkernel->generateInformationString(total_time, current_layer + 1, total_depth, total_align_size);
214 }
215 return ss.str();
216}
217
218void Kernel::setInputParam_impl(std::shared_ptr<Param>& PM){};
219
220void Kernel::setInputDataSet_impl(std::shared_ptr<DataSet>& DS){};
221
223
225
227
228void Kernel::connectRelatedKernels(std::shared_ptr<Kernel>& ker) {
229 bool found_kernel = false;
230 for (auto&& pkernel : _all_kernels) {
231 if (*ker == *pkernel) {
232 found_kernel = true;
233 break;
234 }
235 }
236 if (!found_kernel) {
237 _all_kernels.push_back(ker);
238 for (auto&& pkernel : ker->_child_kernels) connectRelatedKernels(pkernel);
239 }
240}
241
242std::map<std::string, Kernel*>& getDictOfKernels() {
243 static std::map<std::string, Kernel*> static_dict_of_kernels;
244 return static_dict_of_kernels;
245}
246
247std::vector<Kernel*>& Kernel::getKernels() {
248 static std::vector<Kernel*> static_all_kernels;
249 return static_all_kernels;
250}
251}; // namespace PROJECT_NS
provide Exception structs
this file provide Kernel class
this class provides the container and implementation of algorithms
Definition Kernel.h:60
virtual Status & initializeKernel_impl(Status &stat)
Virtual function to initialize the kernel implementation.
Definition Kernel.cpp:222
static std::shared_ptr< Kernel > deserializeKernel(const std::string &str)
Deserialize a string representation into a Kernel object.
Definition Kernel.cpp:187
Kernel * _parent_kernel
Pointer to the parent kernel.
Definition Kernel.h:288
std::shared_ptr< DataSet > getDataSet() const
Get the data set associated with the kernel.
Definition Kernel.cpp:37
Kernel & appendChild(std::shared_ptr< Kernel > ker)
Append a kernel as the last child of the current tree node.
Definition Kernel.cpp:78
std::shared_ptr< Param > getParam() const
Get the parameter associated with the kernel.
Definition Kernel.cpp:35
std::vector< std::shared_ptr< Kernel > > _child_kernels
Vector containing shared pointers to the child kernels of this kernel.
Definition Kernel.h:298
virtual Status & finalizeKernel_impl(Status &stat)
Virtual function to finalize the kernel implementation.
Definition Kernel.cpp:226
static std::vector< Kernel * > & getKernels()
Get the vector of all kernel pointers.
Definition Kernel.cpp:247
virtual const std::string getName()
Get the name of the kernel.
Definition Kernel.h:69
std::shared_ptr< RuleSet > getRuleSet()
Get RuleSet associated with the Kernel.
Definition Kernel.cpp:183
static const std::string serializeKernel(const Kernel &ker)
Serialize a Kernel object into a string representation.
Definition Kernel.cpp:185
int getID() const
Get the ID of the kernel.
Definition Kernel.cpp:74
Kernel & removeAt(std::vector< std::size_t > indexes)
Remove kernels at specified indexes from the tree.
Definition Kernel.cpp:128
std::shared_ptr< Param > _param
Shared pointer to the Param object associated with this kernel.
Definition Kernel.h:273
virtual Status & executeKernel_impl(Status &stat)
Virtual function to execute the kernel implementation.
Definition Kernel.cpp:224
void setInputDataSet(std::shared_ptr< DataSet > &DS)
Set input data set for the kernel and its children.
Definition Kernel.cpp:28
virtual ~Kernel()
Destructor.
Definition Kernel.cpp:19
virtual int getType() const
Get the type of the kernel.
Definition Kernel.cpp:72
Status & executeKernel(Status &stat)
Execute the kernel's algorithm and those of its children.
Definition Kernel.cpp:48
virtual void setInputDataSet_impl(std::shared_ptr< DataSet > &DS)
Virtual function to set input data set for the kernel implementation.
Definition Kernel.cpp:220
virtual void setInputParam_impl(std::shared_ptr< Param > &PM)
Virtual function to set input parameters for the kernel implementation.
Definition Kernel.cpp:218
std::vector< std::shared_ptr< Kernel > > _all_kernels
Vector containing shared pointers to all descendant kernels of this kernel.
Definition Kernel.h:303
const std::string generateInformationString(double total_time=-1.0f, int current_layer=0, int total_depth=0, int total_align_size=0)
Generate a formatted string containing information about the kernel.
Definition Kernel.cpp:189
std::tuple< Kernel *, std::size_t > getLastParentKernelAndChildOrder()
Retrieve the last parent kernel along with the order of its child kernels, if available.
Definition Kernel.cpp:178
std::shared_ptr< DataSet > _dataset
Shared pointer to the DataSet object associated with this kernel.
Definition Kernel.h:278
Kernel(const std::string &customized_name="")
Constructor with an optional specified name.
Definition Kernel.cpp:12
void connectRelatedKernels(std::shared_ptr< Kernel > &ker)
Connect related kernels to this kernel.
Definition Kernel.cpp:228
Kernel & insertAt(std::vector< std::size_t > indexes, std::shared_ptr< Kernel > ker)
Insert a kernel at specified indexes in the tree.
Definition Kernel.cpp:102
Status & finalizeKernel(Status &stat)
Finalize the kernel and its children, performing any necessary cleanup.
Definition Kernel.cpp:66
std::size_t _order_in_parent
Order of this kernel in its parent's children.
Definition Kernel.h:293
bool operator==(const Kernel &ker)
Overloaded equality operator to compare two Kernel objects by their IDs.
Definition Kernel.cpp:76
Kernel & updateAt(std::vector< std::size_t > indexes, std::shared_ptr< Kernel > ker)
Update the kernel at specified indexes in the tree.
Definition Kernel.cpp:147
Status & initializeKernel(Status &stat)
Prepare initial conditions for the kernel and its children.
Definition Kernel.cpp:39
void setInputParam(std::shared_ptr< Param > &PM)
Set input parameters for the kernel and its children.
Definition Kernel.cpp:21
std::shared_ptr< RuleSet > _ruleset
Recorded Rules associated with the Kernel.
Definition Kernel.h:283
#define LOC()
show the location information for debug
Definition fmt.h:49
int kernel_type
Type of the kernel.
Definition Kernel.h:260
int depth
Depth of the kernel in the tree structure.
Definition Kernel.h:262
int kernel_id
ID of the kernel.
Definition Kernel.h:259
int count_exec
Counter for the number of executions performed by this kernel.
Definition Kernel.h:258
int max_align_size
Maximum alignment size used by this kernel.
Definition Kernel.h:263
double exec_time
Total execution time of the kernel.
Definition Kernel.h:261
bool has_parent
Flag indicating whether the kernel has a parent.
Definition Kernel.h:256
int count_calc
Counter for the number of calculations performed by this kernel.
Definition Kernel.h:257
bool is_timing
Flag indicating whether timing is enabled for this kernel.
Definition Kernel.h:255
#define FUNCTION_NAME
Definition macro_utils.h:9
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
std::map< std::string, Kernel * > & getDictOfKernels()
Definition Kernel.cpp:242
constexpr uint32_t hash(const char *str)
Definition hash_fnv1a.h:12