/**************************************************************************** * FILE: datatypes2.c * DESCRIPTION: * Simple comparison between using a derived datatype and individual * send/receive operations for different data types. * AUTHOR: 01/09/99 Blaise Barney * LAST REVISED: ******************************************************************************/ #include "mpi.h" #include #define TRIALS 10 #define REPS 10000 int main(argc,argv) int argc; char *argv[]; { int numtasks, rank, tag=1, i, k; double bandwidth, tbytes, indiv_best, derived_best, t1, t2, ttime; typedef struct { float f1,f2,f3,f4; int i1,i2; } f4i2; f4i2 rbuff, sbuff; MPI_Datatype newtype, oldtypes[2]; int blockcounts[2]; MPI_Aint offsets[2], extent; MPI_Status stat; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); if (numtasks != 2) { printf("Please run this test with 2 tasks. Terminating\n"); MPI_Finalize(); } /* Setup MPI structured type for the 4 floats and 2 ints */ offsets[0] = 0; oldtypes[0] = MPI_FLOAT; blockcounts[0] = 4; MPI_Type_extent(MPI_FLOAT, &extent); offsets[1] = 4 * extent; oldtypes[1] = MPI_INT; blockcounts[1] = 2; MPI_Type_struct(2, blockcounts, offsets, oldtypes, &newtype); MPI_Type_commit(&newtype); /* Initialize the derived type send data */ sbuff.f1 = 1.0; sbuff.f2 = 2.0; sbuff.f3 = 3.0; sbuff.f4 = 4.0; sbuff.i1 = 1; sbuff.i2 = 2; /**************************** task 0 ***********************************/ if (rank == 0) { /* Greetings */ printf("\n****** Derived Datatype vs. Individual Send/Receive *****\n"); printf("Trials= %8d\n",TRIALS); printf("Reps/trial= %8d\n\n",REPS); printf("D/I # ttime tbytes bytes/sec\n"); printf("--- -- --------- --------- ----------\n"); /* Begin timings - save best result over all trials */ derived_best = 0.0; indiv_best = 0.0; tbytes = sizeof(f4i2) * 2 * (float)REPS; for (k=0; k derived_best) derived_best = bandwidth; printf(" D %3d %3.6f %10.0f %10.0f\n",k,ttime,tbytes,bandwidth); /* Now do individual send/receives for structure components */ t1 = MPI_Wtime(); for (i=1; i<=REPS; i++){ MPI_Send(&sbuff.f1, 4, MPI_FLOAT, 1, tag, MPI_COMM_WORLD); MPI_Send(&sbuff.i1, 2, MPI_INT, 1, tag, MPI_COMM_WORLD); MPI_Recv(&rbuff.f1, 4, MPI_FLOAT, 1, tag, MPI_COMM_WORLD, &stat); MPI_Recv(&rbuff.i1, 2, MPI_INT, 1, tag, MPI_COMM_WORLD, &stat); } t2 = MPI_Wtime(); ttime = t2 - t1; bandwidth = tbytes/ttime; if (bandwidth > indiv_best) indiv_best = bandwidth; printf(" I %3d %3.6f %10.0f %10.0f\n",k,ttime,tbytes,bandwidth); } /* k loop */ printf("\nDerived bandwidth= \t%10.0f bytes/sec.\n",derived_best); printf("Individual bandwidth= \t%10.0f bytes/sec.\n",indiv_best); } /* task 0 */ /**************************** task 1 ***********************************/ if (rank == 1) { for (k=0; k