TaskParallelPipeline Task-parallel Pipeline Include the Header TaskParallelPipeline_1TaskParallelPipelineIncludeHeaderFile Understand the Pipeline Scheduling Framework TaskParallelPipeline_1UnderstandPipelineScheduling Create a Task-parallel Pipeline Module Task TaskParallelPipeline_1CreateATaskParallelPipelineModuleTask Connect Pipeline with Other Tasks TaskParallelPipeline_1ConnectWithTasks Example 1: Iterate a Pipeline TaskParallelPipeline_1IterateAPipeline Example 2: Concatenate Two Pipelines TaskParallelPipeline_1ConcatenateTwoPipelines Example 3: Define Multiple Parallel Pipelines TaskParallelPipeline_1DefineMultipleTaskParallelPipelines Reset a Pipeline TaskParallelPipeline_1ResetPipeline Learn More about Taskflow Pipeline TaskParallelPipeline_1TaskParallelPipelineLearnMore Taskflow provides a task-parallel pipeline programming framework for you to implement a pipeline algorithm. Pipeline parallelism refers to a parallel execution of multiple data tokens through a linear chain of pipes or stages. Each stage processes the data token sent from the previous stage, applies the given callable to that data token, and then sends the result to the next stage. Multiple data tokens can be processed simultaneously across different stages. Include the Header You need to include the header file, taskflow/algorithm/pipeline.hpp, for implementing task-parallel pipeline algorithms. #include<taskflow/algorithm/pipeline.hpp> Understand the Pipeline Scheduling Framework A tf::Pipeline object is a composable graph to create a pipeline scheduling framework through a module task in a taskflow (see Composable Tasking). Unlike the conventional pipeline programming frameworks (e.g., Intel TBB Parallel Pipeline), Taskflow's pipeline algorithm does not provide any data abstraction, which often restricts users from optimizing data layouts in their applications, but a flexible framework for users to customize their application data atop an efficient pipeline scheduling framework. The figure above gives an example of our pipeline scheduling framework. The framework consists of three pipes (serial-parallel-serial stages) and four lines (maximum parallelism), where each line processes at most one data token. A pipeline of three pipes and four lines will propagate each data token through a sequential chain of three pipes and can simultaneously process up to four data tokens at the four lines. Each edge represents a task dependency. For example, the edge from pipe-0 to pipe-1 in line 0 represents the task dependency between the first and the second pipes in the first line; the edge from pipe-0 in line 0 to pipe-0 in line 1 represents the task dependency between two adjacent lines when processing two data tokens at the same pipe. Each pipe can be either a serial type or a parallel type, where a serial pipe processes data tokens sequentially and a parallel pipe processes different data tokens simultaneously. Due to the nature of pipeline, Taskflow requires the first pipe to be a serial type. The pipeline scheduling algorithm operates in a circular fashion with a factor of line count. Create a Task-parallel Pipeline Module Task Taskflow leverages modern C++ and template techniques to strike a balance between the expressiveness and generality in designing the pipeline programming model. In general, there are three steps to create a task-parallel pipeline application: Define the pipeline structure (e.g., pipe type, pipe callable, stopping rule, line count) Define the data storage and layout, if needed for the application Define the pipeline taskflow graph using composition The following code creates a pipeline scheduling framework using the example from the previous section. The framework schedules a total of five scheduling tokens labeled from 0 to 4. The first pipe stores the token identifier in a custom data storage, buffer, and each of the rest pipes adds one to the input data from the result of the previous pipe and stores the result into the corresponding line entry in the buffer. 1:tf::Taskflowtaskflow; 2:tf::Executorexecutor; 3: 4://maximumparallelism-eachlineprocessesonetokenatatime 5:constsize_tnum_lines=4; 6: 7://customdatastorage 8:std::array<int, num_lines>buffer; 9: 10://thepipelineconsistsofthreepipes(serial-parallel-serial) 11://anduptofourconcurrentschedulingtokens 12:tf::Pipelinepl(num_lines, 13:tf::Pipe{tf::PipeType::SERIAL,[&buffer](tf::Pipeflow&pf){ 14://generateonly5schedulingtokens 15:if(pf.token()==5){ 16:pf.stop(); 17:} 18://savetheresultofthispipeintothebuffer 19:else{ 20:printf("pipe0:inputtoken=%zu\n",pf.token()); 21:buffer[pf.line()]=pf.token(); 22:} 23:}}, 24: 25:tf::Pipe{tf::PipeType::PARALLEL,[&buffer](tf::Pipeflow&pf){ 26:printf( 27:"pipe1:inputbuffer[%zu]=%d\n", 28:pf.line(),buffer[pf.line()] 29:); 30://propagatethepreviousresulttothispipebyaddingone 31:buffer[pf.line()]=buffer[pf.line()]+1; 32:}}, 33: 34:tf::Pipe{tf::PipeType::SERIAL,[&buffer](tf::Pipeflow&pf){ 35:printf( 36:"pipe2:inputbuffer[%zu]=%d\n", 37:pf.line(),buffer[pf.line()] 38:); 39://propagatethepreviousresulttothispipebyaddingone 40:buffer[pf.line()]=buffer[pf.line()]+1; 41:}} 42:); 43: 44://buildthepipelinegraphusingcomposition 45:tf::Taskpipeline=taskflow.composed_of(pl).name("pipeline"); 46: 47://executethetaskflow 48:executor.run(taskflow).wait(); Debrief: Lines 4-5 define the structure of the pipeline scheduling framework Line 8 defines the data storage as an one-dimensional array of num_lines integers Line 12 defines the number of lines in the pipeline Lines 13-23 define the first serial pipe, which will stop the pipeline scheduling at the fifth token Lines 25-32 define the second parallel pipe Lines 34-41 define the third serial pipe Line 45 defines the pipeline taskflow graph using composition Line 48 executes the taskflow Taskflow leverages Interact with the Runtime and Composable Tasking to implement the pipeline scheduling framework. The taskflow graph of this pipeline example is shown as follows, where 1) one condition task is used to decide which runtime task to run and 2) four runtime tasks are used to schedule tokens at four parallel lines, respectively. In this example, we customize the data storage, buffer, as an one-dimensional array of 4 integers, since the pipeline structure defines only four parallel lines. Each entry of buffer stores stores the data being processed in the corresponding line. For example, buffer[1] stores the processed data at line 1. The following figure shows the data layout of buffer. In practice, you may need to add padding to the data type of the buffer or align it with the cacheline size to avoid false sharing. If the data type varies at different pipes, you can use std::variant to store the data types in a uniform storage. For each scheduling token, you can use tf::Pipeflow::line() to get its line identifier and tf::Pipeflow::pipe() to get its pipe identifier. For example, if a scheduling token is at the third pipe of the forth line, tf::Pipeflow::line() will return 3 and tf::Pipeflow::pipe() will return 2 (index starts from 0). To stop the execution of the pipeline, you need to call tf::Pipeflow::stop() at the first pipe. Once the stop signal has been triggered, the pipeline will stop scheduling any new tokens after the callable. As we can see from this example, tf::Pipeline gives you the full control to customize your application data on top of a pipeline scheduling framework. Calling tf::Pipeflow::stop() not at the first pipe has no effect on the pipeline scheduling. In most cases, std::thread::hardware_concurrency is a good number for line count. Our pipeline algorithm schedules tokens in a circular manner, with a factor of num_lines. That is, token t will be processed at line t % num_lines. The following snippet shows one of the possible outputs of this pipeline program: pipe0:inputtoken=0 pipe1:inputbuffer[0]=0 pipe2:inputbuffer[0]=1 pipe0:inputtoken=1 pipe1:inputbuffer[1]=1 pipe2:inputbuffer[1]=2 pipe0:inputtoken=2 pipe1:inputbuffer[2]=2 pipe2:inputbuffer[2]=3 pipe0:inputtoken=3 pipe1:inputbuffer[3]=3 pipe2:inputbuffer[3]=4 pipe0:inputtoken=4 pipe1:inputbuffer[0]=4 pipe2:inputbuffer[0]=5 There are a total of five tokens running through three pipes. Each pipes prints its input data value, except the first pipe that prints its token identifier. Since the second pipe is a parallel pipe, the output can interleave. Connect Pipeline with Other Tasks You can connect the pipeline module task with other tasks to create a taskflow application that embeds one or multiple pipeline algorithms. We describe three common examples below: Example 1: Iterate a Pipeline Example 2: Concatenate Two Pipelines Example 3: Define Multiple Parallel Pipelines Example 1: Iterate a Pipeline This example emulates a data streaming application that iteratively runs a stream of data through a pipeline using conditional tasking. The taskflow graph consists of one pipeline module task and one condition task. The pipeline module task processes a stream of data. The condition task decides the availability of data and reruns the pipeline when the next stream of data becomes available. 1:tf::Taskflowtaskflow; 2:tf::Executorexecutor; 3: 4:constsize_tnum_lines=4;//maximumparallelismofthepipeline 5: 6:inti=0,N=0; 7://customdatastorage 8:std::array<int, num_lines>buffer; 9: 10://thepipelineconsistsofthreepipes(serial-parallel-serial) 11://anduptofourconcurrentschedulingtokens 12:tf::Pipelinepl(num_lines, 13:tf::Pipe{tf::PipeType::SERIAL,[&i,&buffer](tf::Pipeflow&pf){ 14://only5schedulingtokensareprocessed 15:if(i++==5){ 16:pf.stop(); 17:} 18://savetheresultofthispipeintothebuffer 19:else{ 20:printf("stage0:inputtoken=%zu\n",pf.token()); 21:buffer[pf.line()]=pf.token(); 22:} 23:}}, 24: 25:tf::Pipe{tf::PipeType::PARALLEL,[&buffer](tf::Pipeflow&pf){ 26:printf( 27:"stage1:inputbuffer[%zu]=%d\n", 28:pf.line(),buffer[pf.line()] 29:); 30://propagatethepreviousresulttothispipebyaddingone 31:buffer[pf.line()]=buffer[pf.line()]+1; 32:}}, 33: 34:tf::Pipe{tf::PipeType::SERIAL,[&buffer](tf::Pipeflow&pf){ 35:printf( 36:"stage2:inputbuffer[%zu]=%d\n", 37:pf.line(),buffer[pf.line()] 38:); 39://propagatethepreviousresulttothispipebyaddingone 40:buffer[pf.line()]=buffer[pf.line()]+1; 41:}} 42:); 43: 44:tf::Taskconditional=taskflow.emplace([&N,&i](){ 45:i=0; 46:if(++N<2){ 47:std::cout<<"Rerunthepipeline\n"; 48:return0; 49:} 50:else{ 51:return1; 52:} 53:}).name("conditional"); 54: 55://buildthepipelinegraphusingcomposition 56:tf::Taskpipeline=taskflow.composed_of(pl) 57:.name("pipeline"); 58:tf::Taskinitial=taskflow.emplace([](){std::cout<<"initial\n";}) 59:.name("initial"); 60:tf::Taskstop=taskflow.emplace([](){std::cout<<"stop\n";}) 61:.name("stop"); 62: 63://specifythegraphdependency 64:initial.precede(pipeline); 65:pipeline.precede(conditional); 66:conditional.precede(pipeline,stop); 67: 68://executethetaskflow 69:executor.run(taskflow).wait(); Debrief: Lines 4-5 define the structure of the pipeline scheduling framework Line 8 defines the data storage as an one-dimensional array (num_lines integers) Line 12 defines the number of lines in the pipeline Lines 13-23 define the first serial pipe, which will stop the pipeline scheduling when i is 5 Lines 25-32 define the second parallel pipe Lines 34-41 define the third serial pipe Lines 44-53 define a condition task which returns 0 when N is less than 2, otherwise returns 1 Line 45 resets variable i Lines 56-57 define the pipeline graph using composition Lines 58-61 define two static tasks Line 64-66 define the task dependency Line 69 executes the taskflow The taskflow graph of this pipeline example is illustrated as follows: The following snippet shows one of the possible outputs: initial stage0:inputtoken=0 stage1:inputbuffer[0]=0 stage2:inputbuffer[0]=1 stage0:inputtoken=1 stage1:inputbuffer[1]=1 stage2:inputbuffer[1]=2 stage0:inputtoken=2 stage1:inputbuffer[2]=2 stage2:inputbuffer[2]=3 stage0:inputtoken=3 stage1:inputbuffer[3]=3 stage2:inputbuffer[3]=4 stage0:inputtoken=4 stage1:inputbuffer[0]=4 stage2:inputbuffer[0]=5 Rerunthepipeline stage0:inputtoken=5 stage1:inputbuffer[1]=5 stage2:inputbuffer[1]=6 stage0:inputtoken=6 stage1:inputbuffer[2]=6 stage2:inputbuffer[2]=7 stage0:inputtoken=7 stage1:inputbuffer[3]=7 stage2:inputbuffer[3]=8 stage0:inputtoken=8 stage1:inputbuffer[0]=8 stage2:inputbuffer[0]=9 stage0:inputtoken=9 stage1:inputbuffer[1]=9 stage2:inputbuffer[1]=10 stop The pipeline runs twice as controlled by the condition task conditional. The starting token in the second run of the pipeline is 5 rather than 0 because the pipeline keeps a stateful number of tokens. The last token is 9, which means the pipeline processes in total 10 scheduling tokens. The first five tokens (token 0 to 4) are processed in the first run, and the remaining five tokens (token 5 to 9) are processed in the second run. In the condition task, we use N as a decision-making counter to process the next stream of data. Example 2: Concatenate Two Pipelines This example demonstrates two concatenated pipelines where a sequence of data tokens run synchronously from one pipeline to another pipeline. The first pipeline task precedes the second pipeline task. 1:tf::Taskflowtaskflow("pipeline"); 2:tf::Executorexecutor; 3: 4://definethemaximumparallelismofthepipeline 5:constsize_tnum_lines=4; 6: 7://customdatastorage 8:std::array<int, num_lines>buffer_1; 9:std::array<int, num_lines>buffer_2; 10: 11://thepipeline_1consistsofthreepipes(serial-parallel-serial) 12://anduptofourconcurrentschedulingtokens 13:tf::Pipelinepl_1(num_lines, 14:tf::Pipe{tf::PipeType::SERIAL,[&buffer_1](tf::Pipeflow&pf)mutable{ 15://generateonly4schedulingtokens 16:if(pf.token()==4){ 17:pf.stop(); 18:} 19://savetheresultofthispipeintothebuffer 20:else{ 21:printf("pipeline1,pipe0:inputtoken=%zu\n",pf.token()); 22:buffer_1[pf.line()]=pf.token(); 23:} 24:}}, 25: 26:tf::Pipe{tf::PipeType::PARALLEL,[&buffer_1](tf::Pipeflow&pf){ 27:printf( 28:"pipeline1,pipe1:inputbuffer_1[%zu]=%d\n", 29:pf.line(),buffer_1[pf.line()] 30:); 31://propagatethepreviousresulttothispipebyaddingone 32:buffer_1[pf.line()]=buffer_1[pf.line()]+1; 33:}}, 34: 35:tf::Pipe{tf::PipeType::SERIAL,[&buffer_1](tf::Pipeflow&pf){ 36:printf( 37:"pipeline1,pipe2:inputbuffer_1[%zu]=%d\n", 38:pf.line(),buffer_1[pf.line()] 39:); 40://propagatethepreviousresulttothispipebyaddingone 41:buffer_1[pf.line()]=buffer_1[pf.line()]+1; 42:}} 43:); 44: 45://thepipeline_2consistsofthreepipes(serial-parallel-serial) 46://anduptofourconcurrentschedulingtokens 47:tf::Pipelinepl_2(num_lines, 48:tf::Pipe{tf::PipeType::SERIAL, 49:[&buffer_2,&buffer_1](tf::Pipeflow&pf)mutable{ 50://generateonly4schedulingtokens 51:if(pf.token()==4){ 52:pf.stop(); 53:} 54://savetheresultofthispipeintothebuffer 55:else{ 56:printf("pipeline2,pipe0:inputvalue=%d\n",buffer_1[pf.line()]); 57:buffer_2[pf.line()]=buffer_1[pf.line()]; 58:} 59:}}, 60: 61:tf::Pipe{tf::PipeType::PARALLEL,[&buffer_2](tf::Pipeflow&pf){ 62:printf( 63:"pipeline2,pipe1:inputbuffer_2[%zu]=%d\n", 64:pf.line(),buffer_2[pf.line()] 65:); 66://propagatethepreviousresulttothispipebyadding1 67:buffer_2[pf.line()]=buffer_2[pf.line()]+1; 68:}}, 69: 70:tf::Pipe{tf::PipeType::SERIAL,[&buffer_2](tf::Pipeflow&pf){ 71:printf( 72:"pipeline2,pipe2:inputbuffer_2[%zu]=%d\n", 73:pf.line(),buffer_2[pf.line()] 74:); 75://propagatethepreviousresulttothispipebyadding1 76:buffer_2[pf.line()]=buffer_2[pf.line()]+1; 77:}} 78:); 79: 80://buildthepipelinegraphusingcomposition 81:tf::Taskpipeline_1=taskflow.composed_of(pl_1).name("pipeline_1"); 82:tf::Taskpipeline_2=taskflow.composed_of(pl_2).name("pipeline_2"); 83: 84://specifythegraphdependency 85:pipeline_1.precede(pipeline_2); 86: 87://executethetaskflow 88:executor.run(taskflow).wait(); Debrief: Line 8 defines the data storage (num_lines integers) for pipeline pl_1 Line 9 defines the data storage (num_lines integers) for pipeline pl_2 Lines 14-24 define the first serial pipe in pl_1 Lines 26-33 define the second parallel pipe in pl_1 Lines 35-42 define the third serial pipe in pl_1 Lines 48-59 define the first serial pipe in pl_2 that takes the results of pl_1 as inputs Lines 61-68 define the second parallel pipe in pl_2 Lines 70-77 define the third serial pipe in pl_2 Lines 81-82 define the pipeline graphs using composition Line 85 defines the task dependency Line 88 runs the taskflow The taskflow graph of this pipeline example is illustrated as follows: The following snippet shows one of the possible outputs: pipeline1,pipe0:inputtoken=0 pipeline1,pipe1:inputbuffer_1[0]=0 pipeline1,pipe2:inputbuffer_1[0]=1 pipeline1,pipe0:inputtoken=1 pipeline1,pipe1:inputbuffer_1[1]=1 pipeline1,pipe2:inputbuffer_1[1]=2 pipeline1,pipe0:inputtoken=2 pipeline1,pipe1:inputbuffer_1[2]=2 pipeline1,pipe2:inputbuffer_1[2]=3 pipeline1,pipe0:inputtoken=3 pipeline1,pipe1:inputbuffer_1[3]=3 pipeline1,pipe2:inputbuffer_1[3]=4 pipeline2,pipe1:inputvalue=2 pipeline2,pipe2:inputbuffer_2[0]=2 pipeline2,pipe3:inputbuffer_2[0]=3 pipeline2,pipe1:inputvalue=3 pipeline2,pipe2:inputbuffer_2[1]=3 pipeline2,pipe3:inputbuffer_2[1]=4 pipeline2,pipe1:inputvalue=4 pipeline2,pipe2:inputbuffer_2[2]=4 pipeline2,pipe3:inputbuffer_2[2]=5 pipeline2,pipe1:inputvalue=5 pipeline2,pipe2:inputbuffer_2[3]=5 pipeline2,pipe3:inputbuffer_2[3]=6 The output of pipelines pl_1 and pl_2 can be different from run to run because their second pipes are both parallel types. Due to the task dependency between pipeline_1 and pipeline_2, the output of pl_1 precedes the output of pl_2. Example 3: Define Multiple Parallel Pipelines This example creates two independent pipelines that run in parallel on different data sets. 1:tf::Taskflowtaskflow("pipeline"); 2:tf::Executorexecutor; 3: 4://definethemaximumparallelismofthepipeline 5:constsize_tnum_lines=4; 6: 7://customdatastorage 8:std::array<int, num_lines>buffer_1; 9:std::array<int, num_lines>buffer_2; 10: 11://thepipeline_1consistsofthreepipes(serial-parallel-serial) 12://anduptofourconcurrentschedulingtokens 13:tf::Pipelinepl_1(num_lines, 14:tf::Pipe{tf::PipeType::SERIAL,[&buffer_1](tf::Pipeflow&pf)mutable{ 15://generateonly5schedulingtokens 16:if(pf.token()==5){ 17:pf.stop(); 18:} 19://savetheresultofthispipeintothebuffer 20:else{ 21:printf("pipeline1,pipe0:inputtoken=%zu\n",pf.token()); 22:buffer_1[pf.line()]=pf.token(); 23:} 24:}}, 25: 26:tf::Pipe{tf::PipeType::PARALLEL,[&buffer_1](tf::Pipeflow&pf){ 27:printf( 28:"pipeline1,pipe1:inputbuffer_1[%zu]=%d\n", 29:pf.line(),buffer_1[pf.line()] 30:); 31://propagatethepreviousresulttothispipebyaddingone 32:buffer_1[pf.line()]=buffer_1[pf.line()]+1; 33:}}, 34: 35:tf::Pipe{tf::PipeType::SERIAL,[&buffer_1](tf::Pipeflow&pf){ 36:printf( 37:"pipeline1,pipe2:inputbuffer_1[%zu]=%d\n", 38:pf.line(),buffer_1[pf.line()] 39:); 40://propagatethepreviousresulttothispipebyaddingone 41:buffer_1[pf.line()]=buffer_1[pf.line()]+1; 42:}} 43:); 44: 45://thepipeline_2consistsofthreepipes(serial-parallel-serial) 46://anduptofourconcurrentschedulingtokens 47:tf::Pipelinepl_2(num_lines, 48:tf::Pipe{tf::PipeType::SERIAL,[&buffer_2](tf::Pipeflow&pf)mutable{ 49://generateonly2schedulingtokens 50:if(pf.token()==5){ 51:pf.stop(); 52:} 53://savetheresultofthispipeintothebuffer 54:else{ 55:printf("pipeline2,pipe0:inputtoken=%zu\n",pf.token()); 56:buffer_2[pf.line()]="pipeline"; 57:} 58:}}, 59: 60:tf::Pipe{tf::PipeType::PARALLEL,[&buffer_2](tf::Pipeflow&pf){ 61:printf( 62:"pipeline2,pipe1:inputbuffer_2[%zu]=%d\n", 63:pf.line(),buffer_2[pf.line()] 64:); 65://propagatethepreviousresulttothispipebyconcatenating"_" 66:buffer_2[pf.line()]=buffer_2[pf.line()]; 67:}}, 68: 69:tf::Pipe{tf::PipeType::SERIAL,[&buffer_2](tf::Pipeflow&pf){ 70:printf( 71:"pipeline2,pipe2:inputbuffer_2[%zu]=%d\n", 72:pf.line(),buffer_2[pf.line()] 73:); 74://propagatethepreviousresulttothispipebyconcatenating"2" 75:buffer_2[pf.line()]=buffer_2[pf.line()]; 76:}} 77:); 78: 79:tf::Taskpipeline_1=taskflow.composed_of(pl_1) 80:.name("pipeline_1"); 81:tf::Taskpipeline_2=taskflow.composed_of(pl_2) 82:.name("pipeline_2"); 83:tf::Taskinitial=taskflow.emplace([](){std::cout<<"initial";}) 84:.name("initial"); 85: 86:initial.precede(pipeline_1,pipeline_2); 87: 88:executor.run(taskflow).wait(); Debrief: Line 8 defines the data storage (num_lines integers) for pipeline pl_1 Line 9 defines the data storage (num_lines integers) for pipeline pl_2 Lines 14-24 define the first serial pipe in pl_1 Lines 26-33 define the second parallel pipe in pl_1 Lines 35-42 define the third serial pipe in pl_1 Lines 48-58 define the first serial pipe in pl_2 Lines 60-67 define the second parallel pipe in pl_2 Lines 69-76 define the third serial pipe in pl_2 Lines 79-82 define the pipeline graphs using composition Lines 83-84 define a static task. Line 86 defines the task dependency Line 88 runs the taskflow The taskflow graph of this pipeline example is illustrated as follows: The following snippet shows one of the possible outputs: initial pipeline2,pipe0:inputtoken=0 pipeline2,pipe1:inputbuffer_2[0]=0 pipeline2,pipe2:inputbuffer_2[0]=1 pipeline1,pipe0:inputtoken=0 pipeline1,pipe1:inputbuffer_1[0]=0 pipeline1,pipe2:inputbuffer_1[0]=1 pipeline1,pipe0:inputtoken=1 pipeline1,pipe1:inputbuffer_1[1]=1 pipeline1,pipe0:inputtoken=2 pipeline1,pipe1:inputbuffer_1[2]=2 pipeline1,pipe0:inputtoken=3 pipeline1,pipe1:inputbuffer_1[3]=3 pipeline1,pipe0:inputtoken=4 pipeline1,pipe1:inputbuffer_1[0]=4 pipeline2,pipe0:inputtoken=1 pipeline2,pipe1:inputbuffer_2[1]=1 pipeline2,pipe0:inputbuffer_2[1]=2 pipeline2,pipe0:inputtoken=2 pipeline2,pipe1:inputbuffer_2[2]=2 pipeline2,pipe2:inputbuffer_2[2]=3 pipeline2,pipe0:inputtoken=3 pipeline2,pipe1:inputbuffer_2[3]=3 pipeline2,pipe2:inputbuffer_2[3]=4 pipeline2,pipe0:inputtoken=4 pipeline2,pipe1:inputbuffer_2[0]=4 pipeline2,pipe2:inputbuffer_2[0]=5 pipeline1,pipe2:inputbuffer_1[1]=2 pipeline1,pipe2:inputbuffer_1[2]=3 pipeline1,pipe2:inputbuffer_1[3]=4 pipeline1,pipe2:inputbuffer_1[0]=5 Because pipeline pl_1 and pipeline pl_2 are running in parallel, their outputs may interleave. Reset a Pipeline Our pipeline scheduling framework keeps a stateful number of scheduled tokens at each submitted run. You can reset the pipeline to the initial state using tf::Pipeline::reset(), where the number of scheduled tokens will start from zero in the next run. Borrowed from Example 1: Iterate a Pipeline, the program below resets the pipeline at the second iteration (inside the condition task) so the scheduling token will start from zero in the next run. tf::Taskflowtaskflow("pipeline"); tf::Executorexecutor; //definethemaximumparallelismofthepipeline constsize_tnum_lines=4; //customdatastorage std::array<int, num_lines>buffer; //thepipelineconsistsofthreepipes(serial-parallel-serial) //anduptofourconcurrentschedulingtokens tf::Pipelinepl(num_lines, tf::Pipe{tf::PipeType::SERIAL,[&buffer](tf::Pipeflow&pf)mutable{ //generateonly5schedulingtokens if(pf.token()==5){ pf.stop(); } //savetheresultofthispipeintothebuffer else{ printf("pipe0:inputtoken=%zu\n",pf.token()); buffer[pf.line()]=pf.token(); } }}, tf::Pipe{tf::PipeType::PARALLEL,[&buffer](tf::Pipeflow&pf){ printf( "pipe1:inputbuffer_1[%zu]=%d\n",pf.line(),buffer[pf.line()] ); //propagatethepreviousresulttothispipebyaddingone buffer[pf.line()]=buffer[pf.line()]+1; }}, tf::Pipe{tf::PipeType::SERIAL,[&buffer](tf::Pipeflow&pf){ printf( "pipe2:inputbuffer[%zu][%zu]=%d\n",pf.line(),buffer[pf.line()] ); //propagatethepreviousresulttothispipebyaddingone buffer[pf.line()]=buffer[pf.line()]+1; }} ); tf::Taskconditional=taskflow.emplace([&](){ if(++N<2){ pl.reset(); std::cout<<"Rerunthepipeline\n"; return0; } else{ return1; } }).name("conditional"); tf::Taskpipeline=taskflow.composed_of(pl) .name("pipeline"); tf::Taskinitial=taskflow.emplace([](){std::cout<<"initial";}) .name("initial"); tf::Taskstop=taskflow.emplace([](){std::cout<<"stop";}) .name("stop"); initial.precede(pipeline); pipeline.precede(conditional); conditional.precede(pipeline,stop); executor.run(taskflow).wait(); The following snippet shows one of the possible outputs: initial pipe0:inputtoken=0 pipe1:inputbuffer_1[0]=0 pipe2:inputbuffer_1[0]=1 pipe0:inputtoken=1 pipe1:inputbuffer_1[1]=1 pipe2:inputbuffer_1[1]=2 pipe0:inputtoken=2 pipe1:inputbuffer_1[2]=2 pipe2:inputbuffer_1[2]=3 pipe0:inputtoken=3 pipe1:inputbuffer_1[3]=3 pipe2:inputbuffer_1[3]=4 pipe0:inputtoken=4 pipe1:inputbuffer_1[0]=4 pipe2:inputbuffer_1[0]=5 Rerunthepipeline pipe0:inputtoken=0 pipe1:inputbuffer_1[0]=0 pipe2:inputbuffer_1[0]=1 pipe0:inputtoken=1 pipe1:inputbuffer_1[1]=1 pipe2:inputbuffer_1[1]=2 pipe0:inputtoken=2 pipe1:inputbuffer_1[2]=2 pipe2:inputbuffer_1[2]=3 pipe0:inputtoken=3 pipe1:inputbuffer_1[3]=3 pipe2:inputbuffer_1[3]=4 pipe0:inputtoken=4 pipe1:inputbuffer_1[0]=4 pipe2:inputbuffer_1[0]=5 stop The output can be different from run to run, since the second pipe is a parallel type. At the second iteration from the condition task, we reset the pipeline so the token identifier starts from 0 rather than 5. Learn More about Taskflow Pipeline Visit the following pages to learn more about pipeline: Task-parallel Scalable Pipeline Data-parallel Pipeline Text Processing Pipeline Graph Processing Pipeline Taskflow Processing Pipeline