ParallelIterations

# Parallel Iterations

Include the Header
ParallelIterations_1ParallelIterationsIncludeTheHeader
Create an Index-based Parallel-Iteration Task
ParallelIterations_1A1IndexBasedParallelFor
Capture Indices by Reference
ParallelIterations_1ParallelForEachCaptureIndicesByReference
Create an Iterator-based Parallel-Iteration Task
ParallelIterations_1A1IteratorBasedParallelFor
Capture Iterators by Reference
ParallelIterations_1ParallelForEachCaptureIteratorsByReference
Configure a Partitioner
ParallelIterations_1ParallelIterationsConfigureAPartitioner

Taskflow provides template functions for constructing tasks to perform parallel iterations over ranges of items.

# Include the Header

You need to include the header file, `taskflow/algorithm/for_each.hpp`, for using parallel-iteration algorithms.

    #include<taskflow/algorithm/for_each.hpp>

# Create an Index-based Parallel-Iteration Task

Index-based parallel-for performs parallel iterations over a range `[first, last)` with the given `step` size. The task created by tf::Taskflow::for_each_index(B first, E last, S step, C callable, P&& part) represents parallel execution of the following loop:

    //positivestep
    for(autoi=first;i<last;i+=step){
    callable(i);
    }

    //negativestep
    for(autoi=first;i>last;i+=step){
    callable(i);
    }

We support only integer-based range. The range can go positive or negative direction.

    taskflow.for_each_index(0,100,2,[](inti){});//50loopswitha+step
    taskflow.for_each_index(100,0,-2,[](inti){});//50loopswitha-step

Notice that either positive or negative direction is defined in terms of the range, `[first, last)`, where `end` is excluded. In the positive case, the 50 items are 0, 2, 4, 6, 8, ..., 96, 98. In the negative case, the 50 items are 100, 98, 96, 04, ... 4, 2. An example of the Taskflow graph for the positive case under 12 workers is depicted below:

# Capture Indices by Reference

You can pass indices by reference using std::ref to marshal parameter update between dependent tasks. This is especially useful when the range indices are unknown at the time of creating a for-each-index task, but is initialized from another task.

    int*vec;
    intfirst,last;

    autoinit=taskflow.emplace([&](){
    first=0;
    last=1000;
    vec=newint[1000];
    });

    autopf=taskflow.for_each_index(std::ref(first),std::ref(last),1,
    [&](inti){
    std::cout<<"paralleliterationonindex"<<vec[i]<<'\n';
    }
    );

    //wrong!mustusestd::ref,orfirstandlastarecapturedbycopy
    //autopf=taskflow.for_each_index(first,last,1,[&](inti){
    //std::cout<<"paralleliterationonindex"<<vec[i]<<'\n';
    //});

    init.precede(pf);

When `init` finishes, the parallel-for task `pf` will see `first` as 0 and `last` as 1000 and performs parallel iterations over the 1000 items.

# Create an Iterator-based Parallel-Iteration Task

Iterator-based parallel-for performs parallel iterations over a range specified by two STL-styled iterators, `first` and `last`. The task created by tf::Taskflow::for_each(B first, E last, C callable, P&& part) represents a parallel execution of the following loop:

    for(autoi=first;i<last;i++){
    callable(*i);
    }

tf::Taskflow::for_each(B first, E last, C callable, P&& part) simultaneously applies the callable to the object obtained by dereferencing every iterator in the range `[first, last)`. It is user's responsibility for ensuring the range is valid within the execution of the parallel-for task. Iterators must have the post-increment operator ++ defined.

    std::vector<int>vec={1,2,3,4,5};
    taskflow.for_each(vec.begin(),vec.end(),[](inti){
    std::cout<<"parallelforonitem"<<i<<'\n';
    });

    std::list<std::string>list={"hi","from","t","a","s","k","f","low"};
    taskflow.for_each(list.begin(),list.end(),[](conststd::string&str){
    std::cout<<"parallelforonitem"<<str<<'\n';
    });

# Capture Iterators by Reference

Similar to tf::Taskflow::for_each_index, iterators of tf::Taskflow::for_each are templated to allow capturing range parameters by reference, such that one task can set up the range before another task performs the parallel-for algorithm. For example:

    std::vector<int>vec;
    std::vector<int>::iteratorfirst,last;;

    tf::Taskinit=taskflow.emplace([&](){
    vec.resize(1000);
    first=vec.begin();
    last=vec.end();
    });

    tf::Taskpf=taskflow.for_each(std::ref(first),std::ref(last),[&](inti){
    std::cout<<"paralleliterationonitem"<<i<<'\n';
    });

    //wrong!mustusestd::ref,orfirstandlastarecapturedbycopy
    //tf::Taskpf=taskflow.for_each(first,last,[&](inti){
    //std::cout<<"paralleliterationonitem"<<i<<'\n';
    //});

    init.precede(pf);

When `init` finishes, the parallel-for task `pf` will see `first` pointing to the beginning of `vec` and `last` pointing to the end of `vec` and performs parallel iterations over the 1000 items. The two tasks form an end-to-end task graph where the parameters of parallel-for are computed on the fly.

# Configure a Partitioner

You can configure a partitioner for parallel-iteration tasks to run with different scheduling methods, such as guided partitioning, dynamic partitioning, and static partitioning. The following example creates two parallel-iteration tasks using two different partitioners, one with the static partitioning algorithm and another one with the guided partitioning algorithm:

    std::vector<int>vec(1024,0);

    tf::ExecutionPolicy<tf::StaticPartitioner>static_partitioner;
    tf::ExecutionPolicy<tf::GuidedPartitioner>guided_partitioner;

    //createaparallel-iterationtaskwithstaticpartitioner
    taskflow.for_each(
    vec.begin(),vec.end(),[&](inti){
    std::cout<<"paralleliterationonitem"<<i<<'\n';
    },
    static_partitioner
    );

    //createaparallel-iterationtaskwithguidedpartitioner
    taskflow.for_each(
    vec.begin(),vec.end(),[&](inti){
    std::cout<<"paralleliterationonitem"<<i<<'\n';
    },
    guided_partitioner
    );

By default, parallel-iteration tasks use tf::DefaultPartitioner if no partitioner is specified.