gpt4 book ai didi

c++ - TBB 任务分配断言

转载 作者:行者123 更新时间:2023-11-28 08:14:55 27 4
gpt4 key购买 nike

我正在尝试通过 TBB 任务和延续来遍历树。代码如下。当我运行代码时,它不断中止(经常,但不总是)并出现以下错误:

Assertion t_next->state()==task::allocated failed on line 334 of file ../../src/tbb/custom_scheduler.h Detailed description: if task::execute() returns task, it must be marked as allocated

是什么导致了这个问题?

template<class NodeVisitor>
void
traverse_tree(NodeVisitor& nv)
{
TreeTraversal<NodeVisitor>& tt = *(new(task::allocate_root()) TreeTraversal<NodeVisitor>(nv));
task::spawn_root_and_wait(tt);
}

template<class NodeVisitor>
class TreeTraversal: public task
{
public:
struct Continuation;

public:
TreeTraversal(NodeVisitor nv_):
nv(nv_) {}

task* execute()
{
nv.pre();

Continuation* c = new(allocate_continuation()) Continuation(nv);
c->set_ref_count(nv.size());
for (size_t i = 0; i < nv.size(); ++i)
{
TreeTraversal& tt = *(new(c->allocate_child()) TreeTraversal(nv.child(i)));
spawn(tt);
}

if (!nv.size())
return c;

return NULL;
}

private:
NodeVisitor nv;
};

template<class NodeVisitor>
class TreeTraversal<NodeVisitor>::Continuation: public task
{
public:
Continuation(NodeVisitor& nv_):
nv(nv_) {}
task* execute() { nv.post(); return NULL; }

private:
NodeVisitor nv;
};

最佳答案

我以前从未见过任务被分配为延续,然后从 execute() 返回。这可能是断言失败的原因(更新:一项实验表明它不是,请参阅下面的详细信息)。

与此同时,您可以将 TreeTraversal::execute() 的代码更改为大致如下:

nv.pre();
if (!nv.size())
nv.post();
else {
// Do all the task manipulations
}
return NULL;

更新:下面显示的简化测试在我的双核笔记本电脑上运行良好。这让我假设您的实际代码中可能存在内存损坏,在这种情况下,上面建议的重新洗牌可能只是隐藏问题而不是修复它。

#include "tbb/task.h"
using namespace tbb;

class T: public task {
public:
class Continuation: public task {
public:
Continuation() {}
task* execute() { return NULL; }
};

private:
size_t nv;

public:
T(size_t n): nv(n) {}

task* execute() {
Continuation* c = new(allocate_continuation()) Continuation();
c->set_ref_count(nv);
for (size_t i = 0; i < nv; ++i) {
T& tt = *(new(c->allocate_child()) T(nv-i-1));
spawn(tt);
}
return (nv==0)? c : NULL;
}
};

int main() {
T& t = *new( task::allocate_root() ) T(24);
task::spawn_root_and_wait(t);
return 0;
}

关于c++ - TBB 任务分配断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7985425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com