0
votes

I'm trying to traverse a tree via TBB tasks and continuations. The code is below. When I run the code it keeps aborting (frequently, although not always) with the following error:

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

What can be causing this problem?

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;
};
1
You use in place new, but you never destroy, so destructors are not called. I have no idea if this is the problem. - Mooing Duck
I think TBB is supposed to take care of that destruction. I could be wrong, however. - foxcub
I know nothing of TBB beyond looking at your code. It's possible. - Mooing Duck
@MooingDuck: Yes, TBB is supposed to take care of the destruction of tasks after execution. - Alexey Kukanov
@AlexeyKukanov: They destroy themselves as well? I've never heard of such a thing. That's strange. - Mooing Duck

1 Answers

1
votes

I have never seen before that a task is allocated as a continuation and then returned from execute(). That might be the reason of the assertion failure (update: an experiment showed it is not, see details below).

Meanwhile, you can change the code of TreeTraversal::execute() to be roughly this:

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

Update: a simplified test shown below worked well on my dual-core laptop. That makes me suppose possible memory corruption in your actual code, in which case the re-shuffling suggested above might just hide the issue but not fix it.

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