[mpich-discuss] MPI user-defined datatype size

Zhou, Hui zhouh at anl.gov
Mon Dec 4 12:09:56 CST 2023


Hi Kurt,

MPI_Type_size returns the size of a datatype excluding the gaps (hole). You should use MPI_Type_get_extent to query the extent of the datatype, which should come out as 568.

Alternatively, if you never going to break the struct apart during communication, you may try simply treating the struct as an opaque blob, i.e. use MPI_Type_contiguous(sizeof(struct MsgTupleSeq), MPI_BYTE, &newtype)​.  It is likely to perform better by avoiding treating it as a non-contiguous datatype.

--
Hui
________________________________
From: Mccall, Kurt E. (MSFC-EV41) via discuss <discuss at mpich.org>
Sent: Sunday, December 3, 2023 2:55 PM
To: discuss at mpich.org <discuss at mpich.org>
Cc: Mccall, Kurt E. (MSFC-EV41) <kurt.e.mccall at nasa.gov>
Subject: [mpich-discuss] MPI user-defined datatype size


Hi,



I’m running MPICH 4.1.2.



Should a MPI user-defined datatype created by MPI_Type_create_struct have the same size as the C++ object on which it is based?   Comparing sizeof(cpp_obj) and the output

From MPI_Type_size(mpi_data_type_, &size1), the C++ object is four bytes larger than the MPI datatype.   I don’t know if this is because I have to do something special to take

padding into  account when I create the MPI user datatype.



Here is my C++ message class:



struct MsgTupleSeq

{

    static const int N_INDICES_MAX_ = 4;   // culprit?



    int n_vars_;

    int n_ndxs_;

   short start_[N_INDICES_MAX_];

    short end_[N_INDICES_MAX_];

    short current_[N_INDICES_MAX_];

    long long n_tuples_;

    int unique_id_;

    int subordinate_id_;

    int opcode_;

    char error_msg_[512];

}



That is 564 bytes for the non-static members, but such an object is 568 bytes long with the 4 bytes of padding that the compiler inserts.



I define the MPI user datatype with the code below, using the MsgTupleSeq object to compute the offsets.  MPI_Type_size returns 564 bytes for the user datatype,

so it seems like it isn’t seeing the padding.



    MsgTupleSeq obj;

   MPI_Datatype mpi_data_type;

    int struct_len = 10, i = 0;



    int block_len[struct_len];

    MPI_Datatype types[struct_len];

    MPI_Aint displacements[struct_len];



    // the integer "n_vars_" member

    block_len[i] = 1;

    types[i] = MPI_INT;

    displacements[i] = (size_t) &obj.n_vars_ - (size_t) &obj;



    // the integer "n_ndxs_" member

    ++i;

    block_len[i] = 1;

    types[i] = MPI_INT;

    displacements[i] = (size_t) &obj.n_ndxs_ - (size_t) &obj;



    // the short integer "start_" array member

    ++i;

    block_len[i] = N_INDICES_MAX_;

    types[i] = MPI_SHORT;

    displacements[i] = (size_t) &obj.start_ - (size_t) &obj;



    // the short integer "end_" array member

    ++i;

    block_len[i] = N_INDICES_MAX_;

    types[i] = MPI_SHORT;

    displacements[i] = (size_t) &obj.end_ - (size_t) &obj;



    // the short integer "current_" array member

    ++i;

    block_len[i] = N_INDICES_MAX_;

    types[i] = MPI_SHORT;

    displacements[i] = (size_t) &obj.current_ - (size_t) &obj;



    // the long long integer "n_tuples_" member

    ++i;

    block_len[i] = 1;

    types[i] = MPI_LONG_LONG;

    displacements[i] = (size_t) &obj.n_tuples_ - (size_t) &obj;



    // the integer "unique_id_" member

    ++i;

    block_len[i] = 1;

    types[i] = MPI_INT;

    displacements[i] = (size_t) &obj.unique_id_ - (size_t) &obj;



   // the integer "subordinate_id_" member

    ++i;

    block_len[i] = 1;

    types[i] = MPI_INT;

    displacements[i] = (size_t) &obj.subordinate_id_ - (size_t) &obj;



    // the integer "opcode_" member

    ++i;

    block_len[i] = 1;

    types[i] = MPI_INT;

    displacements[i] = (size_t) &obj.opcode_  - (size_t) &obj;



    // the char array "error_msg_" member

    ++i;

    block_len[i] = 512;

    types[i] = MPI_CHAR;

    displacements[i] = (size_t) &obj.error_msg_[0] - (size_t) &obj;



    MPI_Type_create_struct(struct_len, block_len, displacements, types, &mpi_data_type);

    MPI_Type_commit(&mpi_data_type);








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mpich.org/pipermail/discuss/attachments/20231204/66d57a3d/attachment.html>


More information about the discuss mailing list