Embedded Multicore Building Blocks V1.0.0
Functions

Parallel reduction computation. More...

Functions

template<typename RAI , typename ReturnType , typename ReductionFunction , typename TransformationFunction >
ReturnType embb::algorithms::Reduce (RAI first, RAI last, ReturnType neutral, ReductionFunction reduction, TransformationFunction transformation=Identity(), const embb::mtapi::ExecutionPolicy &policy=embb::mtapi::ExecutionPolicy(), size_t block_size=0)
 Performs a parallel reduction operation on a range of elements. More...
 

Detailed Description

Parallel reduction computation.

Function Documentation

template<typename RAI , typename ReturnType , typename ReductionFunction , typename TransformationFunction >
ReturnType embb::algorithms::Reduce ( RAI  first,
RAI  last,
ReturnType  neutral,
ReductionFunction  reduction,
TransformationFunction  transformation = Identity(),
const embb::mtapi::ExecutionPolicy policy = embb::mtapi::ExecutionPolicy(),
size_t  block_size = 0 
)

Performs a parallel reduction operation on a range of elements.

The range consists of the elements from first to last, excluding the last element. The type of the result (ReturnType) is deduced from the neutral element.

Returns
reduction(transformation(*first), ..., transformation(*(last-1))) where the reduction function is applied pairwise.
Exceptions
embb::base::ErrorExceptionif not enough MTAPI tasks can be created to satisfy the requirements of the algorithm.
Concurrency
Thread-safe if the elements in the range are not modified by another thread while the algorithm is executed.
Note
No guarantee is given on the order in which the functions reduction and transformation are applied to the elements.
For all x of type ReturnType it must hold that reduction(x, neutral) == x.
The reduction operation need not be commutative but must be associative, i.e., reduction(x, reduction(y, z)) == reduction(reduction(x, y), z)) for all x, y, z of type ReturnType.
For nested algorithms, the task limit may be exceeded. In that case, increase the task limit of the MTAPI node.
See also
embb::mtapi::ExecutionPolicy, ZipIterator, Identity
Template Parameters
RAIRandom access iterator
ReturnTypeType of result of reduction operation, deduced from neutral
ReductionFunctionBinary reduction function object with signature ReturnType ReductionFunction(ReturnType, ReturnType) or an embb::mtapi::Job associated with an action function accepting a struct containing two ReturnType members as its argument buffer and a struct containing one ReturnType member as its result buffer.
TransformationFunctionUnary transformation function object with signature ReturnType TransformationFunction(typename std::iterator_traits<RAI>::value_type) or an embb::mtapi::Job associated with an action function accepting a struct containing one InputType member as its argument buffer and a struct containing one ReturnType member as its result buffer.
Parameters
[in]firstRandom access iterator pointing to the first element of the range
[in]lastRandom access iterator pointing to the last plus one element of the range
[in]neutralNeutral element of the reduction operation.
[in]reductionReduction operation to be applied to the elements of the range
[in]transformationTransforms the elements of the range before the reduction operation is applied
[in]policyembb::mtapi::ExecutionPolicy for the reduction computation
[in]block_sizeLower bound for partitioning the range of elements into blocks that are treated in parallel. Partitioning of a block stops if its size is less than or equal to block_size. The default value 0 means that the minimum block size is determined automatically depending on the number of elements in the range divided by the number of available cores.