Discussion:
[OMPI users] Redefining MPI_BOOL depending of sizeof(bool)
Florian Lindner
2018-03-29 07:58:54 UTC
Permalink
Hello,

in a code that I am currently reading I have found that code:


template <size_t>
struct MPI_Select_unsigned_integer_datatype;

template <>
struct MPI_Select_unsigned_integer_datatype<1> {
static MPI_Datatype datatype;
};
MPI_Datatype MPI_Select_unsigned_integer_datatype<1>::datatype = MPI_UNSIGNED_CHAR;

template <>
struct MPI_Select_unsigned_integer_datatype<2> {
static MPI_Datatype datatype;
};
MPI_Datatype MPI_Select_unsigned_integer_datatype<2>::datatype = MPI_UNSIGNED_SHORT;

template <>
struct MPI_Select_unsigned_integer_datatype<4> {
static MPI_Datatype datatype;
};
MPI_Datatype MPI_Select_unsigned_integer_datatype<4>::datatype = MPI_UNSIGNED;

template <>
struct MPI_Select_unsigned_integer_datatype<8> {
static MPI_Datatype datatype;
};
MPI_Datatype MPI_Select_unsigned_integer_datatype<8>::datatype = MPI_UNSIGNED_LONG;

#define MPI_BOOL MPI_Select_unsigned_integer_datatype<sizeof(bool)>::datatype


It redefines MPI_BOOL based on the size of bool. I wonder if this is needed and why?

I was speculating that the compiler could pack multiple bools in one word, when used as a array. But the code above is a
compile time specialization and won't help there.

Best Thanks,
Florian
Florian Lindner
2018-03-29 09:18:19 UTC
Permalink
Post by Florian Lindner
#define MPI_BOOL MPI_Select_unsigned_integer_datatype<sizeof(bool)>::datatype
It redefines MPI_BOOL based on the size of bool. I wonder if this is needed and why?
I was speculating that the compiler could pack multiple bools in one word, when used as a array. But the code above is a
compile time specialization and won't help there.
Ok, I just discovered that MPI has no MPI_BOOL type. According to the standard, there is a MPI_CXX_BOOL and MPI_C_BOOL
(pp. 26).

Since I use C++ I tend to use MPI_CXX_BOOL, which also seems to be present when no CXX bindings are compiled.

Best,
Florian
Jeff Squyres (jsquyres)
2018-03-29 11:00:28 UTC
Permalink
Post by Florian Lindner
#define MPI_BOOL MPI_Select_unsigned_integer_datatype<sizeof(bool)>::datatype
It redefines MPI_BOOL based on the size of bool. I wonder if this is needed and why?
I was speculating that the compiler could pack multiple bools in one word, when used as a array. But the code above is a
compile time specialization and won't help there.
Ok, I just discovered that MPI has no MPI_BOOL type. According to the standard, there is a MPI_CXX_BOOL and MPI_C_BOOL (pp. 26).
I'm not sure why that software is defining MPI_BOOL -- that's bad form. MPI reserves the "namespace" prefix of MPI_ -- middleware and applications should not be defining new symbols with an MPI_ prefix (to prevent exactly this kind of confusion).
--
Jeff Squyres
***@cisco.com
Loading...