-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib-worker.c
76 lines (62 loc) · 1.4 KB
/
fib-worker.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include "m-worker.h"
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
}
/***************************************************************************/
worker_t w_g;
int fib(int n);
struct fib2_s {
int x, n;
};
static void subfunc_1 (void *data) {
struct fib2_s *f = data;
f->x = fib (f->n );
}
/* Compute Fibonacci number using thread systems. */
int fib(int n)
{
if (n < 2)
return n;
struct fib2_s f;
worker_sync_t b;
worker_start(b, w_g);
f.n = n - 2;
worker_spawn (b, subfunc_1, &f);
int y = fib (n-1);
worker_sync(b);
return f.x + y;
}
int main()
{
worker_init(w_g, 0, 0, NULL);
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 with %lu workers.\n", duration,
worker_count(w_g) );
worker_clear(w_g);
return 0;
}