0

TBB タスクと継続を介してツリーをトラバースしようとしています。コードは以下です。コードを実行すると、次のエラーで中断し続けます (常にではありませんが、頻繁に)。

アサーション t_next->state()==task::allocated がファイル ../../src/tbb/custom_scheduler.h の 334 行目で失敗しました割り当てられた

この問題の原因は何ですか?

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;
};
4

1 に答える 1

1

タスクが継続として割り当てられ、 から返されることはこれまで見たことがありません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;
}
于 2011-11-02T21:02:44.350 に答える