c++ - TBB 任务分配断言

标签 c++ tbb

我正在尝试通过 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/

相关文章:

c++ - 如何使 Intel TBB multifunction_node 具有动态端口数?

c++ - 并行执行比串行执行花费更多时间?

c++ - 将 boolean 值写入文件

c++ - 学习 C++ 模板时的一个难题

c++ - 使用锁 C++ 锁定数组的元素

c++ - 英特尔 TBB : pool of graphs

c++ - "[&](int i) "是否转换为 tbb 中的引用并行?

c++ - 有没有办法在所有新的 Qt 项目上强制使用 QT_NO_FOREACH?

c++ - 静态短[9][9] getRandomBoard();

c++ - 英特尔 TBB 中的任务延续