std::execution:: seq, std::execution:: par, std::execution:: par_unseq, std::execution:: unseq
From cppreference.net
C++
Algorithm library
|
헤더에 정의됨
<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); }
가능한 출력:
// 온라인 GNU/gcc 컴파일러 (PARALLEL 매크로가 정의되지 않음) 81ms 80ms 79ms 78ms // g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL 사용 시 165ms 163ms 30ms 27ms
참고 항목
|
(C++17)
(C++17)
(C++17)
(C++20)
|
실행 정책 타입
(클래스) |