并行扫描
Taskflow 提供模板方法来构建任务以对一系列项目执行并行扫描。
头文件
#include <kthread/algorithm/scan.h>
什么是扫描操作?
并行扫描任务执行输入范围的累积和(也称为前缀和或扫描),并将结果写入输出范围。输出 范围的每个元素都包含使用给定二元运算符进行求和的所有先前元素的累计总和。
创建并行包容性扫描任务
kthread::inclusive_scan(B first, E last, D d_first, BOP bop) 生成一个包含扫描,这意味着输出范围的第 N 个元
素是前 N 个输入元素的总和,因此第 N 个输入元素被包括在内。例如,以下代码对五个元素执行包含扫描:
std::vector<int> input = {1, 2, 3, 4, 5};
std::vector<int> output(input.size())
taskflow.inclusive_scan(
input.begin(), input.end(), output.begin(), std::plus<int>{}
);
executor.run(taskflow).wait();
// output is {1, 3, 6, 10, 15}
输出范围可能与输入范围相同,其中扫描操作是就地的,结果写入输入范围。例如,以下代码对五个元素执行就地包容性扫描:
std::vector<int> input = {1, 2, 3, 4, 5};
taskflow.inclusive_scan(
input.begin(), input.end(), input.begin(), std::plus<int>{}
);
executor.run(taskflow).wait();
// input is {1, 3, 6, 10, 15}