-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase_parallel.cpp
More file actions
147 lines (128 loc) · 4.28 KB
/
Copy pathcodebase_parallel.cpp
File metadata and controls
147 lines (128 loc) · 4.28 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
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
#include <fstream>
#include <cstring>
#include <omp.h>
using namespace std;
using namespace chrono;
// Image structure
struct Image {
int width;
int height;
vector<unsigned char> r;
vector<unsigned char> g;
vector<unsigned char> b;
vector<unsigned char> gray;
Image(int w, int h) : width(w), height(h) {
int size = w * h;
r.resize(size);
g.resize(size);
b.resize(size);
gray.resize(size);
}
};
// Function 1: Generate synthetic image data
void generateImageData(Image& img) {
int size = img.width * img.height;
#pragma omp parallel for
for (int i = 0; i < size; i++) {
img.r[i] = (i * 123) % 256;
img.g[i] = (i * 456) % 256;
img.b[i] = (i * 789) % 256;
}
}
// Function 2: Convert RGB to Grayscale (PARALLELIZABLE)
void convertToGrayscale(Image& img) {
int size = img.width * img.height;
#pragma omp parallel for
for (int i = 0; i < size; i++) {
img.gray[i] = (unsigned char)(0.299 * img.r[i] +
0.587 * img.g[i] +
0.114 * img.b[i]);
}
}
// Function 3: Calculate average grayscale value (PARALLELIZABLE - Reduction)
double calculateAverageGray(const Image& img) {
long long sum = 0;
int size = img.width * img.height;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < size; i++) {
sum += img.gray[i];
}
return (double)sum / size;
}
// Function 4: Apply brightness adjustment (PARALLELIZABLE)
void adjustBrightness(Image& img, int offset) {
int size = img.width * img.height;
#pragma omp parallel for
for (int i = 0; i < size; i++) {
int newVal = img.gray[i] + offset;
if (newVal > 255) newVal = 255;
if (newVal < 0) newVal = 0;
img.gray[i] = (unsigned char)newVal;
}
}
// Function 5: Apply threshold (PARALLELIZABLE)
void applyThreshold(Image& img, unsigned char threshold) {
int size = img.width * img.height;
#pragma omp parallel for
for (int i = 0; i < size; i++) {
if (img.gray[i] >= threshold) {
img.gray[i] = 255;
} else {
img.gray[i] = 0;
}
}
}
// Main function with profiling-friendly structure
int main(int argc, char* argv[]) {
// Default image size
int width = 1024;
int height = 1024;
// Parse command line arguments
if (argc > 1) {
width = atoi(argv[1]);
height = atoi(argv[2]);
}
cout << "=== Parallel Image Processing Benchmark (OpenMP) ===" << endl;
cout << "Image size: " << width << "x" << height << " pixels" << endl;
cout << "Total pixels: " << (width * height) << endl;
cout << "Number of threads: " << omp_get_max_threads() << endl << endl;
// Create image
Image img(width, height);
// Generate synthetic data
auto start = high_resolution_clock::now();
generateImageData(img);
auto end = high_resolution_clock::now();
cout << "Data generation: "
<< duration_cast<milliseconds>(end - start).count() << " ms" << endl;
// RGB to Grayscale conversion
start = high_resolution_clock::now();
convertToGrayscale(img);
end = high_resolution_clock::now();
cout << "Grayscale conversion: "
<< duration_cast<milliseconds>(end - start).count() << " ms" << endl;
// Calculate average
start = high_resolution_clock::now();
double avgGray = calculateAverageGray(img);
end = high_resolution_clock::now();
cout << "Average calculation: "
<< duration_cast<milliseconds>(end - start).count()
<< " ms (avg = " << avgGray << ")" << endl;
// Brightness adjustment
start = high_resolution_clock::now();
adjustBrightness(img, 20);
end = high_resolution_clock::now();
cout << "Brightness adjustment: "
<< duration_cast<milliseconds>(end - start).count() << " ms" << endl;
// Threshold
start = high_resolution_clock::now();
applyThreshold(img, 128);
end = high_resolution_clock::now();
cout << "Threshold application: "
<< duration_cast<milliseconds>(end - start).count() << " ms" << endl;
cout << "\n=== Processing Complete ===" << endl;
return 0;
}