]> git.kaiwu.me - klib.git/commitdiff
added the test program for kthread
authorHeng Li <lh3@me.com>
Fri, 11 Oct 2013 03:28:17 +0000 (23:28 -0400)
committerHeng Li <lh3@me.com>
Fri, 11 Oct 2013 03:28:17 +0000 (23:28 -0400)
test/Makefile
test/kthread_test.c [new file with mode: 0644]

index 22c726122147fd22b5ae19561b14f1fbbc6febb8..a392c8ed46dbe4f4ff187b2f2f0ceb0828fb561f 100644 (file)
@@ -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 (file)
index 0000000..5e46ca9
--- /dev/null
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <pthread.h>
+#if HAVE_CILK
+#include <cilk/cilk.h>
+#include <cilk/cilk_api.h>
+#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;
+}