Search code examples
cmultithreadingparallel-processingopenmp

Didn't understand the output from parallel reduction using max operation OpenMP program?


I was reading about parallel programming and came across this basic code. This is the program from parallel reduction using max operation. What exactly we are expecting from this operation?

 
 #include <stdio.h>

 #include <omp.h>

 int main() {
   double arr[10];
   omp_set_num_threads(4);
   double max_val = 0.0;
   int i;
   for (i = 0; i < 10; i++)
     arr[i] = 2.0 + i;
   #pragma omp parallel
   for reduction(max: max_val)
   for (i = 0; i < 10; i++) {
     printf("thread id = %d  and i = %d \n", omp_get_thread_num(), i);
     if (arr[i] > max_val) {
       max_val = arr[i];
     }
   }
   printf("\nmax_val = %f", max_val);
 }
  

and this is the output

thread id = 2  and i = 6
thread id = 2  and i = 7
thread id = 1  and i = 3
thread id = 1  and i = 4
thread id = 1  and i = 5
thread id = 3  and i = 8
thread id = 3  and i = 9
thread id = 0  and i = 0
thread id = 0  and i = 1
thread id = 0  and i = 2

max_val = 11.000000

I am new to openmp. Please help me to understand this code. I am not getting this output. How this result came?


Solution

  • Your code fills up the arr:

       int i;
       for (i = 0; i < 10; i++)
         arr[i] = 2.0 + i;
    

    with values from 2 to 11:

    then in the parallel loop:

       #pragma omp parallel for reduction(max: max_val)
       for (i = 0; i < 10; i++) {
         printf("thread id = %d  and i = %d \n", omp_get_thread_num(), i);
         if (arr[i] > max_val) {
           max_val = arr[i];
         }
       }
    

    When OpenMP reads #pragma omp parallel for OpenMP will divide the iterations of the loop among a team to threads, in your case 4 threads:

     omp_set_num_threads(4);
    

    A more detailed answer about #pragma omp parallel and #pragma omp parallel for can be found here.

    With reduction(max: max_val) OpenMP will create a copy of the variable max_val per thread and after the parallel region the original variable max_val will be equals to the biggest max_val among all threads. Which in your case is 11.0.

    A more detailed answer about the reduction clause can be found here.

    The function omp_get_thread_num() returns back the ID of the thread that have invoked that method.