Skip to content

Commit

Permalink
Merge pull request #164 from shintaro-iwasaki/FixExamples
Browse files Browse the repository at this point in the history
Fix examples
  • Loading branch information
shintaro-iwasaki authored Mar 31, 2020
2 parents b617d77 + 8192312 commit 9b945a6
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 489 deletions.
3 changes: 1 addition & 2 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# examples
fibonacci_future
fibonacci_task
fibonacci_thread_task
fibonacci_recursive
hello_world
hello_world_thread
sched_predef
Expand Down
9 changes: 3 additions & 6 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ TESTS = \
hello_world \
hello_world_thread \
fibonacci_future \
fibonacci_task \
fibonacci_thread_task \
fibonacci_recursive \
sched_predef \
sched_shared_pool \
sched_stack \
Expand All @@ -26,8 +25,7 @@ include $(top_srcdir)/test/Makefile.mk
hello_world_SOURCES = hello_world.c
hello_world_thread_SOURCES = hello_world_thread.c
fibonacci_future_SOURCES = fibonacci_future.c
fibonacci_task_SOURCES = fibonacci_task.c
fibonacci_thread_task_SOURCES = fibonacci_thread_task.c
fibonacci_recursive_SOURCES = fibonacci_recursive.c
sched_predef_SOURCES = sched_predef.c
sched_shared_pool_SOURCES = sched_shared_pool.c
sched_stack_SOURCES = sched_stack.c
Expand All @@ -41,8 +39,7 @@ testing:
./hello_world
./hello_world_thread
./fibonacci_future
./fibonacci_task
./fibonacci_thread_task
./fibonacci_recursive
./sched_predef
./sched_shared_pool
./sched_stack
Expand Down
1 change: 0 additions & 1 deletion examples/fibonacci_future.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ int main(int argc, char *argv[])
for (i = 1; i < num_xstreams; i++) {
ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1, &g_pool,
ABT_SCHED_CONFIG_NULL, &xstreams[i]);
ABT_xstream_start(xstreams[i]);
}

args.n = n;
Expand Down
122 changes: 122 additions & 0 deletions examples/fibonacci_recursive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
* See COPYRIGHT in top-level directory.
*/

/**
* This Fibonacci example showcases recursive parallelism.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <abt.h>

#define N 10
#define NUM_XSTREAMS 4

/* global variables */
ABT_pool g_pool = ABT_POOL_NULL;

/* structure to pass arguments to threads */
typedef struct {
int n;
int result;
} thread_args;

/* Function to compute Fibonacci numbers */
void fibonacci(void *arguments)
{
thread_args *args = (thread_args *)arguments;
int n = args->n;

/* checking for base cases */
if (n <= 2)
args->result = 1;
else {
thread_args a1, a2;
ABT_thread thread1;

a1.n = n - 1;
ABT_thread_create(g_pool, fibonacci, &a1, ABT_THREAD_ATTR_NULL,
&thread1);
a2.n = n - 2;
fibonacci(&a2);

ABT_thread_free(&thread1);
args->result = a1.result + a2.result;
}
}

/* Verification function */
int verify(int n)
{
int i;
int old[2], val;

if (n <= 2)
return 1;

old[0] = old[1] = 1;
for (i = 3; i <= n; i++) {
val = old[0] + old[1];
old[i % 2] = val;
}
return val;
}

/* Main function */
int main(int argc, char *argv[])
{
int n, i, expected;
int num_xstreams;
ABT_xstream *xstreams;
thread_args args;

if (argc > 1 && strcmp(argv[1], "-h") == 0) {
printf("Usage: %s [N=10] [num_ES=4]\n", argv[0]);
return EXIT_SUCCESS;
}
n = argc > 1 ? atoi(argv[1]) : N;
num_xstreams = argc > 2 ? atoi(argv[2]) : NUM_XSTREAMS;
printf("# of ESs: %d\n", num_xstreams);

/* initialization */
ABT_init(argc, argv);

/* shared pool creation */
ABT_pool_create_basic(ABT_POOL_FIFO, ABT_POOL_ACCESS_MPMC, ABT_TRUE,
&g_pool);

/* ES creation */
xstreams = (ABT_xstream *)malloc(sizeof(ABT_xstream) * num_xstreams);
ABT_xstream_self(&xstreams[0]);
ABT_xstream_set_main_sched_basic(xstreams[0], ABT_SCHED_DEFAULT, 1,
&g_pool);
for (i = 1; i < num_xstreams; i++) {
ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1, &g_pool,
ABT_SCHED_CONFIG_NULL, &xstreams[i]);
}

args.n = n;
fibonacci(&args);

/* join ESs */
for (i = 1; i < num_xstreams; i++) {
ABT_xstream_join(xstreams[i]);
ABT_xstream_free(&xstreams[i]);
}

ABT_finalize();

free(xstreams);

printf("Fib(%d): %d\n", n, args.result);
expected = verify(n);
if (args.result != expected) {
fprintf(stderr, "ERROR: expected=%d\n", expected);
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;
}
Loading

0 comments on commit 9b945a6

Please sign in to comment.