#include "mpi.h" #include #define NELEM 25 int main(argc,argv) int argc; char *argv[]; { int numtasks, rank, source=0, dest, tag=1, i; typedef struct { float x, y, z; float velocity; int n, type; } Particle; Particle p[NELEM], particles[NELEM]; MPI_Datatype particletype, oldtypes[2]; int blockcounts[2]; /*-------------------------------------------------------*/ /* MPI_Aint type is used to be consistent with syntax of */ /* MPI_Type_extent routine */ /*-------------------------------------------------------*/ MPI_Aint offsets[2], extent; MPI_Status stat; /*----------------*/ /* Initialize MPI */ /*----------------*/ MPI_Init(&argc,&argv); /*-------------------------------------------------------*/ /* Get the size of the MPI_COMM_WORLD communicator group */ /*-------------------------------------------------------*/ MPI_Comm_size(MPI_COMM_WORLD, &numtasks); /*------------------------------------------------------*/ /* Get my rank in the MPI_COMM_WORLD communicator group */ /*------------------------------------------------------*/ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /*---------------------------------------------------------------*/ /* Setup description of the 4 MPI_FLOAT fields x, y, z, velocity */ /*---------------------------------------------------------------*/ offsets[0] = 0; oldtypes[0] = MPI_FLOAT; blockcounts[0] = 4; /*----------------------------------------------------------------*/ /* Setup description of the 2 MPI_INT fields n, type */ /* Need to first identify the offset by getting size of MPI_FLOAT */ /*----------------------------------------------------------------*/ MPI_Type_extent(MPI_FLOAT, &extent); offsets[1] = 4 * extent; oldtypes[1] = MPI_INT; blockcounts[1] = 2; /*------------------------------------------*/ /* Now define structured type and commit it */ /*------------------------------------------*/ MPI_Type_struct(2, blockcounts, offsets, oldtypes, &particletype); MPI_Type_commit(&particletype); /*-------------------------------------------------------------*/ /* Initialize the particle array and then send it to each task */ /*-------------------------------------------------------------*/ if (rank == 0) { for (i=0; i