| [38374b7] | 1 | /*****************************************************************************
|
|---|
| [8090425] | 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: bug6.cvl
|
|---|
| [38374b7] | 6 | * DESCRIPTION:
|
|---|
| 7 | * This example demonstrates a race condition with a global variable that
|
|---|
| [6fe2cd9] | 8 | * gives obviously wrong results. See bug6fix.c for one solution.
|
|---|
| 9 | * The dotprod_mutex.c example provides a much more efficient way of
|
|---|
| 10 | * solving this problem than bug6fix.c (FYI).
|
|---|
| [8090425] | 11 | * Command line execution:
|
|---|
| [37bfb99] | 12 | * civl verify -inputNUMTHRDS=5 -inputVECLEN=10 bug6.cvl
|
|---|
| [38374b7] | 13 | ******************************************************************************/
|
|---|
| [6fe2cd9] | 14 |
|
|---|
| [38374b7] | 15 | #include "pthread.cvh"
|
|---|
| 16 | #include <civlc.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 |
|
|---|
| 20 | /* Define global data where everyone can see them */
|
|---|
| [37bfb99] | 21 | $input int NUMTHRDS;
|
|---|
| [8090425] | 22 | $input int VECLEN;
|
|---|
| [38374b7] | 23 | int *a, *b;
|
|---|
| 24 | long sum=0;
|
|---|
| 25 |
|
|---|
| 26 | void *dotprod(void *arg)
|
|---|
| 27 | {
|
|---|
| 28 | /* Each thread works on a different set of data.
|
|---|
| [6fe2cd9] | 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 = (long)*arg; // Dereference rather than direct conv
|
|---|
| 34 | offset = tid;
|
|---|
| 35 | len = VECLEN;
|
|---|
| 36 | start = offset*len;
|
|---|
| 37 | end = start + len;
|
|---|
| [38374b7] | 38 |
|
|---|
| [6fe2cd9] | 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 | sum += (a[i] * b[i]);
|
|---|
| 43 | printf("thread: %d done. Global sum now is=%d\n",tid,sum); // Removed l from %ld, unsupported
|
|---|
| 44 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| [38374b7] | 45 | }
|
|---|
| 46 |
|
|---|
| [761eb6f] | 47 | int main (void)
|
|---|
| [38374b7] | 48 | {
|
|---|
| 49 | long i;
|
|---|
| [37bfb99] | 50 | long j[NUMTHRDS];
|
|---|
| [38374b7] | 51 | void *status;
|
|---|
| [37bfb99] | 52 | pthread_t threads[NUMTHRDS];
|
|---|
| [38374b7] | 53 | pthread_attr_t attr;
|
|---|
| 54 |
|
|---|
| 55 | /* Assign storage and initialize values */
|
|---|
| [37bfb99] | 56 | a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 57 | b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| [38374b7] | 58 |
|
|---|
| [37bfb99] | 59 | for (i=0; i<VECLEN*NUMTHRDS; i++)
|
|---|
| [38374b7] | 60 | a[i]= b[i]=1;
|
|---|
| 61 |
|
|---|
| 62 | /* Create threads as joinable, each of which will execute the dot product
|
|---|
| 63 | * routine. Their offset into the global vectors is specified by passing
|
|---|
| 64 | * the "i" argument in pthread_create().
|
|---|
| 65 | */
|
|---|
| 66 | pthread_attr_init(&attr);
|
|---|
| 67 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| [37bfb99] | 68 | for(i=0; i<NUMTHRDS; i++){
|
|---|
| [6fe2cd9] | 69 | //Add values into array and use address so that each value is unique as int to void * conversion is not supported
|
|---|
| [cf74da3] | 70 | j[i] = (long)i;
|
|---|
| 71 | pthread_create(&threads[i], &attr, dotprod, (void *)&j[i]);
|
|---|
| 72 | }
|
|---|
| [ca52517] | 73 | pthread_attr_destroy(&attr);
|
|---|
| [6fe2cd9] | 74 |
|
|---|
| [38374b7] | 75 | /* Wait on the threads for final result */
|
|---|
| [37bfb99] | 76 | for(i=0; i<NUMTHRDS; i++)
|
|---|
| [38374b7] | 77 | pthread_join(threads[i], &status);
|
|---|
| 78 |
|
|---|
| 79 | /* After joining, print out the results and cleanup */
|
|---|
| [bf81b8c] | 80 | printf ("Final Global Sum=%d\n",sum);
|
|---|
| [38374b7] | 81 | free (a);
|
|---|
| 82 | free (b);
|
|---|
| [761eb6f] | 83 | return 0;
|
|---|
| [38374b7] | 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|