23

I have the following C/C++ code using OpenMP:

    int nProcessors=omp_get_max_threads();
    if(argv[4]!=NULL){
        printf("argv[4]: %s\n",argv[4]);
        nProcessors=atoi(argv[4]);
        printf("nProcessors: %d\n",nProcessors);
    }
    omp_set_num_threads(nProcessors);
    printf("omp_get_num_threads(): %d\n",omp_get_num_threads());
    exit(0);

As you can see, I'm trying to set the number of processors to use based on an argument passed on the command line.

However, I'm getting the following output:

argv[4]: 2   //OK
nProcessors: 2   //OK
omp_get_num_threads(): 1   //WTF?!

Why isn't omp_get_num_threads() returning 2?!!!


As has been pointed out, I'm calling omp_get_num_threads() in a serial region, hence the function returns 1.

However, I have the following parallel code:

#pragma omp parallel for private(i,j,tid,_hash) firstprivate(firstTime) reduction(+:nChunksDetected)
    for(i=0;i<fileLen-CHUNKSIZE;i++){
        tid=omp_get_thread_num();
        printf("%d\n",tid);
        int nThreads=omp_get_num_threads();
        printf("%d\n",nThreads);
...

which outputs:

0   //tid
1   //nThreads - this should be 2!
0
1
0
1
0
1
...
Eamorr
  • 9,872
  • 34
  • 125
  • 209

3 Answers3

33

The omp_get_num_threads() call returns 1 in the serial section of the code. See Link

So you need to have parallel code to get the correct value, here how your code should look like:

#include <iostream>
#include <omp.h>

int main (int argc, const char * argv[])
{
    int nProcessors = omp_get_max_threads();

    std::cout<<nProcessors<<std::endl;

    omp_set_num_threads(nProcessors);

    std::cout<<omp_get_num_threads()<<std::endl;

#pragma omp parallel for 
    for(int i = 0; i < 5; i++){
        int tid = omp_get_thread_num();
        std::cout<<tid<<"\t tid"<<std::endl;
        int nThreads = omp_get_num_threads();
        std::cout<<nThreads<<"\t nThreads"<<std::endl;
    }

    exit(0);
}

This code produces:

2

1
0    tid
2    nThreads
0    tid
2    nThreads
0    tid
2    nThreads
1    tid
2    nThreads
1    tid
2    nThreads

It seems that you have either open mp not enabled or your loop is not in the form that can be parallized by openmp

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
tune2fs
  • 7,605
  • 5
  • 41
  • 57
  • OK, but when I go into a parallel region, it's still returning 1... Please see my edit. Many thanks... – Eamorr Jan 23 '12 at 10:03
  • Where are you starting your parallel region, i do not see it in the edit. – tune2fs Jan 23 '12 at 10:05
  • Thanks, do you change the variable i or fileLen inside your parallel region? As it might be that your for loop cannot be parallised by openmp, due side effects. – tune2fs Jan 23 '12 at 10:09
  • No - don't change those variables – Eamorr Jan 23 '12 at 10:11
  • 2
    I have edited my code above and it works for me, please show some minimal running example where your code produces this error. – tune2fs Jan 23 '12 at 10:21
  • 2
    Hey, thanks for that. It turns out that I needed to pass -fopenmp to LIBS and CXXFLAGS in my Makefile... ;( – Eamorr Jan 23 '12 at 10:36
13

you are using the wrong function. use omp_get_max_threads to check for the maximum number of allowed threads.

Bort
  • 2,423
  • 14
  • 22
3

It has been already pointed out that omp_get_num_threads() returns 1 in sequential sections of the code. Accordingly, even if setting, by omp_set_num_threads(), an overall number of threads larger than 1, any call to omp_get_num_threads() will return 1, unless we are in a parallel section. The example below tries to clarify this point

#include <stdio.h>

#include <omp.h>

int main() {

    const int maxNumThreads = omp_get_max_threads();

    printf("Maximum number of threads for this machine: %i\n", maxNumThreads);

    printf("Not yet started a parallel Section: the number of threads is %i\n", omp_get_num_threads());

    printf("Setting the maximum number of threads...\n");
    omp_set_num_threads(maxNumThreads);

    printf("Once again, not yet started a parallel Section: the number of threads is still %i\n", omp_get_num_threads());

    printf("Starting a parallel Section...\n");

#pragma omp parallel for 
    for (int i = 0; i < maxNumThreads; i++) {
        int tid = omp_get_thread_num();
        printf("This is thread %i announcing that the number of launched threads is %i\n", tid, omp_get_num_threads());
    }

}
Vitality
  • 20,705
  • 4
  • 108
  • 146