Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      execution::seqexecution::parexecution::par_unseqexecution::unseq
      (C++17)    (C++17)(C++17)(C++20)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Defined in header<execution>
      inlineconstexpr
      std::execution::sequenced_policy seq{/* unspecified */};
      (since C++17)
      inlineconstexpr
      std::execution::parallel_policy par{/* unspecified */};
      (since C++17)
      inlineconstexpr
      std::execution::parallel_unsequenced_policy par_unseq{/* unspecified */};
      (since C++17)
      inlineconstexpr
      std::execution::unsequenced_policy unseq{/* unspecified */};
      (since C++20)

      The execution policy types

      have the following respective instances:

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

      These instances are used to specify the execution policy of parallel algorithms, i.e., the kinds of parallelism allowed.

      Additional execution policies may be provided by a standard library implementation (possible future additions may includestd::parallel::cuda andstd::parallel::opencl).

      [edit]Example

      Run this code
      #include <algorithm>#include <chrono>#include <cstdint>#include <iostream>#include <random>#include <vector> #ifdef PARALLEL#include <execution>namespace execution= std::execution;#elseenumclass execution{ seq, unseq, par_unseq, par};#endif void measure([[maybe_unused]]auto policy,std::vector<std::uint64_t> v){constauto start=std::chrono::steady_clock::now();#ifdef PARALLELstd::sort(policy, v.begin(), v.end());#elsestd::sort(v.begin(), v.end());#endifconstauto 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);}

      Possible output:

      // online GNU/gcc compiler (PARALLEL macro is not defined)81ms80ms79ms78ms // with g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL165ms163ms30ms27ms

      [edit]See also

      execution policy types
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/execution_policy_tag&oldid=150256"

      [8]ページ先頭

      ©2009-2025 Movatter.jp