Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle sub task #38

Closed
Alex-PLACET opened this issue May 2, 2022 · 3 comments
Closed

How to handle sub task #38

Alex-PLACET opened this issue May 2, 2022 · 3 comments

Comments

@Alex-PLACET
Copy link

Alex-PLACET commented May 2, 2022

Hello,

In case that you create tasks inside a task:

auto main_task = tw::make_task(tw::wait, [exec]{
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7};
auto sub_task = tw::for_each(exec, vec.begin(), vec.end(), [](int& x){ x *= 2; });
sub_task-wait();
});

How can we handle this issue? If we imagine that I have a single worker in my pool, my main task will be paused until my sub task is finished but because the worker is already used by the main_task, my sub_task is blocked because there is no free worker.

Regards

@bloomen
Copy link
Owner

bloomen commented May 2, 2022

Creating tasks within tasks is not supported by transwarp (unless you're using a separate executor but it still feels somewhat hacky). What is encouraged instead is to construct a graph of tasks where your current "sub_task" would be one of the parents to "main_task". Makes it much easier to reason about your tasks and their inter-dependencies. Does that make sense?

@Alex-PLACET
Copy link
Author

Do you have some ideas in mind about this hacky executor ?
In my case I already have a codebase, and it will be hard to put every tasks in a single function.
I will try to dedicate time next weeks to clone the repo and add features like the support of subgraph, subtask. As you know well the code do you think it's possible ?

@bloomen
Copy link
Owner

bloomen commented May 4, 2022

To make your code above work you need to use a separate executor, e.g.:

tw::parallel main_exec{2};
tw::parallel exec{2};
auto main_task = tw::make_task(tw::wait, [&exec]{
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7};
auto sub_task = tw::for_each(exec, vec.begin(), vec.end(), [](int& x){ x *= 2; });
sub_task->wait();
});
main_task->schedule(main_exec);

Adding some sort of subgraph support to transwarp doesn't really make sense. With transwarp you can build up graphs and that's it. Sure, you can create another graph within a task. I wouldn't call that a subgraph tho. A task is generic, it can do whatever.

@bloomen bloomen closed this as completed May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants