std::experimental::parallel::reduce
來自 cppreference.com
定義於標頭檔案 <experimental/numeric> |
||
template< class InputIt > typename std::iterator_traits<InputIt>::value_type reduce( |
(1) | (並行 TS) |
template< class ExecutionPolicy, class InputIterator > typename std::iterator_traits<InputIt>::value_type reduce( |
(2) | (並行 TS) |
template< class InputIt, class T > T reduce( InputIt first, InputIt last, T init ); |
(3) | (並行 TS) |
template< class ExecutionPolicy, class InputIt, class T > T reduce( ExecutionPolicy&& policy, InputIt first, InputIt last, T init ); |
(4) | (並行 TS) |
template< class InputIt, class T, class BinaryOp > T reduce( InputIt first, InputIt last, T init, BinaryOp binary_op ); |
(5) | (並行 TS) |
template< class ExecutionPolicy, class InputIt, class T, class BinaryOp > T reduce( ExecutionPolicy&& policy, |
(6) | (並行 TS) |
1) 等同於 reduce(first, last, typename std::iterator_traits<InputIt>::value_type{})。
3) 等同於 reduce(first, last, init, std::plus<>())。
5) 對範圍
[
first,
last)
進行歸約,可能以未指定的方式進行排列和聚合,並結合初始值 init,使用 binary_op。2,4,6) 與 (1,3,5) 相同,但根據 policy 執行。
如果 binary_op 不滿足結合律或交換律,則行為不確定。
如果 binary_op 修改了 [
first,
last)
中的任何元素或使任何迭代器失效,則行為未定義。
目錄 |
[編輯] 引數
first, last | - | 要應用演算法的元素範圍 |
init | - | 廣義和的初始值 |
policy | - | 執行策略 |
binary_op | - | 二元 函式物件 (FunctionObject),將以未指定順序應用於解引用輸入迭代器的結果、其他 binary_op 的結果和 init。 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 |
[編輯] 返回值
init 和 *first, *(first + 1), ... *(last - 1) 經過 binary_op 的廣義和,
其中廣義和 GSUM(op, a1, ..., aN) 定義如下:
- 如果 N=1,則為 a1
- 如果 N > 1,則為 op(GSUM(op, b1, ..., bK), GSUM(op, bM, ..., bN)),其中
- b1, ..., bN 可以是 a1, ..., aN 的任何排列,並且
- 1 < K+1 = M ≤ N
換句話說,範圍的元素可以以任意順序分組和重新排列。
[編輯] 複雜度
O(last - first) 次 binary_op 的應用。
[編輯] 異常
- 如果作為演算法一部分呼叫的函式丟擲異常,
- 如果 `policy` 是 `parallel_vector_execution_policy`,則呼叫 std::terminate。
- 如果 `policy` 是 `sequential_execution_policy` 或 `parallel_execution_policy`,則演算法以包含所有未捕獲異常的 exception_list 退出。如果只有一個未捕獲異常,演算法可能會直接重新丟擲它,而不將其封裝在 `exception_list` 中。在遇到第一個異常後,演算法在返回之前會執行多少工作是未指定的。
- 如果 `policy` 是其他型別,則行為由實現定義。
- 如果演算法未能分配記憶體(無論是為自身還是在處理使用者異常時構造 `exception_list`),則丟擲 std::bad_alloc。
[編輯] 注意
如果範圍為空,則返回未修改的 init。
- 如果 `policy` 是 `sequential_execution_policy` 的例項,則所有操作都在呼叫執行緒中執行。
- 如果 `policy` 是 `parallel_execution_policy` 的例項,則操作可能在未指定數量的執行緒中執行,且彼此之間序列不定。
- 如果 `policy` 是 `parallel_vector_execution_policy` 的例項,則執行可以是並行化和向量化的:不遵守函式體邊界,使用者程式碼可能會以任意方式重疊和組合(特別是,這意味著使用者提供的可呼叫物件不得獲取互斥鎖來訪問共享資源)。
[編輯] 示例
reduce 是 std::accumulate 的亂序版本
執行此程式碼
#include <chrono> #include <experimental/execution_policy> #include <experimental/numeric> #include <iostream> #include <numeric> #include <vector> int main() { std::vector<double> v(10'000'007, 0.5); { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::accumulate(v.begin(), v.end(), 0.0); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n"; } { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::experimental::parallel::reduce( std::experimental::parallel::par, v.begin(), v.end()); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << "parallel::reduce result " << result << " took " << ms.count() << " ms\n"; } }
可能的輸出
std::accumulate result 5000003.50000 took 12.7365 ms parallel::reduce result 5000003.50000 took 5.06423 ms
[編輯] 另請參閱
對一個範圍的元素進行求和或摺疊 (函式模板) | |
對一個範圍的元素應用函式,並將結果儲存在目標範圍中 (函式模板) | |
(並行 TS) |
應用函式物件,然後亂序歸約 (函式模板) |