source: CIVL/examples/pthread/bug6.cvl@ 37bfb99

1.23 2.0 main test-branch
Last change on this file since 37bfb99 was 37bfb99, checked in by John Edenhofner <johneden@…>, 12 years ago

Updated examples and pthread.cvh

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1039 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[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
8* gives obviously wrong results. Figure out how to fix the problem - or see
9* bug6fix.c for one solution. The dotprod_mutex.c example provides a much
10* more efficient way of solving this problem than bug6fix.c (FYI).
[8090425]11* Command line execution:
[37bfb99]12* civl verify -inputNUMTHRDS=5 -inputVECLEN=10 bug6.cvl
[38374b7]13******************************************************************************/
14#include "pthread.cvh"
15#include <civlc.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19/* Define global data where everyone can see them */
[37bfb99]20$input int NUMTHRDS;
[8090425]21$input int VECLEN;
[38374b7]22int *a, *b;
23long sum=0;
24
25void *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;
[bf81b8c]32 long tid = (long)*arg;
[38374b7]33 offset = tid;
34 len = VECLEN;
35 start = offset*len;
36 end = start + len;
37
38/* Perform my section of the dot product */
[bf81b8c]39 printf("thread: %d starting. start=%d end=%d\n",tid,start,end-1); // Removed l from %ld, unsupported
[38374b7]40 for (i=start; i<end ; i++)
41 sum += (a[i] * b[i]);
[761eb6f]42 printf("thread: %d done. Global sum now is=%d\n",tid,sum); // Removed l from %ld, unsupported
[cf74da3]43 pthread_exit(NULL, false, NULL, 0);
[38374b7]44}
45
46
47
[761eb6f]48int main (void)
[38374b7]49{
50 long i;
[37bfb99]51 long j[NUMTHRDS];
[38374b7]52 void *status;
[37bfb99]53 pthread_t threads[NUMTHRDS];
[38374b7]54 pthread_attr_t attr;
55
56 /* Assign storage and initialize values */
[37bfb99]57 a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
58 b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
[38374b7]59
[37bfb99]60 for (i=0; i<VECLEN*NUMTHRDS; i++)
[38374b7]61 a[i]= b[i]=1;
62
63 /* Create threads as joinable, each of which will execute the dot product
64 * routine. Their offset into the global vectors is specified by passing
65 * the "i" argument in pthread_create().
66 */
67 pthread_attr_init(&attr);
68 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
[37bfb99]69 for(i=0; i<NUMTHRDS; i++){
[cf74da3]70 j[i] = (long)i;
71 pthread_create(&threads[i], &attr, dotprod, (void *)&j[i]);
72 }
[ca52517]73 pthread_attr_destroy(&attr);
[38374b7]74 /* Wait on the threads for final result */
[37bfb99]75 for(i=0; i<NUMTHRDS; i++)
[38374b7]76 pthread_join(threads[i], &status);
77
78 /* After joining, print out the results and cleanup */
[bf81b8c]79 printf ("Final Global Sum=%d\n",sum);
[38374b7]80 free (a);
81 free (b);
[761eb6f]82 return 0;
[38374b7]83}
84
85
Note: See TracBrowser for help on using the repository browser.