-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpipeline.hpp
More file actions
1232 lines (969 loc) · 31.2 KB
/
Copy pathpipeline.hpp
File metadata and controls
1232 lines (969 loc) · 31.2 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "../taskflow.hpp"
/**
@file pipeline.hpp
@brief pipeline include file
*/
namespace tf {
// ----------------------------------------------------------------------------
// Class Definition: Pipeflow
// ----------------------------------------------------------------------------
/**
@class Pipeflow
@brief class to create a pipeflow object used by the pipe callable
Pipeflow represents a <i>scheduling token</i> in the pipeline scheduling
framework. A pipeflow is created by the pipeline scheduler at runtime to
pass to the pipe callable. Users can query the present statistics
of that scheduling token, including the line identifier, pipe identifier,
and token identifier, and build their application algorithms based on
these statistics.
At the first stage, users can explicitly call the stop method
to stop the pipeline scheduler.
@code{.cpp}
tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf){
std::cout << "token id=" << pf.token()
<< " at line=" << pf.line()
<< " at pipe=" << pf.pipe()
<< '\n';
}};
@endcode
Pipeflow can only be created privately by the tf::Pipeline and
be used through the pipe callable.
*/
class Pipeflow {
template <typename... Ps>
friend class Pipeline;
template <typename P>
friend class ScalablePipeline;
template <typename... Ps>
friend class DataPipeline;
public:
/**
@brief default constructor
*/
Pipeflow() = default;
/**
@brief queries the line identifier of the present token
*/
size_t line() const {
return _line;
}
/**
@brief queries the pipe identifier of the present token
*/
size_t pipe() const {
return _pipe;
}
/**
@brief queries the token identifier
*/
size_t token() const {
return _token;
}
/**
@brief stops the pipeline scheduling
Only the first pipe can call this method to stop the pipeline.
Calling stop from other pipes will throw exception.
*/
void stop() {
if(_pipe != 0) {
TF_THROW("only the first pipe can stop the token");
}
_stop = true;
}
private:
// Regular data
size_t _line;
size_t _pipe;
size_t _token;
bool _stop;
};
// ----------------------------------------------------------------------------
// Class Definition: PipeType
// ----------------------------------------------------------------------------
/**
@enum PipeType
@brief enumeration of all pipe types
*/
enum class PipeType : int {
/** @brief parallel type */
PARALLEL = 1,
/** @brief serial type */
SERIAL = 2
};
// ----------------------------------------------------------------------------
// Class Definition: Pipe
// ----------------------------------------------------------------------------
/**
@class Pipe
@brief class to create a pipe object for a pipeline stage
@tparam C callable type
A pipe represents a stage of a pipeline. A pipe can be either
@em parallel direction or @em serial direction (specified by tf::PipeType)
and is coupled with a callable to invoke by the pipeline scheduler.
The callable must take a referenced tf::Pipeflow object in the first argument:
@code{.cpp}
Pipe{PipeType::SERIAL, [](tf::Pipeflow&){}}
@endcode
The pipeflow object is used to query the statistics of a scheduling token
in the pipeline, such as pipe, line, and token numbers.
*/
template <typename C = std::function<void(tf::Pipeflow&)>>
class Pipe {
template <typename... Ps>
friend class Pipeline;
template <typename P>
friend class ScalablePipeline;
public:
/**
@brief alias of the callable type
*/
using callable_t = C;
/**
@brief default constructor
*/
Pipe() = default;
/**
@brief constructs the pipe object
@param d pipe type (tf::PipeType)
@param callable callable type
The constructor constructs a pipe with the given direction
(tf::PipeType::SERIAL or tf::PipeType::PARALLEL) and the given callable.
The callable must take a referenced tf::Pipeflow object in the first argument.
@code{.cpp}
Pipe{PipeType::SERIAL, [](tf::Pipeflow&){}}
@endcode
When creating a pipeline, the direction of the first pipe must be serial
(tf::PipeType::SERIAL).
*/
Pipe(PipeType d, C&& callable) :
_type{d}, _callable{std::forward<C>(callable)} {
}
/**
@brief queries the type of the pipe
Returns the type of the callable.
*/
PipeType type() const {
return _type;
}
/**
@brief assigns a new type to the pipe
@param type a tf::PipeType variable
*/
void type(PipeType type) {
_type = type;
}
/**
@brief assigns a new callable to the pipe
@tparam U callable type
@param callable a callable object constructible from std::function<void(tf::Pipeflow&)>
Assigns a new callable to the pipe with universal forwarding.
*/
template <typename U>
void callable(U&& callable) {
_callable = std::forward<U>(callable);
}
private:
PipeType _type;
C _callable;
};
// ----------------------------------------------------------------------------
// Class Definition: Pipeline
// ----------------------------------------------------------------------------
/**
@class Pipeline
@brief class to create a pipeline scheduling framework
@tparam Ps pipe types
A tf::Pipeline is a composable graph object that allows users to parallelize an application
using pipeline parallelism.
Unlike conventional pipeline programming frameworks (e.g., Intel TBB),
tf::Pipeline does not provide any data abstraction but a task-parallel framework
for users to customize their data layout when leveraging pipeline parallelism.
The following code creates a pipeline with four parallel lines that schedule
five tokens through three pipes of a serial-parallel-serial direction:
@code{.cpp}
tf::Taskflow taskflow;
tf::Executor executor;
const size_t num_lines = 4;
const size_t num_pipes = 3;
// create a custom data buffer
std::array<std::array<int, num_pipes>, num_lines> buffer;
// create a pipeline graph of four concurrent lines and three serial pipes
tf::Pipeline pipeline(num_lines,
// first pipe must define a serial direction
tf::Pipe{tf::PipeType::SERIAL, [&buffer](tf::Pipeflow& pf) {
// generate only 5 scheduling tokens
if(pf.token() == 5) {
pf.stop();
}
// save the token id into the buffer
else {
buffer[pf.line()][pf.pipe()] = pf.token();
}
}},
tf::Pipe{tf::PipeType::PARALLEL, [&buffer] (tf::Pipeflow& pf) {
// propagate the previous result to this pipe by adding one
buffer[pf.line()][pf.pipe()] = buffer[pf.line()][pf.pipe()-1] + 1;
}},
tf::Pipe{tf::PipeType::SERIAL, [&buffer](tf::Pipeflow& pf){
// propagate the previous result to this pipe by adding one
buffer[pf.line()][pf.pipe()] = buffer[pf.line()][pf.pipe()-1] + 1;
}}
);
// build the pipeline graph using composition
tf::Task init = taskflow.emplace([](){ std::cout << "ready\n"; })
.name("starting pipeline");
tf::Task task = taskflow.composed_of(pipeline)
.name("pipeline");
tf::Task stop = taskflow.emplace([](){ std::cout << "stopped\n"; })
.name("pipeline stopped");
// create task dependency
init.precede(task);
task.precede(stop);
// run the pipeline
executor.run(taskflow).wait();
@endcode
The five tokens will be scheduled across four parallel lines in a circular fashion,
as depicted below:
@dotfile images/pipeline_our_structure.dot
At each pipe stage, the program propagates the result to the next pipe
by adding one to the result stored in a custom data storage, @c buffer.
The pipeline scheduler will generate five scheduling tokens and then stop.
Internally, tf::Pipeline uses std::tuple to store the given sequence of pipes.
The definition of each pipe can be different, completely decided by the compiler
to optimize the object layout.
After a pipeline is constructed, it is not possible to change its pipes.
If applications need to change these pipes, please use tf::ScalablePipeline.
*/
template <typename... Ps>
class Pipeline {
static_assert(sizeof...(Ps)>0, "must have at least one pipe");
/**
@private
*/
struct Line {
std::atomic<size_t> join_counter;
};
/**
@private
*/
struct PipeMeta {
PipeType type;
};
public:
/**
@brief constructs a pipeline object
@param num_lines the number of parallel lines
@param ps a list of pipes
Constructs a pipeline of up to @c num_lines parallel lines to schedule
tokens through the given linear chain of pipes.
The first pipe must define a serial direction (tf::PipeType::SERIAL)
or an exception will be thrown.
*/
Pipeline(size_t num_lines, Ps&&... ps);
/**
@brief constructs a pipeline object
@param num_lines the number of parallel lines
@param ps a tuple of pipes
Constructs a pipeline of up to @c num_lines parallel lines to schedule
tokens through the given linear chain of pipes.
The first pipe must define a serial direction (tf::PipeType::SERIAL)
or an exception will be thrown.
*/
Pipeline(size_t num_lines, std::tuple<Ps...>&& ps);
/**
@brief queries the number of parallel lines
The function returns the number of parallel lines given by the user
upon the construction of the pipeline.
The number of lines represents the maximum parallelism this pipeline
can achieve.
*/
size_t num_lines() const noexcept;
/**
@brief queries the number of pipes
The Function returns the number of pipes given by the user
upon the construction of the pipeline.
*/
constexpr size_t num_pipes() const;
/**
@brief resets the pipeline
Resetting the pipeline to the initial state. After resetting a pipeline,
its token identifier will start from zero as if the pipeline was just
constructed.
*/
void reset();
/**
@brief queries the number of generated tokens in the pipeline
The number represents the total scheduling tokens that has been
generated by the pipeline so far.
*/
size_t num_tokens() const noexcept;
/**
@brief obtains the graph object associated with the pipeline construct
This method is primarily used as an opaque data structure for creating
a module task of the this pipeline.
*/
Graph& graph();
private:
Graph _graph;
size_t _num_tokens;
std::tuple<Ps...> _pipes;
std::array<PipeMeta, sizeof...(Ps)> _meta;
std::vector<std::array<Line, sizeof...(Ps)>> _lines;
std::vector<Task> _tasks;
std::vector<Pipeflow> _pipeflows;
template <size_t... I>
auto _gen_meta(std::tuple<Ps...>&&, std::index_sequence<I...>);
void _on_pipe(Pipeflow&, NonpreemptiveRuntime&);
void _build();
};
// constructor
template <typename... Ps>
Pipeline<Ps...>::Pipeline(size_t num_lines, Ps&&... ps) :
_pipes {std::make_tuple(std::forward<Ps>(ps)...)},
_meta {PipeMeta{ps.type()}...},
_lines (num_lines),
_tasks (num_lines + 1),
_pipeflows (num_lines) {
if(num_lines == 0) {
TF_THROW("must have at least one line");
}
if(std::get<0>(_pipes).type() != PipeType::SERIAL) {
TF_THROW("first pipe must be serial");
}
reset();
_build();
}
// constructor
template <typename... Ps>
Pipeline<Ps...>::Pipeline(size_t num_lines, std::tuple<Ps...>&& ps) :
_pipes {std::forward<std::tuple<Ps...>>(ps)},
_meta {_gen_meta(
std::forward<std::tuple<Ps...>>(ps), std::make_index_sequence<sizeof...(Ps)>{}
)},
_lines (num_lines),
_tasks (num_lines + 1),
_pipeflows (num_lines) {
if(num_lines == 0) {
TF_THROW("must have at least one line");
}
if(std::get<0>(_pipes).type() != PipeType::SERIAL) {
TF_THROW("first pipe must be serial");
}
reset();
_build();
}
// Function: _get_meta
template <typename... Ps>
template <size_t... I>
auto Pipeline<Ps...>::_gen_meta(std::tuple<Ps...>&& ps, std::index_sequence<I...>) {
return std::array{PipeMeta{std::get<I>(ps).type()}...};
}
// Function: num_lines
template <typename... Ps>
size_t Pipeline<Ps...>::num_lines() const noexcept {
return _pipeflows.size();
}
// Function: num_pipes
template <typename... Ps>
constexpr size_t Pipeline<Ps...>::num_pipes() const {
return sizeof...(Ps);
}
// Function: num_tokens
template <typename... Ps>
size_t Pipeline<Ps...>::num_tokens() const noexcept {
return _num_tokens;
}
// Function: graph
template <typename... Ps>
Graph& Pipeline<Ps...>::graph() {
return _graph;
}
// Function: reset
template <typename... Ps>
void Pipeline<Ps...>::reset() {
_num_tokens = 0;
for(size_t l = 0; l<num_lines(); l++) {
_pipeflows[l]._pipe = 0;
_pipeflows[l]._line = l;
}
_lines[0][0].join_counter.store(0, std::memory_order_relaxed);
for(size_t l=1; l<num_lines(); l++) {
for(size_t f=1; f<num_pipes(); f++) {
_lines[l][f].join_counter.store(
static_cast<size_t>(_meta[f].type), std::memory_order_relaxed
);
}
}
for(size_t f=1; f<num_pipes(); f++) {
_lines[0][f].join_counter.store(1, std::memory_order_relaxed);
}
for(size_t l=1; l<num_lines(); l++) {
_lines[l][0].join_counter.store(
static_cast<size_t>(_meta[0].type) - 1, std::memory_order_relaxed
);
}
}
// Procedure: _on_pipe
template <typename... Ps>
void Pipeline<Ps...>::_on_pipe(Pipeflow& pf, NonpreemptiveRuntime& rt) {
visit_tuple([&](auto&& pipe){
using callable_t = typename std::decay_t<decltype(pipe)>::callable_t;
if constexpr (std::is_invocable_v<callable_t, Pipeflow&>) {
pipe._callable(pf);
}
else if constexpr(std::is_invocable_v<callable_t, Pipeflow&, NonpreemptiveRuntime&>) {
pipe._callable(pf, rt);
}
else {
static_assert(dependent_false_v<callable_t>, "un-supported pipe callable type");
}
}, _pipes, pf._pipe);
}
// Procedure: _build
template <typename... Ps>
void Pipeline<Ps...>::_build() {
using namespace std::literals::string_literals;
FlowBuilder fb(_graph);
// init task
_tasks[0] = fb.emplace([this]() {
return static_cast<int>(_num_tokens % num_lines());
}).name("cond");
// line task
for(size_t l = 0; l < num_lines(); l++) {
_tasks[l + 1] = fb.emplace([this, l] (tf::NonpreemptiveRuntime& rt) mutable {
auto pf = &_pipeflows[l];
pipeline:
_lines[pf->_line][pf->_pipe].join_counter.store(
static_cast<size_t>(_meta[pf->_pipe].type), std::memory_order_relaxed
);
// First pipe does all jobs of initialization and token dependencies
if (pf->_pipe == 0) {
pf->_token = _num_tokens;
if (pf->_stop = false, _on_pipe(*pf, rt); pf->_stop == true) {
// here, the pipeline is not stopped yet because other
// lines of tasks may still be running their last stages
return;
}
++_num_tokens;
}
else {
_on_pipe(*pf, rt);
}
size_t c_f = pf->_pipe;
size_t n_f = (pf->_pipe + 1) % num_pipes();
size_t n_l = (pf->_line + 1) % num_lines();
pf->_pipe = n_f;
// ---- scheduling starts here ----
// Notice that the shared variable f must not be changed after this
// point because it can result in data race due to the following
// condition:
//
// a -> b
// | |
// v v
// c -> d
//
// d will be spawned by either c or b, so if c changes f but b spawns d
// then data race on f will happen
std::array<int, 2> retval;
size_t n = 0;
// downward dependency
if(_meta[c_f].type == PipeType::SERIAL &&
_lines[n_l][c_f].join_counter.fetch_sub(
1, std::memory_order_acq_rel) == 1
) {
retval[n++] = 1;
}
// forward dependency
if(_lines[pf->_line][n_f].join_counter.fetch_sub(
1, std::memory_order_acq_rel) == 1
) {
retval[n++] = 0;
}
// notice that the task index starts from 1
switch(n) {
case 2: {
rt.schedule(_tasks[n_l+1]);
goto pipeline;
}
case 1: {
// downward dependency
if (retval[0] == 1) {
pf = &_pipeflows[n_l];
}
// forward dependency
goto pipeline;
}
}
}).name("nprt-"s + std::to_string(l));
_tasks[0].precede(_tasks[l+1]);
}
}
// ----------------------------------------------------------------------------
// Class Definition: ScalablePipeline
// ----------------------------------------------------------------------------
/**
@class ScalablePipeline
@brief class to create a scalable pipeline object
@tparam P type of the iterator to a range of pipes
A scalable pipeline is a composable graph object for users to create a
<i>pipeline scheduling framework</i> using a module task in a taskflow.
Unlike tf::Pipeline that instantiates all pipes upon the construction time,
tf::ScalablePipeline allows variable assignments of pipes using range iterators.
Users can also reset a scalable pipeline to a different range of pipes
between runs. The following code creates a scalable pipeline of four
parallel lines to schedule tokens through three serial pipes in a custom storage,
then resetting the pipeline to a new range of five serial pipes:
@code{.cpp}
tf::Taskflow taskflow("pipeline");
tf::Executor executor;
const size_t num_lines = 4;
// create data storage
std::array<int, num_lines> buffer;
// define the pipe callable
auto pipe_callable = [&buffer] (tf::Pipeflow& pf) mutable {
switch(pf.pipe()) {
// first stage generates only 5 scheduling tokens and saves the
// token number into the buffer.
case 0: {
if(pf.token() == 5) {
pf.stop();
}
else {
printf("stage 1: input token = %zu\n", pf.token());
buffer[pf.line()] = pf.token();
}
return;
}
break;
// other stages propagate the previous result to this pipe and
// increment it by one
default: {
printf(
"stage %zu: input buffer[%zu] = %d\n", pf.pipe(), pf.line(), buffer[pf.line()]
);
buffer[pf.line()] = buffer[pf.line()] + 1;
}
break;
}
};
// create a vector of three pipes
std::vector< tf::Pipe<std::function<void(tf::Pipeflow&)>> > pipes;
for(size_t i=0; i<3; i++) {
pipes.emplace_back(tf::PipeType::SERIAL, pipe_callable);
}
// create a pipeline of four parallel lines based on the given vector of pipes
tf::ScalablePipeline pl(num_lines, pipes.begin(), pipes.end());
// build the pipeline graph using composition
tf::Task init = taskflow.emplace([](){ std::cout << "ready\n"; })
.name("starting pipeline");
tf::Task task = taskflow.composed_of(pl)
.name("pipeline");
tf::Task stop = taskflow.emplace([](){ std::cout << "stopped\n"; })
.name("pipeline stopped");
// create task dependency
init.precede(task);
task.precede(stop);
// dump the pipeline graph structure (with composition)
taskflow.dump(std::cout);
// run the pipeline
executor.run(taskflow).wait();
// reset the pipeline to a new range of five pipes and starts from
// the initial state (i.e., token counts from zero)
for(size_t i=0; i<2; i++) {
pipes.emplace_back(tf::PipeType::SERIAL, pipe_callable);
}
pl.reset(pipes.begin(), pipes.end());
executor.run(taskflow).wait();
@endcode
The above example creates a pipeline graph that schedules five tokens over
four parallel lines in a circular fashion, first going through three serial pipes
and then five serial pipes:
@code{.bash}
# initial construction of three serial pipes
o -> o -> o
| | |
v v v
o -> o -> o
| | |
v v v
o -> o -> o
| | |
v v v
o -> o -> o
# resetting to a new range of five serial pipes
o -> o -> o -> o -> o
| | | | |
v v v v v
o -> o -> o -> o -> o
| | | | |
v v v v v
o -> o -> o -> o -> o
| | | | |
v v v v v
o -> o -> o -> o -> o
@endcode
Each pipe has the same type of `%tf::Pipe<%std::function<void(%tf::Pipeflow&)>>`
and is kept in a vector that is amenable to change.
We construct the scalable pipeline using two range iterators pointing to the
beginning and the end of the vector.
At each pipe stage, the program propagates the result to the next pipe
by adding one to the result stored in a custom data storage, @c buffer.
The pipeline scheduler will generate five scheduling tokens and then stop.
A scalable pipeline is move-only.
*/
template <typename P>
class ScalablePipeline {
/**
@private
*/
struct Line {
std::atomic<size_t> join_counter;
};
public:
/**
@brief pipe type
*/
using pipe_t = typename std::iterator_traits<P>::value_type;
/**
@brief default constructor
*/
ScalablePipeline() = default;
/**
@brief constructs an empty scalable pipeline object
@param num_lines the number of parallel lines
An empty scalable pipeline does not have any pipes.
The pipeline needs to be reset to a valid range of pipes
before running.
*/
ScalablePipeline(size_t num_lines);
/**
@brief constructs a scalable pipeline object
@param num_lines the number of parallel lines
@param first iterator to the beginning of the range
@param last iterator to the end of the range
Constructs a pipeline from the given range of pipes specified in
<tt>[first, last)</tt> using @c num_lines parallel lines.
The first pipe must define a serial direction (tf::PipeType::SERIAL)
or an exception will be thrown.
Internally, the scalable pipeline copies the iterators
from the specified range. Those pipe callables pointed to by
these iterators must remain valid during the execution of the pipeline.
*/
ScalablePipeline(size_t num_lines, P first, P last);
/**
@brief disabled copy constructor
*/
ScalablePipeline(const ScalablePipeline&) = delete;
/**
@brief move constructor
Constructs a pipeline from the given @c rhs using move semantics
(i.e. the data in @c rhs is moved into this pipeline).
After the move, @c rhs is in a state as if it is just constructed.
The behavior is undefined if @c rhs is running during the move.
*/
ScalablePipeline(ScalablePipeline&& rhs);
/**
@brief disabled copy assignment operator
*/
ScalablePipeline& operator = (const ScalablePipeline&) = delete;
/**
@brief move constructor
Replaces the contents with those of @c rhs using move semantics
(i.e. the data in @c rhs is moved into this pipeline).
After the move, @c rhs is in a state as if it is just constructed.
The behavior is undefined if @c rhs is running during the move.
*/
ScalablePipeline& operator = (ScalablePipeline&& rhs);
/**
@brief queries the number of parallel lines
The function returns the number of parallel lines given by the user
upon the construction of the pipeline.
The number of lines represents the maximum parallelism this pipeline
can achieve.
*/
size_t num_lines() const noexcept;
/**
@brief queries the number of pipes
The Function returns the number of pipes given by the user
upon the construction of the pipeline.
*/
size_t num_pipes() const noexcept;
/**
@brief resets the pipeline
Resets the pipeline to the initial state. After resetting a pipeline,
its token identifier will start from zero.
*/
void reset();
/**
@brief resets the pipeline with a new range of pipes
@param first iterator to the beginning of the range
@param last iterator to the end of the range
The member function assigns the pipeline to a new range of pipes
specified in <tt>[first, last)</tt> and resets the pipeline to the
initial state. After resetting a pipeline, its token identifier will
start from zero.
Internally, the scalable pipeline copies the iterators
from the specified range. Those pipe callables pointed to by
these iterators must remain valid during the execution of the pipeline.
*/
void reset(P first, P last);
/**
@brief resets the pipeline to a new line number and a
new range of pipes
@param num_lines number of parallel lines
@param first iterator to the beginning of the range
@param last iterator to the end of the range
The member function resets the pipeline to a new number of
parallel lines and a new range of pipes specified in
<tt>[first, last)</tt>, as if the pipeline is just constructed.
After resetting a pipeline, its token identifier will start from zero.
Internally, the scalable pipeline copies the iterators
from the specified range. Those pipe callables pointed to by
these iterators must remain valid during the execution of the pipeline.
*/
void reset(size_t num_lines, P first, P last);
/**
@brief queries the number of generated tokens in the pipeline
The number represents the total scheduling tokens that has been
generated by the pipeline so far.
*/
size_t num_tokens() const noexcept;
/**
@brief obtains the graph object associated with the pipeline construct
This method is primarily used as an opaque data structure for creating
a module task of the this pipeline.
*/
Graph& graph();
private:
Graph _graph;
size_t _num_tokens{0};
std::vector<P> _pipes;
std::vector<Task> _tasks;
std::vector<Pipeflow> _pipeflows;
std::unique_ptr<Line[]> _lines;
void _on_pipe(Pipeflow&, NonpreemptiveRuntime&);
void _build();
Line& _line(size_t, size_t);
};
// constructor
template <typename P>
ScalablePipeline<P>::ScalablePipeline(size_t num_lines) :
_tasks (num_lines + 1),
_pipeflows (num_lines) {
if(num_lines == 0) {
TF_THROW("must have at least one line");
}
_build();
}
// constructor
template <typename P>
ScalablePipeline<P>::ScalablePipeline(size_t num_lines, P first, P last) :
_tasks (num_lines + 1),
_pipeflows (num_lines) {
if(num_lines == 0) {
TF_THROW("must have at least one line");
}
reset(first, last);
_build();
}
// move constructor
template <typename P>
ScalablePipeline<P>::ScalablePipeline(ScalablePipeline&& rhs):
_num_tokens {rhs._num_tokens},
_pipes {std::move(rhs._pipes)},
_pipeflows {std::move(rhs._pipeflows)},
_lines {std::move(rhs._lines)} {
_graph.clear();
_tasks.resize(_pipeflows.size()+1);
rhs._num_tokens = 0;
rhs._tasks.clear();
_build();
}
// move assignment operator
template <typename P>
ScalablePipeline<P>& ScalablePipeline<P>::operator = (ScalablePipeline&& rhs) {
_num_tokens = rhs._num_tokens;
_pipes = std::move(rhs._pipes);
_pipeflows = std::move(rhs._pipeflows);
_lines = std::move(rhs._lines);