dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
parallel_for.h File Reference
#include <cmath>
#include <limits>
#include <memory>
#include <dispenso/detail/can_invoke.h>
#include <dispenso/detail/per_thread_info.h>
#include <dispenso/task_set.h>

Go to the source code of this file.

Classes

struct  dispenso::ParForOptions
 
struct  dispenso::ChunkedRange< IntegerT >
 
struct  dispenso::ChunkedRange< IntegerT >::Static
 
struct  dispenso::ChunkedRange< IntegerT >::Auto
 

Enumerations

enum class  dispenso::ParForChunking { kStatic , kAuto }
 

Functions

template<typename IntegerA , typename IntegerB >
ChunkedRange< std::common_type_t< IntegerA, IntegerB > > dispenso::makeChunkedRange (IntegerA start, IntegerB end, ParForChunking chunking=ParForChunking::kStatic)
 
template<typename IntegerA , typename IntegerB , typename IntegerC >
ChunkedRange< std::common_type_t< IntegerA, IntegerB > > dispenso::makeChunkedRange (IntegerA start, IntegerB end, IntegerC chunkSize)
 
template<typename TaskSetT , typename IntegerT , typename F , typename StateContainer , typename StateGen >
void dispenso::parallel_for (TaskSetT &taskSet, StateContainer &states, const StateGen &defaultState, const ChunkedRange< IntegerT > &range, F &&f, ParForOptions options={})
 
template<typename TaskSetT , typename IntegerT , typename F >
void dispenso::parallel_for (TaskSetT &taskSet, const ChunkedRange< IntegerT > &range, F &&f, ParForOptions options={})
 
template<typename IntegerT , typename F >
void dispenso::parallel_for (const ChunkedRange< IntegerT > &range, F &&f, ParForOptions options={})
 
template<typename F , typename IntegerT , typename StateContainer , typename StateGen >
void dispenso::parallel_for (StateContainer &states, const StateGen &defaultState, const ChunkedRange< IntegerT > &range, F &&f, ParForOptions options={})
 
template<typename TaskSetT , typename IntegerA , typename IntegerB , typename F , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true, std::enable_if_t< detail::CanInvoke< F(IntegerA)>::value, bool > = true>
void dispenso::parallel_for (TaskSetT &taskSet, IntegerA start, IntegerB end, F &&f, ParForOptions options={})
 
template<typename IntegerA , typename IntegerB , typename F , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true>
void dispenso::parallel_for (IntegerA start, IntegerB end, F &&f, ParForOptions options={})
 
template<typename TaskSetT , typename IntegerA , typename IntegerB , typename F , typename StateContainer , typename StateGen , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true, std::enable_if_t< detail::CanInvoke< F(typename StateContainer::reference, IntegerA)>::value, bool > = true>
void dispenso::parallel_for (TaskSetT &taskSet, StateContainer &states, const StateGen &defaultState, IntegerA start, IntegerB end, F &&f, ParForOptions options={})
 
template<typename IntegerA , typename IntegerB , typename F , typename StateContainer , typename StateGen , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true>
void dispenso::parallel_for (StateContainer &states, const StateGen &defaultState, IntegerA start, IntegerB end, F &&f, ParForOptions options={})
 

Detailed Description

Functions for performing parallel for loops.

Definition in file parallel_for.h.

Enumeration Type Documentation

◆ ParForChunking

enum class dispenso::ParForChunking
strong

Chunking strategy. Typically if the cost of each loop iteration is roughly constant, kStatic load balancing is preferred. Additionally, when making a non-waiting parallel_for call in conjunction with other parallel_for calls or with other task submissions to a TaskSet, some dynamic load balancing is automatically introduced, and selecting kStatic load balancing here can be better. If the workload per iteration deviates a lot from constant, and some ranges may be much cheaper than others, select kAuto.

Definition at line 33 of file parallel_for.h.

Function Documentation

◆ makeChunkedRange() [1/2]

template<typename IntegerA , typename IntegerB , typename IntegerC >
ChunkedRange< std::common_type_t< IntegerA, IntegerB > > dispenso::makeChunkedRange ( IntegerA  start,
IntegerB  end,
IntegerC  chunkSize 
)
inline

Create a ChunkedRange with specific chunk size

Parameters
startThe start of the range.
endThe end of the range.
chunkSizeThe chunk size.

Definition at line 193 of file parallel_for.h.

◆ makeChunkedRange() [2/2]

template<typename IntegerA , typename IntegerB >
ChunkedRange< std::common_type_t< IntegerA, IntegerB > > dispenso::makeChunkedRange ( IntegerA  start,
IntegerB  end,
ParForChunking  chunking = ParForChunking::kStatic 
)
inline

Create a ChunkedRange with specified chunking strategy.

Parameters
startThe start of the range.
endThe end of the range.
chunkingThe strategy to use for chunking.

Definition at line 177 of file parallel_for.h.

◆ parallel_for() [1/8]

template<typename IntegerT , typename F >
void dispenso::parallel_for ( const ChunkedRange< IntegerT > &  range,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel on the global thread pool, and wait until complete.

Parameters
rangeThe range defining the loop extents as well as chunking strategy.
fThe functor to execute in parallel. Must have a signature like void(size_t begin, size_t end).
optionsSee ParForOptions for details. options.wait will always be reset to true.

Definition at line 540 of file parallel_for.h.

◆ parallel_for() [2/8]

template<typename IntegerA , typename IntegerB , typename F , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true>
void dispenso::parallel_for ( IntegerA  start,
IntegerB  end,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel on the global thread pool and block on loop completion.

Parameters
startThe start of the loop extents.
endThe end of the loop extents.
fThe functor to execute in parallel. Must have a signature like void(size_t index) or void(size_t begin, size_t end).
optionsSee ParForOptions for details. options.wait will always be reset to true.

Definition at line 647 of file parallel_for.h.

◆ parallel_for() [3/8]

template<typename F , typename IntegerT , typename StateContainer , typename StateGen >
void dispenso::parallel_for ( StateContainer states,
const StateGen defaultState,
const ChunkedRange< IntegerT > &  range,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel on the global thread pool and block until loop completion.

Parameters
statesA container of State (actual type of State TBD by user). The container will be resized to hold a State object per executing thread. Container must provide emplace_back() and must be forward-iterable. Examples include std::vector, std::deque, and std::list. These are the states passed into f, and states must remain a valid object until work is completed.
defaultStateA functor with signature State(). It will be called to initialize the objects for states.
rangeThe range defining the loop extents as well as chunking strategy.
fThe functor to execute in parallel. Must have a signature like void(State &s, size_t begin, size_t end).
optionsSee ParForOptions for details. options.wait will always be reset to true.

Definition at line 564 of file parallel_for.h.

◆ parallel_for() [4/8]

template<typename IntegerA , typename IntegerB , typename F , typename StateContainer , typename StateGen , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true>
void dispenso::parallel_for ( StateContainer states,
const StateGen defaultState,
IntegerA  start,
IntegerB  end,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel on the global thread pool and block until loop completion.

Parameters
statesA container of State (actual type of State TBD by user). The container will be resized to hold a State object per executing thread. Container must provide emplace_back() and must be forward-iterable. Examples include std::vector, std::deque, and std::list. These are the states passed into f, and states must remain a valid object until work is completed.
defaultStateA functor with signature State(). It will be called to initialize the objects for states.
startThe start of the loop extents.
endThe end of the loop extents.
fThe functor to execute in parallel. Must have a signature like void(State &s, size_t index) or void(State &s, size_t begin, size_t end).
optionsSee ParForOptions for details. options.wait will always be reset to true.

Definition at line 757 of file parallel_for.h.

◆ parallel_for() [5/8]

template<typename TaskSetT , typename IntegerT , typename F >
void dispenso::parallel_for ( TaskSetT taskSet,
const ChunkedRange< IntegerT > &  range,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel.

Parameters
taskSetThe task set to schedule the loop on.
rangeThe range defining the loop extents as well as chunking strategy.
fThe functor to execute in parallel. Must have a signature like void(size_t begin, size_t end).
optionsSee ParForOptions for details.

Definition at line 515 of file parallel_for.h.

◆ parallel_for() [6/8]

template<typename TaskSetT , typename IntegerA , typename IntegerB , typename F , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true, std::enable_if_t< detail::CanInvoke< F(IntegerA)>::value, bool > = true>
void dispenso::parallel_for ( TaskSetT taskSet,
IntegerA  start,
IntegerB  end,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel.

Parameters
taskSetThe task set to schedule the loop on.
startThe start of the loop extents.
endThe end of the loop extents.
fThe functor to execute in parallel. Must have a signature like void(size_t index) or void(size_t begin, size_t end).
optionsSee ParForOptions for details.

Definition at line 593 of file parallel_for.h.

◆ parallel_for() [7/8]

template<typename TaskSetT , typename IntegerT , typename F , typename StateContainer , typename StateGen >
void dispenso::parallel_for ( TaskSetT taskSet,
StateContainer states,
const StateGen defaultState,
const ChunkedRange< IntegerT > &  range,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel.

Parameters
taskSetThe task set to schedule the loop on.
statesA container of State (actual type of State TBD by user). The container will be resized to hold a State object per executing thread. Container must provide emplace_back() and must be forward-iterable. Examples include std::vector, std::deque, and std::list. These are the states passed into f, and states must remain a valid object until work is completed.
defaultStateA functor with signature State(). It will be called to initialize the objects for states.
rangeThe range defining the loop extents as well as chunking strategy.
fThe functor to execute in parallel. Must have a signature like void(State &s, size_t begin, size_t end).
optionsSee ParForOptions for details.

Definition at line 344 of file parallel_for.h.

◆ parallel_for() [8/8]

template<typename TaskSetT , typename IntegerA , typename IntegerB , typename F , typename StateContainer , typename StateGen , std::enable_if_t< std::is_integral< IntegerA >::value, bool > = true, std::enable_if_t< std::is_integral< IntegerB >::value, bool > = true, std::enable_if_t< detail::CanInvoke< F(typename StateContainer::reference, IntegerA)>::value, bool > = true>
void dispenso::parallel_for ( TaskSetT taskSet,
StateContainer states,
const StateGen defaultState,
IntegerA  start,
IntegerB  end,
F &&  f,
ParForOptions  options = {} 
)

Execute loop over the range in parallel.

Parameters
taskSetThe task set to schedule the loop on.
statesA container of State (actual type of State TBD by user). The container will be resized to hold a State object per executing thread. Container must provide emplace_back() and must be forward-iterable. Examples include std::vector, std::deque, and std::list. These are the states passed into f, and states must remain a valid object until work is completed.
defaultStateA functor with signature State(). It will be called to initialize the objects for states.
startThe start of the loop extents.
endThe end of the loop extents.
fThe functor to execute in parallel. Must have a signature like void(State &s, size_t index) or void(State &s, size_t begin, size_t end).
optionsSee ParForOptions for details.

Definition at line 683 of file parallel_for.h.