#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 "mpi_gstu.h"
if (myrank == 0) {
struct student bstu ;
strncpy(bstu.name, "Sam Smith", 30);
strncpy(bstu.year, "Second", 10);;
bstu.exam_grade=90;
struct Gstudent Grad;
strncpy(Grad.program, "Math_CS", 20);
Grad.stu=bstu;
MPI_Send(&Grad, 1, MPI_GSTU, 1, 123, MPI_COMM_WORLD);
} else if (myrank == 1) {
struct Gstudent Grad1;
MPI_Recv(&Grad1, 1, MPI_GSTU, 0, 123, MPI_COMM_WORLD, &status);
printf("%s \n",Grad1.stu.name);
printf("%s \n",Grad1.stu.year);
printf("%i \n",Grad1.stu.exam_grade);
printf("%s \n",Grad1.program);
}
MPI_Finalize();
return 0;
}
mpi_gstu.h
struct student {
char name[30];
char year[10]; /*Fresh. Soph.,Jr. Sr.,First,Second,Third*/
int exam_grade;
};
struct Gstudent {
char program[20];
struct student stu;
};
struct Gstudent test_student;
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);
MPI_Datatype MPI_GSTU;
MPI_Datatype gtype[2] = { MPI_CHAR,MPI_STUDENT};
int gblocklen[2] = {20, 1};
MPI_Aint gdisp[2]={0,20};
MPI_Type_create_struct(2, gblocklen, gdisp, gtype, &MPI_GSTU);
MPI_Type_commit(&MPI_GSTU);