GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
Stream reduction pattern

The stream reduction pattern consists on the use of the reduce pattern over a stream of data. Each element is processed using a reduce operator, that generates a new output element. The output elements are sent to an output stream.

The interface to the stream reduction pattern is provided by function grppi::stream_reduce(). As all functions in GrPPI, this function takes as its first argument an execution policy.

grppi::stream_reduce(exec, other_arguments...);

Stream reduction variants

Key elements in stream reduction

The key elements in a stream reduction is the Combiner operation, the window size and the offset.

A Combiner is any C++ callable entity, that is able to combine two values into a single value. A Combiner cmb is any operation taking two values x and y of types T and U and returning a combined value of type T, making valid the following:

T x;
U y;
T res = cmb(x,y);

A window size is an integer value that defines the number of elements that should be collapsed to produce a resulting value.

An offset is an integer value that defines the overlapping degree among windows.

A stand-alone stream reduction also has a Generator. The generator may be any C++ callable entity that produces values in a way that allows to signal an indication of end-of stream. For this purpose, the stream reduction requires that the generator produces values of any type that is compatible with a subset of an optional interface:

auto r = generator(); // r is a value generated by a generator.
if (r) { // r is convertible to bool
auto y = *r; // r can be dereferenced.
}

Finally, a stand-alone stream reduction has a Consumer. The consumer may be any C++ callable entity that takes values of the result type of the Combiner.

Details on stream reduction variants

Stand-alone stream reduction

A stand alone stream reduction has six elements:

Composable stream reduction

A composable stream reduction has four elements:

The input values will be generated by the upper level pattern, which will be also responsible for consuming the output values.


Example: A stream reduction stage in a pipeline.

stageA,
stageB,
window_size, offset, 0,
[](int x, int y) { return x+y; }),
stageC
);

Note: For brevity we do not show here the details of other stages.

For composing complex patterns, the stream_reduce() function may be used to create an object that may be supplied to another pattern to build a composed pattern.


Example: A composable stream reduction stage in a pipeline.

auto chunk_sum = grppi::stream_reduce(exec,
window_size, offset, 0,
[](int x, int y) { return x+y; });
stageA,
stageB,
chunk_sum,
stageC
);

Note: For brevity we do not show here the details of other stages.