Rookie HPC

Datatypes

C | FORTRAN-legacy | FORTRAN-2008

MPI_Datatype

Definition

In C, an MPI datatype is of type MPI_Datatype. When sending a message in MPI, the message length is expressed as a number of elements and not a number of bytes. Example: sending an array that contains 4 ints is expressed as a buffer containing 4 MPI_INT, not 8 or 16 bytes.
List of named predefined dataypes:
MPI_SIGNED_CHAR / MPI_UNSIGNED_CHAR
MPI_SHORT / MPI_UNSIGNED_SHORT
MPI_INT / MPI_UNSIGNED
MPI_LONG / MPI_UNSIGNED_LONG
MPI_LONG_LONG_INT (a.k.a MPI_LONG_LONG) / MPI_UNSIGNED_LONG_LONG

MPI_CHAR
MPI_WCHAR
MPI_FLOAT
MPI_DOUBLE
MPI_LONG_DOUBLE

MPI_INT8_T / MPI_UINT8_T
MPI_INT16_T / MPI_UINT16_T
MPI_INT32_T / MPI_UINT32_T
MPI_INT64_T / MPI_UINT64_T

MPI_C_BOOL
MPI_C_COMPLEX
MPI_C_FLOAT_COMPLEX
MPI_C_DOUBLE_COMPLEX
MPI_C_LONG_DOUBLE_COMPLEX

MPI_AINT
MPI_COUNT
MPI_OFFSET

MPI_BYTE
MPI_PACKED

List of datatypes for reduction functions MPI_MINLOC and MPI_MAXLOC:
MPI_SHORT_INT
MPI_LONG_INT
MPI_FLOAT_INT
MPI_DOUBLE_INT
MPI_LONG_DOUBLE_INT
MPI_2INT

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

/**
 * @brief Use a variable to tell what MPI_Datatype to use.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    MPI_Datatype msg_type = MPI_INT;
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if(my_rank == 0)
    {
        int msg = 12345;
        printf("MPI process %d sends value %d.\n", my_rank, msg);
        MPI_Ssend(&msg, 1, msg_type, 1, 0, MPI_COMM_WORLD);
    }
    else
    {
        int msg;
        MPI_Recv(&msg, 1, msg_type, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        printf("MPI process %d received value %d.\n", my_rank, msg);
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}