-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCTPar.cpp
More file actions
323 lines (257 loc) · 9.46 KB
/
Copy pathDCTPar.cpp
File metadata and controls
323 lines (257 loc) · 9.46 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
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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <chrono>
#include <opencv2/opencv.hpp>
#include <omp.h>
using namespace std;
using namespace cv;
static const float PI = 3.14159265358979323846f;
///////////////////////////////////
//// DCT TABLES STRUCTURE /////////
///////////////////////////////////
struct DCTTables {
int N;
vector<float> alpha;
vector<float> cosTable;
DCTTables(int N_) : N(N_), alpha(N_), cosTable(N_ * N_) {
for (int k = 0; k < N; ++k) {
alpha[k] = (k == 0) ? sqrtf(1.0f / N) : sqrtf(2.0f / N);
}
for (int k = 0; k < N; ++k) {
for (int n = 0; n < N; ++n) {
cosTable[k * N + n] = cosf(PI * (2.0f * n + 1.0f) * k / (2.0f * N));
}
}
}
};
////////////////////////////////
//// MATRIX OPERATIONS /////////
////////////////////////////////
vector<vector<float>> subtractMatrices(const vector<vector<float>>& a, const vector<vector<float>>& b) {
int H = a.size();
int W = a[0].size();
vector<vector<float>> result(H, vector<float>(W));
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) {
result[r][c] = a[r][c] - b[r][c];
}
}
return result;
}
///////////////////////////////
/////// IMAGE LOADING /////////
///////////////////////////////
vector<vector<float>> loadImageFromDisk(const string& filename) {
//// GREYSCALE ////
Mat img = imread(filename, IMREAD_GRAYSCALE);
if (img.empty()) {
cerr << "Errore: impossibile caricare l'immagine " << filename << endl;
return {};
}
int H = img.rows;
int W = img.cols;
//// CONVERT TO VECTOR<VECTOR<FLOAT>> ////
vector<vector<float>> result(H, vector<float>(W));
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) {
result[r][c] = (float)img.at<uchar>(r, c);
}
}
return result;
}
///////////////////////////////////
/////// BLOCK EXTRACTION //////////
///////////////////////////////////
vector<vector<float>> extractBlock(const vector<vector<float>>& img, int startRow, int startCol, int blockSize) {
vector<vector<float>> block(blockSize, vector<float>(blockSize, 0.0f));
int imgHeight = img.size();
int imgWidth = img[0].size();
//// COPY BLOCK DATA ////
for (int r = 0; r < blockSize; r++) {
for (int c = 0; c < blockSize; c++) {
if (startRow + r < imgHeight && startCol + c < imgWidth) {
block[r][c] = img[startRow + r][startCol + c];
}
}
}
return block;
}
///////////////////////////////
///// 2D DCT WITH TABLES //////
///////////////////////////////
vector<vector<float>> dct2d(const vector<vector<float>>& img, const DCTTables& tables) {
int H = img.size();
int W = img[0].size();
vector<vector<float>> out(H, vector<float>(W));
//// PRECALCULATED TABLES ////
const float* alphaArr = tables.alpha.data();
const float* cosArr = tables.cosTable.data();
//// DCT COMPUTATION ////
for (int u = 0; u < H; u++) {
for (int v = 0; v < W; v++) {
float sum = 0.0f;
for (int x = 0; x < H; x++) {
float cos_u = cosArr[u * tables.N + x];
for (int y = 0; y < W; y++) {
float cos_v = cosArr[v * tables.N + y];
sum += img[x][y] * cos_u * cos_v;
}
}
//// STORE RESULT ////
out[u][v] = alphaArr[u] * alphaArr[v] * sum;
}
}
return out;
}
//////////////////////////////
//// 2D IDCT WITH TABLES ////
//////////////////////////////
vector<vector<float>> idct2d(const vector<vector<float>>& X, const DCTTables& tables) {
int H = X.size();
int W = X[0].size();
vector<vector<float>> out(H, vector<float>(W));
//// PRECALCULATED TABLES ////
const float* alphaArr = tables.alpha.data();
const float* cosArr = tables.cosTable.data();
//// IDCT COMPUTATION ////
for (int x = 0; x < H; x++) {
for (int y = 0; y < W; y++) {
float sum = 0.0f;
for (int u = 0; u < H; u++) {
float alpha_u = alphaArr[u];
float cos_u = cosArr[u * tables.N + x];
for (int v = 0; v < W; v++) {
float alpha_v = alphaArr[v];
float cos_v = cosArr[v * tables.N + y];
sum += alpha_u * alpha_v * X[u][v] * cos_u * cos_v;
}
}
//// STORE RESULT ////
out[x][y] = sum;
}
}
return out;
}
//////////////////////////////
//// PROCESS IMAGE BLOCKS ////
//////////////////////////////
void processImageBlocks(const vector<vector<float>>& img,
vector<vector<float>>& dctComplete,
vector<vector<float>>& reconstructed,
int blockSize,
const DCTTables& tables) {
int imgHeight = img.size();
int imgWidth = img[0].size();
int totalBlocks = ((imgHeight + blockSize - 1) / blockSize) *
((imgWidth + blockSize - 1) / blockSize);
int blocksProcessed = 0;
#pragma omp parallel default(none) shared(img, dctComplete, reconstructed, imgHeight, imgWidth, blockSize, blocksProcessed, tables)
{
//// LOCAL THREAD BUFFER ////
vector<vector<float>> block(blockSize, vector<float>(blockSize));
#pragma omp for collapse(2) schedule(dynamic) nowait
for (int startRow = 0; startRow < imgHeight; startRow += blockSize) {
for (int startCol = 0; startCol < imgWidth; startCol += blockSize) {
int actualH = min(blockSize, imgHeight - startRow);
int actualW = min(blockSize, imgWidth - startCol);
//// COPY DATA TO LOCAL BUFFER ////
for (int r = 0; r < actualH; r++) {
for (int c = 0; c < actualW; c++) {
block[r][c] = img[startRow + r][startCol + c];
}
}
//// PADDING WITH ZEROS ////
for (int r = actualH; r < blockSize; r++) {
for (int c = 0; c < blockSize; c++) {
block[r][c] = 0.0f;
}
}
for (int r = 0; r < actualH; r++) {
for (int c = actualW; c < blockSize; c++) {
block[r][c] = 0.0f;
}
}
//// APPLY DCT 2D ////
auto X = dct2d(block, tables);
//// APPLY IDCT 2D ////
auto rec = idct2d(X, tables);
//// COPY RESULTS TO COMPLETE MATRICES ////
for (int r = 0; r < actualH; r++) {
for (int c = 0; c < actualW; c++) {
dctComplete[startRow + r][startCol + c] = X[r][c];
reconstructed[startRow + r][startCol + c] = rec[r][c];
}
}
#pragma omp atomic
blocksProcessed++;
}
}
}
}
///////////////////////
//// MAIN FUNCTION ////
///////////////////////
int main(int argc, char** argv) {
//// THREAD CONFIGURATION ////
int numThreads = omp_get_max_threads();
if (argc >= 3) {
numThreads = atoi(argv[2]);
omp_set_num_threads(numThreads);
}
//// IMAGE LOAD ////
string imagePath;
if (argc >= 2) {
imagePath = argv[1];
} else {
imagePath = "/home/lapemaya/PycharmProjects/SitoBorsa/lora/flux-controlnet-canny-Lora1.png";
}
auto img = loadImageFromDisk(imagePath);
if (img.empty()) {
cerr << "Errore: impossibile caricare l'immagine\n";
return 1;
}
//// IMAGE VARIABLES ////
int imgHeight = img.size();
int imgWidth = img[0].size();
int blockSize = 128;
int numBlocksH = (imgHeight + blockSize - 1) / blockSize;
int numBlocksW = (imgWidth + blockSize - 1) / blockSize;
//// MEMORY ALLOC ////
vector<vector<float>> dctComplete(imgHeight, vector<float>(imgWidth, 0.0f));
vector<vector<float>> reconstructed(imgHeight, vector<float>(imgWidth, 0.0f));
//// DCT TABLES ////
DCTTables tables(blockSize);
//// TIMER START ////
auto start = chrono::high_resolution_clock::now();
//// PROCESS IMAGE BLOCKS ////
processImageBlocks(img, dctComplete, reconstructed, blockSize, tables);
//// TIMER END ////
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
//// OUTPUT METRICS ////
cout << "\n=== TEMPO DI ELABORAZIONE: " << duration.count() << " ms ===\n";
cout << "Throughput: " << (numBlocksH * numBlocksW * 1000.0 / duration.count())
<< " blocchi/secondo\n\n";
//// COMPUTE ERROR METRICS ////
float maxError = 0.0f;
float sumError = 0.0f;
long long totalPixels = 0;
for (int r = 0; r < imgHeight; r++) {
for (int c = 0; c < imgWidth; c++) {
float err = fabsf(reconstructed[r][c] - img[r][c]);
maxError = max(maxError, err);
sumError += err;
totalPixels++;
}
}
//// PRINT METRICS ////
float avgError = sumError / totalPixels;
cout << "=== METRICHE DI QUALITÀ ===\n";
cout << "Errore medio di ricostruzione: " << avgError << "\n";
cout << "Errore massimo di ricostruzione: " << maxError << "\n";
cout << "Pixel totali processati: " << totalPixels << "\n\n";
return 0;
}