Saturday, 15 June 2013

OpenMP min max reduction code

Posted by Mahesh Doijade

OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP min reduction, OpenMP max reduction, OpenMP max reduction, OpenMP max reduction, OpenMP max reduction, OpenMP max reduction, OpenMP max reduction, OpenMP max reduction, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp, openmp,

       One can use reduction clause in OpenMP in order to carry out various operations like addition, multiplication, logical OR, logical AND operations to name some.
      
       reduction(operation:var)
 where,
operation
The operator for the operation to perform on the variables (var) at the end of the parallel region.
var
One more more variables on which to perform scalar reduction. If more than one variable is specified, separate variable names with a comma.

       This clause specifies that one or more variables that are private to each thread are the subject of a reduction operation at the end of the parallel region. But one of the desirable reduction operation is finding maximum or minimum element  from a given list in parallel fashion, this feature was missing in OpenMP previously for a long while, in order to achieve this functionality one had to use critical sections, locks etc., which incurred lot of performance penalties resulting into worst performance in several cases then sequential version. But OpenMP 3.1 specification has come to the rescue giving this crucial functionality that too in performance efficient manner and obviously with minimal burden on the programmer.     
       OpenMP 3.1 added predefined min and max reduction operators for C and C++, and extensions to the atomic construct that allow the value of the shared variable that the construct updates to be captured or written without being read. OpenMP 3.1 is available in GCC 4.7 and further versions. In case you don't have,  you can download it from here.
Following is the sample code which illustrates max operator usage in OpenMP :


#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", omp_get_thread_num(), i);
        if(arr[i] > max_val)
        {
            max_val = arr[i];  
        }
    }
  
    printf("\nmax_val = %f", max_val);
}

      Similarly, one can use this for finding min by replacing max operator with min and changing the comparison in if condition in the for loop.




8 comments:

  1. Searched a lot for this issue but got good working solution from this article thank you.....

    ReplyDelete
  2. Nice post dude.
    continue your work.

    ReplyDelete
  3. thanks a lot for the code and explanation
    I am very new to this and would like to ask what this line "arr[i] = 2.0 + i;" means...
    Many thanks

    ReplyDelete
  4. Thanks a lot for the code.
    I am very new to this and would like to ask what this line "arr[i] = 2.0 + i;" means...
    Many thanks...

    ReplyDelete
    Replies
    1. Thanks Mykhael,
      "arr[i] = 2.0 + i" this line is for adding some dummy data into the array "arr", one can change this to suit his use case.

      Delete
    2. Thanks for the response. I hope you won't mind explaining what you mean by dummy date. Please
      Many thAnks Doijade.

      Delete