Jiseong
Jiseong

Reputation: 89

Strange phenomenon about the number of threads

My CPU has 2 physical cores, and it supports hyperthreading. Thus, I think I can manage 4 threads. (4 logical cores) However, using openmp, I can generate much more cores. It is strange.

I'm a newbie in parallel programming, and I learned that there are few ways to set the number of threads I wanna use. I've used two ways below.

  1. omp_set_num_threads(4);
  2. #pragma omp prarallel num_threads(4);
#pragma omp parallel num_threads(100)
{
    printf("%d\n", omp_get_thread_num());
#pragma omp for
    /* code I wanna parallelize */
} 

What I think strange is, the logical core number is 4 in my CPU, but why omp_get_thread_num() returned 0 to 99. I think it is impossible, what is the meaning of 100 in #pragma omp parallel num_threads(100)?

Upvotes: 0

Views: 120

Answers (1)

Michael Chourdakis
Michael Chourdakis

Reputation: 11158

You can have more threads than your cores. Which thread is run to which CPU core is up to the OS (In Windows, you can set the affinity).The OS will run your threads based on its scheduler.

The number of cores is effectively the number of threads that can run simultaneously.

If you have two threads that each one takes 1 second and you only have 1 core, then they will finish in approximately 2 seconds, whether if you have 2 cores (and they run in parallel) , they will finish in approx 1 second.

Upvotes: 2

Related Questions