forked from oneapi-src/oneAPI-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCT.cpp
More file actions
322 lines (277 loc) · 12 KB
/
DCT.cpp
File metadata and controls
322 lines (277 loc) · 12 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
#include "DCT.hpp"
#include <sycl/sycl.hpp>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "dpc_common.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace dpc_common;
using namespace sycl;
#ifdef PERF_NUM
constexpr int num_tests = 5;
#endif
constexpr int block_dims = 8;
constexpr int block_size = 64;
// API for creating 8x8 DCT matrix
void CreateDCT(float matrix[block_size]) {
int temp[block_dims];
for (int i = 0; i < block_dims; ++i) temp[i] = i;
for (int i = 0; i < block_dims; ++i) {
for (int j = 0; j < block_dims; ++j) {
if (i == 0)
matrix[(i * block_dims) + j] = (1 / sycl::sqrt((float)block_dims));
else
matrix[(i * block_dims) + j] =
sycl::sqrt((float)2 / block_dims) *
sycl::cos(((((float)2 * temp[j]) + 1) * i * 3.14f) /
(2 * block_dims));
}
}
}
// Transposes an 8x8 matrix x and writes output to xinv
void MatrixTranspose(float x[block_size], float xinv[block_size]) {
for (int i = 0; i < block_dims; ++i) {
for (int j = 0; j < block_dims; ++j)
xinv[(j * block_dims) + i] = x[(i * block_dims) + j];
}
}
// Multiply two matrices x and y and write output to xy
SYCL_EXTERNAL void MatrixMultiply(const float x[block_size],
const float y[block_size],
float xy[block_size]) {
for (int i = 0; i < block_dims; ++i) {
for (int j = 0; j < block_dims; ++j) {
xy[(i * block_dims) + j] = 0;
for (int k = 0; k < block_dims; ++k)
xy[(i * block_dims) + j] +=
(x[(i * block_dims) + k] * y[(k * block_dims) + j]);
}
}
}
// Processes an individual 8x8 subset of image data
SYCL_EXTERNAL void ProcessBlock(multi_ptr<const rgb, access::address_space::global_space, sycl::access::decorated::no> indataset,
multi_ptr<rgb, access::address_space::global_space, sycl::access::decorated::no> outdataset,
multi_ptr<const float, access::address_space::global_space, sycl::access::decorated::no> dct,
multi_ptr<const float, access::address_space::global_space, sycl::access::decorated::no> dctinv,
int start_index, int width) {
float interim[block_size], product[block_size], red_input[block_size],
blue_input[block_size], green_input[block_size], temp[block_size];
/*
// Quantization matrix which does 50% quantization
float quant[64] = {16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99 };
*/
// Quantization matrix which does 90% quantization
float quant[64] = {3, 2, 2, 3, 5, 8, 10, 12,
2, 2, 3, 4, 5, 12, 12, 11,
3, 3, 3, 5, 8, 11, 14, 11,
3, 3, 4, 6, 10, 17, 16, 12,
4, 4, 7, 11, 14, 22, 21, 15,
5, 7, 11, 13, 16, 12, 23, 18,
10, 13, 16, 17, 21, 24, 24, 21,
14, 18, 19, 20, 22, 20, 20, 20};
/*
// Quantization matrix which does 10% quantization
float quant[64] = {80, 60, 50, 80, 120, 200, 255, 255,
55, 60, 70, 95, 130, 255, 255, 255,
70, 65, 80, 120, 200, 255, 255, 255,
70, 85, 110, 145, 255, 255, 255, 255,
90, 110, 185, 255, 255, 255, 255, 255,
120, 175, 255, 255, 255, 255, 255, 255,
245, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255 };
*/
// PROCESS RED CHANNEL
// Translating the pixels values from [0, 255] range to [-128, 127] range
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
red_input[i] = indataset[start_index + pixel_index].red;
red_input[i] -= 128;
}
// Computation of the discrete cosine transform of the image section of size
// 8x8 for red values
MatrixMultiply(dct.get(), red_input, temp);
MatrixMultiply(temp, dctinv.get(), interim);
// Computation of quantization phase using the quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] / quant[i]) + 0.5f);
// Computation of dequantizing phase using the same above quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] * quant[i]) + 0.5f);
// Computation of Inverse Discrete Cosine Transform (IDCT)
MatrixMultiply(dctinv.get(), interim, temp);
MatrixMultiply(temp, dct.get(), product);
// Translating the pixels values from [-128, 127] range to [0, 255] range
// and writing to output image data
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
float temp = (product[i] + 128);
outdataset[start_index + pixel_index].red =
(temp > 255.f) ? 255 : (unsigned char)temp;
}
// PROCESS BLUE CHANNEL
// Translating the pixels values from [0, 255] range to [-128, 127] range
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
blue_input[i] = indataset[start_index + pixel_index].blue;
blue_input[i] -= 128;
}
// Computation of the discrete cosine transform of the image section of size
// 8x8 for blue values
MatrixMultiply(dct.get(), blue_input, temp);
MatrixMultiply(temp, dctinv.get(), interim);
// Computation of quantization phase using the quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] / quant[i]) + 0.5f);
// Computation of dequantizing phase using the same above quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] * quant[i]) + 0.5f);
// Computation of Inverse Discrete Cosine Transform (IDCT)
MatrixMultiply(dctinv.get(), interim, temp);
MatrixMultiply(temp, dct.get(), product);
// Translating the pixels values from [-128, 127] range to [0, 255] range
// and writing to output image data
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
float temp = product[i] + 128;
outdataset[start_index + pixel_index].blue =
(temp > 255.f) ? 255 : (unsigned char)temp;
}
// PROCESS GREEN CHANNEL
// Translating the pixels values from [0, 255] range to [-128, 127] range
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
green_input[i] = indataset[start_index + pixel_index].green;
green_input[i] -= 128;
}
// Computation of the discrete cosine transform of the image section of size
// 8x8 for green values
MatrixMultiply(dct.get(), green_input, temp);
MatrixMultiply(temp, dctinv.get(), interim);
// Computation of quantization phase using the quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] / quant[i]) + 0.5f);
// Computation of dequantizing phase using the same above quantization matrix
for (int i = 0; i < block_size; ++i)
interim[i] = sycl::floor((interim[i] * quant[i]) + 0.5f);
// Computation of Inverse Discrete Cosine Transform (IDCT)
MatrixMultiply(dctinv.get(), interim, temp);
MatrixMultiply(temp, dct.get(), product);
// Translating the pixels values from [-128, 127] range to [0, 255] range
// and writing to output image data
for (int i = 0; i < block_size; ++i) {
int pixel_index = i / block_dims * width + i % block_dims;
float temp = product[i] + 128;
outdataset[start_index + pixel_index].green =
(temp > 255.f) ? 255 : (unsigned char)temp;
}
}
// Breaks the image into 8x8 blocks to process DCT
void ProcessImage(rgb* indataset, rgb* outdataset, int width, int height) {
sycl::queue q(default_selector_v, exception_handler);
std::cout << "Running on "
<< q.get_device().get_info<sycl::info::device::name>() << "\n";
try {
int image_size = width * height;
float dct[block_size], dctinv[block_size];
// Creation of 8x8 DCT matrix
CreateDCT(dct);
// Creating a transpose of DCT matrix
MatrixTranspose(dct, dctinv);
buffer indata_buf(indataset, range<1>(image_size));
buffer outdata_buf(outdataset, range<1>(image_size));
buffer dct_buf(dct, range<1>(block_size));
buffer dctinv_buf(dctinv, range<1>(block_size));
q.submit([&](handler& h) {
auto i_acc = indata_buf.get_access(h,read_only);
auto o_acc = outdata_buf.get_access(h);
auto d_acc = dct_buf.get_access(h,read_only);
auto di_acc = dctinv_buf.get_access(h,read_only);
// Processes individual 8x8 chunks in parallel
h.parallel_for(
range<2>(width / block_dims, height / block_dims), [=](auto idx) {
int start_index = idx[0] * block_dims + idx[1] * block_dims * width;
ProcessBlock(i_acc.get_multi_ptr<sycl::access::decorated::no>(), o_acc.get_multi_ptr<sycl::access::decorated::no>(),
d_acc.get_multi_ptr<sycl::access::decorated::no>(), di_acc.get_multi_ptr<sycl::access::decorated::no>(), start_index,
width);
});
});
//q.wait_and_throw();
} catch (sycl::exception e) {
std::cout << "SYCL exception caught: " << e.what() << "\n";
exit(1);
}
}
// This API does the reading and writing from/to the .bmp file. Also invokes the
// image processing API from here
int ReadProcessWrite(char* input, char* output) {
double timersecs;
#ifdef PERF_NUM
double avg_timersecs = 0;
#endif
// Read in the data from the input image file
int image_width = 0, image_height = 0, num_channels = 0;
rgb* indata = (rgb*)stbi_load(input, &image_width, &image_height,
&num_channels, STBI_rgb);
if (!indata) {
std::cout << "The input file could not be opened. Program will now exit\n";
return 1;
} else if (num_channels != 3) {
std::cout
<< "The input file must be an RGB bmp image. Program will now exit\n";
return 1;
} else if (image_width % block_dims != 0 || image_height % block_dims != 0) {
std::cout
<< "The input image must have dimensions which are a multiple of 8\n";
return 1;
}
std::cout << "Filename: " << input << " W: " << image_width
<< " H: " << image_height << "\n\n";
rgb* outdata = (rgb*)malloc(image_width * image_height * sizeof(rgb));
// Invoking the DCT/Quantization API which does some manipulation on the
// bitmap data read from the input .bmp file
#ifdef PERF_NUM
std::cout << "Run all tests...\n\n";
for (int j = 0; j < num_tests; ++j) {
#endif
std::cout << "Start image processing with offloading to GPU...\n";
{
TimeInterval t;
ProcessImage(indata, outdata, image_width, image_height);
timersecs = t.Elapsed();
}
std::cout << "--The processing time is " << timersecs << " seconds\n\n";
#ifdef PERF_NUM
avg_timersecs += timersecs;
}
#endif
stbi_write_bmp(output, image_width, image_height, 3, outdata);
std::cout << "DCT successfully completed on the device.\n"
"The processed image has been written to " << output << "\n";
#ifdef PERF_NUM
std::cout << "\nAverage time for image processing:\n";
std::cout << "--The average processing time was "
<< avg_timersecs / (float)num_tests << " seconds\n";
#endif
// Freeing dynamically allocated memory
stbi_image_free(indata);
std::free(outdata);
return 0;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Program usage is <modified_program> <inputfile.bmp> "
"<outputfile.bmp>\n";
return 1;
}
return ReadProcessWrite(argv[1], argv[2]);
}