-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainParalelo.cpp
More file actions
262 lines (187 loc) · 6.21 KB
/
Copy pathmainParalelo.cpp
File metadata and controls
262 lines (187 loc) · 6.21 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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <chrono>
#include <omp.h>
using namespace std;
// ==========================================================
// CONFIGURACIÓN
// ==========================================================
const int WIDTH = 7680;
const int HEIGHT = 4320;
const int MAX_ITER = 1000;
// ==========================================================
// ESTRUCTURA RGB
// ==========================================================
struct Pixel {
unsigned char r, g, b;
};
// ==========================================================
// GUARDAR IMAGEN PPM
// ==========================================================
void savePPM(const string& filename,
const vector<Pixel>& image,
int width,
int height) {
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();
}
// ==========================================================
// TAREA A: MANDELBROT PARALELO
// ==========================================================
void generateMandelbrot(vector<Pixel>& image,
int width,
int height) {
double xmin = -2.5;
double xmax = 1.0;
double ymin = -1.0;
double ymax = 1.0;
#pragma omp parallel for schedule(dynamic)
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;
}
}
}
// ==========================================================
// TAREA B: FILTRO GAUSSIANO PARALELO
// ==========================================================
void gaussianBlur(const vector<Pixel>& input,
vector<Pixel>& output,
int width,
int height) {
const int kernelSize = 5;
const int radius = kernelSize / 2;
int kernel[5][5] = {
{1, 4, 6, 4, 1},
{4, 16, 24, 16, 4},
{6, 24, 36, 24, 6},
{4, 16, 24, 16, 4},
{1, 4, 6, 4, 1}
};
int kernelSum = 256;
#pragma omp parallel for schedule(dynamic)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = 0;
int g = 0;
int b = 0;
for (int ky = -radius;
ky <= radius;
ky++) {
for (int kx = -radius;
kx <= radius;
kx++) {
int px =
min(max(x + kx, 0),
width - 1);
int py =
min(max(y + ky, 0),
height - 1);
int weight =
kernel[ky + radius]
[kx + radius];
const Pixel& p =
input[py * width + px];
r += p.r * weight;
g += p.g * weight;
b += p.b * weight;
}
}
Pixel result;
result.r = r / kernelSum;
result.g = g / kernelSum;
result.b = b / kernelSum;
output[y * width + x] = result;
}
}
}
// ==========================================================
// MAIN
// ==========================================================
int main() {
cout << "=== VERSION PARALELA OPENMP ===" << endl;
cout << "Hilos disponibles: "
<< omp_get_max_threads()
<< endl;
vector<Pixel> image(WIDTH * HEIGHT);
vector<Pixel> blurred(WIDTH * HEIGHT);
// ------------------------------------------------------
// MANDELBROT
// ------------------------------------------------------
auto startA =
chrono::high_resolution_clock::now();
cout << "Generando Mandelbrot..."
<< endl;
generateMandelbrot(image,
WIDTH,
HEIGHT);
auto endA =
chrono::high_resolution_clock::now();
chrono::duration<double> durationA =
endA - startA;
cout << "Tiempo Mandelbrot: "
<< durationA.count()
<< " segundos" << endl;
savePPM("mandelbrot_parallel.ppm",
image,
WIDTH,
HEIGHT);
// ------------------------------------------------------
// FILTRO
// ------------------------------------------------------
auto startB =
chrono::high_resolution_clock::now();
cout << "Aplicando desenfoque..."
<< endl;
gaussianBlur(image,
blurred,
WIDTH,
HEIGHT);
auto endB =
chrono::high_resolution_clock::now();
chrono::duration<double> durationB =
endB - startB;
cout << "Tiempo Filtro: "
<< durationB.count()
<< " segundos" << endl;
savePPM("mandelbrot_blur_parallel.ppm",
blurred,
WIDTH,
HEIGHT);
cout << "Proceso completado."
<< endl;
return 0;
}