|
| 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