Rookie HPC

About

Docs

Tools

Tests

MPI_Type_contiguous

Definition

MPI_Type_contiguous creates an MPI datatype by replicating an existing one a certain number of times. These replications are created into contiguous locations, resulting in a contiguous data type created. The datatype created must be committed with MPI_Type_commit before it can be used in communications. Other ways to create a datatype are MPI_Type_vector, MPI_Type_create_hvector, MPI_Type_indexed, MPI_Type_create_hindexed, MPI_Type_create_indexed_block, MPI_Type_create_hindexed_block, MPI_Type_create_struct, MPI_Type_create_subarray, MPI_Type_create_darray.

Copy

Feedback

int MPI_Type_contiguous(int count,
                        MPI_Datatype old_type,
                        MPI_Datatype* new_type);

Parameters

count
The number of replications of the existing MPI datatype in the new one.
old_type
The existing MPI datatype to replicate.
new_type
The variable in which store the MPI datatype created.

Returned value

MPI_SUCCESS
The routine successfully completed.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to create a contiguous MPI datatype.
 * @details This program is meant to be run with 2 processes: a sender and a
 * receiver. These two MPI processes will exchange a message made of two
 * integers. To that end, they each create a datatype representing that layout.
 * They then use this datatype to express the message type exchanged.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get the number of processes and check only 2 processes are used
    int size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(size != 2)
    {
        printf("This application is meant to be run with 2 processes.\n");
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

    // Create the datatype
    MPI_Datatype double_int_type;
    MPI_Type_contiguous(2, MPI_INT, &double_int_type);
    MPI_Type_commit(&double_int_type);

    // Get my rank and do the corresponding job
    enum role_ranks { SENDER, RECEIVER };
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    switch(my_rank)
    {
        case SENDER:
        {
            // Send the message
            int buffer_sent[2] = {12345, 67890};
            MPI_Request request;
            printf("MPI process %d sends values %d and %d.\n", my_rank, buffer_sent[0], buffer_sent[1]);
            MPI_Send(&buffer_sent, 1, double_int_type, RECEIVER, 0, MPI_COMM_WORLD);
            break;
        }
        case RECEIVER:
        {
            // Receive the message
            int received[2];
            MPI_Recv(&received, 1, double_int_type, SENDER, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("MPI process %d received values: %d and %d.\n", my_rank, received[0], received[1]);
            break;
        }
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}