2D的Wavefront如下图所示:
#include <taskflow/taskflow.hpp>
std::mutex cout_mutex;
void format_str(std::string const& str) {
std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << str << std::endl;
}
std::string createTaskName(int i, int j) {
return "Task" + std::to_string(i) + "_" + std::to_string(j);
}
int main() {
tf::Executor executor;
tf::Taskflow taskflow;
int num_blocks = 3; // 3*3的 Wavefront
std::vector<std::vector<tf::Task>> Tasks(num_blocks); // 存放每个task对象
for(int i = 0; i < num_blocks; i++) {
for(int j = 0; j < num_blocks; j++) {
const std::string task_name = createTaskName(i,j);
// 创建task[i][j]
Tasks[i].emplace_back(
taskflow.emplace([task_name](){
format_str(task_name);
}).name(task_name)
);
// 构建依赖关系
if(i > 0) {
Tasks[i][j].succeed(Tasks[i-1][j]);
}
if(j > 0) {
Tasks[i][j].succeed(Tasks[i][j-1]);
}
}
}
executor.run(taskflow).wait();
taskflow.dump(std::cout);
}