mpi-derived-datatypes
MPI derived datatypes
Table of Contents
MPI derived datatypes
Derived datatypes describe scattered data (matrix columns, alternating elements, struct fields) so MPI can send them directly without manual packing.
double tmp[N_ROWS]; for (int i = 0; i < N_ROWS; i++) tmp[i] = matrix[i][col]; MPI_Send(tmp, N_ROWS, MPI_DOUBLE, dest, 0, MPI_COMM_WORLD);
This adds an allocation and a copy on every send. Derived datatypes describe the layout of the original data so MPI can gather it directly without an intermediate buffer. MPI_Type_vector describes a strided pattern: count blocks of blocklength elements separated by a stride.
MPI_Datatype col_t; // one column of a row-major matrix: N_ROWS blocks of 1 double, stride = N_COLS MPI_Type_vector(N_ROWS, 1, N_COLS, MPI_DOUBLE, &col_t); MPI_Type_commit(&col_t); MPI_Send(&matrix[0][col], 1, col_t, dest, 0, MPI_COMM_WORLD); MPI_Type_free(&col_t);
MPI_Type_create_struct describes a struct with mixed types, matching a C struct layout. Every derived type must be committed with MPI_Type_commit before use and freed with MPI_Type_free when done.
mpi-derived-datatypes.md ยท Last modified:
