| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: mpithreads_mpi.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This simple program illustrates the use of MPI in a program obtained
|
|---|
| 5 | * by modifying a serial code that performs a dot product. It is the third
|
|---|
| 6 | * of four codes used to show the progression from a serial program to a
|
|---|
| 7 | * hybrid MPI/Pthreads program. The other relevant codes are:
|
|---|
| 8 | * - mpithreads_serial.c - The serial version
|
|---|
| 9 | * - mpithreads_threads.c - A shared memory programming model using
|
|---|
| 10 | * Pthreads
|
|---|
| 11 | * - mpithreads_both.c - A hybrid model that utilizes both MPI and
|
|---|
| 12 | * Pthreads to execute on systems that are comprised of clusters
|
|---|
| 13 | * of SMP's.
|
|---|
| 14 | * Use of the SPMD model was chosen and for convenience, with replication
|
|---|
| 15 | * of the main data on all nodes. A more memory efficient implementation
|
|---|
| 16 | * would be advisable for larger data sets.
|
|---|
| 17 | * SOURCE: Vijay Sonnad, IBM
|
|---|
| 18 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 19 | ******************************************************************************/
|
|---|
| 20 | #include <mpi.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <stdlib.h>
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | The following structure contains the necessary information to allow the
|
|---|
| 26 | function "dotprod" to access its input data and place its output into
|
|---|
| 27 | the structure. Note that this structure is unchanged from the sequential
|
|---|
| 28 | version.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | typedef struct
|
|---|
| 32 | {
|
|---|
| 33 | double *a;
|
|---|
| 34 | double *b;
|
|---|
| 35 | double sum;
|
|---|
| 36 | int veclen;
|
|---|
| 37 | } DOTDATA;
|
|---|
| 38 |
|
|---|
| 39 | /* Define globally accessible variables */
|
|---|
| 40 |
|
|---|
| 41 | #define VECLEN 100
|
|---|
| 42 | DOTDATA dotstr;
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | The function dotprod is very similar to the sequential version except that
|
|---|
| 46 | we now have each node working on a different part of the data. As before,
|
|---|
| 47 | all access to the input is through a structure of type DOTDATA and all
|
|---|
| 48 | output from this function is written into this same structure.
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | void dotprod()
|
|---|
| 52 | {
|
|---|
| 53 |
|
|---|
| 54 | /* Define and use local variables for convenience */
|
|---|
| 55 |
|
|---|
| 56 | int i, start, end, myid, len;
|
|---|
| 57 | double mysum, *x, *y;
|
|---|
| 58 |
|
|---|
| 59 | /* Obtain rank of this node */
|
|---|
| 60 |
|
|---|
| 61 | MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|---|
| 62 |
|
|---|
| 63 | len = dotstr.veclen;
|
|---|
| 64 | start = myid*len;
|
|---|
| 65 | end = start + len;
|
|---|
| 66 | x = dotstr.a;
|
|---|
| 67 | y = dotstr.b;
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | Perform the dot product and assign result to the appropriate variable in
|
|---|
| 71 | the structure.
|
|---|
| 72 | */
|
|---|
| 73 |
|
|---|
| 74 | mysum = 0;
|
|---|
| 75 | for (i=start; i<end ; i++)
|
|---|
| 76 | {
|
|---|
| 77 | mysum += (x[i] * y[i]);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | dotstr.sum += mysum;
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | As before,the main program does very little computation. It does however make
|
|---|
| 86 | all the calls to the MPI routines. This is not a master-worker arrangement
|
|---|
| 87 | and all nodes participate equally in the work.
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | int main (int argc, char* argv[])
|
|---|
| 91 | {
|
|---|
| 92 | int i,len=VECLEN;
|
|---|
| 93 | int myid, numprocs;
|
|---|
| 94 | double *a, *b;
|
|---|
| 95 | double mysum, allsum;
|
|---|
| 96 |
|
|---|
| 97 | /* MPI Initialization */
|
|---|
| 98 | MPI_Init (&argc, &argv);
|
|---|
| 99 | MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
|
|---|
| 100 | MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|---|
| 101 |
|
|---|
| 102 | /* Assign storage and initialize values */
|
|---|
| 103 | a = (double*) malloc (numprocs*len*sizeof(double));
|
|---|
| 104 | b = (double*) malloc (numprocs*len*sizeof(double));
|
|---|
| 105 |
|
|---|
| 106 | for (i=0; i<len*numprocs; i++) {
|
|---|
| 107 | a[i]=1;
|
|---|
| 108 | b[i]=a[i];
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | dotstr.veclen = len;
|
|---|
| 112 | dotstr.a = a;
|
|---|
| 113 | dotstr.b = b;
|
|---|
| 114 | dotstr.sum=0;
|
|---|
| 115 |
|
|---|
| 116 | /* Call the dot product routine */
|
|---|
| 117 | dotprod();
|
|---|
| 118 | mysum = dotstr.sum;
|
|---|
| 119 | printf("Task %d partial sum is %f\n",myid, mysum);
|
|---|
| 120 |
|
|---|
| 121 | /* After the dot product, perform a summation of results on each node */
|
|---|
| 122 | MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
|
|---|
| 123 |
|
|---|
| 124 | if (myid == 0)
|
|---|
| 125 | printf ("Done. MPI version: sum = %f \n", allsum);
|
|---|
| 126 | free (a);
|
|---|
| 127 | free (b);
|
|---|
| 128 | MPI_Finalize();
|
|---|
| 129 | return 0;
|
|---|
| 130 | }
|
|---|