#include "mpi.h"
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i, j, myrank;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
#include "mpistudent.h"
if (myrank == 0) {
struct student bclass[2]; /* Just known to rank 0 */
strncpy(bclass[0].name, "Joe Jones", 30);
strncpy(bclass[0].year, "Sr.", 10);;
bclass[0].exam_grade=95;
strncpy(bclass[1].name, "Sam Smith", 30);
strncpy(bclass[1].year, "Frosh.", 10);;
bclass[1].exam_grade=90;
MPI_Send(&bclass, 2, MPI_STUDENT, 1, 123, MPI_COMM_WORLD);
} else if (myrank == 1) {
struct student cclass[2]; /* Just known to rank 1 */
MPI_Recv(&cclass, 2, MPI_STUDENT, 0, 123, MPI_COMM_WORLD, &status);
printf("%s \n",cclass[1].name);
printf("%s \n",cclass[1].year);
printf("%i \n",cclass[1].exam_grade);
}
MPI_Finalize();
return 0;
}
mpistudent.h
struct student {
char name[30];
char year[10]; /*Fresh. Soph.,Jr. Sr.*/
int exam_grade;
};
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]={0,32,44}; /* Address displacements */
MPI_Type_create_struct(3, blocklen, disp, type, &MPI_STUDENT);
MPI_Type_commit(&MPI_STUDENT);