Skip to content

Commit 9b945a6

Browse files
Merge pull request #164 from shintaro-iwasaki/FixExamples
Fix examples
2 parents b617d77 + 8192312 commit 9b945a6

File tree

10 files changed

+143
-489
lines changed

10 files changed

+143
-489
lines changed

examples/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# examples
22
fibonacci_future
3-
fibonacci_task
4-
fibonacci_thread_task
3+
fibonacci_recursive
54
hello_world
65
hello_world_thread
76
sched_predef

examples/Makefile.am

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ TESTS = \
77
hello_world \
88
hello_world_thread \
99
fibonacci_future \
10-
fibonacci_task \
11-
fibonacci_thread_task \
10+
fibonacci_recursive \
1211
sched_predef \
1312
sched_shared_pool \
1413
sched_stack \
@@ -26,8 +25,7 @@ include $(top_srcdir)/test/Makefile.mk
2625
hello_world_SOURCES = hello_world.c
2726
hello_world_thread_SOURCES = hello_world_thread.c
2827
fibonacci_future_SOURCES = fibonacci_future.c
29-
fibonacci_task_SOURCES = fibonacci_task.c
30-
fibonacci_thread_task_SOURCES = fibonacci_thread_task.c
28+
fibonacci_recursive_SOURCES = fibonacci_recursive.c
3129
sched_predef_SOURCES = sched_predef.c
3230
sched_shared_pool_SOURCES = sched_shared_pool.c
3331
sched_stack_SOURCES = sched_stack.c
@@ -41,8 +39,7 @@ testing:
4139
./hello_world
4240
./hello_world_thread
4341
./fibonacci_future
44-
./fibonacci_task
45-
./fibonacci_thread_task
42+
./fibonacci_recursive
4643
./sched_predef
4744
./sched_shared_pool
4845
./sched_stack

examples/fibonacci_future.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ int main(int argc, char *argv[])
120120
for (i = 1; i < num_xstreams; i++) {
121121
ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1, &g_pool,
122122
ABT_SCHED_CONFIG_NULL, &xstreams[i]);
123-
ABT_xstream_start(xstreams[i]);
124123
}
125124

126125
args.n = n;

examples/fibonacci_recursive.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2+
/*
3+
* See COPYRIGHT in top-level directory.
4+
*/
5+
6+
/**
7+
* This Fibonacci example showcases recursive parallelism.
8+
*/
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <abt.h>
14+
15+
#define N 10
16+
#define NUM_XSTREAMS 4
17+
18+
/* global variables */
19+
ABT_pool g_pool = ABT_POOL_NULL;
20+
21+
/* structure to pass arguments to threads */
22+
typedef struct {
23+
int n;
24+
int result;
25+
} thread_args;
26+
27+
/* Function to compute Fibonacci numbers */
28+
void fibonacci(void *arguments)
29+
{
30+
thread_args *args = (thread_args *)arguments;
31+
int n = args->n;
32+
33+
/* checking for base cases */
34+
if (n <= 2)
35+
args->result = 1;
36+
else {
37+
thread_args a1, a2;
38+
ABT_thread thread1;
39+
40+
a1.n = n - 1;
41+
ABT_thread_create(g_pool, fibonacci, &a1, ABT_THREAD_ATTR_NULL,
42+
&thread1);
43+
a2.n = n - 2;
44+
fibonacci(&a2);
45+
46+
ABT_thread_free(&thread1);
47+
args->result = a1.result + a2.result;
48+
}
49+
}
50+
51+
/* Verification function */
52+
int verify(int n)
53+
{
54+
int i;
55+
int old[2], val;
56+
57+
if (n <= 2)
58+
return 1;
59+
60+
old[0] = old[1] = 1;
61+
for (i = 3; i <= n; i++) {
62+
val = old[0] + old[1];
63+
old[i % 2] = val;
64+
}
65+
return val;
66+
}
67+
68+
/* Main function */
69+
int main(int argc, char *argv[])
70+
{
71+
int n, i, expected;
72+
int num_xstreams;
73+
ABT_xstream *xstreams;
74+
thread_args args;
75+
76+
if (argc > 1 && strcmp(argv[1], "-h") == 0) {
77+
printf("Usage: %s [N=10] [num_ES=4]\n", argv[0]);
78+
return EXIT_SUCCESS;
79+
}
80+
n = argc > 1 ? atoi(argv[1]) : N;
81+
num_xstreams = argc > 2 ? atoi(argv[2]) : NUM_XSTREAMS;
82+
printf("# of ESs: %d\n", num_xstreams);
83+
84+
/* initialization */
85+
ABT_init(argc, argv);
86+
87+
/* shared pool creation */
88+
ABT_pool_create_basic(ABT_POOL_FIFO, ABT_POOL_ACCESS_MPMC, ABT_TRUE,
89+
&g_pool);
90+
91+
/* ES creation */
92+
xstreams = (ABT_xstream *)malloc(sizeof(ABT_xstream) * num_xstreams);
93+
ABT_xstream_self(&xstreams[0]);
94+
ABT_xstream_set_main_sched_basic(xstreams[0], ABT_SCHED_DEFAULT, 1,
95+
&g_pool);
96+
for (i = 1; i < num_xstreams; i++) {
97+
ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1, &g_pool,
98+
ABT_SCHED_CONFIG_NULL, &xstreams[i]);
99+
}
100+
101+
args.n = n;
102+
fibonacci(&args);
103+
104+
/* join ESs */
105+
for (i = 1; i < num_xstreams; i++) {
106+
ABT_xstream_join(xstreams[i]);
107+
ABT_xstream_free(&xstreams[i]);
108+
}
109+
110+
ABT_finalize();
111+
112+
free(xstreams);
113+
114+
printf("Fib(%d): %d\n", n, args.result);
115+
expected = verify(n);
116+
if (args.result != expected) {
117+
fprintf(stderr, "ERROR: expected=%d\n", expected);
118+
exit(EXIT_FAILURE);
119+
}
120+
121+
return EXIT_SUCCESS;
122+
}

0 commit comments

Comments
 (0)