| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: bug6fix.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This solution uses a mutex variable to protect the global sum while each
|
|---|
| 5 | * thread updates it. A much more efficient method would be that used in the
|
|---|
| 6 | * dotprod_mutex.c example.
|
|---|
| 7 | * SOURCE: 07/06/05 Blaise Barney
|
|---|
| 8 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 | #include <pthread.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 |
|
|---|
| 14 | /* Define global data where everyone can see them */
|
|---|
| 15 | #define NUMTHRDS 3
|
|---|
| 16 | #define VECLEN 4
|
|---|
| 17 | pthread_mutex_t mutexsum;
|
|---|
| 18 | int *a, *b;
|
|---|
| 19 | long sum=0.0;
|
|---|
| 20 |
|
|---|
| 21 | void *dotprod(void *arg)
|
|---|
| 22 | {
|
|---|
| 23 | /* Each thread works on a different set of data.
|
|---|
| 24 | * The offset is specified by the arg parameter. The size of
|
|---|
| 25 | * the data for each thread is indicated by VECLEN.
|
|---|
| 26 | */
|
|---|
| 27 | int i, start, end, offset, len;
|
|---|
| 28 | long tid;
|
|---|
| 29 | tid = (long)arg;
|
|---|
| 30 | offset = tid;
|
|---|
| 31 | len = VECLEN;
|
|---|
| 32 | start = offset*len;
|
|---|
| 33 | end = start + len;
|
|---|
| 34 |
|
|---|
| 35 | /* Perform my section of the dot product */
|
|---|
| 36 | printf("thread: %ld starting. start=%d end=%d\n",tid,start,end-1);
|
|---|
| 37 | for (i=start; i<end ; i++) {
|
|---|
| 38 | pthread_mutex_lock(&mutexsum);
|
|---|
| 39 | sum += (a[i] * b[i]);
|
|---|
| 40 | pthread_mutex_unlock(&mutexsum);
|
|---|
| 41 | }
|
|---|
| 42 | printf("thread: %ld done. Global sum now is=%li\n",tid,sum);
|
|---|
| 43 |
|
|---|
| 44 | pthread_exit((void*) 0);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | int main (int argc, char *argv[])
|
|---|
| 48 | {
|
|---|
| 49 | long i;
|
|---|
| 50 | void *status;
|
|---|
| 51 | pthread_t threads[NUMTHRDS];
|
|---|
| 52 | pthread_attr_t attr;
|
|---|
| 53 |
|
|---|
| 54 | /* Assign storage and initialize values */
|
|---|
| 55 | a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 56 | b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 57 |
|
|---|
| 58 | for (i=0; i<VECLEN*NUMTHRDS; i++)
|
|---|
| 59 | a[i]=b[i]=1;
|
|---|
| 60 |
|
|---|
| 61 | /* Initialize mutex variable */
|
|---|
| 62 | pthread_mutex_init(&mutexsum, NULL);
|
|---|
| 63 |
|
|---|
| 64 | /* Create threads as joinable, each of which will execute the dot product
|
|---|
| 65 | * routine. Their offset into the global vectors is specified by passing
|
|---|
| 66 | * the "i" argument in pthread_create().
|
|---|
| 67 | */
|
|---|
| 68 | pthread_attr_init(&attr);
|
|---|
| 69 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 70 | for(i=0;i<NUMTHRDS;i++)
|
|---|
| 71 | pthread_create(&threads[i], &attr, dotprod, (void *)i);
|
|---|
| 72 |
|
|---|
| 73 | pthread_attr_destroy(&attr);
|
|---|
| 74 |
|
|---|
| 75 | /* Wait on the other threads for final result */
|
|---|
| 76 |
|
|---|
| 77 | for(i=0;i<NUMTHRDS;i++) {
|
|---|
| 78 | pthread_join(threads[i], &status);
|
|---|
| 79 | }
|
|---|
| 80 | /* After joining, print out the results and cleanup */
|
|---|
| 81 | printf ("Final Global Sum=%li\n",sum);
|
|---|
| 82 | free (a);
|
|---|
| 83 | free (b);
|
|---|
| 84 | pthread_mutex_destroy(&mutexsum);
|
|---|
| 85 | pthread_exit(NULL);
|
|---|
| 86 | }
|
|---|