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

Lecture 11: omp_get_num_threads() #21

Open
alyfarahat opened this issue May 6, 2016 · 3 comments
Open

Lecture 11: omp_get_num_threads() #21

alyfarahat opened this issue May 6, 2016 · 3 comments

Comments

@alyfarahat
Copy link

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
Copy link
Member

cswiercz commented May 6, 2016

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
Copy link
Author

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
Copy link
Member

cswiercz commented May 9, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants