-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPMD.cpp
More file actions
290 lines (207 loc) · 7.23 KB
/
Copy pathSPMD.cpp
File metadata and controls
290 lines (207 loc) · 7.23 KB
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
#include <iostream>
#include <vector>
#include <fstream>
#include <chrono>
#include <omp.h>
#include <cstdlib>
using namespace std;
// ==========================================================
// 8K RESOLUTION
// ==========================================================
const int WIDTH = 7680;
const int HEIGHT = 4320;
const int MAX_ITER = 1000;
// ==========================================================
// PIXEL STRUCT
// ==========================================================
struct Pixel {
unsigned char r, g, b;
};
// ==========================================================
// SAVE RGB IMAGE
// ==========================================================
void savePPM(const string& filename,
const vector<Pixel>& image) {
ofstream file(filename, ios::binary);
file << "P6\n"
<< WIDTH << " "
<< HEIGHT << "\n255\n";
for (const auto& pixel : image) {
file.write(
reinterpret_cast<const char*>(&pixel),
3
);
}
file.close();
}
// ==========================================================
// SAVE GRAYSCALE IMAGE
// ==========================================================
void saveGrayPPM(const string& filename,
const unsigned char* image,
size_t size) {
ofstream file(filename, ios::binary);
file << "P5\n"
<< WIDTH << " "
<< HEIGHT << "\n255\n";
file.write(
reinterpret_cast<const char*>(image),
size
);
file.close();
}
// ==========================================================
// TASK A - MANDELBROT
// ==========================================================
void generateMandelbrot(vector<Pixel>& image) {
double xmin = -2.5;
double xmax = 1.0;
double ymin = -1.0;
double ymax = 1.0;
#pragma omp parallel for schedule(guided)
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
double cr =
xmin +
(x / (double)WIDTH) *
(xmax - xmin);
double ci =
ymin +
(y / (double)HEIGHT) *
(ymax - ymin);
double zr = 0.0;
double zi = 0.0;
int iter = 0;
while (zr * zr + zi * zi <= 4.0 &&
iter < MAX_ITER) {
double temp =
zr * zr - zi * zi + cr;
zi = 2.0 * zr * zi + ci;
zr = temp;
iter++;
}
Pixel pixel;
if (iter == MAX_ITER) {
pixel = {0, 0, 0};
} else {
unsigned char color =
(unsigned char)
(255.0 * iter / MAX_ITER);
pixel.r = color;
pixel.g = 255 - color;
pixel.b = (color * 2) % 255;
}
image[y * WIDTH + x] = pixel;
}
}
}
// ==========================================================
// RGB -> GRAYSCALE
// ==========================================================
void convertToGray(
const vector<Pixel>& input,
unsigned char* gray)
{
#pragma omp parallel for schedule(static)
for (long long i = 0;
i < (long long)(WIDTH * HEIGHT);
i++) {
gray[i] =
(input[i].r +
input[i].g +
input[i].b) / 3;
}
}
// ==========================================================
// TASK B - BLUR ROW (loop SIMD aislado)
// ==========================================================
__attribute__((noinline))
static void blurRow(
const unsigned char* __restrict__ row_m2,
const unsigned char* __restrict__ row_m1,
const unsigned char* __restrict__ row_0,
const unsigned char* __restrict__ row_p1,
const unsigned char* __restrict__ row_p2,
unsigned char* __restrict__ out_row)
{
#pragma omp simd safelen(16) simdlen(16)
for (int x = 2; x < WIDTH - 2; x++) {
int sum =
row_m2[x-2]*1 + row_m2[x-1]*4 + row_m2[x]*6 + row_m2[x+1]*4 + row_m2[x+2]*1 +
row_m1[x-2]*4 + row_m1[x-1]*16+ row_m1[x]*24+ row_m1[x+1]*16+ row_m1[x+2]*4 +
row_0 [x-2]*6 + row_0 [x-1]*24+ row_0 [x]*36+ row_0 [x+1]*24+ row_0 [x+2]*6 +
row_p1[x-2]*4 + row_p1[x-1]*16+ row_p1[x]*24+ row_p1[x+1]*16+ row_p1[x+2]*4 +
row_p2[x-2]*1 + row_p2[x-1]*4 + row_p2[x]*6 + row_p2[x+1]*4 + row_p2[x+2]*1;
out_row[x] = sum / 256;
}
}
// ==========================================================
// TASK B - GAUSSIAN BLUR
// ==========================================================
void gaussianBlurSIMD(const unsigned char* __restrict__ input,
unsigned char* __restrict__ output)
{
const unsigned char* in =
(const unsigned char*)__builtin_assume_aligned(input, 32);
unsigned char* out =
(unsigned char*)__builtin_assume_aligned(output, 32);
#pragma omp parallel for schedule(static)
for (int y = 2; y < HEIGHT - 2; y++) {
blurRow(
in + (y-2)*WIDTH,
in + (y-1)*WIDTH,
in + (y )*WIDTH,
in + (y+1)*WIDTH,
in + (y+2)*WIDTH,
out + y*WIDTH
);
}
}
// ==========================================================
// MAIN
// ==========================================================
int main() {
omp_set_num_threads(2);
cout << "=============================\n";
cout << "OPENMP SIMD PROJECT\n";
cout << "=============================\n";
cout << "Threads: " << omp_get_max_threads() << "\n";
// ======================================================
// ARRAYS
// ======================================================
vector<Pixel> mandelbrot(WIDTH * HEIGHT);
unsigned char* gray =
(unsigned char*)aligned_alloc(32, WIDTH * HEIGHT);
unsigned char* blurred =
(unsigned char*)aligned_alloc(32, WIDTH * HEIGHT);
// ======================================================
// TASK A
// ======================================================
cout << "\nGenerating 8K Mandelbrot...\n";
auto startA = chrono::high_resolution_clock::now();
generateMandelbrot(mandelbrot);
auto endA = chrono::high_resolution_clock::now();
chrono::duration<double> timeA = endA - startA;
cout << "Task A Time: " << timeA.count() << " seconds\n";
savePPM("mandelbrot.ppm", mandelbrot);
// ======================================================
// RGB -> GRAY
// ======================================================
cout << "\nConverting to grayscale...\n";
convertToGray(mandelbrot, gray);
saveGrayPPM("gray.ppm", gray, WIDTH * HEIGHT);
// ======================================================
// TASK B
// ======================================================
cout << "\nApplying SIMD Gaussian Blur...\n";
auto startB = chrono::high_resolution_clock::now();
gaussianBlurSIMD(gray, blurred);
auto endB = chrono::high_resolution_clock::now();
chrono::duration<double> timeB = endB - startB;
cout << "Task B Time: " << timeB.count() << " seconds\n";
saveGrayPPM("blurred_simd.ppm", blurred, WIDTH * HEIGHT);
cout << "\nDone.\n";
free(gray);
free(blurred);
return 0;
}