[mpich-discuss] MPI user-defined datatype size
George Bosilca
bosilca at icl.utk.edu
Mon Dec 4 09:25:16 CST 2023
MPI does not know about the padding because it does not really create a
struct in the same sense as the compiler. As a result, you need to resize
the datatype to account for the padding. Check the examples 5.16 and 5.17
in the MPI standard to see how to use it.
For your particular example, replacing the last 2 lines of code with:
MPI_Type_create_struct(struct_len, block_len, displacements, types,
&tmp_ddt);
MPI_Type_create_resized(tmp_ddt, 0, sizeof(), &mpi_data_type);
MPI_Type_connit(mpi_data_type);
should make the MPI struct match the compiler struct.
While you can manipulate the pointers directly and compute the
displacements by hand, you should not use a size_t for that. MPI_Aint is a
signed type, ssize_t or some form of ptrdiff_t. Instead you should use the
MPI suggested way via MPI_Get_address (to obtain the address of an object
as an MPI_Aint), and then MPI_Aint_diff to compute relative displacements.
George.
On Sun, Dec 3, 2023 at 3:55 PM Mccall, Kurt E. (MSFC-EV41) via discuss <
discuss at mpich.org> wrote:
> 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);
>
>
>
>
>
>
>
>
> _______________________________________________
> discuss mailing list discuss at mpich.org
> To manage subscription options or unsubscribe:
> https://lists.mpich.org/mailman/listinfo/discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mpich.org/pipermail/discuss/attachments/20231204/b411ee13/attachment-0001.html>
More information about the discuss
mailing list