#include "mpi.h"
#include <stdio.h>
#include <string.h>

struct student {
    char  name[30];
    char  year[10]; /*Fresh. Soph.,Jr. Sr.*/
    int exam_grade;
};

int main(int argc, char *argv[])
{
    struct student astudent; /* Just used to define MPI_STUDENT*/
    int i, j, myrank;
    MPI_Init(&argc, &argv);
    MPI_Status status;
    MPI_Datatype MPI_STUDENT;
    MPI_Datatype type[3] = { MPI_CHAR, MPI_CHAR, MPI_INT}; /* The "MPI types" of student */
    int blocklen[3] = { 30, 10, 1};   /* The array lengths */
    MPI_Aint disp[3];   /* Address displacements */
    MPI_Aint start_address;
    MPI_Aint address;
    MPI_Get_address( &astudent, &start_address);
    MPI_Get_address( &astudent.name , &address);
    disp[0] = address - start_address;
    MPI_Get_address( &astudent.year , &address);
    disp[1] = address - start_address;
    MPI_Get_address( &astudent.exam_grade, &address);
    disp[2] = address - start_address;
    /* Address displacements for MPI_STUDENT */
    MPI_Type_create_struct(3, blocklen, disp, type, &MPI_STUDENT);
    MPI_Type_commit(&MPI_STUDENT);
    /* MPI_STUDENT is now an available MPI datatype */
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    if (myrank == 0) {
        struct student bstudent; /* Just known to rank 0 */
        strncpy(bstudent.name, "Joe Jones", 30);
        strncpy(bstudent.year, "Sr.", 10);;
        bstudent.exam_grade=95;
        MPI_Send(&bstudent, 1, MPI_STUDENT, 1, 123, MPI_COMM_WORLD);
    } else if (myrank == 1) {
        struct student cstudent;  /* Just known to rank 1 */
        MPI_Recv(&cstudent, 1, MPI_STUDENT, 0, 123, MPI_COMM_WORLD, &status);
        /* printf("%s \n",bstudent.name);   Error - Not known to rank 1 */
        printf("%s \n",cstudent.name);
        printf("%s \n",cstudent.year);
        printf("%i \n",cstudent.exam_grade);
    }
    MPI_Finalize();
    return 0;
}




Some Notes

Another Version

      

Another Version(Graduate Students)