uwhpsc-2016 / lectures

Notes, slides, and code from the in-class lectures.
7 stars 21 forks source link

Lecture 11: omp_get_num_threads() #21

Open alyfarahat opened 8 years ago

alyfarahat commented 8 years ago

Around minute 42:00, it seems that the code on the slide calls omp_get_num_threads() before starting the omp parallel block. Wouldn't that return just 1-thread?

cswiercz commented 8 years ago

This is just a snippet of code which, somewhat implicitly, assumes that the threads have been set at some point before that location via omp_set_num_threads().

Which raises the question, "If you don't explicitly set the number of threads then what is the default?" On my machine it happens to be eight threads.

alyfarahat commented 8 years ago

I have tried to experiment with the included code. With the commented line disabled, I get 10 threads to run in parallel. With the commented line enabled, I only get 1 thread (the master thread). At least, based on what I observe, it seems that omp_get_num_threads() returns the number of active threads at the time of its call, and not the number of threads omp is configured to spawn.

void func1()                                                                                                  
{                                                                                                             
   omp_set_num_threads( 10 );                                                                                 

   unsigned num_of_threads = omp_get_num_threads();                                                           

   #pragma omp parallel \
   // num_threads(num_of_threads)                                                                                       
   {                                                                                                          
      printf("id = %d\n", omp_get_thread_num());                                                              
   }                                                                                                          

   return;                                                                                                    
}
cswiercz commented 8 years ago

Wow. That's a very good observation. I suppose I have been (un)fortunate enough to not sure omp_set_num_threads() in the wrong context.