Contiguous Derived Datatype: C Example


Using MPI_Type_contiguous


This exercise presents a simple program to illustrate the use of MPI_Type_contiguous. The program defines a new data type representing three contiguous integers. An array (ibuf) is filled at the root process (rank=0). Three elements of the new data type is broadcast from ibuf from the root process to all processes. Copy this program and modify it to define a new data type representing 5 contiguous integers and broadcast 2 elements of the new type to all processes from the root process.


        program type_contiguous
        include "mpif.h"
        integer ibuf(20)
        integer rank,size,ierr

        call MPI_INIT(ierr)
        call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
        call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)

        if (rank==0) then
           do i = 1, 20
              ibuf(i) = i

        call MPI_TYPE_CONTIGUOUS(3, MPI_INTEGER, inewtype, ierr)
        call MPI_TYPE_COMMIT(inewtype, ierr)
        call MPI_BCAST(ibuf, 3, inewtype, 0, MPI_COMM_WORLD, ierr)
        write(*,*) 'ibuf =', ibuf

        call MPI_FINALIZE(ierr)