2013-04-14 79 views
3

有没有类似于PPL在TBB中的任务延续? 我知道手册册分配tbb::task S的水平低TBB方法和手动分配也延续任务和手动管理裁判计数他们:英特尔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?

+1

只是一些随机的想法。提升期货有'然后',我认为自1.53以来,可能不是所有的方法都实现了,而其他方法可能有一些错误。检查文档。 TBB没有类似的东西,接近的就是TBB流程图。您可以为您的消息创建流程方案,TBB将尽可能并行化。虽然这不像'then'那么简单,但它也更强大。最后,我想提到的是,TBB并不关注基于任务的并行性,而是关注将数据并行问题提取出来的算法模式。 – inf

+0

@bamboon嗯,我会给一个想法,谢谢你回答 –

+0

问题是比较两种不同语言方言的实现,并抱怨一个而不是其他问题的具体问题的脆弱性。 TBB例子可以循环两次新的增量产卵,现在脆弱性消失了。可能还有更高效的spawn_all()或者其他东西! – mabraham

回答

1

没有什么直接的东西,我发布了一个例子,说明如何在我的博客here上使用task_group(tbb)进行此操作。

语法类似但自从任务存在之前发布以来不是100%。

void SimpleContinuation() 
{ 
    auto task1 = run_task([](){ContinueableTask(1);}); 
    //task 2 depends on task 1 
    auto task2 = run_when(task1, [](){ContinueableTask(2);}); 
    wait_for_all(task1, task2); 
} 
+0

它不回答关于它在TBB中的问题。 TBB没有'run_task'和'run_when'方法 – Anton

+0

对不起,我没有得到这些函数是在你的博客上task_group的顶部实现的 – Anton