'How to create new tasks with OpenMP while other tasks are processed?
I've been looking around for days now, and I couldn't find the explanation (and thus a solution) to my problem.
I have a file of millions of lines that I want to process independently. Each line represents an object on which I call a function, whose execution time depends on some properties of the object itself. The execution time is not balanced, it can take less than a second in some cases, or minutes or hours.
I parse the file with a while loop and use tasks:
ifstream fin(FileName);
#pragma omp parallel num_threads(N)
{
#pragma omp single
{
string line;
#pragma omp task untied
while (getline(fin, line)) {
#pragma omp task firstprivate(line)
function(line);
}
}
}
What I observe is that sometimes some threads are suspended, like if the task pool is empty and they wait for the creation of new tasks. I noticed that I reach the point where all but one thread are suspended, and as soon as this thread ends performing its task, they all restart running. In my first version I wasn't using the untied clause and I thought the problem could be that the thread that is reading the file (say thread 0) is busy executing a long task itself and cannot go on with reading the file and creating new tasks. But, adding the untied clause (so that any other thread can continue reading the file) doesn't solve the issue.
I have some questions:
If the number of tasks is much bigger than the pool size (which I understood is 64*number_of_threads), does the thread 0 suspend its current task (i.e. reading the file and generating new tasks) and help the other threads execute tasks? I usually read that it helps other threads when it ends creating tasks (like here: how-do-omp-single-and-omp-task-provide-parallelism), but I have the impression it's not my case.
Is the untied clause doing what I expect? That is, if the thread 0 is busy in executing a task, then another thread can continue reading the file and fill the pool of tasks? If this is true, why I see threads that are not doing anything?
Is there a barrier that is preventing any thread to go on reading the file and create new tasks until the pool is empty? I would like that as soon as a thread sees that the task pool is empty, it fills it with new tasks. I would be happy even if a thread is dedicated only to read a new line as soon as a task is picked from the pool by another thread.
Thanks for any suggestion!
Solution 1:[1]
Remove the task before the while. A typical idiom is that one thread (from parallel/single) generates all the tasks, and the others take them off the internal task queue. So your while loop should be executed by a thread, not in a task. It will then generate the tasks, corresponding to your lines, for the other threads to execute.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
