OpenMP behavior defining processor and thread

I start with OpenMP, I just compiled with gcc -fopenmp openmp_c_helloworld.c following code snippet:

 #include <omp.h> #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { int th_id, nthreads; #pragma omp parallel private(th_id) { th_id = omp_get_thread_num(); printf("Hello World from thread %d\n", th_id); #pragma omp barrier if ( th_id == 0 ) { nthreads = omp_get_num_threads(); printf("There are %d threads\n",nthreads); } } return EXIT_SUCCESS; } 

I just run the executable on an Intel quad-core processor with HyperThreading, and I get the following output:

 Hello World from thread 2 Hello World from thread 0 Hello World from thread 3 Hello World from thread 1 There are 4 threads 

Technically speaking, I have 8 threads available on my processor and 4 processor cores, why does OpenMP show me only 4 threads?

+4
source share
1 answer

Simply put, I think because OpenMP is looking for the number of processors (cores), not the number of processor threads. See this page : `

The default implementation is usually the number of processors per node, although it can be dynamic (see the next token).

Something you could try is to set the number of threads in your program equal to the number of processor threads and see if there is a performance improvement (you will need to create your own benchmarking program). In parallel programming, good performance is achieved when the number of worker threads is equal to the number of processor threads. You can save a stream or two extra for I / O.

+1
source

Source: https://habr.com/ru/post/1384451/


All Articles