Portable way of accessing an array of bool (C++11) from Nvidia PTX

396 Views Asked by At

I need to do GPU computations on an boolean array bool[] (note, not a std::vector<bool>) which was created in CPU memory (with C++11 code) and then copied to the GPU via cuMemCpy or similar.

First question:

sizeof(bool) reports 1 byte. Is this guaranteed by the C++11 standard?

Second question:

Is true (false) always represented as 1 (0) (in the unsigned char representation) or does the compiler have freedom here ? (It could use any non-zero integer less than 256 if it wanted)

Third question (PTX specific):

In PTX logical operations or, xor, etc. only operate on types larger than 8 bit. That is I can do logical operations on an unsigned int with or.u32 <out>,<in1>,<in2>. However since C++11 bool type seems to be 8 bits does this mean I can not operate on an array of bools that was copied directly from CPU to GPU memory and thus do I need to convert the array of bools first into some type PTX logical operations can operate on, i.e. u32, u16, etc.?

1

There are 1 best solutions below

0
On

First answer:

No, this is not guaranteed. See [expr.sizeof]/1, and the associated footnote:

... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined75. ...

75) sizeof(bool) is not required to be 1.


Second Answer:

I'm pretty sure that the value representation for bool objects is implementation defined, but I can't find anything explicitly stating that. The closest that I can get is [basic.types]/4:

... For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.


Third Answer:

I don't know, but from your description, it certainly looks like you would have to change the types.