Rookie HPC

About

Docs

Tools

Tests

MPI_Datatype

Definition

MPI_Datatype represents the type of elements considered. 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 integers is expressed as a buffer containing 4 MPI_INT, not 8 or 16 bytes. Here is the list of MPI datatypes:

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;
        printf("MPI process %d receives value %d.\n", my_rank, msg);
        MPI_Recv(&msg, 1, msg_type, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}