std::experimental::reduce

From cppreference.com
< cpp‎ | experimental‎ | simd
 
 
Technical specifications
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
 
 
 
Defined in header <experimental/simd>
template< class T, class Abi, class BinaryOperation = plus<> >

T

reduce( const simd<T, Abi>& v, BinaryOperation binary_op = {} );
(1) (parallelism TS v2)
template< class M, class V, class BinaryOperation >

typename V::value_type
reduce( const const_where_expression<M, V>& x,

        typename V::value_type identity_element, BinaryOperation binary_op = {} );
(2) (parallelism TS v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, plus<> binary_op ) noexcept;
(3) (parallelism TS v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, multiplies<> binary_op ) noexcept;
(4) (parallelism TS v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, bit_and<> binary_op ) noexcept;
(5) (parallelism TS v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, bit_or<> binary_op ) noexcept;
(6) (parallelism TS v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, bit_xor<> binary_op ) noexcept;
(7) (parallelism TS v2)
1) Reduces all values in v over binary_op.
2) Reduces the values in x where the associated mask element is true over binary_op.
3) Returns the sum of all values in x where the associated mask element is true.
4) Returns the product of all values in x where the associated mask element is true.
5) Returns the aggregation using bitwise-and of all values in x where the associated mask element is true.
6) Returns the aggregation using bitwise-or of all values in x where the associated mask element is true.
7) Returns the aggregation using bitwise-xor of all values in x where the associated mask element is true.

The behavior is non-deterministic if binary_op is not associative or not commutative.

Parameters

v - the simd vector to apply the reduction to
x - the return value of a where expression to apply the reduction to
identity_element - a value that acts as identity element for binary_op; binary_op(identity_element, a) == a must hold for all finite a of type V::value_type
binary_op - binary FunctionObject that will be applied in unspecified order to arguments of type V::value_type or simd<V::value_type, A>, with unspecified ABI tag A. binary_op(v, v) must be convertible to V.

Return value

Example

#include <array>
#include <cstddef>
#include <experimental/simd>
#include <iostream>
#include <numeric>
 
namespace stdx = std::experimental;
 
int main() {
  using V = stdx::native_simd<float>;
  alignas(stdx::memory_alignment_v<V>) std::array<float, 1024> data;
  std::iota(data.begin(), data.end(), 0);
  V acc = 0;
  for (std::size_t i = 0; i < data.size(); i += acc.size()) {
    acc += V(&data[i], stdx::vector_aligned);
  }
  std::cout << "reduce(" << acc[0];
  for (std::size_t i = 1; i < V::size(); ++i) {
    std::cout << ", " << acc[i];
  }
  std::cout << ")\n= (1024 - 1) * 1024 / 2\n= " << reduce(acc) << '\n';
}

Output:

reduce(130560, 130816, 131072, 131328)
= (1024 - 1) * 1024 / 2
= 523776

See also

Template:cpp/experimental/simd hminTemplate:cpp/experimental/simd hmax
(C++17)
similar to std::accumulate, except out of order
(function template)