interplane.Get Omega
Read in your language

klib

[Ported from github.com/attractivechaos/klib] added block arena

Brought by drakestapleton

branch master Branches Tags History

klib / test / kthread_test.c

69 lines · 1707 bytes Raw Blame History
1#include <stdio.h>
2#include <assert.h>
3#include <stdlib.h>
4#include <pthread.h>
5#if HAVE_CILK
6#include <cilk/cilk.h>
7#include <cilk/cilk_api.h>
8#endif
9
10typedef struct {
11 int max_iter, w, h;
12 double xmin, xmax, ymin, ymax;
13 int *k;
14} global_t;
15
16static void compute(void *_g, int i, int tid)
17{
18 global_t *g = (global_t*)_g;
19 double x, x0 = g->xmin + (g->xmax - g->xmin) * (i%g->w) / g->w;
20 double y, y0 = g->ymin + (g->ymax - g->ymin) * (i/g->w) / g->h;
21 int k;
22
23 assert(g->k[i] < 0);
24 x = x0, y = y0;
25 for (k = 0; k < g->max_iter; ++k) {
26 double z = x * y;
27 x *= x; y *= y;
28 if (x + y >= 4) break;
29 x = x - y + x0;
30 y = z + z + y0;
31 }
32 g->k[i] = k;
33}
34
35void kt_for(int n_threads, int n_items, void (*func)(void*,int,int), void *data);
36
37int main(int argc, char *argv[])
38{
39 int i, tmp, tot, type = 0, n_threads = 2;
40 global_t global = { 10240*100, 800, 600, -2., -1.2, -1.2, 1.2, 0 };
41// global_t global = { 10240*1, 8, 6, -2., -1.2, -1.2, 1.2, 0 };
42
43 if (argc > 1) {
44 type = argv[1][0] == 'o'? 2 : argv[1][0] == 'c'? 3 : argv[1][0] == 'n'? 1 : 0;
45 if (argv[1][0] >= '0' && argv[1][0] <= '9')
46 n_threads = atoi(argv[1]);
47 } else {
48 fprintf(stderr, "Usage: ./a.out [openmp | cilk | #threads]\n");
49 }
50 tot = global.w * global.h;
51 global.k = calloc(tot, sizeof(int));
52 for (i = 0; i < tot; ++i) global.k[i] = -1;
53 if (type == 0) {
54 kt_for(n_threads, tot, compute, &global);
55 } else if (type == 2) {
56 #pragma omp parallel for
57 for (i = 0; i < tot; ++i)
58 compute(&global, i, 0);
59 } else if (type == 3) {
60 #if HAVE_CILK
61 cilk_for (i = 0; i < tot; ++i)
62 compute(&global, i, 0);
63 #endif
64 }
65 for (i = tmp = 0; i < tot; ++i) tmp += (global.k[i] < 0);
66 free(global.k);
67 assert(tmp == 0);
68 return 0;
69}