-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsum-array.c
More file actions
69 lines (57 loc) · 2.57 KB
/
psum-array.c
File metadata and controls
69 lines (57 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* psum-array.c - A simple parallel sum program where each thread sums
* into its own distinct global array element.
*/
#include "csapp.h"
#define MAXTHREADS 32
void *sum_array(void *vargp); /* Thread routine */
/* Global shared variables */
long psum[MAXTHREADS]; /* Partial sum computed by each thread */
long nelems_per_thread; /* Number of elements summed by each thread */
int main(int argc, char **argv)
{
long i, nelems, log_nelems, nthreads, myid[MAXTHREADS], result = 0;
pthread_t tid[MAXTHREADS];
/* Get input arguments */
if (argc != 3) {
printf("Usage: %s <nthreads> <log_nelems>\n", argv[0]);
exit(0);
}
nthreads = atoi(argv[1]);
log_nelems = atoi(argv[2]);
nelems = (1L << log_nelems);
/* Check input arguments */
if ((nelems % nthreads) != 0 || (log_nelems > 31)) {
printf("Error: invalid nelems\n");
exit(0);
}
nelems_per_thread = nelems / nthreads;
/* Create peer threads and wait for them to finish */
for (i = 0; i < nthreads; i++) { //line:conc:psumarray:createloop1
myid[i] = i; //line:conc:psumarray:createloop2
Pthread_create(&tid[i], NULL, sum_array, &myid[i]); //line:conc:psumarray:createloop3
} //line:conc:psumarray:createloop4
for (i = 0; i < nthreads; i++) //line:conc:psumarray:waitloop1
Pthread_join(tid[i], NULL); //line:conc:psumarray:waitloop2
/* Add up the partial sums computed by each thread */
for (i = 0; i < nthreads; i++) //line:conc:psumarray:sumloop1
result += psum[i]; //line:conc:psumarray:sumloop2
/* Check final answer */
if (result != (nelems * (nelems-1))/2) //line:conc:psumarray:check1
printf("Error: result=%ld\n", result); //line:conc:psumarray:check2
exit(0);
}
/* $begin psumarraythread */
/* Thread routine for psum-array.c */
void *sum_array(void *vargp)
{
long myid = *((long *)vargp); /* Extract the thread ID */ //line:conc:psumarray:extractid
long start = myid * nelems_per_thread; /* Start element index */ //line:conc:psumarray:getstart
long end = start + nelems_per_thread; /* End element index */ //line:conc:psumarray:getend
long i;
for (i = start; i < end; i++) { //line:conc:psumarray:threadsumloop1
psum[myid] += i; //line:conc:psumarray:threadsumloop2
} //line:conc:psumarray:threadsumloop3
return NULL;
}
/* $end psumarraythread */