-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelcuda.cu
165 lines (132 loc) · 4.47 KB
/
mandelcuda.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
#include <bits/stdint-uintn.h>
#include <stdio.h>
#include <sys/param.h>
struct RenderSettings {
uint32_t *outputBuffer;
int width;
int height;
double zoom;
double xoffset;
double yoffset;
unsigned int iterations;
uint32_t *deviceBuffer;
};
uint32_t *deviceBuffer;
char cudaInitialized = 0;
__global__ void mandelbrotCalc(struct RenderSettings rs) {
int *deviceBuffer = (int *)rs.deviceBuffer;
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
double cReal, cImag, zReal, zImag, z2Real, z2Imag, zrzi;
int color;
int colorbias;
double x1 = rs.xoffset - 2.0 / rs.zoom * rs.width / rs.height;
double x2 = rs.xoffset + 2.0 / rs.zoom * rs.width / rs.height;
double y1 = rs.yoffset + 2.0 / rs.zoom;
double pixel_pitch = (x2 - x1) / rs.width;
int x, y;
for (int w = index; w < rs.height * rs.width; w += stride) {
y = w / rs.width;
if (y > 0) {
x = w % rs.width;
} else {
x = w;
}
cImag = y1 - pixel_pitch * y;
cReal = x1 + pixel_pitch * x;
zReal = cReal;
zImag = cImag;
color = 0x000000FF; // black as default for values that converge to 0
for (int i = 0; i < rs.iterations; i++) {
z2Real = zReal * zReal;
z2Imag = zImag * zImag;
zrzi = zReal * zImag;
zReal = cReal + z2Real - z2Imag;
zImag = zrzi + zrzi + cImag;
if (z2Real + z2Imag > 4.0f) {
colorbias = MIN(255, i * 510.0 / rs.iterations);
color = (color | (colorbias << 24) | (colorbias << 16) | colorbias << 8);
break;
}
}
deviceBuffer[w] = color;
}
}
__global__ void mandelbrotCalcSP(struct RenderSettings rs) {
int *deviceBuffer = (int *)rs.deviceBuffer;
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
float cReal, cImag, zReal, zImag, z2Real, z2Imag, zrzi;
int color;
int colorbias;
float x1 = rs.xoffset - 2.0 / rs.zoom * rs.width / rs.height;
float x2 = rs.xoffset + 2.0 / rs.zoom * rs.width / rs.height;
float y1 = rs.yoffset + 2.0 / rs.zoom;
float pixel_pitch = (x2 - x1) / rs.width;
int x, y;
for (int w = index; w < rs.height * rs.width; w += stride) {
y = w / rs.width;
if (y > 0) {
x = w % rs.width;
} else {
x = w;
}
cImag = y1 - pixel_pitch * y;
cReal = x1 + pixel_pitch * x;
zReal = cReal;
zImag = cImag;
color = 0x000000FF; // black as default for values that converge to 0
for (int i = 0; i < rs.iterations; i++) {
z2Real = zReal * zReal;
z2Imag = zImag * zImag;
zrzi = zReal * zImag;
zReal = cReal + z2Real - z2Imag;
zImag = zrzi + zrzi + cImag;
if (z2Real + z2Imag > 4.0f) {
colorbias = MIN(255, i * 510.0 / rs.iterations);
color = (color | (colorbias << 24) | (colorbias << 16) | colorbias << 8);
break;
}
}
deviceBuffer[w] = color;
}
}
extern "C" void freeCUDA() {
if (cudaInitialized == 1) {
cudaFree(deviceBuffer);
cudaInitialized = 0;
}
}
extern "C" void initCUDA(struct RenderSettings rs) {
// allocates device buffer on first run
// destroys and re-allocates buffer if window dimensions change
static int width = 0;
static int height = 0;
if (cudaInitialized == 0) {
cudaMalloc((void **)&deviceBuffer, rs.width * rs.height * 4);
width = rs.width;
height = rs.height;
cudaInitialized = 1;
} else {
if (rs.width != width || rs.height != height) {
freeCUDA();
initCUDA(rs);
}
}
}
extern "C" void mandelbrotCUDA(struct RenderSettings rs) {
initCUDA(rs);
uint32_t *screenBuffer = rs.outputBuffer;
rs.deviceBuffer = deviceBuffer;
mandelbrotCalc<<<2048, 1024>>>(rs);
cudaDeviceSynchronize();
cudaMemcpy(screenBuffer, deviceBuffer, rs.width * rs.height * 4, cudaMemcpyDeviceToHost);
}
extern "C" void mandelbrotCUDAsp(struct RenderSettings rs) {
initCUDA(rs);
uint32_t *screenBuffer = rs.outputBuffer;
rs.deviceBuffer = deviceBuffer;
mandelbrotCalcSP<<<2048, 1024>>>(rs);
cudaDeviceSynchronize();
cudaMemcpy(screenBuffer, deviceBuffer, rs.width * rs.height * 4, cudaMemcpyDeviceToHost);
}