-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumreduction.c
180 lines (149 loc) · 3.88 KB
/
sumreduction.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
#define ARRAY_SIZE 1000000000
typedef struct {
int nextid;
pthread_mutex_t nextid_mutex;
int* array;
int* sum;
int num_threads;
int nums_per_thread;
int counter;
pthread_mutex_t counter_mutex;
sem_t* barrier_sem;
} Shared;
int sem_post_n(sem_t *sem, int n) {
int i, ret;
for (i = 0; i<n; i++) {
ret = sem_post(sem);
if (ret) exit(-1);
}
return ret;
}
void synch(Shared *state, int num_threads) {
pthread_mutex_lock(&(state->counter_mutex));
if(state->counter == (num_threads)-1) {
// last thread
state->counter = 0;
int i;
for (i = 0; i < (num_threads - 1); i++) {
sem_post(state->barrier_sem);
}
pthread_mutex_unlock(&(state->counter_mutex));
} else {
// not last thread
++(state->counter);
pthread_mutex_unlock(&(state->counter_mutex));
sem_wait(state->barrier_sem);
}
}
void * kernel( void* state_param ) {
Shared *state = (Shared*) state_param;
pthread_mutex_lock(&(state->nextid_mutex));
int pn = state->nextid++;
pthread_mutex_unlock(&(state->nextid_mutex));
struct timeval tv1, tv2;
if (pn == 0) {
//start measuring time
gettimeofday(&tv1, NULL);
}
state->sum[pn] = 0;
int i=0;
for (i = state->nums_per_thread * pn; i < state->nums_per_thread * (pn+1); i++) {
state->sum[pn] += state->array[i];
}
//reduction
int half = state->num_threads;
int synch_i = 1;
while (half > 1) {
synch(state, half);
if (half%2 != 0 && pn == 0) {
state->sum[0] = state->sum[0] + state->sum[half-1];
}
half = half/2;
if (pn < half) {
state->sum[pn] = state->sum[pn] + state->sum[pn+half];
} else {
break;
}
}
if (pn == 0) {
//stop measuring time
gettimeofday(&tv2, NULL);
printf (";%f",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
fprintf(stderr,"Sum = %d\n", state->sum[0]);
}
return 0;
}
int genval(int seed) {
switch(seed % 3) {
case 0: return (seed%2 ? seed%5 : 5-(seed%5))+seed/100;
case 1: return (seed%2 ? 7-(seed%7) : seed%7)+seed/1000;
case 2: return (seed%2 ? seed%11 : -(11-(seed%11)))+seed/10000;
}
return 0; //nowarn
}
int is_base_two(int num) {
int cmp = 1;
while (cmp <= num) {
if (cmp == num) return 1;
cmp <<= 1;
}
return 0;
}
int main(int argc, char** argv) {
/*if (argc != 2) {
fprintf(stderr, "Please specify number of threads\n");
return -1;
}
int num_threads = atoi(argv[1]);
if (ARRAY_SIZE % num_threads) {
fprintf(stderr, "Please specify number of threads submultiple of %d\n", ARRAY_SIZE);
return -2;
}*/
int* array = malloc(ARRAY_SIZE * sizeof(int));
int i;
for (i=0; i < ARRAY_SIZE; i++) {
array[i] = genval(i);
}
int num_threads;
for (num_threads = 1; num_threads<=128; num_threads++) {
if ((num_threads%10) != 0 && !is_base_two(num_threads)) {
continue;
}
fprintf(stderr, "%d threads\n", num_threads);
printf("%d;", num_threads);
int run;
for (run=0; run < 10; run++) {
fprintf(stderr, "run %d\n", run);
Shared shared_state;
shared_state.array = array;
shared_state.nextid = 0;
pthread_mutex_init(&(shared_state.nextid_mutex), NULL);
shared_state.nums_per_thread = (ARRAY_SIZE + (num_threads-1))/num_threads;
shared_state.num_threads = num_threads;
shared_state.counter = 0;
pthread_mutex_init(&(shared_state.counter_mutex), NULL);
shared_state.barrier_sem = sem_open("/barrier_sem", O_CREAT|O_EXCL, S_IRWXU , 0);
shared_state.sum = malloc(num_threads * sizeof(int));
pthread_t* children = malloc(num_threads * sizeof(pthread_t));
for (i=0; i < num_threads; i++) {
pthread_create (&children[i], NULL, kernel, (void *) &shared_state) ;
}
for (i=num_threads-1; i >= 0; i--) {
pthread_join(children[i], NULL) ;
}
free(shared_state.sum);
sem_close(shared_state.barrier_sem);
sem_unlink("/barrier_sem");
}
printf("\n");
}
free(array);
return 0;
}