5

I am using openMP for my lab assignments and everything worked fine few weeks ago, but now it runs only on one thread, I've read and I guess that this is due to conflict between packages. This is the code below:

#include "omp.h"
#include <stdio.h>
int main()
{
    omp_set_num_threads(4);
    #pragma omp parallal
    {
        int id = omp_get_thread_num();
        printf("Hello (%d)", id );
        printf("World (%d)\n", id);
        return 0;
    }
}

I get the output:

$ gcc -fopenmp hello.c
$ ./a.out
Hello (0)World (0)

I have tried reinstalling gcc, but to no help.

sourav c.
  • 46,120

1 Answers1

4

There are two errors in your code, that is why you are unable to use multiple threads.

First Error is a typo in OMP Compiler Directives. You have written

#pragma omp parallal

Correct one should be, (the word is parallel not parallal)

#pragma omp parallel

Second Error is an invalid return 0 inside OpenMP structured block. The correct code should look like,

#include "omp.h"
#include <stdio.h>
int main()
{
    omp_set_num_threads(4);
    #pragma omp parallel
    {
        int id = omp_get_thread_num();
        printf("Hello (%d)", id );
        printf("World (%d)\n", id );
    }
return 0;
}

Compilation:

$ gcc prog.c -fopenmp
$ ./a.out
Hello (0)World (0)
Hello (2)World (2)
Hello (1)World (1)
Hello (3)World (3)
sourav c.
  • 46,120