This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_filtering_mpi.c
72 lines (57 loc) · 1.83 KB
/
image_filtering_mpi.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
#include "mpi.h"
#include "./image_filtering.h"
#include "./image_IO.h"
#define MASTER 0
// Each task applies the filter on it's images. If the hibrid implementation
// is run, then each image is split into pieces by openmp.
void apply_filter(int nr_of_images, u_int8_t **brightness, u_int8_t ***images,
uint **widths, uint **heights)
{
for (int i = 0; i < nr_of_images; ++i) {
u_int64_t image_size = 4LL * (*widths)[i] * (*heights)[i];
float factor = 2.f * ((float)((*brightness)[i])) / 100;
#pragma omp parallel for shared(images)
for (u_int64_t pixel = 0; pixel < image_size; pixel += 4LL) {
if (!(*images)[i][pixel + 3]) continue;
u_int16_t pixel_storage[3];
pixel_storage[0] = (*images)[i][pixel] * factor;
pixel_storage[1] = (*images)[i][pixel + 1] * factor;
pixel_storage[2] = (*images)[i][pixel + 2] * factor;
(*images)[i][pixel] = (pixel_storage[0] > 255) ?
255 : pixel_storage[0];
(*images)[i][pixel + 1] = (pixel_storage[1] > 255) ?
255 : pixel_storage[1];
(*images)[i][pixel + 2] = (pixel_storage[2] > 255) ?
255 : pixel_storage[2];
}
}
free(*brightness);
}
int main(int argc, char *argv[])
{
u_int8_t **images;
uint *widths;
uint *heights;
u_int8_t *brightness;
int nr_of_images;
int rank, ntasks;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
if (rank == MASTER) {
if (parse_args(argc, argv) != 0) {
MPI_Finalize();
return -1;
}
}
nr_of_images = images_read(argv[1], &images, &widths, &heights, &brightness);
if (nr_of_images < 1) {
if (nr_of_images == 0) return 0;
return -1;
}
int real_size = (rank == MASTER) ? nr_of_images / ntasks : nr_of_images;
apply_filter(real_size, &brightness, &images, &widths, &heights);
images_write(nr_of_images, &images, &widths, &heights);
MPI_Finalize();
return 0;
}