std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
Определено в заголовочном файле <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
См. также
|
(C++17)(C++17)(C++17)(C++20) | типы политик выполнения (класс) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag