-
Notifications
You must be signed in to change notification settings - Fork 3
/
initializations.c
319 lines (302 loc) · 10.4 KB
/
initializations.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
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
#include <string.h>
#include <math.h>
#include "initializations.h"
#include "processing_img.h"
//-------------------------------------------
// Forward declarations
//-------------------------------------------
int* BW_initialization(const char* filename, int width, int height,
int img_block_width, int img_block_height, int proc_num);
int** RGB_initialization(const char* filename, int width, int height);
int** send_image(int* img_buffer, int width, int height, int proc_num,
int img_block_width, int img_block_height,
int proc_per_row, MPI_Request** requests);
int* extract_img_block(int* img, int img_length, int proc_per_row,
int block_id, int img_block_width, int img_block_height);
int** read_RGB_img(const char* filename, int width, int height);
int initialize_MPI();
int* read_BW_img(const char* filename, int width, int height);
//-------------------------------------------
// Datatypes
//-------------------------------------------
MPI_Datatype mpi_column, mpi_row, mpi_block_img, mpi_block;
//-------------------------------------------
// Methods
//-------------------------------------------
int** initalization_phase(const char* filename, int width, int height,
unsigned int isBW) {
int proc_num = initialize_MPI();
int proc_per_row;
proc_per_row = (int) sqrt(proc_num);
int img_block_width = width / proc_per_row;
int img_block_height = height / proc_per_row;
// Create datatype for columns
MPI_Type_vector(img_block_height, 1, img_block_width + 2, MPI_INT,
&mpi_column);
MPI_Type_commit(&mpi_column);
MPI_Type_vector(1, img_block_width, 0, MPI_INT, &mpi_row);
MPI_Type_commit(&mpi_row);
MPI_Type_vector(1, img_block_width * img_block_height, 0, MPI_INT,
&mpi_block_img);
MPI_Type_commit(&mpi_block_img);
MPI_Type_vector(img_block_height, img_block_width,
img_block_width + 2, MPI_INT, &mpi_block);
MPI_Type_commit(&mpi_block);
// Start separating master and slave procs
int rank;
int* block = NULL;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
if (isBW) {
//printf("Master: about to initialize BW\n");
int* img_buffer =
BW_initialization(filename, width, height,
img_block_width, img_block_height, proc_num);
block = malloc((img_block_width + 2) *
(img_block_height + 2) * sizeof(int));
MPI_Scatter(img_buffer, 1, mpi_block_img,
block + img_block_width + 3, 1, mpi_block,
0, MPI_COMM_WORLD);
free(img_buffer);
}
else
{
//printf("Master: about to initialize RGB\n");
int** rgb_buffer = RGB_initialization(filename, width, height);
int** rgb_block = malloc(3* sizeof(int*));
int i;
for (i = 0; i < 3 ; i++)
{
rgb_block[i] = malloc((img_block_width + 2) *
(img_block_height + 2) * sizeof(int));
MPI_Scatter(rgb_buffer[i], 1, mpi_block_img,
rgb_block[i] + img_block_width + 3, 1, mpi_block,
0, MPI_COMM_WORLD);
free(rgb_buffer[i]);
}
free(rgb_buffer);
return rgb_block;
}
}
else {
if (isBW) {
int block_length =
(img_block_width + 2) * (img_block_height + 2);
block = malloc(block_length * sizeof(int));
memset(block, '\0', block_length * sizeof(int));
MPI_Scatter(NULL, 1, mpi_block_img, block + img_block_width + 3,
1, mpi_block, 0, MPI_COMM_WORLD);
}
else
{
int block_length =
(img_block_width + 2) * (img_block_height + 2);
int** rgb_block = malloc(3* sizeof(int*));
int i;
for (i = 0; i < 3 ; i++)
{
rgb_block[i] = malloc(block_length * sizeof(int));
memset(rgb_block[i], '\0', block_length * sizeof(int));
MPI_Scatter(NULL, 1, mpi_block_img, rgb_block[i] + img_block_width + 3,
1, mpi_block, 0, MPI_COMM_WORLD);
}
return rgb_block;
}
}
//handling of BW return (only)
int** rblock = █
return rblock;
}
int* BW_initialization(const char* filename, int width, int height,
int img_block_width, int img_block_height,
int proc_num) {
// Read image
int* img_buffer = read_BW_img(filename, width, height);
if (img_buffer == NULL)
{
return IMG_LOAD_FAILURE;
}
return img_buffer;
}
/*
* Extracts a block from the image (the block denoted by block_id)
*/
int* extract_img_block(int* img, int img_length, int proc_per_row,
int block_id, int img_block_width, int img_block_height) {
int i,j;
int current_row = block_id / proc_per_row;
int current_col = block_id - current_row * proc_per_row;
int start_idx =
current_row * img_block_height * img_block_width * proc_per_row
+ current_col * img_block_width;
//printf("Start_idx == %d\n", start_idx);
//printf("max_idx == %d\n", img_block_width * img_block_height);
int* buffer = NULL;
buffer = malloc(img_block_width * img_block_height * sizeof(int));
memset(buffer, '\0', img_block_width * img_block_height * sizeof(int));
// In the for loop, insert the -1 pixel from the start
// and the +1 from the end
int idx = 0;
for (i = start_idx, j = 1; i < img_length && j<=img_block_width*img_block_height; i++,j++) {
buffer[idx] = img[i];
idx++;
if (j%img_block_width==0 && j!=0){
i += img_block_width * (proc_per_row - 1);
continue;
}
}
return buffer;
}
int** RGB_initialization(const char* filename, int width, int height) {
int** img_buffers = read_RGB_img(filename, width, height);
if (img_buffers == NULL)
return IMG_LOAD_FAILURE;
return img_buffers;
}
/*
* Initializes the MPI framework
*/
int initialize_MPI() {
int numprocs;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
return numprocs;
}
/*
* Send image blocks to all other processes
*/
int** send_image(int* img_buffer, int width, int height, int proc_num,
int img_block_width, int img_block_height,
int proc_per_row, MPI_Request** requests) {
int i;
int** img_blocks = malloc((proc_num - 1) * sizeof(int*));
*requests = malloc((proc_num - 1) * sizeof(MPI_Request));
for (i = 1; i < proc_num; i++) {
int* img_block = extract_img_block(img_buffer, width * height,
proc_per_row, i, img_block_width, img_block_height);
// MPI_Isend(img_block, img_block_width * img_block_height, mpi_block_img,
// i, IMG_BLOCK_MSG, MPI_COMM_WORLD, &((*requests)[i - 1]));
}
return img_blocks;
}
/*
* Will send the appropriate image blocks to the other processes
*/
/*
* Reads the BW image into an int[] buffer
* Return value:
* On success, the buffer is returned
* Otherwise, NULL is returned
*/
int* read_BW_img(const char* filename, int width, int height) {
FILE* img_file = fopen(filename, "rb");
if (img_file == NULL) {
fprintf(stderr, "read_BW_img - Could open image file\n");
return NULL;
}
unsigned char *img = NULL;
img = malloc(width * height);
if (img == NULL) {
fprintf(stderr, "read_BW_img - Could not allocate img buffer\n");
return NULL;
}
if (fread(img, 1, width * height, img_file) < width * height) {
fprintf(stderr, "read_BW_img - Sth went wrong with image reading\n");
free(img);
img = NULL;
return NULL;
}
int i;
int *img_int = malloc(width * height * sizeof(int));
for ( i = 0; i < width * height; i++)
{
img_int[i] = img[i];
}
free(img);
fclose(img_file);
return img_int;
}
/*
* Reads the RGB image into 3 int[] buffers and returns an (int*)[] that
* points to these 3 buffers
* Return value:
* On success, the buffer mentioned above
* On failure, NULL is returned
*/
int** read_RGB_img(const char* filename, int width, int height) {
FILE* img_file = fopen(filename, "rb");
if (img_file == NULL) {
fprintf(stderr, "read_RGB_img - Could open image file\n");
return NULL;
}
unsigned char *img = NULL;
img = malloc(width * height * 3);
if (img == NULL) {
fprintf(stderr, "read_RGB_img - Could not allocate img buffer\n");
return NULL;
}
if (fread(img, 1, width * height * 3, img_file) < 3 * width * height) {
fprintf(stderr, "read_RGB_img - Sth went wrong with image reading\n");
free(img);
img = NULL;
fclose(img_file);
return NULL;
}
int** rgb_buffers = malloc(3 * sizeof(int*));
if (rgb_buffers == NULL) {
fprintf(stderr, "read_RGB_img - Failed to allocate space for "
"buffers\n");
free(img);
img = NULL;
return NULL;
}
int* red = malloc(width * height * sizeof(int));
if (red == NULL) {
fprintf(stderr, "read_RGB_img - Failed to allocate space for "
"buffers\n");
free(img);
free(rgb_buffers);
img = NULL;
rgb_buffers = NULL;
return NULL;
}
int* green = malloc(width * height * sizeof(int));
if (green == NULL) {
fprintf(stderr, "read_RGB_img - Failed to allocate space for "
"buffers\n");
free(img);
free(rgb_buffers);
free(red);
img = NULL;
rgb_buffers = NULL;
red = NULL;
return NULL;
}
int* blue = malloc(width * height * sizeof(int));
if (blue == NULL) {
fprintf(stderr, "read_RGB_img - Failed to allocate space for "
"buffers\n");
free(img);
free(rgb_buffers);
free(red);
free(green);
img = NULL;
rgb_buffers = NULL;
red = NULL;
green = NULL;
return NULL;
}
int i, j;
for (i = 0 , j = 0; j < width * height; i += 3, j++) {
red[j] = img[i];
green[j] = img[i + 1];
blue[j] = img[i + 2];
}
rgb_buffers[0] = red;
rgb_buffers[1] = green;
rgb_buffers[2] = blue;
return rgb_buffers;
}