SCM Repository
Annotation of /benchmarks/programs/mandelbrot/bmark-teem.c
Parent Directory
|
Revision Log
Revision 3349 - (view) (download) (as text)
1 : | jhr | 1908 | /*! \file bmark-teem.c |
2 : | * | ||
3 : | * \author John Reppy | ||
4 : | */ | ||
5 : | |||
6 : | /* | ||
7 : | jhr | 3349 | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) |
8 : | * | ||
9 : | * COPYRIGHT (c) 2015 The University of Chicago | ||
10 : | jhr | 1908 | * All rights reserved. |
11 : | */ | ||
12 : | |||
13 : | // unu quantize -b 8 -i bmark-teem.nrrd -o bmark-teem.png | ||
14 : | |||
15 : | #include <stdio.h> | ||
16 : | #include <math.h> | ||
17 : | #include <stdbool.h> | ||
18 : | #include "teem/nrrd.h" | ||
19 : | #include "teem/ell.h" | ||
20 : | #include "teem-defs.h" | ||
21 : | |||
22 : | #if 1 | ||
23 : | # define REAL float | ||
24 : | # define POW powf | ||
25 : | # define SIN sinf | ||
26 : | # define nrrdTypeReal nrrdTypeFloat | ||
27 : | #else | ||
28 : | # define REAL double | ||
29 : | # define POW pow | ||
30 : | # define SIN sin | ||
31 : | # define nrrdTypeReal nrrdTypeDouble | ||
32 : | #endif | ||
33 : | |||
34 : | #define RESOLUTION 2000 | ||
35 : | #define MAX_ITER 2000 | ||
36 : | #define FOV 0.15 | ||
37 : | |||
38 : | STATIC_INLINE REAL lerp (REAL out_min, REAL out_max, REAL in_min, REAL in, REAL in_max) | ||
39 : | { | ||
40 : | return (out_min * (in_max - in) + out_max * (in - in_min)) / (in_max - in_min); | ||
41 : | } | ||
42 : | |||
43 : | STATIC_INLINE REAL sqr (REAL x) { return x*x; } | ||
44 : | |||
45 : | STATIC_INLINE REAL dot2 (REAL vec1[2], REAL vec2[2]) | ||
46 : | { | ||
47 : | return (vec1[0] * vec2[0] + vec1[1] * vec2[1]); | ||
48 : | } | ||
49 : | |||
50 : | int main (int argc, const char *argv[]) | ||
51 : | { | ||
52 : | const char *me = argv[0]; | ||
53 : | char *outFile = "bmark-teem.nrrd"; | ||
54 : | REAL center[2] = {-1.2, -0.3}; | ||
55 : | |||
56 : | airArray *mop; | ||
57 : | mop = airMopNew(); | ||
58 : | |||
59 : | Nrrd *nOut = nrrdNew(); | ||
60 : | airMopAdd(mop, nOut, (airMopper)nrrdNuke, airMopAlways); | ||
61 : | if (nrrdAlloc_va(nOut, nrrdTypeReal, 3, | ||
62 : | AIR_CAST(size_t, 3), | ||
63 : | AIR_CAST(size_t, RESOLUTION), | ||
64 : | AIR_CAST(size_t, RESOLUTION))) | ||
65 : | { | ||
66 : | char *err = biffGetDone(NRRD); | ||
67 : | airMopAdd (mop, err, airFree, airMopAlways); | ||
68 : | fprintf (stderr, "%s: Trouble allocating:\n%s", me, err); | ||
69 : | airMopError (mop); | ||
70 : | return -1; | ||
71 : | } | ||
72 : | REAL *outData = AIR_CAST(REAL *, nOut->data); | ||
73 : | |||
74 : | double t0 = airTime(); // start timing | ||
75 : | |||
76 : | for (int ciIdx = 1; ciIdx <= RESOLUTION; ciIdx++) { | ||
77 : | for (int crIdx = 1; crIdx <= RESOLUTION; crIdx++) { | ||
78 : | REAL c[2] = { | ||
79 : | lerp(center[0]-FOV, center[0]+FOV, 1.0, (REAL)crIdx, (REAL)RESOLUTION), | ||
80 : | lerp(center[1]-FOV, center[1]+FOV, 1.0, (REAL)ciIdx, (REAL)RESOLUTION) | ||
81 : | }; | ||
82 : | REAL z[2] = {0.0, 0.0}; | ||
83 : | int iter = 0; | ||
84 : | while (true) { | ||
85 : | // z = z^2 + c | ||
86 : | REAL re = sqr(z[0]) - sqr(z[1]); | ||
87 : | REAL im = 2.0 * z[0] * z[1]; | ||
88 : | ELL_2V_SET(z, re + c[0], im + c[1]); | ||
89 : | iter += 1; | ||
90 : | if (dot2(z, z) > 4.0) { | ||
91 : | // point has escaped; set color based on iteration | ||
92 : | REAL t = 11.0* POW((REAL)iter, 0.2); | ||
93 : | REAL red = (SIN(t) + 1.7) / 2.7; | ||
94 : | REAL green = (SIN(t + 2.0*M_PI/3.0) + 1.7) / 2.7; | ||
95 : | REAL blue = (SIN(t - 2.0*M_PI/3.0) + 1.7) / 2.7; | ||
96 : | ELL_3V_SET(outData + 3*(crIdx-1 + RESOLUTION*(ciIdx-1)), red, green, blue); | ||
97 : | break; | ||
98 : | } | ||
99 : | else if (iter > MAX_ITER) { | ||
100 : | ELL_3V_SET(outData + 3*(crIdx-1 + RESOLUTION*(ciIdx-1)), 0.0, 0.0, 0.0); | ||
101 : | break; | ||
102 : | } | ||
103 : | } | ||
104 : | } | ||
105 : | } | ||
106 : | |||
107 : | double totalTime = airTime() - t0; | ||
108 : | printf("usr=%f\n", totalTime); | ||
109 : | |||
110 : | if (nrrdSave(outFile, nOut, NULL)) { | ||
111 : | char *err = biffGetDone(NRRD); | ||
112 : | airMopAdd(mop, err, airFree, airMopAlways); | ||
113 : | fprintf(stderr, "%s: Trouble saving:\n%s", me, err); | ||
114 : | airMopError(mop); | ||
115 : | return -1; | ||
116 : | } | ||
117 : | |||
118 : | airMopOkay(mop); | ||
119 : | return 0; | ||
120 : | |||
121 : | } |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |