user2817012
user2817012

Reputation: 261

Issues with number of threads with openMP in C

The code:

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

int main(int argc, char** argv){

    omp_set_dynamic(0);
    omp_set_num_threads(4);

    #pragma omp paralell
    {
        printf("%d\n", omp_get_thread_num());
    }

}

The output:

0

Shouldn't the output be some permutation of 0, 1, 2, and 3?

Upvotes: 1

Views: 101

Answers (3)

kangshiyin
kangshiyin

Reputation: 9781

And don't forget the enable OpenMP support of the compiler, which is disabled by default for mainstream compilers like gcc/icc/vc++

Upvotes: 0

PhillipD
PhillipD

Reputation: 1817

If you copy & pasted your source code, I think its because "parallel" is spelled wrong. I just found out that gcc silently ignores mispelling for openmp pragmas if the -W flag is not set. Compiling with -Wall gives

warning: ignoring #pragma omp paralell [-Wunknown-pragmas]
#pragma omp paralell

It is therefore a good idea to let gcc print warnings.

Upvotes: 1

Igor Popov
Igor Popov

Reputation: 2620

Writing omp_set_dynamic(0); you indicate that the runtime will not dynamically adjust the number of threads. The argument of this function should be nonzero to avail the dynamic adjustment of num. of threads. Also you misspelled parallel in the code.

Upvotes: 5

Related Questions