| 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 |
|
|---|
| 14 | #include <civlc.h>
|
|---|
| 15 | #include "pthread.cvh"
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <stdlib.h>
|
|---|
| 18 |
|
|---|
| 19 | /* Define global data where everyone can see them */
|
|---|
| 20 | $input int NUMTHRDS;
|
|---|
| 21 | $input int VECLEN;
|
|---|
| 22 | pthread_mutex_t mutexsum;
|
|---|
| 23 | int *a, *b;
|
|---|
| 24 | long sum=0.0;
|
|---|
| 25 |
|
|---|
| 26 | void *dotprod(void *arg)
|
|---|
| 27 | {
|
|---|
| 28 | /* Each thread works on a different set of data.
|
|---|
| 29 | * The offset is specified by the arg parameter. The size of
|
|---|
| 30 | * the data for each thread is indicated by VECLEN.
|
|---|
| 31 | */
|
|---|
| 32 | int i, start, end, offset, len;
|
|---|
| 33 | long tid;
|
|---|
| 34 | tid = (long)*arg; // Dereference rather than direct conv
|
|---|
| 35 | offset = tid;
|
|---|
| 36 | len = VECLEN;
|
|---|
| 37 | start = offset*len;
|
|---|
| 38 | end = start + len;
|
|---|
| 39 |
|
|---|
| 40 | /* Perform my section of the dot product */
|
|---|
| 41 | printf("thread: %d starting. start=%d end=%d\n",tid,start,end-1); // Removed l from %ld, unsupported
|
|---|
| 42 | for (i=start; i<end ; i++) {
|
|---|
| 43 | pthread_mutex_lock(&mutexsum);
|
|---|
| 44 | sum += (a[i] * b[i]);
|
|---|
| 45 | pthread_mutex_unlock(&mutexsum);
|
|---|
| 46 | }
|
|---|
| 47 | printf("thread: %d done. Global sum now is=%d\n",tid,sum); // Removed l from %ld, unsupported
|
|---|
| 48 |
|
|---|
| 49 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main (void)
|
|---|
| 53 | {
|
|---|
| 54 | long i;
|
|---|
| 55 | long j[NUMTHRDS];
|
|---|
| 56 | void *status;
|
|---|
| 57 | pthread_t threads[NUMTHRDS];
|
|---|
| 58 | pthread_attr_t attr;
|
|---|
| 59 |
|
|---|
| 60 | /* Assign storage and initialize values */
|
|---|
| 61 | a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 62 | b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 63 |
|
|---|
| 64 | for (i=0; i<VECLEN*NUMTHRDS; i++)
|
|---|
| 65 | a[i]=b[i]=1;
|
|---|
| 66 |
|
|---|
| 67 | /* Initialize mutex variable */
|
|---|
| 68 | pthread_mutex_init(&mutexsum, NULL);
|
|---|
| 69 |
|
|---|
| 70 | /* Create threads as joinable, each of which will execute the dot product
|
|---|
| 71 | * routine. Their offset into the global vectors is specified by passing
|
|---|
| 72 | * the "i" argument in pthread_create().
|
|---|
| 73 | */
|
|---|
| 74 | pthread_attr_init(&attr);
|
|---|
| 75 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 76 | for(i=0;i<NUMTHRDS;i++){
|
|---|
| 77 | //Add values into array and use address so that each value is unique as int to void * conversion is not supported
|
|---|
| 78 | j[i] = (long)i;
|
|---|
| 79 | pthread_create(&threads[i], &attr, dotprod, (void *)&j[i]);
|
|---|
| 80 | }
|
|---|
| 81 | pthread_attr_destroy(&attr);
|
|---|
| 82 |
|
|---|
| 83 | /* Wait on the other threads for final result */
|
|---|
| 84 | for(i=0;i<NUMTHRDS;i++) {
|
|---|
| 85 | pthread_join(threads[i], &status);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /* After joining, print out the results and cleanup */
|
|---|
| 89 | printf ("Final Global Sum=%d\n",sum);
|
|---|
| 90 |
|
|---|
| 91 | free (a);
|
|---|
| 92 | free (b);
|
|---|
| 93 | pthread_mutex_destroy(&mutexsum);
|
|---|
| 94 | //pthread_exit(NULL);
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|