source: CIVL/examples/pthread/bug6fix.cvl@ bf81b8c

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

Added bug fix programs and some documentation

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

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[9f23e8b]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* SUPERSOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
6* et al. O'Reilly and Associates.
7* FILE: bug6fix.cvl
8* DESCRIPTION:
9* This solution uses a mutex variable to protect the global sum while each
10* thread updates it. A much more efficient method would be that used in the
11* dotprod_mutex.cvl example.
12* Command line execution:
13* civl verify bug6fix.cvl
14******************************************************************************/
15#include <pthread.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19/* Define global data where everyone can see them */
20#define NUMTHRDS 8
21#define VECLEN 100000
22pthread_mutex_t mutexsum;
23int *a, *b;
24long sum=0.0;
25
26void *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;
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: %ld starting. start=%d end=%d\n",tid,start,end-1);
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: %ld done. Global sum now is=%li\n",tid,sum);
48
49 pthread_exit((void*) 0);
50}
51
52int main (int argc, char *argv[])
53{
54long i;
55void *status;
56pthread_t threads[NUMTHRDS];
57pthread_attr_t attr;
58
59/* Assign storage and initialize values */
60a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
61b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
62
63for (i=0; i<VECLEN*NUMTHRDS; i++)
64 a[i]=b[i]=1;
65
66/* Initialize mutex variable */
67pthread_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 */
73pthread_attr_init(&attr);
74pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
75for(i=0;i<NUMTHRDS;i++)
76 pthread_create(&threads[i], &attr, dotprod, (void *)i);
77
78pthread_attr_destroy(&attr);
79
80/* Wait on the other threads for final result */
81
82for(i=0;i<NUMTHRDS;i++) {
83 pthread_join(threads[i], &status);
84 }
85/* After joining, print out the results and cleanup */
86printf ("Final Global Sum=%li\n",sum);
87free (a);
88free (b);
89pthread_mutex_destroy(&mutexsum);
90pthread_exit(NULL);
91}
Note: See TracBrowser for help on using the repository browser.