taskflow / taskflow

A General-purpose Parallel and Heterogeneous Task Programming System

Home Page:https://taskflow.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

multiple subflows not working

jacobsologub opened this issue · comments

I'm trying to map out the following...

group

SUB-2 task(s) are never kicked off (SUB-1 works). If I remove the return 0; from B then both SUB-1 and SUB-2 task(s) are executed.

tf::Executor executor;
tf::Taskflow taskflow;

bool b_success = true;

auto a = taskflow.emplace ([&] () {
    DBG ("A");
    return 0;
});

auto cond = taskflow.emplace ([&] () {
    DBG ("COND");
    return 1;
});

auto b = taskflow.emplace ([&] () {
    DBG ("B");
    return 0;
});

auto c = taskflow.emplace ([&] () {
    DBG ("C");
});

auto sub1 = taskflow.emplace ([&] (tf::Subflow& subflow) {
    if (! b_success) {
        return;
    }
    
    auto a = subflow.emplace ([&] () {
        DBG ("SUB-1 A");
        return 0;
    });
    
    auto cond = subflow.emplace ([&] () {
        DBG ("SUB-1 COND");
        return 1;
    });
    
    auto b = subflow.emplace ([&] () {
        DBG ("SUB-1 B");
        return 0;
    });

    a.precede (cond);
    cond.precede (cond, b);
});

auto sub2 = taskflow.emplace ([&] (tf::Subflow& subflow) {
    if (! b_success) {
        return;
    }
    
    auto a = subflow.emplace ([&] () {
        DBG ("SUB-2 A");
        return 0;
    });
    
    auto cond = subflow.emplace ([&] () {
        DBG ("SUB-2 COND");
        return 1;
    });
    
    auto b = subflow.emplace ([&] () {
        DBG ("SUB-2 B");
        return 0;
    });

    a.precede (cond);
    cond.precede (cond, b);
});

a.precede (cond);
cond.precede (cond, b);
b.precede (sub1, sub2);

c.succeed (sub1, sub2);
executor.run (taskflow).wait();

return 0 invokes conditional tasking and means execute the first (index 0) subsequent task (subflow 1 in your case). If you were to return 1 then it would execute subflow 2 instead. If you want to execute both, you should remove the return altogether and not using conditional tasking.

It is also possible to use a multi-condition task to branch to multiple branches at once, but if you want to execute all branches of a conditional task, then using conditional tasking seems unnecessary.

Thank you for your help, @danielytics !

Thank for the details @danielytics! I was very excited to jump in and I did not fully read the documentation. Really enjoying the library!