Search code examples
c++c++11openmp

Use OpenMP in C++11 to find the maximum of the calculated values


I am looking to find the maximum of the calculated values inside of a for loop and also store its corresponding index, max_calc_value and i_max here, below is my pseudo code. I was wondering if it is possible to do a certain kind of reduction here:

double max_calc_value = -DBL_MAX; // minimum double value
#pragma omp parallel for
for (int i = 20; i < 1000; i++) {
    this_value = my_slow_function(large_double_vector_array, param1*i, .., param5+i);
    if (this_value > max_calc_value){
        max_calc_value = this_value;
        i_max = i;
    }
}

Solution

  • The best way to handle it is to define a custom reduction operation as shown in Gilles' answer. If your compiler only supports OpenMP 3.1 or earlier (custom reduction operations were introduced in OpenMP 4.0), then the proper solution is to perform local reduction in each thread and then sequentially combine the local reductions:

    double max_calc_value = -DBL_MAX; // minimum double value
    int i_max = -1;
    #pragma omp parallel
    {
        int my_i_max = -1;
        double my_value = -DBL_MAX;
    
        #pragma omp for
        for (int i = 20; i < 1000; i++) {
            this_value = my_slow_function(large_double_vector_array, param1*i, .., param5+i);
            if (this_value > my_value){
                my_value = this_value;
                my_i_max = i;
            }
        }
    
        #pragma omp critical
        {
            if (my_value > max_calc_value) {
                max_calc_value = my_value;
                i_max = my_i_max;
            }
        }
    }
    

    This minimises the synchronisation overhead from the critical construct and in a simplified way shows how the reduction clause is actually implemented.