Search code examples
cmpimpich

Avoid assumed extents for structures containing float types?


I see the following warning message when configuring MPICH (a popular MPI implementation) to use GNU (4.8) compilers on a GNU/Linux x86_64 system:

checking for max C struct floating point alignment with long doubles... sixteen

configure: WARNING: Structures containing long doubles may be aligned differently from structures with floats or longs. MPICH does not handle this case automatically and you should avoid assumed extents for structures containing float types.

Can you give a (C language) example that illustrates what I should avoid?

(Pointers to relevant documentation also appreciated.)


Solution

  • The MPICH configure scripts are checking how the compiler aligns both members of a struct, and the struct itself.

    struct { char a; float b; } char_float;
    struct { float b; char a; } float_char;
    struct { char a; double b; } char_double;
    struct { double b; char a; } double_char;
    

    All four of those might have different alignment or padding. I think you already know this bit, but it's covered in better detail at this question: Structure padding and packing

    To your question about what you should avoid:

    struct { char a; long double b; } char_long_double;
    struct { long double b; char a; } long_double_char;
    struct { long double a; int b; char c; } long_double_int_char;
    

    These are structs with a 'long double'. The configure script detected that the way your compiler deals with these structs differs from the way the compiler deals with structs containing 'float' or 'double' types. If you wish to use MPICH's MPI_Type_create_struct to describe something with 'long double' types, it might provide unexpected behavior.

    When this code was written about 8 years ago, long doubles were pretty rarely used, and when they were, they behaved like doubles. If you are using long doubles, send a note -- or even better a test case! -- to [email protected] and the MPICH guys will investigate if it's still a problem.