-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_normalization.c
218 lines (184 loc) · 5.99 KB
/
image_normalization.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
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include <time.h>
#include <omp.h>
static int x;
static int y;
#define BENCH_GETTIME(x) do { \
gettimeofday((x), NULL); \
} while(0)
#include <sys/time.h>
void print_duration(struct timeval start, struct timeval end) {
double duration = ((end.tv_sec-start.tv_sec)*1000000
+ end.tv_usec - start.tv_usec)/1000.0;
fprintf(stderr, "duration = %lf\n", duration);
}
unsigned char *readPPM(const char *filename) {
char buff[16];
unsigned char *result;
FILE *fp;
int c, rgb_comp_color;
fp = fopen(filename, "rb");
fgets(buff, sizeof(buff), fp);
c = getc(fp);
while(c == '#') {
while(getc(fp) != '\n') ;
c = getc(fp);
}
ungetc(c, fp);
fscanf(fp, "%d %d", &x, &y);
fscanf(fp, "%d", &rgb_comp_color);
while(fgetc(fp) != '\n') ;
result = (unsigned char *)malloc(3 * x * y * sizeof(unsigned char));
fread(result, 3 * x, y, fp);
fclose(fp);
return result;
}
void writePPM(const char *filename, unsigned char *image) {
FILE *fp;
fp = fopen(filename, "wb");
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", x, y);
fprintf(fp, "%d\n", 255);
fwrite(image, 3 * x, y, fp);
fclose(fp);
}
void findMinMax(unsigned char *img, unsigned char *min, unsigned char *max, int size) {
long i;
unsigned char value;
unsigned char *localMin = malloc(3 * sizeof(unsigned char));
unsigned char *localMax = malloc(3 * sizeof(unsigned char));
for(i = 0; i < 3; i++) {
localMin[i] = 255;
localMax[i] = 0;
}
// the computation is divided among threads, each calculates localMin and localMax
#pragma omp parallel firstprivate(localMin, localMax) private(value) shared(img, size, min, max)
{
#pragma omp for
for(i = 0; i < size; i++) {
value = img[i];
if(value < localMin[i % 3]) {
localMin[i % 3] = value;
}
if(value > localMax[i % 3]) {
localMax[i % 3] = value;
}
}
// localMin and localMax are reduced into global min and max
for(i = 0; i < 3; i++) {
#pragma omp critical
{
min[i] = min[i] < localMin[i] ? min[i] : localMin[i];
max[i] = max[i] > localMax[i] ? max[i] : localMax[i];
}
}
}
free(localMin);
free(localMax);
}
void normalize(unsigned char *img, int newMin, int newMax, unsigned char *min, unsigned char *max, int size) {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
long i;
for(i = 0; i < 3; i++) {
if(max[i] == min[i]) {
return;
}
}
if(img) {
// every thread normalizes its part of the image
#pragma omp parallel for shared(img) firstprivate(min, max, newMin, newMax)
for(i = 0; i < size; i++) {
int j = i % 3;
img[i] = (img[i] - min[j]) * (newMax - newMin) / (max[j] - min[j]) + newMin;
}
}
}
int main(int argc, char *argv[]) {
// the image to normalize
unsigned char *image = readPPM("test.ppm");
int numtasks, rank;
int *displs;
int *recvcounts;
int *sendcounts;
// MPI initialization
printf("initializing MPI\n");
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
printf("rank: %d\n", rank);
printf("size: %d\n", numtasks);
// min size of the arrays and extra pixel to allocate
int nmin = x * y * 3 / numtasks;
int nextra = x * y * 3 % numtasks;
// integer array (of length numtasks)
// Entry i specifies the displacement (relative to sendbuf from which to take the outgoing data to process i
displs = (int *)malloc(numtasks * sizeof(int));
// integer array (of lenght numtasks)
// Entry i specifies the number of elements in receive buffer of the process i
recvcounts = (int *)malloc(numtasks * sizeof(int));
// integer array (of lenght numtasks)
// Entry i specifies the number of elements in send buffer of the process i
sendcounts = (int *)malloc(numtasks * sizeof(int));
long i, k;
// initialization of the three previous arrays
for(i = 0; i < numtasks; i++) {
if(i < nextra) {
sendcounts[i] = recvcounts[i] = nmin + 1;
} else {
sendcounts[i] = recvcounts[i] = nmin;
}
displs[i] = k;
k += sendcounts[i];
}
// The main process is process 0
int source = 0;
// every process allocates its receive buffer
unsigned char *recvBuffer = (unsigned char *)malloc(recvcounts[rank] * sizeof(unsigned char));
// the image is scattered in parts to all processes in the communicator MPI_COMM_WORLD
MPI_Scatterv(image, sendcounts, displs, MPI_UNSIGNED_CHAR, recvBuffer, recvcounts[rank], MPI_UNSIGNED_CHAR, source, MPI_COMM_WORLD);
// global min and max (for each RGB component of the image)
unsigned char *min = (unsigned char *)malloc(3 * sizeof(unsigned char));
unsigned char *max = (unsigned char *)malloc(3 * sizeof(unsigned char));
// local min and max (for each RGB component of the image) calculated by a single process
unsigned char *localMin = (unsigned char *)malloc(3 * sizeof(unsigned char));
unsigned char *localMax = (unsigned char *)malloc(3 * sizeof(unsigned char));
// initialization of localMin and localMax
for(i = 0; i < 3; i++) {
localMin[i] = 255;
localMax[i] = 0;
}
struct timeval tStart, tEnd;
// every process finds the min and max of its part of the image
BENCH_GETTIME(&tStart);
findMinMax(recvBuffer, localMin, localMax, recvcounts[rank]);
BENCH_GETTIME(&tEnd);
print_duration(tStart, tEnd);
// localMin and localMax are reduced into min and max
MPI_Allreduce(localMin, min, 3, MPI_UNSIGNED_CHAR, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce(localMax, max, 3, MPI_UNSIGNED_CHAR, MPI_MAX, MPI_COMM_WORLD);
// every process normalizes its parts of the image
BENCH_GETTIME(&tStart);
normalize(recvBuffer, atoi(argv[1]), atoi(argv[2]), min, max, recvcounts[rank]);
BENCH_GETTIME(&tEnd);
print_duration(tStart, tEnd);
// every normalized part is gathered into image
MPI_Gatherv(recvBuffer, sendcounts[rank], MPI_UNSIGNED_CHAR, image, recvcounts, displs, MPI_UNSIGNED_CHAR, source, MPI_COMM_WORLD);
// the main process saves the image
if(rank == 0) {
writePPM("result.ppm", image);
}
// free allocated memory
free(min);
free(max);
free(localMin);
free(localMax);
free(recvBuffer);
free(displs);
free(recvcounts);
free(sendcounts);
free(image);
MPI_Finalize();
}