Open alyfarahat opened 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.
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;
}
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.
Around minute 42:00, it seems that the code on the slide calls
omp_get_num_threads()
before starting theomp parallel
block. Wouldn't that return just 1-thread?