OAOzden Akinci
MPI lecture notes / Chapter 4 of 7

Derived datatypes

Describe non-contiguous data such as matrix columns and C structs to MPI, so it can be sent without copying it into a buffer first.

The problem

Basic datatypes such as MPI_DOUBLE describe contiguous arrays. Real data is often not contiguous: a column of a row-major matrix, the boundary face of a 3D block, or the fields of a struct. You could copy such data into a temporary buffer, send it, and copy it out on the other side. Derived datatypes let you describe the layout to MPI once instead, and the library handles packing, often more efficiently than a hand-written loop, and sometimes with no copy at all.

Building a type in three steps

  1. Construct the type with one of the constructors.
  2. Commit it with MPI_Type_commit. A type must be committed before it is used in communication.
  3. Free it with MPI_Type_free when you no longer need it.

The constructors you will use most

Constructor Describes
MPI_Type_contiguous(count, old, &new) count consecutive elements, a convenience for treating them as one unit
MPI_Type_vector(count, blocklen, stride, old, &new) count blocks of blocklen elements, each stride elements apart: columns, strided faces
MPI_Type_create_subarray(...) a rectangular sub-block of an n-dimensional array, such as a halo layer or a local tile
MPI_Type_indexed(...) blocks of different lengths at arbitrary displacements
MPI_Type_create_struct(...) a C struct with members of different types

Example: sending a matrix column

In C, double a[4][6] is stored row by row. Column 2 consists of four elements, each six doubles after the previous one:

MPI_Datatype column;
MPI_Type_vector(ROWS, 1, COLS, MPI_DOUBLE, &column);   /* count, blocklength, stride */
MPI_Type_commit(&column);

/* rank 0: send column 2, starting at a[0][2] */
MPI_Send(&a[0][2], 1, column, 1, 0, MPI_COMM_WORLD);

/* rank 1: receive into a plain contiguous array */
MPI_Recv(col, ROWS, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
rank 1 received column 2: 2 12 22 32

The matrix holds 10*row + col, so the values confirm that exactly column 2 arrived. Notice that the two sides use different type descriptions: one column on the sender, four doubles on the receiver. That is allowed. What must match is the type signature, the sequence of basic types (four doubles in both cases), not the layout in memory.

In Fortran, arrays are stored column by column, so there it is the rows that are non-contiguous and need a vector type.

Structs

To send an array of structs, describe the struct’s members and their offsets. Use offsetof from <stddef.h> rather than computing offsets by hand, because the compiler may insert padding:

typedef struct { int id; double pos[3]; } particle_t;

int          blocklens[2] = {1, 3};
MPI_Aint     disps[2]     = {offsetof(particle_t, id), offsetof(particle_t, pos)};
MPI_Datatype types[2]     = {MPI_INT, MPI_DOUBLE};
MPI_Datatype tmp, particle_type;

MPI_Type_create_struct(2, blocklens, disps, types, &tmp);
MPI_Type_create_resized(tmp, 0, sizeof(particle_t), &particle_type);  /* include trailing padding */
MPI_Type_commit(&particle_type);
MPI_Type_free(&tmp);

The MPI_Type_create_resized step sets the type’s extent to the real size of the struct, so that sending several particles in a row steps through the array correctly.

Where derived types pay off

  • Halo exchange in a structured grid: describe each face once with MPI_Type_create_subarray or MPI_Type_vector, then exchange it with MPI_Sendrecv every time step.
  • Parallel I/O: the same types describe which part of a file each process owns (chapter 7).
  • Readability: one well-named type replaces a pack loop and an unpack loop that must stay in sync.

Exercises

  1. Send columns 1 and 4 together in one message. (Hint: a vector type of the column, resized, or MPI_Type_indexed.)
  2. Use MPI_Type_create_subarray to send the 2 × 3 block in the middle of the matrix.
  3. Send an array of ten particle_t from rank 0 to rank 1 and check that the values arrive correctly. What goes wrong if you leave out the resize step?