-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.cu
executable file
·348 lines (286 loc) · 9.74 KB
/
mandelbrot.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @author Eddie Davis (eddiedavis@u.boisestate.edu)
* @author Jeff Pope (jeffreymithoug@u.boisestate.edu)
* @file mandelbrot.cu
* @brief CS530 PA4: Mandelbrot-CUDA Impementation
* @date 12/4/2016
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <cuda_runtime.h>
#define RMIN 0.3129928802767
#define RMAX 0.31299305009252
#define IMIN 0.0345483210604
#define IMAX 0.0345485012278
#define RADIUS_SQ 4.0 /* 2^2 */
#define DEF_WIDTH 2400 /* Image width in pixels */
#define DEF_HEIGHT 2400 /* Image height in pixels */
#define DEF_BLK_SZ 32 /* BLOCK_SIZE = GCD(WIDTH, THREADS_PER_BLOCK) = GCD(2400, 1024) */
#define MIN_BLK_SZ 1
#define MAX_COLOR UCHAR_MAX /* 255 */
#define OUT_FILE "Mandelbrot.pgm"
#define DEF_ITER 1000
#define DEBUG 0
typedef int DTYPE;
//typedef unsigned char DTYPE;
/**
* writeOutput
*
* Writes Mandelbrot image in PGM format.
*
* @param fileName Filename to write PGM data.
* @param data Output array data (Mandelbrot pixels)
* @param width Image width
* @param height Image height
*/
void writeOutput(const char *fileName, DTYPE *data, int width, int height) {
int i, j; /* index variables */
int max = -1; /* for pgm file output */
int size = width * height;
/* PGM file format requires the largest pixel value, calculate this */
for (i = 0; i < size; ++i) {
if (data[i] > max) {
max = data[i];
}
}
/* open the file for writing. omit error checking. */
FILE * fout = fopen(fileName, "w");
/* PGM file header */
fprintf(fout, "P2\n");
fprintf(fout, "%d\t%d\n", width, height);
fprintf(fout, "%d\n",max);
/* throw out the data */
for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
fprintf(fout, "%d\t", data[i * width + j]);
}
fprintf(fout,"\n");
}
/* flush the buffer and close the file */
fflush(fout);
fclose(fout);
}
/**
* cudaAssert
*
* CUDA error handler.
*
* @param code cudaError_t error code struct.
* @param file Name of file in which error occurred.
* @param line Line number on which error occurred.
*/
#define cudaAssert(ans) { _cudaAssert((ans), __FILE__, __LINE__); }
inline void _cudaAssert(cudaError_t code, const char *file, int line) {
if (code != cudaSuccess) {
fprintf(stderr, "cudaAssert: %s %s %d\n", cudaGetErrorString(code), file, line);
exit(code);
}
}
/**
* cudaPrintDevice
*
* Prints revelevant information about the given CUDA device.
*
* @param file File pointer to write device properties.
* @param prop cudaDeviceProp structure pointer.
* @param dnum CUDA device number.
*/
void cudaPrintDevice(FILE *file, cudaDeviceProp *prop, int dnum) {
fprintf(file, "Device Number: %d\n", dnum);
fprintf(file, " Device name: %s\n", prop->name);
fprintf(file, " Memory Clock Rate (KHz): %d\n", prop->memoryClockRate);
fprintf(file, " Memory Bus Width (bits): %d\n", prop->memoryBusWidth);
fprintf(file, " Peak Memory Bandwidth (GB/s): %f\n",
2.0 * prop->memoryClockRate * (prop->memoryBusWidth / 8) / 1.0e6);
fprintf(file, " Compute Version: %d.%d\n", prop->major, prop->minor);
fprintf(file, " Compute Mode: ");
switch (prop->computeMode) {
case cudaComputeModeExclusive:
fprintf(file, "Exclusive");
break;
case cudaComputeModeProhibited:
fprintf(file, "Prohibited");
break;
default:
fprintf(file, "Default");
break;
}
fprintf(file, "\n");
fprintf(file, " SM count: %d\n", prop->multiProcessorCount);
fprintf(file, " Shared mem/block: %zd\n", prop->sharedMemPerBlock);
fprintf(file, " Threads per warp: %d\n", prop->warpSize);
fprintf(file, " Max threads per block: %d\n", prop->maxThreadsPerBlock);
fprintf(file, " Max block size: (");
for (int j = 0; j < 3; j++) {
fprintf(file, "%d,", prop->maxThreadsDim[j]);
}
fprintf(file, ")\n Max grid size: (");
for (int j = 0; j < 3; j++) {
fprintf(file, "%d,", prop->maxGridSize[j]);
}
fprintf(file, ")\n\n");
}
/**
* mand (CUDA kernel function)
*
* Generates the Mandelbrot set.
*
* @param output Output array to receive computed Mandelbrot pixels.
* @param maxIter Max iterations to test for escape values.
* @param width Image width.
* @param height Image height.
* @param realRange Range of real component.
* @param imagRange Range of imaginary component.
*/
__global__ void mand(DTYPE* output, int maxIter, int width, int height, double realRange, double imagRange) {
int col = blockDim.x * blockIdx.x + threadIdx.x; // Image col (X coord)
int row = blockDim.y * blockIdx.y + threadIdx.y; // Image row (Y coord)
if (col < width && row < height) {
int idx = row * width + col;
double cReal = RMIN + row * realRange;
double cImag = IMIN + col * imagRange;
double zReal = 0.0;
double zImag = 0.0;
double zReal2 = zReal;
double zImag2 = zImag;
double zCurr;
double zMag;
int iter = 0;
for (; iter < maxIter; ++iter) {
zCurr = zReal;
zReal2 = zReal * zReal;
zImag2 = zImag * zImag;
zReal = zReal2 - zImag2 + cReal;
zImag = (2.0 * zCurr * zImag) + cImag;
zMag = zReal2 + zImag2;
if (zMag > RADIUS_SQ) {
break;
}
}
output[idx] = (DTYPE) floor(((double) (MAX_COLOR * iter)) / (double) maxIter);
}
}
/**
* main
*
* Main function.
*
* @param argc Argument count.
* @param argv Argument values.
* @return
*/
int main(int argc, char ** argv) {
int nDevices = 0;
DTYPE *output = NULL;
DTYPE *d_output = NULL;
float time; /*timer*/
int maxIter = DEF_ITER;
if (argc > 1) {
maxIter = atoi(argv[1]); /* first command line argument... */
}
if (maxIter < 1) {
printf("usage: %s [MAX_ITERATION=%d] [WIDTH=%d] [HEIGHT=%d] [BLOCK_X=%d] [BLOCK_Y=1]\n",
argv[0], DEF_ITER, DEF_WIDTH, DEF_HEIGHT, MIN_BLK_SZ);
return 0;
}
int width = DEF_WIDTH;
if (argc > 2) {
width = atoi(argv[2]);
}
int height = DEF_HEIGHT;
if (argc > 3) {
height = atoi(argv[3]);
}
cudaAssert(cudaGetDeviceCount(&nDevices));
if (nDevices < 1) {
printf("ERROR: No valid CUDA devices on this machine!\n");
return -1;
}
if (DEBUG) {
fprintf(stderr, "nDevices = %d\n", nDevices);
cudaDeviceProp prop;
for (int i = 0; i < nDevices; i++) {
cudaAssert(cudaGetDeviceProperties(&prop, i));
cudaPrintDevice(stderr, &prop, i);
}
}
// Get data size...
int nPixels = width * height;
int nBytes = nPixels * sizeof(DTYPE);
if (DEBUG) fprintf(stderr, "nPixels = %d, nBytes = %d\n", nPixels, nBytes);
/* Allocate memory on host to store output values for pixels */
output = (DTYPE *) malloc(nBytes);
if (output == NULL) {
perror("output");
return -1;
}
// Set block size...
int blockX = 0;
if (argc > 4) {
blockX = atoi(argv[4]);
}
if (blockX < 1) {
blockX = MIN_BLK_SZ;
}
int blockY = 0;
if (argc > 5) {
blockY = atoi(argv[5]);
}
if (blockY < 1) {
blockY = MIN_BLK_SZ;
}
if (blockX == MIN_BLK_SZ && blockY == MIN_BLK_SZ) {
blockX = DEF_BLK_SZ;
}
dim3 blockSize(blockX, blockY);
if (DEBUG) fprintf(stderr, "blockSize = (%d,%d,%d)\n", blockSize.x, blockSize.y, blockSize.z);
// Set grid size...
int gridX = width / blockSize.x;
int gridY = height / blockSize.y;
dim3 gridSize(gridX, gridY);
if (DEBUG) fprintf(stderr, "gridSize = (%d,%d,%d)\n", gridSize.x, gridSize.y, gridSize.z);
printf("Running Mandelbrot-CUDA with (w,h,mi,bx,by,gx,gy) = (%d,%d,%d,%d,%d,%d,%d)...\n",
width, height, maxIter, blockSize.x, blockSize.y, gridSize.x, gridSize.y);
// Create event timers...
cudaEvent_t start, stop;
cudaAssert(cudaEventCreate(&start));
cudaAssert(cudaEventCreate(&stop));
// Start timer...
cudaAssert(cudaEventRecord(start));
// Allocate memory on device...
if (DEBUG) fprintf(stderr, "cudaMalloc...\n");
cudaAssert(cudaMalloc(&d_output, nBytes));
double realRange = (RMAX - RMIN) / (double) (width - 1);
double imagRange = (IMAX - IMIN) / (double) (height - 1);
// Invoke the kernel...
if (DEBUG) {
fprintf(stderr, "kernel: mand(d_output[%d], maxIter=%d, realRange=%lf, imagRange=%lf)...\n",
nPixels, maxIter, realRange, imagRange);
}
mand<<<gridSize, blockSize>>>(d_output, maxIter, width, height, realRange, imagRange);
// cudaMemcpy is an implicit barrier so need need for sync.
// Copy data back to host...
if (DEBUG) fprintf(stderr, "cudaMemcpy...\n");
cudaAssert(cudaMemcpy(output, d_output, nBytes, cudaMemcpyDeviceToHost));
// Free data on device...
if (DEBUG) fprintf(stderr, "cudaFree...\n");
cudaAssert(cudaFree(d_output));
// Stop timer...
cudaAssert(cudaEventRecord(stop));
// Get elapsed time...
if (DEBUG) fprintf(stderr, "cudaEventSynchronize...\n");
cudaAssert(cudaEventSynchronize(stop));
if (DEBUG) fprintf(stderr, "cudaEventElapsedTime...\n");
cudaAssert(cudaEventElapsedTime(&time, start, stop));
// Write the output...
if (DEBUG) fprintf(stderr, "writeOutput...\n");
writeOutput(OUT_FILE, output, width, height);
// Free host data...
free(output);
// Report timing...
printf("Elapsed time: %lf sec\n", time * 1E-3);
return 0;
}