C/C++ min/max reduction with _Bool/bool

Discuss the OpenMP 3.1 API Specifications with the OpenMP Arch. Review Board. (Read Only)

C/C++ min/max reduction with _Bool/bool

Postby nathanweeks » Fri Sep 14, 2012 4:55 am

The OpenMP 3.1 API spec states on p. 104, l. 1, that the private copy of a list item in a reduction clause is initialized as follows for min and max operators:
Code: Select all
max    Least representable value in the reduction list item type
min    Largest representable value in the reduction list item type

How should the terms "least representable value" and "largest representable value" be interpreted when it comes to the C _Bool data type (and C++ bool, for that matter)? The final C11 draft (N1570) states the following on p. 51:
When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.

Based on this, I think it would be least surprising for the programmer if the "least representable value" for _Bool is 0, and the "largest representable value" is 1, despite the underlying representation taking at least a byte of storage. There doesn't seem to be a consensus among several compilers I've checked; e.g., consider the following program:
Code: Select all
#include <stdio.h>

int main(void) {
    _Bool min_val = 1, max_val = 0;

#pragma omp parallel reduction(min:min_val) reduction(max:max_val) \
        num_threads(1)
    {
        printf("min_val: %i\n", min_val);
        printf("max_val: %i\n", max_val);
    }
}


Output using compiler A:
Code: Select all
min_val: 1
max_val: 0


Output using compiler B:
Code: Select all
min_val: 255
max_val: 0


Output using compiler C:
Code: Select all
min_val: 127
max_val: -128
--
Nathan Weeks
Iowa State University HPC Group
http://weeks.public.iastate.edu/
nathanweeks
 
Posts: 41
Joined: Sun May 17, 2009 6:19 am
Location: Iowa State University

Re: C/C++ min/max reduction with _Bool/bool

Postby MarkB » Fri Sep 14, 2012 7:01 am

Hi Nathan,

I would say that compilers B and C are broken, since printing the value of a _Bool should always result in 0 or 1, and no changes to the OpenMP specification are required: is that your take on this too?

Mark.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: C/C++ min/max reduction with _Bool/bool

Postby nathanweeks » Fri Sep 14, 2012 8:53 am

Arguments to printf() are subject to default argument promotions, which, for a type _Bool argument, results in an integer promotion; specifically, it will be converted to an int before being printed---the C11 standard states on pp. 50-51:
  • The rank of _Bool shall be less than the rank of all other standard integer types.
...
The following may be used in an expression wherever an int or unsigned int may be used:
  • An object or expression with an integer type (other than int or unsigned int)
    whose integer conversion rank is less than or equal to the rank of int and
    unsigned int.
...
If an int can represent all values of the original type (as restricted by the width, for a
bit-field), the value is converted to an int; otherwise, it is converted to an unsigned
int
. These are called the integer promotions.

While I haven't explicitly verified it, I'd wager these compilers implement _Bool as an integer type with a width of 8 bits, so I wouldn't be surprised if printf("%i\n", x) resulted in values other than 0 or 1 for an uninitialized variable x of type _Bool, but it is surprising to me that a _Bool would be initialized by the OpenMP runtime library to a value other than 0 or 1.
--
Nathan Weeks
Iowa State University HPC Group
http://weeks.public.iastate.edu/
nathanweeks
 
Posts: 41
Joined: Sun May 17, 2009 6:19 am
Location: Iowa State University


Return to OpenMP 3.1 API Specifications

Who is online

Users browsing this forum: No registered users and 0 guests