Namespaces
Variants

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

ja.cppreference.net より
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとレンジのアルゴリズム (C++20)
制約付きアルゴリズム(例:ranges::copy、ranges::sort、...)
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割済み範囲に対する)
集合操作(ソート済み範囲に対する)
マージ操作(ソート済み範囲に対する)
ヒープ操作
最小値/最大値操作
(C++11)
(C++17)
辞書順比較操作
順列操作


 
ヘッダ <execution>
inline constexpr
std::execution::sequenced_policy seq { /* unspecified */ };
で定義 (C++17 以降)
inline constexpr
std::execution::parallel_policy par { /* unspecified */ };
(C++17 以降)
inline constexpr
std::execution::parallel_unsequenced_policy par_unseq { /* unspecified */ };
(C++17 以降)
inline constexpr
std::execution::unsequenced_policy unseq { /* unspecified */ };
(C++20 以降)

実行ポリシーの型

  • std::execution::sequenced_policy,
  • std::execution::parallel_policy,
  • std::execution::parallel_unsequenced_policy, および
  • std::execution::unsequenced_policy

には、それぞれ以下のインスタンスが存在する:

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

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

追加の実行ポリシーが標準ライブラリの実装によって提供される可能性がある(将来の追加には std::parallel::cuda や std::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

関連項目

実行ポリシーの型
(クラス)