名前空間
変種
操作

std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
execution::seqexecution::parexecution::par_unseqexecution::unseq
(C++17)    (C++17)(C++17)(C++20)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダ <execution> で定義
inline constexpr
std::execution::sequenced_policy seq { /* 未指定 */ };
(C++17以降)
inline constexpr
std::execution::parallel_policy par { /* 未指定 */ };
(C++17以降)
inline constexpr
std::execution::parallel_unsequenced_policy par_unseq { /* 未指定 */ };
(C++17以降)
inline constexpr
std::execution::unsequenced_policy unseq { /* 未指定 */ };
(C++20以降)

実行ポリシーの型

は、それぞれ次のインスタンスを持ちます。

  • std::execution::seq,
  • std::execution::par,
  • std::execution::par_unseq、および
  • std::execution::unseq.

これらのインスタンスは、並列アルゴリズムの実行ポリシー、すなわち許可される並列処理の種類を指定するために使用されます。

追加の実行ポリシーは、標準ライブラリの実装によって提供される場合があります(将来の追加として、std::parallel::cudastd::parallel::opencl が考えられます)。

[編集]

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <random>
#include <vector>
 
#ifdef PARALLEL
#include <execution>
    namespace execution = std::execution;
#else
    enum class execution { seq, unseq, par_unseq, par };
#endif
 
void measure([[maybe_unused]] auto policy, std::vector<std::uint64_t> v)
{
    const auto start = std::chrono::steady_clock::now();
#ifdef PARALLEL
    std::sort(policy, v.begin(), v.end());
#else
    std::sort(v.begin(), v.end());
#endif
    const auto finish = std::chrono::steady_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)
              << '\n';
};
 
int main()
{
    std::vector<std::uint64_t> v(1'000'000);
    std::mt19937 gen {std::random_device{}()};
    std::ranges::generate(v, gen);
 
    measure(execution::seq, v);
    measure(execution::unseq, v);
    measure(execution::par_unseq, v);
    measure(execution::par, v);
}

実行結果の例

// online GNU/gcc compiler (PARALLEL macro is not defined)
81ms
80ms
79ms
78ms
 
// with g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL
165ms
163ms
30ms
27ms

[編集] 関連項目

実行ポリシー型
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)