Newer
Older
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
/*
* Parallel fibonacci.
*
* This is just a toy program to see how well the
* underlying FJ framework supports extremely fine-grained
* tasks.
*
* Written by G. Back for CS3214 Fall 2014.
*/
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include "threadpool.h"
#include "threadpool_lib.h"
/* Data to be passed to callable. */
struct problem_parameters {
unsigned n;
};
static void *
fibonacci(struct thread_pool * pool, void * _data)
{
struct problem_parameters * p = _data;
if (p->n <= 1)
return (void *) 1;
struct problem_parameters left_half = { .n = p->n - 1 };
struct problem_parameters right_half = { .n = p->n - 2 };
struct future * f = thread_pool_submit(pool, fibonacci, &right_half);
uintptr_t lresult = (uintptr_t) fibonacci(pool, &left_half);
uintptr_t rresult = (uintptr_t) future_get(f);
future_free(f);
return (void *)(lresult + rresult);
}
static void usage(char *av0, int nthreads) {
fprintf(stderr, "Usage: %s [-d <n>] [-n <n>] <N>\n"
" -n number of threads in pool, default %d\n"
, av0, nthreads);
exit(0);
}
int
main(int ac, char *av[])
{
int nthreads = 4;
int c;
while ((c = getopt(ac, av, "n:h")) != EOF) {
switch (c) {
case 'n':
nthreads = atoi(optarg);
break;
case 'h':
usage(av[0], nthreads);
}
}
if (optind == ac)
usage(av[0], nthreads);
int n = atoi(av[optind]);
struct thread_pool * pool = thread_pool_new(nthreads);
struct problem_parameters roottask = { .n = n };
unsigned long long F[n+1];
F[0] = F[1] = 1;
int i;
for (i = 2; i < n + 1; i++) {
F[i] = F[i-1] + F[i-2];
}
printf("starting...\n");
struct benchmark_data* bdata = start_benchmark();
struct future *f = thread_pool_submit(pool, fibonacci, &roottask);
unsigned long long Fvalue = (unsigned long long) future_get(f);
stop_benchmark(bdata);
future_free(f);
if (Fvalue != F[n]) {
printf("result %lld should be %lld\n", Fvalue, F[n]);
abort();
} else {
printf("result ok.\n");
report_benchmark_results(bdata);
}
thread_pool_shutdown_and_destroy(pool);
return 0;
}