From 46ab5e42be9b676242e3a7aef8748b08ac52a303 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 26 Apr 2014 15:09:01 -0400 Subject: future: Add to benchmark. --- libdimension/bench/future.c | 93 +++++++++++++++++++++++++++++++++------------ libdimension/future.c | 20 +++++++--- 2 files changed, 83 insertions(+), 30 deletions(-) (limited to 'libdimension') diff --git a/libdimension/bench/future.c b/libdimension/bench/future.c index 0e3acc6..f42430e 100644 --- a/libdimension/bench/future.c +++ b/libdimension/bench/future.c @@ -25,8 +25,28 @@ #define ITERATIONS 100000 +static void +dmnsn_bench_future_increment(void) +{ + dmnsn_future *future = dmnsn_new_future(); + dmnsn_future_set_total(future, ITERATIONS); + + sandglass_t sandglass; + if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { + perror("sandglass_create()"); + exit(EXIT_FAILURE); + } + + /* Benchmark the increment operation. */ + sandglass_bench_fine(&sandglass, dmnsn_future_increment(future)); + printf("dmnsn_future_increment(): %ld\n", sandglass.grains); + + dmnsn_delete_future(future); +} + static int -dmnsn_bench_future(void *ptr, unsigned int thread, unsigned int nthreads) +dmnsn_bench_future_ccthread(void *ptr, unsigned int thread, + unsigned int nthreads) { dmnsn_future *future = (dmnsn_future *)ptr; for (int i = 0; i < ITERATIONS; ++i) { @@ -36,42 +56,29 @@ dmnsn_bench_future(void *ptr, unsigned int thread, unsigned int nthreads) } static int -dmnsn_bench_thread(void *ptr) +dmnsn_bench_future_thread(void *ptr) { dmnsn_future *future = (dmnsn_future *)ptr; size_t nthreads = 2*dmnsn_ncpus(); dmnsn_future_set_total(future, nthreads*ITERATIONS); - sandglass_t sandglass; - if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { - perror("sandglass_create()"); - return EXIT_FAILURE; - } - - /* Benchmark the increment operation. */ - sandglass_bench_fine(&sandglass, dmnsn_future_increment(future)); - printf("dmnsn_future_increment(): %ld\n", sandglass.grains); - - /* Reset the progress. */ - dmnsn_lock_mutex(&future->mutex); - future->progress = 0; - dmnsn_unlock_mutex(&future->mutex); - /* Now run a bunch of increments concurrently. */ - return dmnsn_execute_concurrently(future, &dmnsn_bench_future, future, - nthreads); + return dmnsn_execute_concurrently(future, dmnsn_bench_future_ccthread, + future, nthreads); } -int -main(void) +static void +dmnsn_bench_future_concurrent(void) { - dmnsn_timer timer1, timer2; + printf("\nNo pausing:\n"); + dmnsn_future *future = dmnsn_new_future(); + dmnsn_timer timer1, timer2; dmnsn_timer_start(&timer1); - dmnsn_timer_start(&timer2); + timer2 = timer1; - dmnsn_new_thread(future, &dmnsn_bench_thread, future); + dmnsn_new_thread(future, dmnsn_bench_future_thread, future); dmnsn_future_wait(future, 0.5); dmnsn_timer_stop(&timer1); @@ -81,3 +88,41 @@ main(void) dmnsn_timer_stop(&timer2); printf("100%%: " DMNSN_TIMER_FORMAT "\n", DMNSN_TIMER_PRINTF(timer2)); } + +static void +dmnsn_bench_future_pausing(void) +{ + printf("\nWith pausing:\n"); + + dmnsn_future *future = dmnsn_new_future(); + bool hit_fifty = false; + + dmnsn_timer timer1, timer2; + dmnsn_timer_start(&timer1); + timer2 = timer1; + + dmnsn_new_thread(future, dmnsn_bench_future_thread, future); + + while (!dmnsn_future_is_done(future)) { + dmnsn_future_pause(future); + if (dmnsn_future_progress(future) >= 0.5 && !hit_fifty) { + dmnsn_timer_stop(&timer1); + printf(" 50%%: " DMNSN_TIMER_FORMAT "\n", DMNSN_TIMER_PRINTF(timer1)); + hit_fifty = true; + } + dmnsn_future_resume(future); + } + + dmnsn_future_join(future); + dmnsn_timer_stop(&timer2); + printf("100%%: " DMNSN_TIMER_FORMAT "\n", DMNSN_TIMER_PRINTF(timer2)); +} + +int +main(void) +{ + dmnsn_bench_future_increment(); + dmnsn_bench_future_concurrent(); + dmnsn_bench_future_pausing(); + return EXIT_SUCCESS; +} diff --git a/libdimension/future.c b/libdimension/future.c index c344a8e..f62c01d 100644 --- a/libdimension/future.c +++ b/libdimension/future.c @@ -55,6 +55,19 @@ dmnsn_new_future(void) return future; } +static void +dmnsn_delete_future(dmnsn_future *future) +{ + if (future) { + dmnsn_destroy_cond(&future->resume_cond); + dmnsn_destroy_cond(&future->all_running_cond); + dmnsn_destroy_cond(&future->none_running_cond); + dmnsn_destroy_cond(&future->cond); + dmnsn_destroy_mutex(&future->mutex); + dmnsn_free(future); + } +} + /* Join the worker thread and delete `future'. */ int dmnsn_future_join(dmnsn_future *future) @@ -73,12 +86,7 @@ dmnsn_future_join(dmnsn_future *future) } /* Free the future object */ - dmnsn_destroy_cond(&future->resume_cond); - dmnsn_destroy_cond(&future->all_running_cond); - dmnsn_destroy_cond(&future->none_running_cond); - dmnsn_destroy_cond(&future->cond); - dmnsn_destroy_mutex(&future->mutex); - dmnsn_free(future); + dmnsn_delete_future(future); } return retval; -- cgit v1.2.3