/***************************************************************************** * FILE: mpithreads_mpi.c * DESCRIPTION: * This simple program illustrates the use of MPI in a program obtained * by modifying a serial code that performs a dot product. It is the third * of four codes used to show the progression from a serial program to a * hybrid MPI/Pthreads program. The other relevant codes are: * - mpithreads_serial.c - The serial version * - mpithreads_threads.c - A shared memory programming model using * Pthreads * - mpithreads_both.c - A hybrid model that utilizes both MPI and * Pthreads to execute on systems that are comprised of clusters * of SMP's. * * Use of the SPMD model was chosen and for convenience, with replication * of the main data on all nodes. A more memory efficient implementation * would be advisable for larger data sets. * * SOURCE: Vijay Sonnad, IBM * LAST REVISED: 10/8/99 Blaise Barney ******************************************************************************/ #include #include #include /* The following structure contains the necessary information to allow the function "dotprod" to access its input data and place its output into the structure. Note that this structure is unchanged from the sequential version. */ typedef struct { double *a; double *b; double sum; int veclen; } DOTDATA; /* Define globally accessible variables */ #define VECLEN 100 DOTDATA dotstr; /* The function dotprod is very similar to the sequential version except that we now have each node working on a different part of the data. As before, all access to the input is through a structure of type DOTDATA and all output from this function is written into this same structure. */ void *dotprod() { /* Define and use local variables for convenience */ int i, start, end, myid, len; double mysum, *x, *y; /* Obtain rank of this node */ MPI_Comm_rank (MPI_COMM_WORLD, &myid); len = dotstr.veclen; start = myid*len; end = start + len; x = dotstr.a; y = dotstr.b; /* Perform the dot product and assign result to the appropriate variable in the structure. */ mysum = 0; for (i=start; i