1 Star 0 Fork 0

ly-code/taskflow

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dependent_async_algorithm.cpp 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
twhuang 提交于 2023-06-13 19:38 +08:00 . updated unittest for dep_async
/**
This program demonstrates how to use dependent async tasks to create
dependent algorithm tasks.
*/
#include <taskflow/taskflow.hpp>
#include <taskflow/algorithm/for_each.hpp>
#include <taskflow/algorithm/transform.hpp>
#include <taskflow/algorithm/reduce.hpp>
int main(){
const size_t N = 65536;
tf::Executor executor;
int sum{1};
std::vector<int> data(N);
// for-each
tf::AsyncTask A = executor.silent_dependent_async(tf::make_for_each_task(
data.begin(), data.end(), [](int& i){ i = 1; }
));
// transform
tf::AsyncTask B = executor.silent_dependent_async(tf::make_transform_task(
data.begin(), data.end(), data.begin(), [](int& i) { return i*2; }
), A);
// reduce
tf::AsyncTask C = executor.silent_dependent_async(tf::make_reduce_task(
data.begin(), data.end(), sum, std::plus<int>{}
), B);
// wait for all async task to complete
executor.wait_for_all();
// verify the result
if(sum != N*2 + 1) {
throw std::runtime_error("INCORRECT RESULT");
}
else {
std::cout << "CORRECT RESULT\n";
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ly-code/taskflow.git
git@gitee.com:ly-code/taskflow.git
ly-code
taskflow
taskflow
master

搜索帮助