/
julia.cu
325 lines (274 loc) · 8.94 KB
/
julia.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// julia.cu - CUDA code to generate julia set image
//
// See:
// http://selkie.macalester.edu/csinparallel/modules/CUDAArchitecture/build/html/1-Mandelbrot/Mandelbrot.html
//
// To compile:
// $ nvcc julia.cu -g -D SHOW_X -o julia -lX11 -lgomp -lm
//
// Use Zn+1 = Zn^4 + C for recursion
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <stdint.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <omp.h>
static int dim = 512;
static int n = 512;
static int m = 512;
static int max_iter = 100;
static uint32_t *colors;
uint32_t *dev_colors;
// X11 data
#ifdef SHOW_X
static Display *dpy;
static XImage *bitmap;
static Window win;
static Atom wmDeleteMessage;
static GC gc;
//destroy window and x variables
static void exit_x11(void){
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
// create Xwindow
static void init_x11(){
// Attempt to open the display
dpy = XOpenDisplay(NULL);
// Failure
if (!dpy) exit(0);
uint32_t long white = WhitePixel(dpy,DefaultScreen(dpy));
uint32_t long black = BlackPixel(dpy,DefaultScreen(dpy));
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
0, 0, dim, dim, 0, black, white);
// We want to be notified when the window appears
XSelectInput(dpy, win, StructureNotifyMask);
// Make it appear
XMapWindow(dpy, win);
while (1){
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify) break;
}
XTextProperty tp;
char name[128] = "Julia Set";
char *n = name;
Status st = XStringListToTextProperty(&n, 1, &tp);
if (st) XSetWMName(dpy, win, &tp);
// Wait for the MapNotify event
XFlush(dpy);
int depth = DefaultDepth(dpy, DefaultScreen(dpy));
Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
bitmap = XCreateImage(dpy, visual, depth, ZPixmap, 0,
(char*) malloc(dim * dim * 32), dim, dim, 32, 0);
// Init GC
gc = XCreateGC(dpy, win, 0, NULL);
XSetForeground(dpy, gc, black);
XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &wmDeleteMessage, 1);
}
#endif
//create colors used to draw the set
void init_colours(void) {
float freq = 6.3 / max_iter;
for (int i = 0; i < max_iter; i++){
char r = sin(freq * i + 1) * 127 + 128;
char g = sin(freq * i + 3) * 127 + 128;
char b = sin(freq * i + 5) * 127 + 128;
colors[i] = b + 256 * g + 256 * 256 * r;
}
colors[max_iter] = 0;
}
void checkErr(cudaError_t err, const char* msg){
if (err != cudaSuccess){
fprintf(stderr, "%s (error code %d: '%s'", msg, err, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
}
/* the julia set is defined as all complex numbers c such that the
equation zn+1 = zn^4 + c remains bounded. In practice, we calculate max_iter
iterations of this formula and if the magnitude of zn is < 16 we assume it
is in the set. The greater max_iters the more accurate our representation */
__device__ uint32_t julia_double(double cr, double ci, int max_iter) {
double zr = 0;
double zi = 0;
double zr4 = 0;
double zi4 = 0;
double z4r3i = 0;
double z6r2i2 = 0;
double z4ri3 = 0;
double zrsqr = 0;
double zisqr = 0;
uint32_t i;
for (i = 0; i < max_iter; i++){
zi += ci;
zr += cr;
zr4 = zr * zr * zr * zr;
zi4 = zi * zi * zi * zi;
z4r3i = 4 * zr * zr * zr * zi;
z6r2i2 = 6 * zr * zr * zi * zi;
z4ri3 = 4 * zr * zi * zi * zi;
zr = zr4 + zi4 - z6r2i2;
zi = z4r3i - z4ri3;
zrsqr = zr * zr;
zisqr = zi * zi;
//the fewer iterations it takes to diverge, the farther from the set
if (zrsqr + zisqr > 16.0) break;
}
return i;
}
/* turn each x y coordinate into a complex number and run the formula on it */
__global__ void julia_kernel(uint32_t *counts, double xmin, double ymin,
double step, int max_iter, int dim, uint32_t *colors) {
int pix_per_thread = dim * dim / (gridDim.x * blockDim.x);
int tId = blockDim.x * blockIdx.x + threadIdx.x;
int offset = pix_per_thread * tId;
for (int i = offset; i < offset + pix_per_thread; i++){
int x = i % dim;
int y = i / dim;
double cr = xmin + x * step;
double ci = ymin + y * step;
counts[y * dim + x] = colors[julia_double(cr, ci, max_iter)];
}
if (gridDim.x * blockDim.x * pix_per_thread < dim * dim
&& tId < (dim * dim) - (blockDim.x * gridDim.x)){
int i = blockDim.x * gridDim.x * pix_per_thread + tId;
int x = i % dim;
int y = i / dim;
double cr = xmin + x * step;
double ci = ymin + y * step;
counts[y * dim + x] = colors[julia_double(cr, ci, max_iter)];
}
}
/* For each point, evaluate its colour */
static void display_double(double xcen, double ycen, double scale,
uint32_t *dev_counts, uint32_t *colors){
dim3 numBlocks(dim,dim);
double xmin = xcen - (scale/2);
double ymin = ycen - (scale/2);
double step = scale / dim;
cudaError_t err = cudaSuccess;
#ifdef BENCHMARK
double start = omp_get_wtime();
#endif
julia_kernel<<<n, m>>>(dev_counts, xmin , ymin, step, max_iter, dim, colors);
checkErr(err, "Failed to run Kernel");
#ifdef SHOW_X
err = cudaMemcpy(bitmap->data, dev_counts, dim * dim * sizeof(uint32_t), cudaMemcpyDeviceToHost);
#else
void *data = malloc(dim * dim * sizeof(uint32_t));
err = cudaMemcpy(data, dev_counts, dim * dim * sizeof(uint32_t), cudaMemcpyDeviceToHost);
#endif
checkErr(err, "Failed to copy dev_counts back");
#ifdef BENCHMARK
double stop = omp_get_wtime();
printf("Blocks: %d\tThreads per Block: %d\tSize:%dx%d\tDepth: %d\tTime: %f\n",
n, m, dim, dim, max_iter, stop - start);
#endif
#ifdef SHOW_X
XPutImage(dpy, win, gc, bitmap,
0, 0, 0, 0,
dim, dim);
XFlush(dpy);
#endif
}
int main(int argc, char** argv){
cudaError_t err = cudaSuccess;
if (argc >= 2)
n = atoi(argv[1]);
if (argc >= 3)
m = atoi(argv[2]);
if (argc >= 4)
dim = atoi(argv[3]);
if (argc >= 5)
max_iter = atoi(argv[4]);
size_t color_size = (max_iter +1) * sizeof(uint32_t);
colors = (uint32_t *) malloc(color_size);
cudaMalloc((void**)&dev_colors, color_size);
double xcen = 0.0;
double ycen = 0.156;
double scale = 3;
#ifdef SHOW_X
init_x11();
#endif
init_colours();
cudaMemcpy(dev_colors, colors, color_size, cudaMemcpyHostToDevice);
free(colors);
uint32_t *dev_counts = NULL;
size_t img_size = dim * dim * sizeof(uint32_t);
err = cudaMalloc(&dev_counts, img_size);
checkErr(err, "Failed to allocate dev_counts");
display_double(xcen, ycen, scale, dev_counts, dev_colors);
#ifdef SHOW_X
while(1) {
XEvent event;
KeySym key;
char text[255];
XNextEvent(dpy, &event);
while (XPending(dpy) > 0)
XNextEvent(dpy, &event);
/* Just redraw everything on expose */
if ((event.type == Expose) && !event.xexpose.count){
XPutImage(dpy, win, gc, bitmap,
0, 0, 0, 0,
dim, dim);
}
/* Press 'x' to exit */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'x') break;
/* Press 'a' to go left */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'a'){
xcen -= 20 * scale / dim;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Press 'w' to go up */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'w'){
ycen -= 20 * scale / dim;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Press 's' to go down */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 's'){
ycen += 20 * scale / dim;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Press 'd' to go right */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'd'){
xcen += 20 * scale / dim;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Press 'q' to zoom out */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'q'){
scale *= 1.25;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Press 'e' to zoom in */
if ((event.type == KeyPress) &&
XLookupString(&event.xkey, text, 255, &key, 0) == 1)
if (text[0] == 'e'){
scale *= .80;
display_double(xcen, ycen, scale, dev_counts, dev_colors);
}
/* Or simply close the window */
if ((event.type == ClientMessage) &&
((Atom) event.xclient.data.l[0] == wmDeleteMessage))
break;
}
exit_x11();
#endif
cudaFree(dev_counts);
cudaFree(dev_colors);
return 0;
}