-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulia.cu
331 lines (269 loc) · 10.7 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
326
327
328
329
330
331
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// julia.cu (c) Stephen Smithbower 2012
//
// CPU and Cuda implementations of the Julia set associated with the Newton
// iteration for the complex function f(z) = z^3 - 1.
//
// Utilizes [libbmp - BMP library] for image saving. The main source for this
// can be found @ http://code.google.com/p/libbmp/
////////////////////////////////////////////////////////////////////////////////
// -w Image width, default 1024.
// -h Image height, default 768.
// -z Zoom level, default 2.2
// -i Maximum number of Newton's method iterations, default 200.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//*************
// This code is presented as-is, without warrenty or support. You are welcome
// to use this code in whatever way you wish, however I would appreciate
// some credit =)
//*************
////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////
#include <math.h>
#include <cuda_runtime.h>
#include "bmpfile.h"
#include "bmpfile.c"
////////////////////////////////////////////////////////////////////////////////
// Default Globals
////////////////////////////////////////////////////////////////////////////////
int width = 1024; //Width of the image, in pixels.
int height = 768; //Height of the image, in pixels.
int max_iterations = 200; //Maximum number iterations of Newton's method.
//200 seems to work out pretty well.
float zoom = 2.2f; //Viewing region (-x : x).
float epsilon = 0.01f; //Maximum difference when comparing floats.
////////////////////////////////////////////////////////////////////////////////
// Function Prototypes
////////////////////////////////////////////////////////////////////////////////
void cpu_julia(int *matrix);
__global__ void gpu_julia(int *matrix, int width, int height, int max_iterations, float zoom, float epsilon);
////////////////////////////////////////////////////////////////////////////////
// Complex Number Helper Functions
////////////////////////////////////////////////////////////////////////////////
__device__ __host__ void complex_add(float a, float b, float c, float d, float *realOut, float *imgOut)
{
*realOut = a + c;
*imgOut = b + d;
}
__device__ __host__ void complex_sub(float a, float b, float c, float d, float *realOut, float *imgOut)
{
*realOut = a - c;
*imgOut = b - d;
}
__device__ __host__ void complex_mul(float a, float b, float c, float d, float *realOut, float *imgOut)
{
*realOut = (a * c) - (b * d);
*imgOut = (b * c) + (a * d);
}
__device__ __host__ void complex_div(float a, float b, float c, float d, float *realOut, float *imgOut)
{
*realOut = ((a * c) + (b * d)) / (pow(c, 2) + pow(d, 2));
*imgOut = ((b * c) - (a * d))/ (pow(c, 2) + pow(d, 2));
}
////////////////////////////////////////////////////////////////////////////////
// Bitmap Helper Function
////////////////////////////////////////////////////////////////////////////////
void write_bitmap(int *matrix, char *filename)
{
bmpfile_t *bmp;
bmp = bmp_create(width, height, 24);
rgb_pixel_t pixel;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
pixel.red = 0;
pixel.green = 0;
pixel.blue = 0;
switch(matrix[x * height + y])
{
case 1:
pixel.red = 255;
break;
case 2:
pixel.blue = 255;
break;
case 3:
pixel.green = 255;
break;
}
bmp_set_pixel(bmp, x, y, pixel);
}
bmp_save(bmp, filename);
bmp_destroy(bmp);
}
////////////////////////////////////////////////////////////////////////////////
// Main Function
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
//Input parameters.
for (int i = 0; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (argv[i][1] == 'w') //Width of the image.
width = atoi(argv[++i]);
if (argv[i][1] == 'h') //Height of the image.
height = atoi(argv[++i]);
if (argv[i][1] == 'z') //Zoom level.
zoom = atof(argv[++i]);
if (argv[i][1] == 'i') //Maximum number of iterations.
max_iterations = atoi(argv[++i]);
if (argv[i][1] == 'e') //Set epsilon - controls calculation precision.
epsilon = atof(argv[++i]);
}
}
int dif_count = 0;
///////////
// Initialize memory.
///////////
//Host.
int *cpu_image = (int*)malloc(sizeof(int) * width * height);
int *gpu_image = (int*)malloc(sizeof(int) * width * height);
//Device.
int *device_image;
cudaMalloc((void**)&device_image, sizeof(int) * width * height);
///////////
// Perform CPU calculations (gold).
///////////
cpu_julia(cpu_image);
write_bitmap(cpu_image, "cpu.bmp");
///////////
// Perform GPU calculations, 128 threads per block (arbitrary).
///////////
gpu_julia<<<ceil((float)width * (float)height / 128.0f), 128>>>(device_image, width, height, max_iterations, zoom, epsilon);
cudaThreadSynchronize();
cudaMemcpy(gpu_image, device_image, sizeof(int) * width * height, cudaMemcpyDeviceToHost);
write_bitmap(gpu_image, "gpu.bmp");
///////////
// Validate GPU results.
///////////
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
int index = (x * height) + y;
if (cpu_image[index] != gpu_image[index])
dif_count++;
}
if (dif_count < (width * height * 0.01f)) //Fewer than 1% difference.
printf("GPU Passes!\n");
else
printf("GPU FAILS =(\n");
///////////
// Memory cleanup.
///////////
free(cpu_image);
free(gpu_image);
cudaFree(device_image);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// CPU Implementation
////////////////////////////////////////////////////////////////////////////////
void cpu_julia(int *matrix)
{
float newRe, newIm, oldRe, oldIm;
float z_3_r, z_3_i, z_2_r, z_2_i, inner_r, inner_i;
float ratio = (float)height / (float)width;
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
///////////
// Set up starting value based on x, y (x = real, y = imaginary).
///////////
newRe = (((float)x / (float)width) - 0.5f) * 2.0f * zoom;
newIm = ratio * (((float)y / (float)height) - 0.5f) * 2.0f * zoom;
///////////
// Newton's Method. z[+ 1] = z - ((z^3 - 1) / 3z^2)
///////////
for(int i = 0; i < max_iterations; i++)
{
oldRe = newRe;
oldIm = newIm;
//Clear everything.
z_3_r = z_3_i = z_2_r = z_2_i = inner_r = inner_i = 0;
complex_mul(oldRe, oldIm, oldRe, oldIm, &z_2_r, &z_2_i); // z^2
complex_mul(z_2_r, z_2_i, oldRe, oldIm, &z_3_r, &z_3_i); // z^3
z_3_r -= 1.0f; //z^3 - 1
z_2_r *= 3.0f; // 3z^2
z_2_i *= 3.0f;
complex_div(z_3_r, z_3_i, z_2_r, z_2_i, &inner_r, &inner_i); // ((z^3 - 1) / 3z^2)
complex_sub(oldRe, oldIm, inner_r, inner_i, &newRe, &newIm); //z - ((z^3 - 1) / 3z^2)
//If we've mostly converged, break out early.
if (abs(newRe - oldRe) < epsilon && abs(newIm - oldIm) < epsilon)
break;
}
///////////
// Figure out which root we've converged to.
///////////
if (abs(1.0f - newRe) < epsilon && abs(0 - newIm) < epsilon)
matrix[x * height + y] = 1;
else
if (newRe - 0.5f < epsilon && 0.86603f - newIm < epsilon)
matrix[x * height + y] = 2;
else
if (newRe - 0.5f < epsilon && newIm - 0.86603f < epsilon)
matrix[x * height + y] = 3;
else
matrix[x * height + y] = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
// GPU Implementation
////////////////////////////////////////////////////////////////////////////////
__global__ void gpu_julia(int *matrix, int width, int height, int max_iterations, float zoom, float epsilon)
{
//Compute global thread id to index global memory.
//Each thread is one pixel.
int threadID = (blockIdx.x * blockDim.x) + threadIdx.x;
float newRe, newIm, oldRe, oldIm;
float z_3_r, z_3_i, z_2_r, z_2_i, inner_r, inner_i;
//Guard to make sure we're not writing to memory we don't own.
if (threadID < width * height)
{
///////////
// Set up starting value based on x, y (x = real, y = imaginary).
///////////
int x = (threadID / height);
int y = (threadID % height);
newRe = (((float)x / (float)width) - 0.5f) * 2.0f * zoom;
newIm = ((float)height / (float)width) * (((float)y / (float)height) - 0.5f) * 2.0f * zoom;
///////////
// Newton's Method. z[+ 1] = z - ((z^3 - 1) / 3z^2)
///////////
for(int i = 0; i < max_iterations; i++)
{
//Clear everything.
z_3_r = z_3_i = z_2_r = z_2_i = inner_r = inner_i = 0;
oldRe = newRe;
oldIm = newIm;
complex_mul(oldRe, oldIm, oldRe, oldIm, &z_2_r, &z_2_i); // z^2
complex_mul(z_2_r, z_2_i, oldRe, oldIm, &z_3_r, &z_3_i); // z^3
z_3_r -= 1.0f; //z^3 - 1
z_2_r *= 3.0f; // 3z^2
z_2_i *= 3.0f;
complex_div(z_3_r, z_3_i, z_2_r, z_2_i, &inner_r, &inner_i); // ((z^3 - 1) / 3z^2)
complex_sub(oldRe, oldIm, inner_r, inner_i, &newRe, &newIm); //z - ((z^3 - 1) / 3z^2)
//If we've mostly converged, break out early.
if (abs(newRe - oldRe) < epsilon && abs(newIm - oldIm) < epsilon)
break;
}
///////////
// Figure out which root we've converged to.
///////////
if (abs(1.0f - newRe) < epsilon && abs(0 - newIm) < epsilon)
matrix[threadID] = 1;
else
if (newRe - 0.5f < epsilon && 0.86603f - newIm < epsilon)
matrix[threadID] = 2;
else
if (newRe - 0.5f < epsilon && newIm - 0.86603f < epsilon)
matrix[threadID] = 3;
else
matrix[threadID] = 0;
}
}