Search code examples
copenmp

OpenMP does not terminates


I've took an example of the simple OpenMP code.

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

int main(int argc, char** argv){
    int i;
    int thread_id;
    omp_set_num_threads(4);

    #pragma omp parallel
    {
        thread_id = omp_get_thread_num();

        for( int i = 0; i < omp_get_num_threads(); i++){
            if(i == omp_get_thread_num()){
                printf("Hello from process: %d\n", thread_id);
            }
            #pragma omp barrier
        }
    }
    return 0;
}

Compiled it with gcc -fopenmp omp_test_hello.c -o a.exe. I did not change or set any environment variables (I've decided that internal directives in code should work similarly). When I execute file I get the following output:

Hello from process: 0
Hello from process: 3
Hello from process: 3
Hello from process: 3

After that, the execution of the program does not stop, so it seems that something blocks it from termination.

I've tried even more simple example of the code without the barrier and for loop. It does not terminates similarly, though the output include "signals" from all threads:

int main(int argc, char** argv){
    int i;
    int thread_id;
    omp_set_num_threads(4);

    #pragma omp parallel
    {
        thread_id = omp_get_thread_num();
        printf("Hello from process: %d\n", thread_id);
    }
    return 0;
}

Output:

Hello from process: 2
Hello from process: 1
Hello from process: 0
Hello from process: 3

I've managed to test these examples also on Linux and get same results. So what could be the problem?


Solution

  • Problem solved by reinstalling mingw compiler.