Template Numerical Library: Segment Reduction Lambda Function Reference

Template Numerical Library version\ main:c7077e4fd

Loading...
Searching...
No Matches
Segment Reduction Lambda Function Reference

This page provides a comprehensive reference for all lambda function signatures used in segment reduction operations.

# Fetch Lambda Functions

The fetch lambda is used to extract and transform values from segment elements during reduction.

## Full Form (With All Parameters)

auto fetch = [=] __cuda_callable__ ( IndexType segmentIdx, IndexType localIdx, IndexType globalIdx ) -> FetchValue { ... }
__cuda_callable__
#define __cuda_callable__
Definition Macros.h:49

Parameters:

- segmentIdx - The index of the segment
- localIdx - The rank (position) of the element within the segment
- globalIdx - The global index of the element in the corresponding container
- Returns: A value of type FetchValue to be used in the reduction

## Brief Form (Global Index Only)

In case when only the global index is needed to fetch the value, the brief form can be used. It often leads to better performance.

auto fetch = [=] __cuda_callable__ ( IndexType globalIdx ) -> FetchValue { ... }

Parameters:

- globalIdx - The global index of the element in the corresponding container
- Returns: A value of type FetchValue to be used in the reduction

Note: The actual behavior depends on the kernel type used for the reduction. Some optimized kernels may perform significantly better with the brief variant of the fetch lambda function.

# Reduction Lambda Functions

The reduction lambda defines how values are combined during the reduction operation.

## Basic Reduction (Without Arguments)

auto reduction = [=] __cuda_callable__ ( const Value& a, const Value& b ) -> Value { ... }

Parameters:

- a - First value to be reduced
- b - Second value to be reduced
- Returns: The result of reducing a and b

## Reduction With Argument (Position Tracking)

auto reduction = [] __cuda_callable__ ( Result& a, const Result& b, Index& aIdx, const Index& bIdx ) -> Result { ... }

Parameters:

- a - First value to be reduced (mutable reference)
- b - Second value to be reduced (const reference)
- aIdx - Index/position associated with value a (mutable reference for tracking)
- bIdx - Index/position associated with value b (const reference)

Note: This variant is used when you need to track which element produced the final result (e.g., finding the maximum value and its position within the segment).

# Storer Lambda Functions

The storer lambda is used to store the final reduction result for each segment.

## Basic Storer (Segment Index Only)

auto storer = [=] __cuda_callable__ ( IndexType segmentIdx, const Value& value ) { ... }

Parameters:

- segmentIdx - The index of the segment
- value - The result of the reduction for this segment

## Storer With Local Index (Position Tracking)

auto storer = [=] __cuda_callable__ ( IndexType segmentIdx, IndexType localIdx, const Value& value ) { ... }

Parameters:

- segmentIdx - The index of the segment
- localIdx - The local index (position) of the element within the segment that produced the final result
- value - The result of the reduction for this segment

Note: This variant is typically used with Reduction With Argument (Position Tracking) to track both the value and its position within the segment.

## Storer With Segment Index Array

auto storer = [=] __cuda_callable__ ( IndexType indexOfSegmentIdx, IndexType segmentIdx, const Value& value ) { ... }

Parameters:

- indexOfSegmentIdx - The position within the segmentIndexes array (when reducing over a subset of segments)
- segmentIdx - The actual index of the segment
- value - The result of the reduction for this segment

## Storer With Index Array and Local Index

auto storer = [=] __cuda_callable__ ( IndexType indexOfSegmentIdx, IndexType segmentIdx, IndexType localIdx, const Value&
value ) { ... }

Parameters:

- indexOfSegmentIdx - The position within the segmentIndexes array
- segmentIdx - The actual index of the segment
- localIdx - The local index (position) of the element within the segment
- value - The result of the reduction for this segment

# Condition Lambda Functions

The condition lambda determines which segments should be processed (used in "If" variants).

## Condition Check

auto condition = [=] __cuda_callable__ ( IndexType segmentIdx ) -> bool { ... }

Parameters:

- segmentIdx - The index of the segment
- Returns: true if the segment should be processed, false otherwise

# Reduction Function Objects

Instead of lambda functions, reduction operations can also be specified using function objects from Function objects for reduction operations or Function objects for reduction operations with argument.

When using function objects:

- They must provide a static template method getIdentity to automatically deduce the identity value
- For WithArgument variants, they must be instances of Function objects for reduction operations with argument
- Common examples: Min, Max, Sum, Product, MinWithArg, MaxWithArg

# Related Pages

- Overview of Segment Reduction Functions - Overview of segment reduction functions