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