How do I detect the number of threads in OpenMp before the parallel region starts? If I use nested parallelism the environment variable OMP_NUM_THREADS
looks like 4,64
.
get_nested_num_threads();
#pragma omp parallel
{
// starting 4 threads
#pragma omp parallel
{
// starting 64 threads for each of the 4
}
}
This answer leads to my implementation of querying OMP_NUM_THREADS
with the following code:
#include <string.h>
#include <stdlib.h>
int get_nested_num_threads(){
char delimiter[] = ",";
char *ptr = NULL;
char *num_threads = NULL;
num_threads = getenv("OMP_NUM_THREADS");
int threads=1, nested=0;
ptr = strtok(num_threads, delimiter);
while ( ptr != NULL ){
threads *= atoi(ptr);
ptr = strtok(NULL,delimiter);
nested += 1;
}
assert( nested <= 2 );
return threads;
}
Unfortunately if I call getenv("OMP_NUM_THREADS")
then I observe a nested parallelism of 4,4
instead of 4,64
. Which is really strange to me. Do you have an explanation for that?