-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy path_flow_graph_body_impl.h
470 lines (397 loc) · 16.8 KB
/
_flow_graph_body_impl.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright (c) 2005-2025 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB__flow_graph_body_impl_H
#define __TBB__flow_graph_body_impl_H
#ifndef __TBB_flow_graph_H
#error Do not #include this internal file directly; use public TBB headers instead.
#endif
// included in namespace tbb::detail::d2 (in flow_graph.h)
typedef std::uint64_t tag_value;
// TODO revamp: find out if there is already helper for has_policy.
template<typename ... Policies> struct Policy {};
template<typename ... Policies> struct has_policy;
template<typename ExpectedPolicy, typename FirstPolicy, typename ...Policies>
struct has_policy<ExpectedPolicy, FirstPolicy, Policies...> :
std::integral_constant<bool, has_policy<ExpectedPolicy, FirstPolicy>::value ||
has_policy<ExpectedPolicy, Policies...>::value> {};
template<typename ExpectedPolicy, typename SinglePolicy>
struct has_policy<ExpectedPolicy, SinglePolicy> :
std::integral_constant<bool, std::is_same<ExpectedPolicy, SinglePolicy>::value> {};
template<typename ExpectedPolicy, typename ...Policies>
struct has_policy<ExpectedPolicy, Policy<Policies...> > : has_policy<ExpectedPolicy, Policies...> {};
namespace graph_policy_namespace {
struct rejecting { };
struct reserving { };
struct queueing { };
struct lightweight { };
// K == type of field used for key-matching. Each tag-matching port will be provided
// functor that, given an object accepted by the port, will return the
/// field of type K being used for matching.
template<typename K, typename KHash=d1::tbb_hash_compare<typename std::decay<K>::type > >
__TBB_requires(tbb::detail::hash_compare<KHash, K>)
struct key_matching {
typedef K key_type;
typedef typename std::decay<K>::type base_key_type;
typedef KHash hash_compare_type;
};
// old tag_matching join's new specifier
typedef key_matching<tag_value> tag_matching;
// Aliases for Policy combinations
typedef Policy<queueing, lightweight> queueing_lightweight;
typedef Policy<rejecting, lightweight> rejecting_lightweight;
} // namespace graph_policy_namespace
// -------------- function_body containers ----------------------
//! A functor that takes no input and generates a value of type Output
template< typename Output >
class input_body : no_assign {
public:
virtual ~input_body() {}
virtual Output operator()(d1::flow_control& fc) = 0;
virtual input_body* clone() = 0;
};
//! The leaf for input_body
template< typename Output, typename Body>
class input_body_leaf : public input_body<Output> {
public:
input_body_leaf( const Body &_body ) : body(_body) { }
Output operator()(d1::flow_control& fc) override { return body(fc); }
input_body_leaf* clone() override {
return new input_body_leaf< Output, Body >(body);
}
Body get_body() { return body; }
private:
Body body;
};
//! A functor that takes an Input and generates an Output
template< typename Input, typename Output >
class function_body : no_assign {
public:
virtual ~function_body() {}
virtual Output operator()(const Input &input) = 0;
virtual function_body* clone() = 0;
};
//! the leaf for function_body
template <typename Input, typename Output, typename B>
class function_body_leaf : public function_body< Input, Output > {
public:
function_body_leaf( const B &_body ) : body(_body) { }
Output operator()(const Input &i) override { return tbb::detail::invoke(body,i); }
B get_body() { return body; }
function_body_leaf* clone() override {
return new function_body_leaf< Input, Output, B >(body);
}
private:
B body;
};
//! the leaf for function_body specialized for Input and output of continue_msg
template <typename B>
class function_body_leaf< continue_msg, continue_msg, B> : public function_body< continue_msg, continue_msg > {
public:
function_body_leaf( const B &_body ) : body(_body) { }
continue_msg operator()( const continue_msg &i ) override {
body(i);
return i;
}
B get_body() { return body; }
function_body_leaf* clone() override {
return new function_body_leaf< continue_msg, continue_msg, B >(body);
}
private:
B body;
};
//! the leaf for function_body specialized for Output of continue_msg
template <typename Input, typename B>
class function_body_leaf< Input, continue_msg, B> : public function_body< Input, continue_msg > {
public:
function_body_leaf( const B &_body ) : body(_body) { }
continue_msg operator()(const Input &i) override {
body(i);
return continue_msg();
}
B get_body() { return body; }
function_body_leaf* clone() override {
return new function_body_leaf< Input, continue_msg, B >(body);
}
private:
B body;
};
//! the leaf for function_body specialized for Input of continue_msg
template <typename Output, typename B>
class function_body_leaf< continue_msg, Output, B > : public function_body< continue_msg, Output > {
public:
function_body_leaf( const B &_body ) : body(_body) { }
Output operator()(const continue_msg &i) override {
return body(i);
}
B get_body() { return body; }
function_body_leaf* clone() override {
return new function_body_leaf< continue_msg, Output, B >(body);
}
private:
B body;
};
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
class metainfo_tag_type;
#endif
// TODO: add description
struct invoke_body_with_tag_helper {
using first_priority = int;
using second_priority = double;
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
template <typename Body, typename... Args>
static auto invoke(first_priority, Body&& body, metainfo_tag_type&& tag, Args&&... args)
noexcept(noexcept(tbb::detail::invoke(std::forward<Body>(body), std::forward<Args>(args)..., std::move(tag))))
-> decltype(tbb::detail::invoke(std::forward<Body>(body), std::forward<Args>(args)..., std::move(tag)), void())
{
tbb::detail::invoke(std::forward<Body>(body), std::forward<Args>(args)..., std::move(tag));
}
#endif
template <typename Body, typename... Args>
static void invoke(second_priority, Body&& body __TBB_FLOW_GRAPH_METAINFO_ARG(metainfo_tag_type&&),
Args&&... args)
noexcept(noexcept(tbb::detail::invoke(std::forward<Body>(body), std::forward<Args>(args)...)))
{
tbb::detail::invoke(std::forward<Body>(body), std::forward<Args>(args)...);
}
};
// TODO: add comment
template <typename Body, typename... Args>
void invoke_body_with_tag(Body&& body __TBB_FLOW_GRAPH_METAINFO_ARG(metainfo_tag_type&& tag), Args&&... args)
noexcept(noexcept(invoke_body_with_tag_helper::invoke(1, std::forward<Body>(body) __TBB_FLOW_GRAPH_METAINFO_ARG(std::move(tag)),
std::forward<Args>(args)...)))
{
invoke_body_with_tag_helper::invoke(/*overload priority helper*/1,
std::forward<Body>(body) __TBB_FLOW_GRAPH_METAINFO_ARG(std::move(tag)),
std::forward<Args>(args)...);
}
//! function_body that takes an Input and a set of output ports
template<typename Input, typename OutputSet>
class multifunction_body : no_assign {
public:
virtual ~multifunction_body () {}
virtual void operator()(const Input &/* input*/, OutputSet &/*oset*/
__TBB_FLOW_GRAPH_METAINFO_ARG(metainfo_tag_type&& /*tag*/)) = 0;
virtual multifunction_body* clone() = 0;
virtual void* get_body_ptr() = 0;
};
//! leaf for multifunction. OutputSet can be a std::tuple or a vector.
template<typename Input, typename OutputSet, typename B>
class multifunction_body_leaf : public multifunction_body<Input, OutputSet> {
public:
multifunction_body_leaf(const B &_body) : body(_body) { }
// body may explicitly put() to one or more of oset.
void operator()(const Input &input, OutputSet &oset __TBB_FLOW_GRAPH_METAINFO_ARG(metainfo_tag_type&& tag)) override {
invoke_body_with_tag(body __TBB_FLOW_GRAPH_METAINFO_ARG(std::move(tag)), input, oset);
}
void* get_body_ptr() override { return &body; }
multifunction_body_leaf* clone() override {
return new multifunction_body_leaf<Input, OutputSet,B>(body);
}
private:
B body;
};
// ------ function bodies for hash_buffers and key-matching joins.
template<typename Input, typename Output>
class type_to_key_function_body : no_assign {
public:
virtual ~type_to_key_function_body() {}
virtual Output operator()(const Input &input) = 0; // returns an Output
virtual type_to_key_function_body* clone() = 0;
};
// specialization for ref output
template<typename Input, typename Output>
class type_to_key_function_body<Input,Output&> : no_assign {
public:
virtual ~type_to_key_function_body() {}
virtual const Output & operator()(const Input &input) = 0; // returns a const Output&
virtual type_to_key_function_body* clone() = 0;
};
template <typename Input, typename Output, typename B>
class type_to_key_function_body_leaf : public type_to_key_function_body<Input, Output> {
public:
type_to_key_function_body_leaf( const B &_body ) : body(_body) { }
Output operator()(const Input &i) override { return tbb::detail::invoke(body, i); }
type_to_key_function_body_leaf* clone() override {
return new type_to_key_function_body_leaf< Input, Output, B>(body);
}
private:
B body;
};
template <typename Input, typename Output, typename B>
class type_to_key_function_body_leaf<Input,Output&,B> : public type_to_key_function_body< Input, Output&> {
public:
type_to_key_function_body_leaf( const B &_body ) : body(_body) { }
const Output& operator()(const Input &i) override {
return tbb::detail::invoke(body, i);
}
type_to_key_function_body_leaf* clone() override {
return new type_to_key_function_body_leaf< Input, Output&, B>(body);
}
private:
B body;
};
// --------------------------- end of function_body containers ------------------------
// --------------------------- node task bodies ---------------------------------------
//! A task that calls a node's forward_task function
template< typename NodeType >
class forward_task_bypass : public graph_task {
NodeType &my_node;
public:
forward_task_bypass( graph& g, d1::small_object_allocator& allocator, NodeType &n
, node_priority_t node_priority = no_priority
) : graph_task(g, allocator, node_priority),
my_node(n) {}
d1::task* execute(d1::execution_data& ed) override {
graph_task* next_task = my_node.forward_task();
if (SUCCESSFULLY_ENQUEUED == next_task)
next_task = nullptr;
else if (next_task)
next_task = prioritize_task(my_node.graph_reference(), *next_task);
finalize<forward_task_bypass>(ed);
return next_task;
}
d1::task* cancel(d1::execution_data& ed) override {
finalize<forward_task_bypass>(ed);
return nullptr;
}
};
//! A task that calls a node's apply_body_bypass function, passing in an input of type Input
// return the task* unless it is SUCCESSFULLY_ENQUEUED, in which case return nullptr
template< typename NodeType, typename Input, typename BaseTaskType = graph_task>
class apply_body_task_bypass
: public BaseTaskType
{
NodeType &my_node;
Input my_input;
using check_metainfo = std::is_same<BaseTaskType, graph_task>;
using without_metainfo = std::true_type;
using with_metainfo = std::false_type;
graph_task* call_apply_body_bypass_impl(without_metainfo) {
return my_node.apply_body_bypass(my_input
__TBB_FLOW_GRAPH_METAINFO_ARG(message_metainfo{}));
}
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
graph_task* call_apply_body_bypass_impl(with_metainfo) {
return my_node.apply_body_bypass(my_input, message_metainfo{this->get_msg_wait_context_vertices()});
}
#endif
graph_task* call_apply_body_bypass() {
return call_apply_body_bypass_impl(check_metainfo{});
}
public:
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
template <typename Metainfo>
apply_body_task_bypass( graph& g, d1::small_object_allocator& allocator, NodeType &n, const Input &i,
node_priority_t node_priority, Metainfo&& metainfo )
: BaseTaskType(g, allocator, node_priority, std::forward<Metainfo>(metainfo).waiters())
, my_node(n), my_input(i) {}
#endif
apply_body_task_bypass( graph& g, d1::small_object_allocator& allocator, NodeType& n, const Input& i,
node_priority_t node_priority = no_priority )
: BaseTaskType(g, allocator, node_priority), my_node(n), my_input(i) {}
d1::task* execute(d1::execution_data& ed) override {
graph_task* next_task = call_apply_body_bypass();
if (SUCCESSFULLY_ENQUEUED == next_task)
next_task = nullptr;
else if (next_task)
next_task = prioritize_task(my_node.graph_reference(), *next_task);
BaseTaskType::template finalize<apply_body_task_bypass>(ed);
return next_task;
}
d1::task* cancel(d1::execution_data& ed) override {
BaseTaskType::template finalize<apply_body_task_bypass>(ed);
return nullptr;
}
};
//! A task that calls a node's apply_body_bypass function with no input
template< typename NodeType >
class input_node_task_bypass : public graph_task {
NodeType &my_node;
public:
input_node_task_bypass( graph& g, d1::small_object_allocator& allocator, NodeType &n )
: graph_task(g, allocator), my_node(n) {}
d1::task* execute(d1::execution_data& ed) override {
graph_task* next_task = my_node.apply_body_bypass( );
if (SUCCESSFULLY_ENQUEUED == next_task)
next_task = nullptr;
else if (next_task)
next_task = prioritize_task(my_node.graph_reference(), *next_task);
finalize<input_node_task_bypass>(ed);
return next_task;
}
d1::task* cancel(d1::execution_data& ed) override {
finalize<input_node_task_bypass>(ed);
return nullptr;
}
};
// ------------------------ end of node task bodies -----------------------------------
template<typename T, typename DecrementType, typename DummyType = void>
class threshold_regulator;
template<typename T, typename DecrementType>
class threshold_regulator<T, DecrementType,
typename std::enable_if<std::is_integral<DecrementType>::value>::type>
: public receiver<DecrementType>, no_copy
{
T* my_node;
protected:
graph_task* try_put_task( const DecrementType& value ) override {
graph_task* result = my_node->decrement_counter( value );
if( !result )
result = SUCCESSFULLY_ENQUEUED;
return result;
}
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
// Intentionally ignore the metainformation
// If there are more items associated with passed metainfo to be processed
// They should be stored in the buffer before the limiter_node
graph_task* try_put_task(const DecrementType& value, const message_metainfo&) override {
return try_put_task(value);
}
#endif
graph& graph_reference() const override {
return my_node->my_graph;
}
template<typename U, typename V> friend class limiter_node;
void reset_receiver( reset_flags ) {}
public:
threshold_regulator(T* owner) : my_node(owner) {
// Do not work with the passed pointer here as it may not be fully initialized yet
}
};
template<typename T>
class threshold_regulator<T, continue_msg, void> : public continue_receiver, no_copy {
T *my_node;
#if __TBB_PREVIEW_FLOW_GRAPH_TRY_PUT_AND_WAIT
// Intentionally ignore the metainformation
// If there are more items associated with passed metainfo to be processed
// They should be stored in the buffer before the limiter_node
graph_task* execute(const message_metainfo&) override {
#else
graph_task* execute() override {
#endif
return my_node->decrement_counter( 1 );
}
protected:
graph& graph_reference() const override {
return my_node->my_graph;
}
public:
typedef continue_msg input_type;
typedef continue_msg output_type;
threshold_regulator(T* owner)
: continue_receiver( /*number_of_predecessors=*/0, no_priority ), my_node(owner)
{
// Do not work with the passed pointer here as it may not be fully initialized yet
}
};
#endif // __TBB__flow_graph_body_impl_H