Search code examples
multithreadingparallel-processingopenmp

OpenMP task output


I don't understand the following question:

Specify four possible outputs of the code when it is compiled with -fopenmp and executed with two OpenMP threads:

int x = 0; 
#pragma omp parallel
{
    #pragma omp single
    {
        #pragma omp task
        {
            x++;
            printf("task 1: %d\n", x);
        }
        #pragma omp task
        {
            x++;
            printf("task 2: %d\n", x);
        }
    }
}

What "special" impact would the -fopenmp have on this?

Are these possible outputs:

  1. task 1: 1, task 2: 2
  2. task 2: 1, task 1: 2

What about other possible outputs?


Solution

  • The two possibilities that you showed:

    1. task 1: 1

      task 2: 2

    2. task 2: 1

      task 1: 2

    And

    When the x++ execute first in both tasks, and print the first task then the second:

    1. task 1: 2

      task 2: 2

    When the x++ execute first in both tasks, and print the second task then the first:

    1. task 2: 2

      task 1: 2