/* */ /* CS 4331: Parallel Programming Fall, 2012 */ /* */ /* This MPI program computes a vector inner product in parallel. */ /* */ /* The computation is vip = sum(i=0; i #include /* for the random number generator */ #include "mpi.h" #define MAX_LEN 1 << 18 /* maximum vector length */ #define PROC_0 0 /* processor 0 */ #define B0_TYPE 176 /* message "types" */ #define B1_TYPE 177 int main(argc,argv) int argc; char *argv[]; { int numprocs, p, /* number of processors, proc index */ myid, /* this processor's "rank" */ length, /* vector length */ i; double b0[MAX_LEN], b1[MAX_LEN], /* vectors */ vip, /* local vector inner product */ result; /* global " " " */ double start_time, end_time; /* "wallclock" times */ char proc_name[MPI_MAX_PROCESSOR_NAME]; int proc_name_len; MPI_Status stat; /* MPI structure containing return */ /* codes for message passing operations */ MPI_Init(&argc,&argv); /* initialize MPI */ MPI_Comm_size(MPI_COMM_WORLD, &numprocs); /*how many processors? */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /*which one am I? */ MPI_Get_processor_name(proc_name,&proc_name_len); /* what's my name? */ if (myid == PROC_0) { printf("There are %d processes.\n", numprocs); printf("Hello from processor 0: %s\n", proc_name); /* generate and distribute vectors for other processors */ srand48(0xFEEDFACE); for (p=1; p=1024; length/=2) { /* synchronize the processors (roughly) on each pass */ MPI_Barrier(MPI_COMM_WORLD); /* start the clock in processor 0 */ if (myid == PROC_0) start_time = MPI_Wtime(); /* compute the local inner product */ vip = 0.0; for (i=0; i