-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequence.h
380 lines (341 loc) · 12.4 KB
/
Sequence.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*****************************************************************************
* @file: Sequence.hpp
* @brief: A series of processors that are connected by links.
*
* Copyright (c) 2022 O-Net Communications Inc.
* All rights reserved.
* **************************************************************************/
#pragma once
#include <common/Timer.h>
#include <taskflow/taskflow.hpp>
#include <unordered_map>
#include <vector>
#include "Container.h"
#include "Link.h"
namespace cppbase { namespace sequence {
class Sequence : public Container
{
public:
struct SequenceExecutionStatus : public Processor::ExecutionStatus
{
uint64_t trigger_count{0};
uint64_t exec_count{0};
uint64_t ok_count{0};
uint64_t ng_count{0};
SequenceExecutionStatus& operator=(const Processor::ExecutionStatus& status)
{
id = status.id;
exec_status = status.exec_status;
err_msg = status.err_msg;
exec_time_us = status.exec_time_us;
return *this;
}
};
public:
Sequence() = default;
virtual ~Sequence()
{
if (m_exec_thread.joinable())
{
m_mode = Mode::PROGRAM;
m_exec_thread.join();
}
}
template <typename T>
Processor* CreateProcessor()
{
m_processors.push_back(std::make_shared<T>());
return m_processors.back().get();
}
const SequenceExecutionStatus& GetExecutionStatus() const { return m_exec_status; }
/**
* @brief Add a link from src processor to dst processor
*/
void AddLink(Processor* src, Processor* dst, uint32_t out_id = 0, uint32_t in_id = 0)
{
if (!src || !dst)
{
throw std::invalid_argument("Source or destination processor is null");
}
AddLink(src->GetId(), dst->GetId(), out_id, in_id);
}
/**
* Add a link from src processor to dst processor
*/
void AddLink(uuids::uuid src, uuids::uuid dst, uint32_t out_id = 0, uint32_t in_id = 0)
{
if (src.is_nil() || dst.is_nil())
{
throw std::invalid_argument("Source or destination processor is null");
}
if (m_proc_links.find(src) == m_proc_links.end())
{
std::vector<Link> links;
links.emplace_back(src, dst, out_id, in_id);
m_proc_links.emplace(src, links);
} else
{
bool exists = false;
auto& links = m_proc_links[src];
Link new_link{src, dst, out_id, in_id};
for (auto& link : links)
{
if (link == new_link)
{
exists = true;
break;
}
}
if (!exists)
links.push_back(new_link);
}
}
void RemoveLink(uuids::uuid src, uuids::uuid dst = uuids::uuid(), uint32_t out_id = 0,
uint32_t in_id = 0)
{
if (src.is_nil())
{
throw std::invalid_argument("Source processor must not be null");
}
if (m_proc_links.find(src) == m_proc_links.end())
{
throw std::out_of_range("No link starting with specified source processor");
}
auto& links = m_proc_links[src];
Link new_link{src, dst, out_id, in_id};
links.erase(std::remove_if(links.begin(), links.end(), [&new_link](auto& link) {
if (new_link.m_src == link.m_src)
{
if (new_link.m_dst.is_nil()) // if m_dst is nullptr, remove all links with same m_src
return true;
return new_link == link;
}
}));
}
ExecutionStatus Execute(const std::vector<cppbase::Variant>& inputs) override
{
std::vector<cppbase::Variant> seq_results;
std::unordered_map<uuids::uuid, std::vector<cppbase::Variant>> proc_inputs_map;
std::unordered_map<uuids::uuid, tf::Task> proc_task_map;
tf::Executor executor{1};
tf::Taskflow taskflow{uuids::to_string(m_id)};
std::vector<std::future<void>> futures;
ExecStatus exec_status{ExecStatus::PASS};
m_exec_status.trigger_count++;
if (!PreExecute(inputs))
{
return m_exec_status;
}
for (auto& proc : m_processors)
{
auto proc_id = proc->GetId();
// Initialize processor's inputs with sequence's inputs at the beginning, based on
// the processor input map in the sequence. The inputs may be overwriten during the
// execution if there are links linked into the processor.
proc_inputs_map.emplace(proc_id, std::vector<cppbase::Variant>(proc->GetInputTypes().size()));
if (m_proc_inputs.find(proc_id) != m_proc_inputs.end())
{
auto& proc_inputs = m_proc_inputs[proc_id];
for (auto& input : proc_inputs)
{
proc_inputs_map[proc_id][input.second] = inputs[input.first];
}
}
// Add a task for each processor to the task map
proc_task_map.emplace(proc_id, taskflow.emplace([this, proc_id, &proc_inputs_map,
&exec_status, &seq_results]() {
auto* proc = const_cast<Processor*>(GetProcessor(proc_id));
try
{
// Get the inputs for the processor and execute
auto& proc_inputs = proc_inputs_map[proc_id];
auto status = proc->Execute(proc_inputs);
if (status.exec_status == ExecStatus::PASS)
{
const auto& results = proc->GetResults();
// Update the successor's inputs if there're links between the current processor
// and its successor.
auto it = m_proc_links.find(proc_id);
if (it != m_proc_links.end())
{
for (auto& link : it->second)
{
if (!link.m_dst.is_nil())
{
if (link.m_src_id >= results.size())
{
std::cerr
<< "Link source id is out of results range, source_id = "
<< link.m_src_id << " results.size() = " << results.size()
<< std::endl;
throw std::out_of_range(
"Link source id is out of results range");
}
auto& dst_inputs = proc_inputs_map[link.m_dst];
if (dst_inputs.size() == link.m_src_id)
{
proc_inputs_map[link.m_dst].push_back(results[link.m_src_id]);
} else if (dst_inputs.size() > link.m_src_id)
{
proc_inputs_map[link.m_dst][link.m_dst_id] =
results[link.m_src_id];
}
}
}
}
}
} catch (...)
{
exec_status = ExecStatus::FAIL;
}
seq_results.emplace_back(proc->GetExecutionStatus());
}));
}
// Sort the processors according to the links
for (auto element : m_proc_links)
{
auto& task = proc_task_map[element.first];
for (auto& link : m_proc_links[element.first])
{
if (!link.m_dst.is_nil())
{
task.precede(proc_task_map[link.m_dst]);
}
}
}
// Now run the taskflow which will run each added task
auto future = executor.run(taskflow);
future.wait();
PostExecute(seq_results);
{
std::lock_guard<std::mutex> lock(m_results_mutex);
m_results = seq_results;
}
m_exec_status = Processor::GetExecutionStatus();
m_exec_status.exec_count++;
m_exec_status.exec_status = exec_status;
if (m_exec_status.exec_status == ExecStatus::PASS)
{
m_exec_status.ok_count++;
} else if (m_exec_status.exec_status == ExecStatus::FAIL)
{
m_exec_status.ng_count++;
}
for (auto& callback : m_on_complete_callbacks)
{
futures.push_back(std::async(std::launch::async, callback, m_results));
}
return m_exec_status;
}
void SetMode(Mode mode) override
{
if (m_mode == mode)
return;
std::lock_guard<std::recursive_mutex> lock(m_exec_mutex);
m_mode = mode;
for (auto& proc : m_processors)
{
proc->SetMode(m_mode);
}
if (m_mode == Mode::RUN || m_mode == Mode::TEST)
{
m_exec_thread = std::thread([this]() {
while (m_mode == Mode::RUN || m_mode == Mode::TEST)
{
Execute({});
}
});
} else
{
m_exec_thread.join();
}
std::vector<std::future<void>> futures;
for (auto& callback : m_on_mode_changed_callbacks)
{
futures.push_back(std::async(std::launch::async, callback, m_mode));
}
}
std::vector<const Processor*> GetSuccessors(const uuids::uuid& proc_id) const
{
std::vector<const Processor*> successors;
auto it = m_proc_links.find(proc_id);
if (it != m_proc_links.end())
{
for (auto& link : it->second)
{
if (!link.m_dst.is_nil())
{
successors.push_back(GetProcessor(link.m_dst));
}
}
}
return successors;
}
std::vector<const Processor*> GetPredecessors(const uuids::uuid& proc_id) const
{
std::vector<const Processor*> predecessors;
for (auto& element : m_proc_links)
{
for (auto& link : element.second)
{
if (link.m_dst == proc_id)
{
predecessors.push_back(GetProcessor(link.m_src));
}
}
}
return predecessors;
}
std::vector<const Link*> GetLinks(const Processor* proc) const
{
std::vector<const Link*> links;
auto it = m_proc_links.find(proc->GetId());
if (it != m_proc_links.end())
{
for (auto& link : it->second)
{
links.push_back(&link);
}
}
return links;
}
const Link* GetLink(const uuids::uuid& src_id, const uuids::uuid& dst_id)
{
auto it = m_proc_links.find(src_id);
if (it != m_proc_links.end())
{
for (auto& link : it->second)
{
if (link.m_dst == dst_id)
{
return &link;
}
}
}
return nullptr;
}
void ResetExecutionStatus() { m_exec_status = SequenceExecutionStatus(); }
void MapProcessorInput(const uuids::uuid& proc_id, uint32_t seq_input_id, uint32_t proc_input_id)
{
if (m_proc_inputs.find(proc_id) == m_proc_inputs.end())
{
m_proc_inputs[proc_id] = std::vector<std::pair<uint32_t, uint32_t>>();
}
m_proc_inputs[proc_id].push_back(std::make_pair(seq_input_id, proc_input_id));
}
protected:
// maps from a processor to links which have the key as the source processor
std::unordered_map<uuids::uuid, std::vector<Link>> m_proc_links;
std::thread m_exec_thread;
SequenceExecutionStatus m_exec_status;
std::unordered_map<uuids::uuid, std::vector<std::pair<uint32_t, uint32_t>>> m_proc_inputs;
template<class Archive>
void serialize(Archive& archive, const uint32_t)
{
SERIALIZE_BASE_CLASS(archive, Container);
archive(m_proc_links, m_proc_inputs);
}
ENABLE_TYPE_INFO(Container)
SERIALIZATION_FRIEND_ACCESS
};
}} // namespace ovf2::sequence