是否有类似于PPL任务连续性的TBB方法呢? 我熟悉TBB的底层方法,手动分配并手动分配连续任务,并手动管理它们的引用计数:
struct FibContinuation: public task {
long* const sum;
long x, y;
FibContinuation( long* sum_ ) : sum(sum_) {}
task* execute() {
*sum = x+y;
return NULL;
}
};
struct FibTask: public task {
const long n;
long* const sum;
FibTask( long n_, long* sum_ ) :
n(n_), sum(sum_)
{}
task* execute() {
if( n<CutOff ) {
*sum = SerialFib(n);
return NULL;
} else {
// long x, y; This line removed
FibContinuation& c =
*new( allocate_continuation() ) FibContinuation(sum);
FibTask& a = *new( c.allocate_child() ) FibTask(n-2,&c.x);
FibTask& b = *new( c.allocate_child() ) FibTask(n-1,&c.y);
// Set ref_count to "two children plus one for the wait".
c.set_ref_count(2);
spawn( b );
spawn( a );
// *sum = x+y; This line removed
return NULL;
}
}
};
这太可怕了。您需要事先知道要生成多少子任务,并手动设置适当的引用计数。这是非常脆弱的编码...
PPL指定继续方式的方法非常直接:
create_task([]()->bool
{
// compute something then return a bool result
return true
}).then([](bool aComputedResult)
{
// do something with aComputedResult
});
你如何在TBB中实现这一点?
then
,我认为自 1.53 以来,可能并没有实现所有方法,而其他一些方法可能存在一些错误。请查看文档。TBB 没有类似的东西,最接近的是 TBB 流程图。您可以为消息创建流程图,TBB 将在可能的情况下并行化。虽然这不像then
那么简单,但它也更加强大。最后,我想提到的是,TBB 不是专注于基于任务的并行性,而是专注于抽象数据并行问题的算法模式。 - Stephan Dollberg