-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipeline.cpp
202 lines (150 loc) · 4.48 KB
/
pipeline.cpp
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
#include "rf_pipelines_internals.hpp"
using namespace std;
namespace rf_pipelines {
#if 0
} // namespace rf_pipelines
#endif
pipeline::pipeline(const string &name_) :
pipeline_object("pipeline", name_)
{ }
pipeline::pipeline(const vector<shared_ptr<pipeline_object>> &elements_, const string &name_) :
pipeline_object("pipeline", name_),
elements(elements_)
{
for (const auto &p: elements)
if (p.get() == nullptr)
_throw("null pointer in pipeline constructor");
}
// For subclasses (e.g. wi_sub_pipeline)
pipeline::pipeline(const string &class_name_, const string &name_) :
pipeline_object(class_name_, name_)
{ }
void pipeline::add(const shared_ptr<pipeline_object> &p)
{
if (p.get() == nullptr)
_throw("null pointer in pipeline constructor");
if (this->state != UNBOUND)
_throw("pipeline::add() was called after bind()");
elements.push_back(p);
}
void pipeline::_bind(ring_buffer_dict &rb_dict, Json::Value &json_attrs)
{
if (elements.size() == 0)
_throw("pipeline is empty (length-zero)");
// Initial values, updated in loop below
this->nt_chunk_out = nt_chunk_in;
this->nt_maxgap = 0;
this->nt_contig = 1;
for (size_t i = 0; i < elements.size(); i++) {
auto p = elements[i];
run_params params = this->get_params();
params.container_depth++;
params.container_index = i;
elements[i]->bind(params, rb_dict, nt_chunk_out, nt_maxlag + nt_maxgap, json_attrs, this->out_mp);
this->nt_chunk_out = p->nt_chunk_out;
this->nt_maxgap += p->nt_maxgap;
}
}
ssize_t pipeline::_advance()
{
// Initial values, updated in loop below
ssize_t ret = SSIZE_MAX;
this->pos_lo = pos_hi.load();
for (size_t i = 0; i < elements.size(); i++) {
auto p = elements[i];
ssize_t m = p->pos_lo; // only needed for debug print
ssize_t n = p->advance(pos_lo, pos_max);
ret = min(ret, n);
pos_lo = p->pos_lo.load();
if (_params.noisy())
p->_print("advance " + to_string(m) + " -> " + to_string(pos_lo));
}
return ret;
}
ssize_t pipeline::get_preferred_chunk_size()
{
if (elements.size() == 0)
_throw("pipeline is empty (length-zero)");
return elements[0]->get_preferred_chunk_size();
}
Json::Value pipeline::jsonize() const
{
if (elements.size() == 0)
_throw("pipeline is empty (length-zero)");
Json::Value ret;
ret["class_name"] = "pipeline";
ret["name"] = name;
for (size_t i = 0; i < elements.size(); i++)
ret["elements"].append(elements[i]->jsonize());
return ret;
}
shared_ptr<pipeline> pipeline::from_json(const Json::Value &x)
{
if (string_from_json(x,"class_name") != "pipeline")
throw runtime_error("rf_pipelines: expected class_name=\"pipeline\" in pipeline json constructor");
// Old versions of jsonize() didn't always include the 'name'.
string name = x.isMember("name") ? string_from_json(x, "name") : "";
vector<shared_ptr<pipeline_object>> elements;
for (const auto &s: array_from_json(x, "elements"))
elements.push_back(pipeline_object::from_json(s));
return make_shared<pipeline> (elements, name);
}
void pipeline::_allocate()
{
for (auto &p: this->elements)
p->allocate();
}
void pipeline::_deallocate()
{
for (auto &p: this->elements)
p->deallocate();
}
void pipeline::_start_pipeline(Json::Value &json_attrs)
{
for (auto &p: this->elements)
p->start_pipeline(json_attrs);
}
void pipeline::_end_pipeline(Json::Value &json_output)
{
for (int i = 0; i < (int)elements.size(); i++) {
json_output["pipeline"].append(Json::Value(Json::objectValue));
elements[i]->end_pipeline(json_output["pipeline"][i]);
}
}
void pipeline::_reset()
{
for (auto &p: this->elements)
p->reset();
}
void pipeline::_unbind()
{
for (auto &p: this->elements)
p->unbind();
}
void pipeline::_get_info(Json::Value &j)
{
j["pipeline"] = Json::Value(Json::arrayValue);
double mb_cumul = 0.0;
for (auto &p: this->elements) {
Json::Value jr = p->get_info();
double mb = double_from_json(jr, "mb_cumul");
j["pipeline"].append(jr);
mb_cumul += mb;
}
j["mb_cumul"] = mb_cumul;
}
void pipeline::_visit_pipeline(std::function<void(const std::shared_ptr<pipeline_object>&,int)> f, const std::shared_ptr<pipeline_object> &self, int depth)
{
rf_assert(self.get() == this);
f(self, depth);
for (auto &p: this->elements)
visit_pipeline(f, p, depth+1);
}
namespace {
struct _init {
_init() {
pipeline_object::register_json_deserializer("pipeline", pipeline::from_json);
}
} init;
}
} // namespace rf_pipelines