int main(int argc, char *argv[]) { int ret; ABT_xstream xstream; /* init and thread creation */ ABT_test_init(argc, argv); ret = ABT_xstream_self(&xstream); ABT_TEST_ERROR(ret, "ABT_xstream_self"); /* Get the pools attached to an execution stream */ ABT_pool pool; ret = ABT_xstream_get_main_pools(xstream, 1, &pool); ABT_TEST_ERROR(ret, "ABT_xstream_get_main_pools"); ret = ABT_thread_create(pool, fn1, NULL, ABT_THREAD_ATTR_NULL, &th1); ABT_TEST_ERROR(ret, "ABT_thread_create"); ret = ABT_thread_create(pool, fn2, NULL, ABT_THREAD_ATTR_NULL, &th2); ABT_TEST_ERROR(ret, "ABT_thread_create"); ret = ABT_thread_create(pool, fn3, NULL, ABT_THREAD_ATTR_NULL, &th3); ABT_TEST_ERROR(ret, "ABT_thread_create"); ret = ABT_eventual_create(EVENTUAL_SIZE, &myeventual); ABT_TEST_ERROR(ret, "ABT_eventual_create"); ABT_test_printf(1, "START\n"); void *data; ABT_test_printf(1, "Thread main iteration %d waiting for eventual\n", 0); ABT_eventual_wait(myeventual,&data); ABT_test_printf(1, "Thread main continue iteration %d returning from " "eventual\n", 0); /* switch to other user-level threads */ ABT_thread_yield(); /* join other threads */ ret = ABT_thread_join(th1); ABT_TEST_ERROR(ret, "ABT_thread_join"); ret = ABT_thread_join(th2); ABT_TEST_ERROR(ret, "ABT_thread_join"); ret = ABT_thread_join(th3); ABT_TEST_ERROR(ret, "ABT_thread_join"); ABT_test_printf(1, "END\n"); ret = ABT_test_finalize(0); return ret; }
void thread_create(void *arg) { int rank, i, ret; ABT_thread self; ABT_thread_id id; ABT_pool my_pool; ABT_thread *threads; assert((size_t)arg == 0); ret = ABT_xstream_self_rank(&rank); ABT_TEST_ERROR(ret, "ABT_xstream_self_rank"); ret = ABT_thread_self(&self); ABT_TEST_ERROR(ret, "ABT_thread_self"); ret = ABT_thread_get_id(self, &id); ABT_TEST_ERROR(ret, "ABT_thread_get_id"); ret = ABT_thread_get_last_pool(self, &my_pool); ABT_TEST_ERROR(ret, "ABT_thread_get_last_pool"); threads = (ABT_thread *)malloc(num_threads * sizeof(ABT_thread)); /* Create ULTs */ for (i = 0; i < num_threads; i++) { ret = ABT_thread_create(my_pool, thread_func, (void *)1, ABT_THREAD_ATTR_NULL, &threads[i]); ABT_TEST_ERROR(ret, "ABT_thread_create"); } ABT_test_printf(1, "[U%lu:E%u]: created %d ULTs\n", id, rank, num_threads); /* Join ULTs */ for (i = 0; i < num_threads; i++) { ret = ABT_thread_join(threads[i]); ABT_TEST_ERROR(ret, "ABT_thread_join"); } ABT_test_printf(1, "[U%lu:E%u]: joined %d ULTs\n", id, rank, num_threads); /* Revive ULTs with a different function */ for (i = 0; i < num_threads; i++) { ret = ABT_thread_revive(my_pool, thread_func2, (void *)2, &threads[i]); ABT_TEST_ERROR(ret, "ABT_thread_revive"); } ABT_test_printf(1, "[U%lu:E%u]: revived %d ULTs\n", id, rank, num_threads); /* Join and free ULTs */ for (i = 0; i < num_threads; i++) { ret = ABT_thread_free(&threads[i]); ABT_TEST_ERROR(ret, "ABT_thread_free"); } ABT_test_printf(1, "[U%lu:E%u]: freed %d ULTs\n", id, rank, num_threads); free(threads); }
void sighandler(int sig) { printf("%s\n", "Caught signal for terminating..."); printf("%s\n", "Joining threads..."); int i; /* join other threads */ for(i = 0; i < num_threads;i++){ ABT_thread_join(threads[i]); ABT_thread_free(&threads[i]); } printf("%s\n", "Joining streams..."); /* join ESs */ for (i = 1; i < CORES; i++) { ABT_xstream_join(xstreams[i]); ABT_xstream_free(&xstreams[i]); } ABT_finalize(); abt_io_finalize(abtio); free(xstreams); exit(0); }
/* Main function */ int main(int argc, char *argv[]) { int n, i, expected; int num_xstreams; ABT_xstream *xstreams; ABT_thread thread; 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]); ABT_xstream_start(xstreams[i]); } args.n = n; args.future = ABT_FUTURE_NULL; ABT_thread_create(g_pool, fibonacci, &args, ABT_THREAD_ATTR_NULL, &thread); /* join the thread */ ABT_thread_join(thread); ABT_thread_free(&thread); /* 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; }
/* Main function */ int main(int argc, char *argv[]) { int n, i, result, expected; int num_xstreams; ABT_xstream *xstreams; ABT_thread thread; thread_args args_thread; ABT_task task; task_args *args_task; 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); if (n <= 2) { result = 1; goto fn_result; } /* 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]); ABT_xstream_start(xstreams[i]); } /* creating thread */ args_thread.n = n - 1; args_thread.eventual = ABT_EVENTUAL_NULL; ABT_thread_create(g_pool, fibonacci_thread, &args_thread, ABT_THREAD_ATTR_NULL, &thread); /* creating task */ args_task = (task_args *)malloc(sizeof(task_args)); args_task->n = n - 2; args_task->result = 0; ABT_mutex_create(&args_task->mutex); args_task->parent = NULL; ABT_task_create(g_pool, fibonacci_task, args_task, &task); /* switch to other user-level threads */ ABT_thread_yield(); /* join other threads */ ABT_thread_join(thread); ABT_thread_free(&thread); /* join ESs */ for (i = 1; i < num_xstreams; i++) { ABT_xstream_join(xstreams[i]); ABT_xstream_free(&xstreams[i]); } result = args_thread.result + args_task->result; free(args_task); ABT_finalize(); free(xstreams); fn_result: printf("Fib(%d): %d\n", n, result); expected = verify(n); if (result != expected) { fprintf(stderr, "ERROR: expected=%d\n", expected); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }