Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
15 Views

Problem in affinity of a omp task to a thread

for(every item in the array)
{
#pragma omp task
foo(item);
}
Generally, a task created by a thread will be wired to the thread that created it. In the above pseudo code, as every task is created, it will be run by the thread which created it. So, irrespective of the number of processors, all the tasks will be run by the same thread(also the hardware thread). In this case, the tasking feature does not seem to take advantage of the multiple cores . How can the said scenario take advantage of multi core processors?
0 Kudos
1 Reply
Highlighted
Black Belt
15 Views

Ashok -

If you have each thread running the for-loop, you may be right about each task being executed by the thread that creates it, but you would also run the loop more times than it would have been done in the serial code. If you put a worksharing construct around the for-loop, I would wonder why you would create tasks when iterations of the loop will be assigned to a different thread.

I think you have made an incorrect assumption about tasks. Check out the OpenMP 3.0 spec from openmp.org. Section 2.7 deals with tasks. The Description of the task construct (page 60, lines 20-24) states:

When a thread encounters a task construct, a task is generated from the code for the associated structured block. ... The encountering thread may immediately execute the task, or defer its execution. In the latter case, any thread in the team may be assigned the task.

You haven't shown how you created threads, which is important. Here is the way I've coded solutions using the OpenMP task construct:

#pragma omp parallel
{
#pragma omp single
MyComputation();
}

. . .

void MyComputation()
{
. . .

for(every item in the array) {
#pragma omp task
foo(item);
}
#pragma omp taskwait
. . .
}


In the above structure, threads are started at the beginning parallel region, but only one of them is allowed to call MyComputation(). The rest of the threads will wait a the implicit barrier for the singel region. The thread executing MyComputation()will spawn tasks as it runs through the iteration of the for-loop in the function. The OpenMP runtime, seeing threads just waiting at the implicit barrier, will assign the generated tasks to those idle threads. The (optional) taskwait pragma will set up a barrier to ensure that all tasks have completed before the code following the for-loop is executed.

--clay
0 Kudos