C/C++ min/max reduction with real floating type

Discuss the OpenMP 3.1 API Specifications with the OpenMP Arch. Review Board. (Read Only)
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help: https://stackoverflow.com/questions/tagged/openmp
Locked
nathanweeks
Posts: 41
Joined: Sun May 17, 2009 6:19 am
Location: Iowa State University
Contact:

C/C++ min/max reduction with real floating type

Post by nathanweeks »

For C/C++ min/max reduction operators with list items of type float, double, or long double, what should the initial values of the private copies be? E.g., for a min reduction, the initial value of the private copy should be the "largest representable value in the reduction list item type" (OpenMP 3.1, p. 104, l. 1). For a list item of type float, would that be FLT_MAX, or infinity (assuming the C/C++ implementation supports infinities)?

Consider the following program:

Code: Select all

#include <float.h>
#include <math.h>
#include <stdio.h>

int main(void) {
    float min_val = 0.0f;

#pragma omp parallel reduction(min:min_val) num_threads(1)
    if (isinf(min_val))
        printf("initial min_val: inf\n");
    else if (min_val == FLT_MAX)
        printf("initial min_val: FLT_MAX\n");
}
Out of four compilers I tested, two resulted in an output of "FLT_MAX", while the other two resulted in an output of "inf".

I think that infinity makes more sense, but it isn't clear to me whether or not the C11 standard considers infinity a "representable value" for real floating types.

If FLT_MAX is the correct answer, it would be beneficial to change the wording in the OpenMP spec on p. 104, l. 1 to:
max Least representable finite value in the reduction list item type
min Largest representable finite value in the reduction list item type
If infinity is the correct answer for C/C++ implementations supporting infinities, it would be helpful to make that explicit in the OpenMP spec.
--
Nathan Weeks
Iowa State University HPC Group
http://weeks.public.iastate.edu/

jakub
Posts: 74
Joined: Fri Oct 26, 2007 3:19 am

Re: C/C++ min/max reduction with real floating type

Post by jakub »

Note that in GCC the value depends on compiler switches, Inf by default and FLT_MAX with -ffinite-math-only (included in -ffast-math or -Ofast), because when finite math only is requested, the compiler better shouldn't generate infinities.

Locked