I got omp_get_num_threads always return 1 in gcc (works in icc)

13,044

Solution 1

You almost certainly forgot to use the -fopenmp flag when you were compiling.

Solution 2

I've never seen omp_get_num_threads() work with gcc.
I use my own routine to compute the number of running threads :

#include <omp.h>
#include <stdio.h>

int omp_thread_count() {
    int n = 0;
    #pragma omp parallel reduction(+:n)
    n += 1;
    return n;
}

int main() {
    printf("%d, %d\n",omp_thread_count(),omp_get_num_threads());
    return 0;
}

Solution 3

As pointed out, omp_get_num_threads() always returns 1 in sequential code sections. Here's the simplest way that I came up with to get around that limitation:

#include <omp.h>
#include <stdio.h>

int main(int argc, char **argv)
{
  #pragma omp parallel
  {
    #pragma omp single
    printf("num_threads = %d\n", omp_get_num_threads());
  }
  return 0;
}

Compile with -fopenmp and run. The output should be:

$ gcc -fopenmp test.c
$ ./a.out
num_threads = 4

Of course, 4 is just what my computer shows by default; on your computer it will print the default number of threads (the number of cores, or the value of the environment variable OMP_NUM_THREADS if it is set).

I tested this solution and found it to work with gcc 4.8.5 on Linux, icc 18.0.1 on Linux, gcc 6.4.0 on macOS, and clang 3.9.1 on macOS.

Solution 4

In a sequential section of the program omp_get_num_threads returns 1. (https://gcc.gnu.org/onlinedocs/libgomp/omp_005fget_005fnum_005fthreads.html)

It is only in the parallel section that omp_get_num_threads would return a value other than 1.

I have tried gcc-4.9.1. Still, omp_get_num_threads() returns 1.

The recognition of the number of threads differ between icc and gcc.

Using icc-10, I can use maxNumCompThreads(2) to specify the number of threads.

Share:
13,044
ios learner
Author by

ios learner

Updated on June 09, 2022

Comments

  • ios learner
    ios learner about 2 years

    I have this old question but no answer works for me from online, the code is:

    #include "stdio.h"
    #include "omp.h"
    
    main ()
    {
        omp_set_num_threads(4); //initialise thread count for 4 core cpu                                                                                                                             
        int j;
        printf ("%d\n", omp_get_max_threads());
        printf ("%d\n", omp_get_num_threads());
    #pragma omp parallel for
        for (int j=0; j<10; ++j)
            {
                printf ("%d\n", omp_get_num_threads());
                int threadNum;
                threadNum = omp_get_thread_num();
                printf("This is thread %d\n", threadNum);
            }
        }
        return 0;
    }
    

    In G++ 4.4.5, linux 2.6.32-5-amd64, it produces:

    4
    1
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    1
    This is thread 0
    

    If we shift to ICC 12.1.0, it gives me:

    4
    1
    4
    This is thread 0
    4
    This is thread 0
    4  
    This is thread 0
    4
    This is thread 1
    4
    This is thread 1
    4
    This is thread 1
    4
    This is thread 2
    4 
    This is thread 2
    4
    This is thread 3
    4
    This is thread 3
    

    Any ideas?

  • Jay Shin
    Jay Shin over 6 years
    I didn't forget that flag, and yet it still is 1 for me.