How does OpenMP determine the Number of cores in the system?

2.8k views Asked by At

How are number of cores defined in OpenMP, how does this API know about the existence of cores.

1

There are 1 answers

2
Zulan On

OpenMP defines an Internal Control Variable (ICV) nthreads-var, which basically controls the number of threads of a parallel region.

The initial value of nthreads-var (if not defined by the environment Variable OMP_NUM_THREADS), is implementation defined. The number of cores might seem like a sensible default. GCCs (libgomp) implements it for BSD, Linux, mingw32, POSIX, rtems in gomp_init_num_threads. You can check out the specific implementations under libgomp/config/<platform>/proc.c.

Linux checks the thread affinity via pthread_getaffinity_np and sets the number of threads to the number of CPUs the process is allowed to run on. MinGW similarly via GetProcessAffinityMask. The remaining platforms basically use sysconf(_SC_NPROCESSORS_ONLN).