When threads exceeds no. of processors

General OpenMP discussion

When threads exceeds no. of processors

Postby lancer6238 » Sun Aug 10, 2008 7:44 pm

Hi all, I'm just starting to learn openMP, and am still trying to wrap my mind around the concept of threads.

I have some difficulty understanding how the threads are created/run in the following code (run on a quad-core machine):

Code: Select all
omp_set_dynamic(0);
omp_set_nested(1);
omp_set_num_threads(10);
#pragma omp parallel
{
   #pragma omp parallel
   {
      #pragma omp single
      printf("Num threads in nested region is = %d\n", omp_get_num_threads());
   }
}


The output I got was

Code: Select all
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10
Num threads in nested region is = 10


What I don't understand is on a quad-core machine, the maximum number of threads is 4. So how is it possible to have 10 threads each with 10 "nested" threads? How is the work distributed among the threads?

Thank you.

Regards,
Rayne
lancer6238
 
Posts: 6
Joined: Sun Aug 10, 2008 7:31 pm

Re: When threads exceeds no. of processors

Postby ejd » Mon Aug 11, 2008 5:34 am

You don't say what operating system you are running on. In any case, if you look at the Windows task manager under the processes tab you will see a large number of processes running on your system (much greater than the number of physical processors you have). If you are running Unix, try running "ps -ef" and again you will see a large number of processes running. Each of these processes is essentially the same as a thread (in OpenMP terminology). So you can see that there are already a large number of threads running on your 4 processor machine. The operating system handles how these threads are scheduled to run on the processors. You will also note, that even though there are a large number of processes running, very little of your CPU is actually being used.

In OpenMP, the processes are structured. The parallel pragma says to create threads, which are then given work and put on the operating system processor queues. The operating system schedules how these processes are to be run. So you can create many more threads than you have processors. However, there is overhead associated with this and if you over-subscribe the system (have many more threads than you do physical processors to process them), then instead of reducing the time it takes to do the work, you will increase it. Generally, for your OpenMP jobs, it is not a good idea to use more threads than you have physical processors for this reason.

The other thing is that not all implementations of OpenMP work the same way. Some will create as many threads as you request. Others will create a subset and partition the work out on these threads themselves. The Microsoft Windows implementation for example, I believe from the documentation, will only use a maximum of 64 threads.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 11 guests