From 007ef52ce4ed717e080a60ce299aa5d498f57201 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Thu, 10 Oct 2013 23:28:17 -0400 Subject: [PATCH] added the test program for kthread --- test/Makefile | 6 +++- test/kthread_test.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/kthread_test.c diff --git a/test/Makefile b/test/Makefile index 22c7261..a392c8e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,7 +3,8 @@ CXX=g++ CFLAGS=-g -Wall -O2 -I.. CXXFLAGS=$(CFLAGS) PROGS=kbtree_test khash_keith khash_keith2 khash_test klist_test kseq_test kseq_bench \ - kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2 kstring_test + kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2 kstring_test \ + kthread_test all:$(PROGS) @@ -54,3 +55,6 @@ kstring_bench2:kstring_bench2.c ../kstring.h ../kstring.c kstring_test:kstring_test.c ../kstring.h ../kstring.c $(CC) $(CFLAGS) -o $@ kstring_test.c ../kstring.c + +kthread_test:kthread_test.c ../kthread.c + $(CC) $(CFLAGS) -fopenmp -o $@ kthread_test.c ../kthread.c diff --git a/test/kthread_test.c b/test/kthread_test.c new file mode 100644 index 0000000..5e46ca9 --- /dev/null +++ b/test/kthread_test.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#if HAVE_CILK +#include +#include +#endif + +typedef struct { + int max_iter, w, h; + double xmin, xmax, ymin, ymax; + int *k; +} global_t; + +static void compute(void *_g, int i, int tid) +{ + global_t *g = (global_t*)_g; + double x, x0 = g->xmin + (g->xmax - g->xmin) * (i%g->w) / g->w; + double y, y0 = g->ymin + (g->ymax - g->ymin) * (i/g->w) / g->h; + int k; + + assert(g->k[i] < 0); + x = x0, y = y0; + for (k = 0; k < g->max_iter; ++k) { + double z = x * y; + x *= x; y *= y; + if (x + y >= 4) break; + x = x - y + x0; + y = z + z + y0; + } + g->k[i] = k; +} + +void kt_for(int n_threads, void (*func)(void*,int,int), void *data, int n_items); + +int main(int argc, char *argv[]) +{ + int i, tmp, tot, type = 0, n_threads = 2; + global_t global = { 10240*100, 800, 600, -2., -1.2, -1.2, 1.2, 0 }; +// global_t global = { 10240*1, 8, 6, -2., -1.2, -1.2, 1.2, 0 }; + + if (argc > 1) { + type = argv[1][0] == 'o'? 2 : argv[1][0] == 'c'? 3 : argv[1][0] == 'n'? 1 : 0; + if (argv[1][0] >= '0' && argv[1][0] <= '9') + n_threads = atoi(argv[1]); + } else { + fprintf(stderr, "Usage: ./a.out [openmp | cilk | #threads]\n"); + } + tot = global.w * global.h; + global.k = calloc(tot, sizeof(int)); + for (i = 0; i < tot; ++i) global.k[i] = -1; + if (type == 0) { + kt_for(n_threads, compute, &global, tot); + } else if (type == 2) { + #pragma omp parallel for + for (i = 0; i < tot; ++i) + compute(&global, i, 0); + } else if (type == 3) { + #if HAVE_CILK + cilk_for (i = 0; i < tot; ++i) + compute(&global, i, 0); + #endif + } + for (i = tmp = 0; i < tot; ++i) tmp += (global.k[i] < 0); + free(global.k); + assert(tmp == 0); + return 0; +} -- 2.47.3