Back to Taskflow

template<typename C = DefaultClosureWrapper> tf::StaticPartitioner class

docs/classtf_1_1StaticPartitioner.html

4.0.02.8 KB
Original Source

template<typename C = DefaultClosureWrapper> tf::StaticPartitioner class

class to construct a static partitioner for scheduling parallel algorithms

Template parameters
C

The partitioner divides iterations into chunks and distributes chunks to workers in order. If the chunk size is not specified (default 0), the partitioner resorts to a chunk size that equally distributes iterations into workers.

std::vector\<int\> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}taskflow.for\_each(data.begin(), data.end(), [](int i){}, StaticPartitioner(0));executor.run(taskflow).run();

In addition to partition size, the application can specify a closure wrapper for a static partitioner. A closure wrapper allows the application to wrap a partitioned task (i.e., closure) with a custom function object that performs additional tasks. For example:

std::atomic\<int\> count = 0;tf::Taskflow taskflow;taskflow.for\_each\_index(0, 100, 1, [](){printf("%d\n", i); },tf::StaticPartitioner(0, [](auto&& closure){// do something before invoking the partitioned task// ...// invoke the partitioned taskclosure();// do something else after invoking the partitioned task// ...});executor.run(taskflow).wait();

Base classes

template<typename C = DefaultClosureWrapper> class PartitionerBase<DefaultClosureWrapper>class to derive a partitioner for scheduling parallel algorithms

Public static functions

static auto type() -> PartitionerType constexprqueries the partition type (static)

Constructors, destructors, conversion operators

StaticPartitioner() defaulteddefault constructorStaticPartitioner(size_t sz) explicit construct a static partitioner with the given chunk sizeStaticPartitioner(size_t sz, C&& closure) explicit construct a static partitioner with the given chunk size and the closure

Public functions

auto adjusted_chunk_size(size_t N, size_t W, size_t w) const -> size_tqueries the adjusted chunk size

Function documentation

template<typename C> size_t tf::StaticPartitioner<C>::adjusted_chunk_size(size_t N, size_t W, size_t w) const

queries the adjusted chunk size

Returns the given chunk size if it is not zero, or returns N/W + (w < NW), where N is the number of iterations, W is the number of workers, and w is the worker ID.