Datatypes

C | Fortran-2008 | Fortran-90

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_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;
}