名稱空間
變體
操作

std::experimental::parallel::transform_reduce

來自 cppreference.com
< cpp‎ | 實驗性
定義於標頭檔案 <experimental/numeric>
模板< InputIt, UnaryOp, T, BinaryOp >

T transform_reduce( InputIt first, InputIt last,

                    UnaryOp unary_op, T init, BinaryOp binary_op );
(1) (並行 TS)
模板< ExecutionPolicy,

          InputIt, UnaryOp, T, BinaryOp >
T transform_reduce( ExecutionPolicy&& policy,
                    InputIt first, InputIt last,

                    UnaryOp unary_op, T init, BinaryOp binary_op );
(2) (並行 TS)

對範圍 [firstlast) 中的每個元素應用 unary_op,並將結果(可能以未指定的方式進行置換和聚合)連同初始值 init 一起透過 binary_op 進行歸約。

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

如果 unary_opbinary_op 修改了範圍 [firstlast) 中的任何元素或使任何迭代器失效,則行為未定義。

目錄

[編輯] 引數

first, last - 要應用演算法的元素範圍
init - 廣義和的初始值
policy - 執行策略
unary_op - 一元 函式物件,將應用於輸入範圍的每個元素。返回型別必須可作為 binary_op 的輸入
binary_op - 二元 函式物件,將以未指定順序應用於 unary_op 的結果、其他 binary_op 的結果和 init
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。

[編輯] 返回值

initunary_op(*first), unary_op(*(first + 1)), ... unary_op(*(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

換句話說,unary_op 的結果可以以任意順序分組和排列。

[編輯] 複雜度

O(last - first)unary_opbinary_op 的應用。

[編輯] 異常

  • 如果作為演算法一部分呼叫的函式丟擲異常,
  • 如果 policyparallel_vector_execution_policy,則呼叫 std::terminate
  • 如果 policysequential_execution_policyparallel_execution_policy,則演算法以包含所有未捕獲異常的 exception_list 退出。如果只有一個未捕獲異常,演算法可能會重新丟擲它,而無需包裝在 exception_list 中。演算法在遇到第一個異常後返回之前會執行多少工作是未指定的。
  • 如果 policy 是其他型別,則行為由實現定義。
  • 如果演算法未能分配記憶體(無論是用於自身還是在處理使用者異常時構造 exception_list),則丟擲 std::bad_alloc

[編輯] 注意

unary_op 不應用於 init

如果範圍為空,則返回未修改的 init

  • 如果 policysequential_execution_policy 的例項,所有操作都在呼叫執行緒中執行。
  • 如果 policyparallel_execution_policy 的例項,操作可以在未指定數量的執行緒中執行,彼此之間的順序是不確定的。
  • 如果 policyparallel_vector_execution_policy 的例項,執行可以同時並行化和向量化:不遵守函式體邊界,使用者程式碼可以以任意方式重疊和組合(特別是,這意味著使用者提供的 Callable 不得獲取互斥鎖來訪問共享資源)。

[編輯] 示例

transform_reduce 可用於並行化 std::inner_product

#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple.hpp>
#include <experimental/execution_policy>
#include <experimental/numeric>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector<double> xvalues(10007, 1.0), yvalues(10007, 1.0);
 
    double result = std::experimental::parallel::transform_reduce(
        std::experimental::parallel::par,
        boost::iterators::make_zip_iterator(
            boost::make_tuple(std::begin(xvalues), std::begin(yvalues))),
        boost::iterators::make_zip_iterator(
            boost::make_tuple(std::end(xvalues), std::end(yvalues))),
        [](auto r) { return boost::get<0>(r) * boost::get<1>(r); }
        0.0,
        std::plus<>()
    );
    std::cout << result << '\n';
}

輸出

10007

[編輯] 另請參閱

對一個範圍的元素進行求和或摺疊
(函式模板) [編輯]
對一個範圍的元素應用函式,並將結果儲存在目標範圍中
(函式模板) [編輯]
(並行 TS)
類似於 std::accumulate,但順序是亂序的
(函式模板) [編輯]