#include #include #define SIZE 4 int main(int argc, char *argv[]) { int nprocs, myrank, source=0, dest, tag=1, i, j; int blockcounts[2]; /* PartStruct is a derived data type with two kinds of variables (int and double) */ typedef struct { int class, subclass; double dim; } PartStruct; PartStruct recvbuf[SIZE], sendbuf[SIZE]; MPI_Datatype particletype, orgdatatype[2]; /* MPI_Aint type used to be consistent with syntax of MPI_Type_extent routine */ MPI_Aint offsets[2], extent; MPI_Status stat; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* Allocates the 2 MPI_INT for the fields class and subclass */ offsets[0] = 0; orgdatatype[0] = MPI_INT; blockcounts[0] = 2; /* Allocates space for 1 MPI_DOUBLE field dim after figuring out the offset by getting size of MPI_INT */ MPI_Type_extent(MPI_INT, &extent); offsets[1] = 2 * extent; orgdatatype[1] = MPI_DOUBLE; blockcounts[1] = 1; /* Now define structured type and commit it */ MPI_Type_struct(2, blockcounts,offsets, orgdatatype, &particletype); MPI_Type_commit(&particletype); /* Initialize the sendbuf structure and then send it to each task */ if(myrank == 0) { for(i = 0; i < SIZE; i++) { sendbuf[i].class = i+100; sendbuf[i].subclass = i+200; sendbuf[i].dim = 1.0 *(i+1); } for(i=0; i