Loading...
Searching...
No Matches
tf::StaticPartitioner< C > Class Template Reference

class to construct a static partitioner for scheduling parallel algorithms More...

#include <taskflow/algorithm/partitioner.hpp>

Inheritance diagram for tf::StaticPartitioner< C >:
[legend]
Collaboration diagram for tf::StaticPartitioner< C >:
[legend]

Public Member Functions

 StaticPartitioner ()=default
 default constructor
 
 StaticPartitioner (size_t sz)
 construct a static partitioner with the given chunk size
 
 StaticPartitioner (size_t sz, C &&closure)
 construct a static partitioner with the given chunk size and the closure
 
size_t adjusted_chunk_size (size_t N, size_t W, size_t w) const
 queries the adjusted chunk size
 
- Public Member Functions inherited from tf::PartitionerBase< DefaultClosureWrapper >
 PartitionerBase ()=default
 default constructor
 
 PartitionerBase (size_t chunk_size)
 construct a partitioner with the given chunk size
 
 PartitionerBase (size_t chunk_size, DefaultClosureWrapper &&closure_wrapper)
 construct a partitioner with the given chunk size and closure wrapper
 
size_t chunk_size () const
 query the chunk size of this partitioner
 
void chunk_size (size_t cz)
 update the chunk size of this partitioner
 
const DefaultClosureWrapperclosure_wrapper () const
 acquire an immutable access to the closure wrapper object
 
DefaultClosureWrapperclosure_wrapper ()
 acquire a mutable access to the closure wrapper object
 
void closure_wrapper (F &&fn)
 modify the closure wrapper object
 
TF_FORCE_INLINE decltype(auto) operator() (F &&callable)
 wraps the given callable with the associated closure wrapper
 

Static Public Member Functions

static constexpr PartitionerType type ()
 queries the partition type (static)
 

Additional Inherited Members

- Public Types inherited from tf::PartitionerBase< DefaultClosureWrapper >
using closure_wrapper_type
 the closure type
 
- Static Public Attributes inherited from tf::PartitionerBase< DefaultClosureWrapper >
static constexpr bool is_default_wrapper_v
 indicating if the given closure wrapper is a default wrapper (i.e., empty)
 
- Protected Attributes inherited from tf::PartitionerBase< DefaultClosureWrapper >
size_t _chunk_size
 chunk size
 
DefaultClosureWrapper _closure_wrapper
 closure wrapper
 

Detailed Description

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

class to construct a static partitioner for scheduling parallel algorithms

Template Parameters
Cclosure wrapper type (default tf::DefaultClosureWrapper)

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();
class to construct a static partitioner for scheduling parallel algorithms
Definition partitioner.hpp:551

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 task
closure();
// do something else after invoking the partitioned task
// ...
}
);
executor.run(taskflow).wait();
Task for_each_index(B first, E last, S step, C callable, P part=P())
constructs an index-based parallel-for task
class to create a taskflow object
Definition taskflow.hpp:64

Member Function Documentation

◆ adjusted_chunk_size()

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

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.


The documentation for this class was generated from the following file: