-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlurExprmt.c
537 lines (419 loc) · 15.6 KB
/
BlurExprmt.c
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdint.h>
#include "Utils.h"
#include "Picture.h"
#include "PicProcess.h"
#include "C-Thread-Pool/thpool.h"
#include <unistd.h>
// Blur pre-definied MACROS
#define NO_RGB_COMPONENTS 3
#define BLUR_REGION_SIZE 9
#define NO_THREADS 4 // Number of threads for our process
void copy_picture(struct picture *new_pic, struct picture* pic);
// Global time variables used to store start and end times for a process.
struct timespec start, end;
/* 0 - Purely sequential blur without using any pthread functionality */
void threadless_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
blur_picture(pic);
save_picture_to_file(pic, "blurtest/threadless.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("0. Threadless Blur Elapsed Time: %ld\n\n", delta_us);
}
/* 1 - Sequential blur using only 1 thread in the threadpool */
void blur_task(void *pic) {
blur_picture((struct picture *) pic);
}
void sequential_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with 1 threads\n");
threadpool thpool = thpool_init(1);
thpool_add_work(thpool, blur_task, (void*)(uintptr_t) pic);
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
save_picture_to_file(pic, "blurtest/sequential.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("1. Sequential Blur Elapsed Time: %ld\n\n", delta_us);
}
// Arguments struct for row_by_row_blur and col_by_col_blur
struct pic_args {
struct picture *tmp;
struct picture *pic;
int index;
};
/* 2 - Row by row parallel blur */
void row_by_row_task(void *pic_args) {
struct pic_args *temp_pic_args = (struct pic_args *) pic_args;
struct picture *tmp = temp_pic_args->tmp;
struct picture *pic = temp_pic_args->pic;
int j = temp_pic_args->index;
for(int i = 1 ; i < tmp->width - 1; i++){
struct pixel rgb;
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
for(int n = -1; n <= 1; n++){
for(int m = -1; m <= 1; m++){
rgb = get_pixel(tmp, i+n, j+m);
sum_red += rgb.red;
sum_green += rgb.green;
sum_blue += rgb.blue;
}
}
rgb.red = sum_red / BLUR_REGION_SIZE;
rgb.green = sum_green / BLUR_REGION_SIZE;
rgb.blue = sum_blue / BLUR_REGION_SIZE;
set_pixel(pic, i, j, &rgb);
}
free(pic_args);
}
void row_by_row_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with %d threads\n", NO_THREADS);
threadpool thpool = thpool_init(NO_THREADS);
struct picture tmp;
tmp.img = copy_image(pic->img);
tmp.width = pic->width;
tmp.height = pic->height;
// Iterates over the rows of the image
for(int j = 1 ; j < tmp.height - 1; j++){
struct pic_args *pic_args2 = malloc(sizeof (struct pic_args));
pic_args2->tmp = &tmp;
pic_args2->pic = pic;
pic_args2->index = j;
thpool_add_work(thpool, row_by_row_task, (void*)(uintptr_t) pic_args2);
}
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
clear_picture(&tmp);
save_picture_to_file(pic, "blurtest/row_by_row.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("2. Row-by-row Blur Elapsed Time: %ld\n\n", delta_us);
}
/* 3 - Column by column parallel blur */
void col_by_col_task(void *pic_args) {
struct pic_args *temp_pic_args = (struct pic_args *) pic_args;
struct picture *tmp = temp_pic_args->tmp;
struct picture *pic = temp_pic_args->pic;
int i = temp_pic_args->index;
for(int j = 1 ; j < tmp->height - 1; j++){
struct pixel rgb;
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
for(int n = -1; n <= 1; n++){
for(int m = -1; m <= 1; m++){
rgb = get_pixel(tmp, i+n, j+m);
sum_red += rgb.red;
sum_green += rgb.green;
sum_blue += rgb.blue;
}
}
rgb.red = sum_red / BLUR_REGION_SIZE;
rgb.green = sum_green / BLUR_REGION_SIZE;
rgb.blue = sum_blue / BLUR_REGION_SIZE;
set_pixel(pic, i, j, &rgb);
}
free(pic_args);
}
void col_by_col_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with %d threads\n", NO_THREADS);
threadpool thpool = thpool_init(NO_THREADS);
struct picture tmp;
tmp.img = copy_image(pic->img);
tmp.width = pic->width;
tmp.height = pic->height;
// Iterates over the columns of the image
for(int i = 1 ; i < tmp.width - 1; i++){
struct pic_args *pic_args2 = malloc(sizeof (struct pic_args));
pic_args2->tmp = &tmp;
pic_args2->pic = pic;
pic_args2->index = i;
thpool_add_work(thpool, col_by_col_task, (void*)(uintptr_t) pic_args2);
}
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
clear_picture(&tmp);
save_picture_to_file(pic, "blurtest/col_by_col.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("3. Column-by-column Blur Elapsed Time: %ld\n\n", delta_us);
}
/* 4 - Sector-by-sector (1/2) parallel blur */
// Arguments struct for half_sector_blur
struct half_sector_pic_args {
struct picture *tmp;
struct picture *pic;
int start_x_coord;
int end_x_coord;
};
void half_sector_task(void *pic_args) {
struct half_sector_pic_args *temp_pic_args = (struct half_sector_pic_args *) pic_args;
struct picture *tmp = temp_pic_args->tmp;
struct picture *pic = temp_pic_args->pic;
int start_x_coord = temp_pic_args->start_x_coord;
int end_x_coord = temp_pic_args->end_x_coord;
for(int i = start_x_coord ; i < end_x_coord; i++){
for(int j = 1 ; j < tmp->height - 1; j++){
struct pixel rgb;
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
for(int n = -1; n <= 1; n++){
for(int m = -1; m <= 1; m++){
rgb = get_pixel(tmp, i+n, j+m);
sum_red += rgb.red;
sum_green += rgb.green;
sum_blue += rgb.blue;
}
}
rgb.red = sum_red / BLUR_REGION_SIZE;
rgb.green = sum_green / BLUR_REGION_SIZE;
rgb.blue = sum_blue / BLUR_REGION_SIZE;
set_pixel(pic, i, j, &rgb);
}
}
free(pic_args);
}
void half_sector_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with %d threads\n", NO_THREADS);
threadpool thpool = thpool_init(NO_THREADS);
struct picture tmp;
tmp.img = copy_image(pic->img);
tmp.width = pic->width;
tmp.height = pic->height;
// Calculate midpoint of image (horizontally) to divide image into 2 sectors
int midpoint = pic->width / 2;
// Assign arguments and allocate a thread to blur left half of image
struct half_sector_pic_args *pic_args1 = malloc(sizeof (struct half_sector_pic_args));
pic_args1->tmp = &tmp;
pic_args1->pic = pic;
pic_args1->start_x_coord = 1;
pic_args1->end_x_coord = midpoint;
thpool_add_work(thpool, half_sector_task, (void*)(uintptr_t) pic_args1);
// Assign arguments and allocate a thread to blur right half of image
struct half_sector_pic_args *pic_args2 = malloc(sizeof (struct half_sector_pic_args));
pic_args2->tmp = &tmp;
pic_args2->pic = pic;
pic_args2->start_x_coord = midpoint;
pic_args2->end_x_coord = pic->width - 1;
thpool_add_work(thpool, half_sector_task, (void*)(uintptr_t) pic_args2);
thpool_wait(thpool);
printf("Killing threadpool\n");
thpool_destroy(thpool);
clear_picture(&tmp);
save_picture_to_file(pic, "blurtest/half_sector.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("4. Half-sector Blur Elapsed Time: %ld\n\n", delta_us);
}
/* 5 - Sector-by-sector (1/4) parallel blur */
// Arguments struct for quarter_sector_blur
struct quarter_sector_pic_args {
struct picture *tmp;
struct picture *pic;
int start_x_coord;
int end_x_coord;
int start_y_coord;
int end_y_coord;
};
void quarter_sector_task(void *pic_args) {
struct quarter_sector_pic_args *temp_pic_args = (struct quarter_sector_pic_args *) pic_args;
struct picture *tmp = temp_pic_args->tmp;
struct picture *pic = temp_pic_args->pic;
int start_x_coord = temp_pic_args->start_x_coord;
int end_x_coord = temp_pic_args->end_x_coord;
int start_y_coord = temp_pic_args->start_y_coord;
int end_y_coord = temp_pic_args->end_y_coord;
for(int i = start_x_coord ; i < end_x_coord; i++){
for(int j = start_y_coord ; j < end_y_coord; j++){
struct pixel rgb;
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
for(int n = -1; n <= 1; n++){
for(int m = -1; m <= 1; m++){
rgb = get_pixel(tmp, i+n, j+m);
sum_red += rgb.red;
sum_green += rgb.green;
sum_blue += rgb.blue;
}
}
rgb.red = sum_red / BLUR_REGION_SIZE;
rgb.green = sum_green / BLUR_REGION_SIZE;
rgb.blue = sum_blue / BLUR_REGION_SIZE;
set_pixel(pic, i, j, &rgb);
}
}
free(pic_args);
}
void quarter_sector_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with %d threads\n", NO_THREADS);
threadpool thpool = thpool_init(NO_THREADS);
struct picture tmp;
tmp.img = copy_image(pic->img);
tmp.width = pic->width;
tmp.height = pic->height;
// Calculate midpoint of image (horizontally and vertically) to divide image into 2 sectors
int x_midpoint = pic->height / 2;
int y_midpoint = pic->width / 2;
// Assign arguments and allocate a thread to blur top-left of image
struct quarter_sector_pic_args *pic_args1 = malloc(sizeof (struct quarter_sector_pic_args));
pic_args1->tmp = &tmp;
pic_args1->pic = pic;
pic_args1->start_x_coord = 1;
pic_args1->end_x_coord = x_midpoint;
pic_args1->start_y_coord = 1;
pic_args1->end_y_coord = y_midpoint;
thpool_add_work(thpool, quarter_sector_task, (void*)(uintptr_t) pic_args1);
// Assign arguments and allocate a thread to blur top-right of image
struct quarter_sector_pic_args *pic_args2 = malloc(sizeof (struct quarter_sector_pic_args));
pic_args2->tmp = &tmp;
pic_args2->pic = pic;
pic_args2->start_x_coord = x_midpoint;
pic_args2->end_x_coord = pic->width - 1;
pic_args2->start_y_coord = 1;
pic_args2->end_y_coord = y_midpoint;
thpool_add_work(thpool, quarter_sector_task, (void*)(uintptr_t) pic_args2);
// Assign arguments and allocate a thread to blur bottom-left of image
struct quarter_sector_pic_args *pic_args3 = malloc(sizeof (struct quarter_sector_pic_args));
pic_args3->tmp = &tmp;
pic_args3->pic = pic;
pic_args3->start_x_coord = 1;
pic_args3->end_x_coord = x_midpoint;
pic_args3->start_y_coord = y_midpoint;
pic_args3->end_y_coord = pic->height - 1;
thpool_add_work(thpool, quarter_sector_task, (void*)(uintptr_t) pic_args3);
// Assign arguments and allocate a thread to blur top-right of image
struct quarter_sector_pic_args *pic_args4 = malloc(sizeof (struct quarter_sector_pic_args));
pic_args4->tmp = &tmp;
pic_args4->pic = pic;
pic_args4->start_x_coord = x_midpoint;
pic_args4->end_x_coord = pic->width - 1;
pic_args4->start_y_coord = y_midpoint;
pic_args4->end_y_coord = pic->height - 1;
thpool_add_work(thpool, quarter_sector_task, (void*)(uintptr_t) pic_args4);
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
clear_picture(&tmp);
save_picture_to_file(pic, "blurtest/quarter_sector.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("5. Quarter-sector Blur Elapsed Time: %ld\n\n", delta_us);
}
/* 6 - Pixel by pixel parallel blur */
// Arugments for pixel_by_pixel_blur to be passed to individual threads
struct pixel_pic_args {
struct picture *tmp;
struct picture *pic;
int i;
int j;
};
void pixel_by_pixel_task(void *pixel_pic_args) {
struct pixel_pic_args *temp_pic_args = (struct pixel_pic_args *) pixel_pic_args;
struct picture *tmp = temp_pic_args->tmp;
struct picture *pic = temp_pic_args->pic;
int i = temp_pic_args->i;
int j = temp_pic_args->j;
struct pixel rgb;
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
for(int n = -1; n <= 1; n++){
for(int m = -1; m <= 1; m++){
rgb = get_pixel(tmp, i+n, j+m);
sum_red += rgb.red;
sum_green += rgb.green;
sum_blue += rgb.blue;
}
}
rgb.red = sum_red / BLUR_REGION_SIZE;
rgb.green = sum_green / BLUR_REGION_SIZE;
rgb.blue = sum_blue / BLUR_REGION_SIZE;
set_pixel(pic, i, j, &rgb);
free(pixel_pic_args);
}
void pixel_by_pixel_blur(struct picture *pic) {
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
printf("Making threadpool with %d threads\n", NO_THREADS);
threadpool thpool = thpool_init(NO_THREADS);
struct picture tmp;
tmp.img = copy_image(pic->img);
tmp.width = pic->width;
tmp.height = pic->height;
// Iterates over the columns of the image
for(int i = 1 ; i < tmp.width - 1; i++){
for(int j = 1 ; j < tmp.height - 1; j++){
struct pixel_pic_args *pic_args = malloc(sizeof (struct pixel_pic_args));
pic_args->tmp = &tmp;
pic_args->pic = pic;
pic_args->i = i;
pic_args->j = j;
thpool_add_work(thpool, pixel_by_pixel_task, (void*)(uintptr_t) pic_args);
}
}
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
clear_picture(&tmp);
save_picture_to_file(pic, "blurtest/pixel_by_pixel.jpg");
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("6. Pixel-by-pixel Blur Elapsed Time: %ld\n\n", delta_us);
}
// ---------- MAIN PROGRAM ---------- \\
int main(int argc, char **argv){
printf("Support Code for Running the Blur Optimisation Experiments... \n");
char input_file[20] = "images/lake.jpeg";
struct picture pic;
init_picture_from_file(&pic, input_file);
/* Conduct tests for each of the 7 optimisation experiments
Each experiment prints out time elapsed to run the experiment */
struct picture threadless_pic;
copy_picture(&threadless_pic, &pic);
threadless_blur(&threadless_pic);
struct picture sequential_pic;
copy_picture(&sequential_pic, &pic);
sequential_blur(&sequential_pic);
struct picture row_by_row_pic;
copy_picture(&row_by_row_pic, &pic);
row_by_row_blur(&row_by_row_pic);
struct picture col_by_col_pic;
copy_picture(&col_by_col_pic, &pic);
col_by_col_blur(&col_by_col_pic);
struct picture half_sector_pic;
copy_picture(&half_sector_pic, &pic);
half_sector_blur(&half_sector_pic);
struct picture quarter_sector_pic;
copy_picture(&quarter_sector_pic, &pic);
quarter_sector_blur(&quarter_sector_pic);
struct picture pixel_by_pixel_pic;
copy_picture(&pixel_by_pixel_pic, &pic);
pixel_by_pixel_blur(&pixel_by_pixel_pic);
clear_picture(&pic);
clear_picture(&threadless_pic);
clear_picture(&sequential_pic);
clear_picture(&row_by_row_pic);
clear_picture(&col_by_col_pic);
clear_picture(&half_sector_pic);
clear_picture(&quarter_sector_pic);
clear_picture(&pixel_by_pixel_pic);
}
// Copies pic to new_pic
void copy_picture(struct picture *new_pic, struct picture* pic) {
new_pic->img = copy_image(pic->img);
new_pic->width = pic->width;
new_pic->height = pic->height;
}