std::experimental::reduce, std::experimental::hmin, std::experimental::hmax
來自 cppreference.com
< cpp | experimental | simd
定義於標頭檔案 <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 |
(2) | (並行技術規範 v2) |
template< class M, class V > typename V::value_type |
(3) | (並行技術規範 v2) |
template< class M, class V > typename V::value_type |
(4) | (並行技術規範 v2) |
template< class M, class V > typename V::value_type |
(5) | (並行技術規範 v2) |
template< class M, class V > typename V::value_type |
(6) | (並行技術規範 v2) |
template< class M, class V > typename V::value_type |
(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 |
(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 |
(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 的所有值透過按位異或運算的聚合結果。
如果 binary_op 不滿足結合律或交換律,則行為不確定。
目錄 |
[編輯] 引數
v | - | 要應用歸約的 simd 向量 |
x | - | 要應用歸約的 where 表示式的返回值 |
identity_element | - | 對 binary_op 起標識元素作用的值;對於型別為 V::value_type 的所有有限 a,必須滿足 binary_op(identity_element, a) == a |
binary_op | - | 二元 函式物件,將以未指定順序應用於型別為 V::value_type 或 simd<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,但順序是亂序的 (函式模板) |