名稱空間
變體
操作

std::execution::bulk, std::execution::bulk_chunked, std::execution::bulk_unchunked

來自 cppreference.com
< cpp‎ | execution
 
 
 
定義於標頭檔案 <execution>
std::execution::sender

auto bulk( std::execution::sender auto input,
           std::integral auto size,
           auto&& policy,
           std::invocable<decltype(size),

                          /*values-sent-by*/(input)...> function );
(1) (C++26 起)
std::execution::sender

auto bulk_chunked( std::execution::sender auto input,
                   std::integral auto size,
                   auto&& policy,
                   std::invocable<decltype(size), decltype(size),

                                  /*values-sent-by*/(input)...> function2 );
(2) (C++26 起)
std::execution::sender

auto bulk_unchunked( std::execution::sender auto input,
                     std::integral auto size,
                     std::invocable<decltype(size), decltype(size),

                                    /*values-sent-by*/(input)...> function );
(3) (C++26 起)

目錄

[編輯] 引數

input - 傳送者,一旦執行便傳送函式執行所需的值
policy - 附在 function/function2 上的執行策略
function - 可呼叫物件,針對範圍 [0size) 內的每個索引呼叫,同時傳遞輸入傳送器產生的值。
function2 - function 相同,但使用一對索引 (be) 呼叫,其中 b < e,以便對於範圍 [[0size) 中的每個索引 i,恰好有一個對 function2 的呼叫使得 b <= i < e

[編輯] 返回值

返回一個傳送器,描述由輸入傳送器描述的任務圖,並添加了一個節點,用於呼叫所提供的函式,索引範圍為 [0size),同時將輸入傳送器傳送的值作為引數傳遞。

保證 function/function2 不會在返回的傳送器啟動之前開始執行。

[編輯] 錯誤完成

所有由 input 傳遞進來的錯誤都會被轉發。

此外,傳送器可以以包含以下內容的 std::exception_ptr 錯誤完成:

  • function 丟擲的任何異常
  • 如果實現未能分配所需資源,則為 std::bad_alloc
  • 其他內部錯誤(例如,無法將異常從執行上下文傳播到呼叫者)的派生自 std::runtime_error 的異常。

[編輯] 取消

未定製的 std::execution::bulkstd::execution::bulk_chunkstd::execution::bulk_unchunked 會轉發來自 input 的停止完成訊號。它們不提供額外的機制來生成停止完成訊號。

[編輯] 注意

當呼叫 std::execution::bulkstd::execution::bulk_chunked 時,function/function2 的不同調用可能發生在同一個執行代理上。

當呼叫 std::execution::bulk_unchunked 時,function 的不同調用必須發生在不同的執行代理上。

std::execution::bulk 的預設實現基於 std::execution::bulk_chunked。雖然可以定製 std::execution::bulk,但預計大多數情況下只定制 std::execution::bulk_chunked

在沒有定製 std::execution::bulkstd::execution::bulk_chunked 的情況下,std::execution::bulkstd::execution::bulk_chunk 的行為是序列執行 function,這並非特別有用。預計實現會有定製,使得在不同調度器上執行 std::execution::bulkstd::execution::bulk_chunked 更有用。

std::execution::bulk_unchunked 旨在用於 function 可能在不同調用之間存在依賴關係,並且需要併發前向進度保證(並行前向進度不足以滿足)的情況。以 1000 的大小執行 std::execution::bulk_unchunked 將需要 1000 個執行代理(例如,執行緒)併發執行。

std::execution::bulk_unchunked 不需要執行策略,因為已經預期 function 能夠併發執行。

[編輯] 示例

execution::bulk 的可能用法。

std::vector<double> x;
std::vector<double> y;
//...
sender auto process_elements
    = just(get_coefficient())
    | bulk(x.size(), [&](size_t i, double a)
    {
        y[i] = a * x[i] + y[i];
    });
// process_elements describes the work described by calling a function to
// get a coefficient `a`, and using it to execute
//   y[i] = a * x[i] + y[i]
// for each `i` in range [0, x.size())

execution::bulk_chunked 的可能用法。

std::vector<std::uint32_t> data = ...;
std::atomic<std::uint32_t> sum{0};
sender auto s = bulk_chunked(just(), par, 100000,
    [&sum, &data](int begin, int end)
    {
        auto partial_sum = std::accumulate(data.begin() + begin, data.begin() + end, 0U);
        sum.fetch_add(partial_sum);
    });
// the atomic object will not be touched 100000 times; will execute faster than bulk()