回收父母作为孩子在那里
Recycling parent as child in tbb
我想使用tbb::task
s并行化一个看似简单的问题。我的任务可以分成子任务,子任务的数量不能选择,而是由任务的状态决定(事先不知道)。由于父任务不需要其子任务的结果,所以我想将父任务作为子任务回收。我在在线文档或示例中找不到一个很好的工作示例,因此我在这里提出问题。我现在的想法是这样写:
struct my_task : tbb::task {
typedef implementation_defined task_data;
task_data DATA;
my_task(task_data const&data) : DATA(data) {}
void reset_state(task_data const&data) { DATA=data; }
bool is_small() const;
void serial_execution();
bool has_more_sub_tasks() const;
task_data parameters_for_next_sub_task();
tbb::task*execute()
{
if(is_small()) {
serial_execution();
return nullptr;
}
tbb::empty_task&Continuation = allocate_continuation(); // <-- correct?
task_data first_sub_task = parameters_for_next_sub_task();
int sub_task_counter = 1;
tbb::task_list further_sub_tasks;
for(; has_more_sub_tasks(); ++sub_task_counter)
further_sub_tasks.push_back(*new(Continuation.allocate_child())
my_task(parameters_for_next_sub_task());
Continuation.set_ref_count(sub_task_counter); // <-- correct?
spawn(further_sub_tasks);
recycle_as_child_of(Continuation); // <-- correct?
reset_state(first_sub_task); // change state
return this; // <-- correct?
}
};
my_task*root_task = new(tbb::task::allocate_root())
my_task(parameters_for_root_task());
tbb::task::spawn_root_and_wait(*root_task);
这是正确的和/或最好的方法吗?(注意,在上面的代码中,既不生成也不返回空continuation任务)
创建延续的那一行应该是:
tbb::empty_task&Continuation = *new(allocate_continuation()) tbb::empty_task;
set_ref_count和reset_state之间的逻辑看起来是正确的。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- 链接 openGL 库失败,因为它无法打开 libgl.so ...但它就在那里
- C++:使用数组、指针和 for 循环制作直方图。我就在那里,我可能想得太辛苦了
- C 是否具有接口类概念,如果它在那里,那么它与Java接口类别有何不同
- 为什么静态必须在那里
- "我"的意思,为什么在那里?
- 班级"Oscar"可能在那里做什么?
- 为什么"ostream & os"必须在那里C++操作员过载?
- 田野不能被重新爱,虽然它显然在那里!(实现地图.h)
- 定义一个全局变量并在那里初始化它本身可以吗?
- 找不到命名空间,尽管它就在那里
- C:主要未找到,但它在那里 |编译错误
- "missing return statement",但我知道它在那里
- 我的程序显示空的表面,它需要在那里显示地图
- 'ld:找不到建筑???符号",即使它在那里
- 回收父母作为孩子在那里