| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: mpithreads_serial.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This is a simple serial program that computes the dot product of two
|
|---|
| 5 | * vectors. It is the first of four codes used to show the progression
|
|---|
| 6 | * from a serial program to a hybrid MPI/Pthreads program. The other
|
|---|
| 7 | * relevant codes are:
|
|---|
| 8 | * - mpithreads_threads.c - A shared memory programming model using
|
|---|
| 9 | * Pthreads
|
|---|
| 10 | * - mpithreads_mpi.c - A distributed memory programming model with MPI
|
|---|
| 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 | * SOURCE: Vijay Sonnad, IBM
|
|---|
| 15 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 16 | ******************************************************************************/
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 |
|
|---|
| 20 | /*
|
|---|
| 21 | The following structure contains the necessary information to allow the
|
|---|
| 22 | function "dotprod" to access its input data and place its output so that
|
|---|
| 23 | it can be accessed later.
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | typedef struct
|
|---|
| 27 | {
|
|---|
| 28 | double *a;
|
|---|
| 29 | double *b;
|
|---|
| 30 | double sum;
|
|---|
| 31 | int veclen;
|
|---|
| 32 | } DOTDATA;
|
|---|
| 33 |
|
|---|
| 34 | #ifdef _CIVL
|
|---|
| 35 | $input int VECLEN;
|
|---|
| 36 | $output double _sum;
|
|---|
| 37 | #else
|
|---|
| 38 | #define VECLEN 100
|
|---|
| 39 | #endif
|
|---|
| 40 | DOTDATA dotstr;
|
|---|
| 41 |
|
|---|
| 42 | /*
|
|---|
| 43 | We will use a function (dotprod) to perform the scalar product. All input to
|
|---|
| 44 | this routine is obtained through a structure of type DOTDATA and all output
|
|---|
| 45 | from this function is written into this same structure. While this is
|
|---|
| 46 | unnecessarily restrictive for a sequential program, it will turn out to be
|
|---|
| 47 | useful when we modify the program to compute in parallel.
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | void* dotprod(void)
|
|---|
| 51 | {
|
|---|
| 52 |
|
|---|
| 53 | /* Define and use local variables for convenience */
|
|---|
| 54 |
|
|---|
| 55 | int start, end, i;
|
|---|
| 56 | double mysum, *x, *y;
|
|---|
| 57 |
|
|---|
| 58 | start=0;
|
|---|
| 59 | end = dotstr.veclen;
|
|---|
| 60 | x = dotstr.a;
|
|---|
| 61 | y = dotstr.b;
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | Perform the dot product and assign result to the appropriate variable in
|
|---|
| 65 | the structure.
|
|---|
| 66 | */
|
|---|
| 67 |
|
|---|
| 68 | mysum = 0;
|
|---|
| 69 | for (i=start; i<end ; i++)
|
|---|
| 70 | {
|
|---|
| 71 | mysum += (x[i] * y[i]);
|
|---|
| 72 | }
|
|---|
| 73 | dotstr.sum = mysum;
|
|---|
| 74 |
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /*
|
|---|
| 78 | The main program initializes data and calls the dotprd() function. Finally,
|
|---|
| 79 | it prints the result.
|
|---|
| 80 | */
|
|---|
| 81 |
|
|---|
| 82 | int main (int argc, char* argv[])
|
|---|
| 83 | {
|
|---|
| 84 | int i,len;
|
|---|
| 85 | double *a, *b;
|
|---|
| 86 |
|
|---|
| 87 | /* Assign storage and initialize values */
|
|---|
| 88 | len = VECLEN;
|
|---|
| 89 | a = (double*) malloc (len*sizeof(double));
|
|---|
| 90 | b = (double*) malloc (len*sizeof(double));
|
|---|
| 91 |
|
|---|
| 92 | for (i=0; i<len; i++) {
|
|---|
| 93 | a[i]=1;
|
|---|
| 94 | b[i]=a[i];
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | dotstr.veclen = len;
|
|---|
| 98 | dotstr.a = a;
|
|---|
| 99 | dotstr.b = b;
|
|---|
| 100 | dotstr.sum=0;
|
|---|
| 101 |
|
|---|
| 102 | /* Perform the dotproduct */
|
|---|
| 103 | dotprod ();
|
|---|
| 104 | #ifdef _CIVL
|
|---|
| 105 | _sum=dotstr.sum;
|
|---|
| 106 | #endif
|
|---|
| 107 | /* Print result and release storage */
|
|---|
| 108 | printf ("Done. Serial version: sum = %f \n", dotstr.sum);
|
|---|
| 109 | free (a);
|
|---|
| 110 | free (b);
|
|---|
| 111 | }
|
|---|