名稱空間
變體
操作

std::experimental::reduce, std::experimental::hmin, std::experimental::hmax

來自 cppreference.com
< cpp‎ | experimental‎ | simd
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
 
定義於標頭檔案 <experimental/simd>
template< class T, class Abi, class BinaryOperation = std::plus<> >
T reduce( const simd<T, Abi>& v, BinaryOperation binary_op = {} );
(1) (並行技術規範 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) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, std::plus<> binary_op ) noexcept;
(3) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, std::multiplies<> binary_op ) noexcept;
(4) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, std::bit_and<> binary_op ) noexcept;
(5) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, std::bit_or<> binary_op ) noexcept;
(6) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

reduce( const const_where_expression<M, V>& x, std::bit_xor<> binary_op ) noexcept;
(7) (並行技術規範 v2)
template< class T, class Abi >
T hmin( const simd<T, Abi>& v ) noexcept;
(8) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

hmin( const const_where_expression<M, V>& x ) noexcept;
(9) (並行技術規範 v2)
template< class T, class Abi >
T hmax( const simd<T, Abi>& v ) noexcept;
(10) (並行技術規範 v2)
template< class M, class V >

typename V::value_type

hmax( const const_where_expression<M, V>& x ) noexcept;
(11) (並行技術規範 v2)
1)v 中的所有值執行 binary_op 歸約。
2)x 中關聯掩碼元素為 true 的值執行 binary_op 歸約。
3) 返回 x 中關聯掩碼元素為 true 的所有值的和。
4) 返回 x 中關聯掩碼元素為 true 的所有值的積。
5) 返回 x 中關聯掩碼元素為 true 的所有值透過按位與運算的聚合結果。
6) 返回 x 中關聯掩碼元素為 true 的所有值透過按位或運算的聚合結果。
7) 返回 x 中關聯掩碼元素為 true 的所有值透過按位異或運算的聚合結果。
8)v 中的所有值執行 std::min 歸約。
9)x 中關聯掩碼元素為 true 的所有值執行 std::min 歸約。
10)v 中的所有值執行 std::max 歸約。
11)x 中關聯掩碼元素為 true 的所有值執行 std::max 歸約。

如果 binary_op 不滿足結合律或交換律,則行為不確定。

目錄

[編輯] 引數

v - 要應用歸約的 simd 向量
x - 要應用歸約的 where 表示式的返回值
identity_element - binary_op 起標識元素作用的值;對於型別為 V::value_type 的所有有限 a,必須滿足 binary_op(identity_element, a) == a
binary_op - 二元 函式物件,將以未指定順序應用於型別為 V::value_typesimd<V::value_type, A> 的引數,其中 ABI 標籤 A 未指定。binary_op(v, v) 必須可轉換為 V

[編輯] 返回值

操作的結果,型別為

1,8,10) T
2-7,9,11) V::value_type

[編輯] 示例

#include <array>
#include <cassert>
#include <cstddef>
#include <experimental/simd>
#include <functional>
#include <iostream>
#include <numeric>
namespace stdx = std::experimental;
 
int main()
{
    using V = stdx::native_simd<double>;
 
    alignas(stdx::memory_alignment_v<V>) std::array<V::value_type, 1024> data;
    std::iota(data.begin(), data.end(), 0);
 
    V::value_type acc{};
    for (std::size_t i = 0; i < data.size(); i += V::size())
        acc += stdx::reduce(V(&data[i], stdx::vector_aligned), std::plus{});
    std::cout << "sum of data = " << acc << '\n';
 
    using W = stdx::fixed_size_simd<int, 4>;
    alignas(stdx::memory_alignment_v<W>) std::array<int, 4> arr{2, 5, 4, 1};
    auto w = W(&arr[0], stdx::vector_aligned);
    assert(stdx::hmin(w) == 1 and stdx::hmax(w) == 5);
}

輸出

sum of data = 523776

[編輯] 參閱

(C++17)
類似於 std::accumulate,但順序是亂序的
(函式模板) [編輯]