Vaishali Aggarwal
Vaishali Aggarwal

Reputation: 27

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

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

Upvotes: 1

Views: 2989

Answers (1)

Zulan
Zulan

Reputation: 22670

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).

Upvotes: 4

Related Questions