-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib-naive.c
323 lines (276 loc) · 7.33 KB
/
fib-naive.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#define NESTED_FUNCTION 0
#if defined(_WIN32)
# include <windows.h>
#elif (defined(__APPLE__) && defined(__MACH__)) \
|| defined(__DragonFly__) || defined(__FreeBSD__) \
|| defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/param.h>
# include <sys/sysctl.h>
# define USE_SYSCTL
#else
# include <unistd.h>
#endif
/* Return the number of CPU of the system */
int get_cpu_count(void)
{
#if defined(_WIN32)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif defined(USE_SYSCTL)
int nm[2];
int count = 0;
size_t len = sizeof (count);
nm[0] = CTL_HW;
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
return MAX(1, count);
#elif defined (_SC_NPROCESSORS_ONLN)
return sysconf(_SC_NPROCESSORS_ONLN);
#else
return 1;
#endif
}
static int
cputime (void)
{
#if 0
/* Wrong for multi thread code since it measures the time spent
by all threads not the real time */
struct rusage rus;
getrusage (0, &rus);
return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
/**********************************************************************/
#define MAX_THREAD 32
typedef enum {
TERMINATE_THREAD = -1,
WAITING_FOR_DATA = 0,
THREAD_RUNNING = 1
} workstate_t;
typedef struct {
int num_spawn;
int num_terminated_spawn;
} spawn_block_t[1];
typedef struct {
pthread_t idx;
pthread_mutex_t mutex;
pthread_cond_t cond;
workstate_t working;
volatile int * num_spawn_ptr;
void * data;
void (*func) (void *data);
} mt_comm_t;
typedef struct {
int is_initialized;
int num_thread;
size_t size;
pthread_mutex_t master_mutex;
pthread_cond_t master_cond;
mt_comm_t comm[MAX_THREAD];
} mt_t;
/* Define shared data between thread */
mt_t mt_g;
/* Code of the main function of each working thread */
static void *mt_thread_main (void *arg)
{
mt_comm_t *mc = arg;
pthread_mutex_lock(&mc->mutex);
while (1) {
/* Wait for data to process... */
/* If not working, sleep until wake up */
if (mc->working <= WAITING_FOR_DATA) {
/* Check for terminate condition */
if (mc->working == TERMINATE_THREAD) {
break;
}
/* Wait for next cycle */
pthread_cond_wait(&mc->cond, &mc->mutex);
continue;
}
pthread_mutex_unlock(&mc->mutex);
/* Execute thread */
(*mc->func) (mc->data);
/* Unwork thread */
pthread_mutex_lock(&mc->mutex);
mc->working = WAITING_FOR_DATA;
/* Enter Signal terminaison block */
pthread_mutex_lock (&mt_g.master_mutex);
/* Signal terminaison */
*mc->num_spawn_ptr += 1;
pthread_cond_broadcast(&mt_g.master_cond);
pthread_mutex_unlock (&mt_g.master_mutex);
}
pthread_mutex_unlock(&mc->mutex);
return NULL;
}
/* TODO: Support failure in initialization */
void thread_init(int num_thread, size_t size)
{
int rc;
/* Initialize global memory for MT handling */
memset(&mt_g, 0, sizeof mt_g);
mt_g.size = size;
/* Initialize global mutex */
rc = pthread_mutex_init(&mt_g.master_mutex, NULL);
if (rc != 0)
abort();
rc = pthread_cond_init(&mt_g.master_cond, NULL);
if (rc != 0)
abort();
/* Initialize threads */
mt_g.num_thread = num_thread - 1;
for (int i = 0 ; i < num_thread-1; i++) {
rc = pthread_mutex_init(&mt_g.comm[i].mutex, NULL);
if (rc != 0)
abort();
rc = pthread_cond_init(&mt_g.comm[i].cond, NULL);
if (rc != 0)
abort();
rc = pthread_create (&mt_g.comm[i].idx,
NULL, mt_thread_main, &mt_g.comm[i]);
if (rc != 0)
abort();
}
mt_g.is_initialized = 1;
}
int thread_quit(void)
{
int rc;
int previous = mt_g.num_thread+1;
/* If the thread system has been initialized */
if (mt_g.is_initialized != 0) {
for(int i = 0; i < mt_g.num_thread ; i++) {
mt_comm_t *mc = &mt_g.comm[i];
/* Request terminaison */
rc = pthread_mutex_lock (&mc->mutex);
assert (rc == 0);
assert (mc->working == WAITING_FOR_DATA);
mc->working = TERMINATE_THREAD; /* Request terminaison */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
/* Join it to terminate it */
rc = pthread_join(mt_g.comm[i].idx, NULL);
assert (rc == 0);
/* mutex_destroy needs mutex to be unlocked */
rc = pthread_mutex_destroy(&mt_g.comm[i].mutex);
assert (rc == 0);
rc = pthread_cond_destroy(&mt_g.comm[i].cond);
assert (rc == 0);
}
/* mutex_destroy needs mutex to be unlocked */
rc = pthread_mutex_destroy(&mt_g.master_mutex);
assert (rc == 0);
rc = pthread_cond_destroy(&mt_g.master_cond);
assert (rc == 0);
mt_g.is_initialized = 0;
mt_g.num_thread = 0;
}
return previous;
}
void spawn_start(spawn_block_t block)
{
block->num_spawn = 0;
block->num_terminated_spawn = 0;
}
void spawn (spawn_block_t block, void (*func)(void *data), void *data)
{
for(int i = 0; i < mt_g.num_thread ; i++) {
mt_comm_t *mc = &mt_g.comm[i];
/* If the thread is not working */
if (mc->working == WAITING_FOR_DATA) {
pthread_mutex_lock (&mc->mutex);
/* If the thread is still not working */
if ( (mc->working != WAITING_FOR_DATA)) {
pthread_mutex_unlock (&mc->mutex);
continue;
}
/* Setup data for the thread */
mc->working = THREAD_RUNNING;
mc->func = func;
mc->data = data;
mc->num_spawn_ptr = &block->num_terminated_spawn;
block->num_spawn +=1;
/* Signal to thread that some work are available */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
return ;
}
}
/* No thread available. Call the function ourself */
(*func) (data);
}
void spawn_sync(spawn_block_t block)
{
/* If the number of spawns is greated than the number
of terminated spawns, some spawns are still working.
So wait for terminaison */
if (block->num_spawn > block->num_terminated_spawn) {
pthread_mutex_lock (&mt_g.master_mutex);
while (1) {
if (block->num_spawn == block->num_terminated_spawn)
break;
pthread_cond_wait(&mt_g.master_cond, &mt_g.master_mutex);
}
pthread_mutex_unlock (&mt_g.master_mutex);
}
}
/***************************************************************************/
int fib(int n);
struct fib2_s {
int x, n;
};
#if NESTED_FUNCTION == 0
static void subfunc_1 (void *data) {
struct fib2_s *f = data;
f->x = fib (f->n );
}
#endif
/* Compute Fibonacci number using thread systems. */
int fib(int n)
{
if (n < 2)
return n;
struct fib2_s f;
spawn_block_t b;
spawn_start(b);
f.n = n - 2;
#if NESTED_FUNCTION == 0
spawn (b, subfunc_1, &f);
#else
void subfunc_2 (void *data) {
f.x = fib(f.n);
}
spawn (b, subfunc_2, &f);
#endif
int y = fib (n-1);
spawn_sync(b);
return f.x + y;
}
int main()
{
int worker = get_cpu_count();
thread_init(worker, 0);
int n = 39;
int start = cputime();
int result = fib(n);
int end = cputime();
// Display our results
double duration = (double)(end - start) / 1000;
printf("Fibonacci number #%d is %d.\n", n, result);
printf("Calculated in %.3f seconds using %d workers.\n",
duration, worker);
return 0;
}